BEncryption.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**
  2. * @file BEncryption.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Block cipher encryption abstraction.
  25. */
  26. #ifndef BADVPN_SECURITY_BENCRYPTION_H
  27. #define BADVPN_SECURITY_BENCRYPTION_H
  28. #include <stdint.h>
  29. #include <string.h>
  30. #ifdef BADVPN_USE_CRYPTODEV
  31. #include <crypto/cryptodev.h>
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <fcntl.h>
  35. #include <sys/ioctl.h>
  36. #include <string.h>
  37. #include <stdint.h>
  38. #include <unistd.h>
  39. #endif
  40. #include <openssl/blowfish.h>
  41. #include <openssl/aes.h>
  42. #include <misc/debug.h>
  43. #include <system/DebugObject.h>
  44. #define BENCRYPTION_MODE_ENCRYPT 1
  45. #define BENCRYPTION_MODE_DECRYPT 2
  46. #define BENCRYPTION_CIPHER_BLOWFISH 1
  47. #define BENCRYPTION_CIPHER_BLOWFISH_BLOCK_SIZE 8
  48. #define BENCRYPTION_CIPHER_BLOWFISH_KEY_SIZE 16
  49. #define BENCRYPTION_CIPHER_AES 2
  50. #define BENCRYPTION_CIPHER_AES_BLOCK_SIZE 16
  51. #define BENCRYPTION_CIPHER_AES_KEY_SIZE 16
  52. /**
  53. * Block cipher encryption abstraction.
  54. */
  55. typedef struct {
  56. DebugObject d_obj;
  57. int mode;
  58. int cipher;
  59. #ifdef BADVPN_USE_CRYPTODEV
  60. int use_cryptodev;
  61. #endif
  62. union {
  63. BF_KEY blowfish;
  64. struct {
  65. AES_KEY encrypt;
  66. AES_KEY decrypt;
  67. } aes;
  68. #ifdef BADVPN_USE_CRYPTODEV
  69. struct {
  70. int fd;
  71. int cfd;
  72. int cipher;
  73. uint32_t ses;
  74. } cryptodev;
  75. #endif
  76. };
  77. } BEncryption;
  78. /**
  79. * Checks if the given cipher number is valid.
  80. *
  81. * @param cipher cipher number
  82. * @return 1 if valid, 0 if not
  83. */
  84. static int BEncryption_cipher_valid (int cipher);
  85. /**
  86. * Returns the block size of a cipher.
  87. *
  88. * @param cipher cipher number. Must be valid.
  89. * @return block size in bytes
  90. */
  91. static int BEncryption_cipher_block_size (int cipher);
  92. /**
  93. * Returns the key size of a cipher.
  94. *
  95. * @param cipher cipher number. Must be valid.
  96. * @return key size in bytes
  97. */
  98. static int BEncryption_cipher_key_size (int cipher);
  99. /**
  100. * Initializes the object.
  101. *
  102. * @param enc the object
  103. * @param mode whether encryption or decryption is to be done, or both.
  104. * Must be a bitwise-OR of at least one of BENCRYPTION_MODE_ENCRYPT
  105. * and BENCRYPTION_MODE_DECRYPT.
  106. * @param cipher cipher number. Must be valid.
  107. * @param key encryption key
  108. */
  109. static void BEncryption_Init (BEncryption *enc, int mode, int cipher, uint8_t *key);
  110. /**
  111. * Frees the object.
  112. *
  113. * @param enc the object
  114. */
  115. static void BEncryption_Free (BEncryption *enc);
  116. /**
  117. * Encrypts data.
  118. * The object must have been initialized with mode including
  119. * BENCRYPTION_MODE_ENCRYPT.
  120. *
  121. * @param enc the object
  122. * @param in data to encrypt
  123. * @param out ciphertext output
  124. * @param len number of bytes to encrypt. Must be >=0 and a multiple of
  125. * block size.
  126. * @param iv initialization vector. Updated such that continuing a previous encryption
  127. * starting with the updated IV is equivalent to performing just one encryption.
  128. */
  129. static void BEncryption_Encrypt (BEncryption *enc, uint8_t *in, uint8_t *out, int len, uint8_t *iv);
  130. /**
  131. * Decrypts data.
  132. * The object must have been initialized with mode including
  133. * BENCRYPTION_MODE_DECRYPT.
  134. *
  135. * @param enc the object
  136. * @param in data to decrypt
  137. * @param out plaintext output
  138. * @param len number of bytes to decrypt. Must be >=0 and a multiple of
  139. * block size.
  140. * @param iv initialization vector. Updated such that continuing a previous decryption
  141. * starting with the updated IV is equivalent to performing just one decryption.
  142. */
  143. static void BEncryption_Decrypt (BEncryption *enc, uint8_t *in, uint8_t *out, int len, uint8_t *iv);
  144. int BEncryption_cipher_valid (int cipher)
  145. {
  146. switch (cipher) {
  147. case BENCRYPTION_CIPHER_BLOWFISH:
  148. case BENCRYPTION_CIPHER_AES:
  149. return 1;
  150. default:
  151. return 0;
  152. }
  153. }
  154. int BEncryption_cipher_block_size (int cipher)
  155. {
  156. switch (cipher) {
  157. case BENCRYPTION_CIPHER_BLOWFISH:
  158. return BENCRYPTION_CIPHER_BLOWFISH_BLOCK_SIZE;
  159. case BENCRYPTION_CIPHER_AES:
  160. return BENCRYPTION_CIPHER_AES_BLOCK_SIZE;
  161. default:
  162. ASSERT(0)
  163. return 0;
  164. }
  165. }
  166. int BEncryption_cipher_key_size (int cipher)
  167. {
  168. switch (cipher) {
  169. case BENCRYPTION_CIPHER_BLOWFISH:
  170. return BENCRYPTION_CIPHER_BLOWFISH_KEY_SIZE;
  171. case BENCRYPTION_CIPHER_AES:
  172. return BENCRYPTION_CIPHER_AES_KEY_SIZE;
  173. default:
  174. ASSERT(0)
  175. return 0;
  176. }
  177. }
  178. void BEncryption_Init (BEncryption *enc, int mode, int cipher, uint8_t *key)
  179. {
  180. ASSERT(!(mode&~(BENCRYPTION_MODE_ENCRYPT|BENCRYPTION_MODE_DECRYPT)))
  181. ASSERT((mode&BENCRYPTION_MODE_ENCRYPT) || (mode&BENCRYPTION_MODE_DECRYPT))
  182. enc->mode = mode;
  183. enc->cipher = cipher;
  184. #ifdef BADVPN_USE_CRYPTODEV
  185. switch (enc->cipher) {
  186. case BENCRYPTION_CIPHER_AES:
  187. enc->cryptodev.cipher = CRYPTO_AES_CBC;
  188. break;
  189. default:
  190. goto fail1;
  191. }
  192. if ((enc->cryptodev.fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
  193. DEBUG("failed to open /dev/crypto");
  194. goto fail1;
  195. }
  196. if (ioctl(enc->cryptodev.fd, CRIOGET, &enc->cryptodev.cfd)) {
  197. DEBUG("failed ioctl(CRIOGET)");
  198. goto fail2;
  199. }
  200. struct session_op sess;
  201. memset(&sess, 0, sizeof(sess));
  202. sess.cipher = enc->cryptodev.cipher;
  203. sess.keylen = BEncryption_cipher_key_size(enc->cipher);
  204. sess.key = key;
  205. if (ioctl(enc->cryptodev.cfd, CIOCGSESSION, &sess)) {
  206. DEBUG("failed ioctl(CIOCGSESSION)");
  207. goto fail3;
  208. }
  209. enc->cryptodev.ses = sess.ses;
  210. enc->use_cryptodev = 1;
  211. goto success;
  212. fail3:
  213. ASSERT_FORCE(close(enc->cryptodev.cfd) == 0)
  214. fail2:
  215. ASSERT_FORCE(close(enc->cryptodev.fd) == 0)
  216. fail1:
  217. enc->use_cryptodev = 0;
  218. #endif
  219. int res;
  220. switch (enc->cipher) {
  221. case BENCRYPTION_CIPHER_BLOWFISH:
  222. BF_set_key(&enc->blowfish, BENCRYPTION_CIPHER_BLOWFISH_KEY_SIZE, key);
  223. break;
  224. case BENCRYPTION_CIPHER_AES:
  225. if (enc->mode&BENCRYPTION_MODE_ENCRYPT) {
  226. res = AES_set_encrypt_key(key, 128, &enc->aes.encrypt);
  227. ASSERT(res >= 0)
  228. }
  229. if (enc->mode&BENCRYPTION_MODE_DECRYPT) {
  230. res = AES_set_decrypt_key(key, 128, &enc->aes.decrypt);
  231. ASSERT(res >= 0)
  232. }
  233. break;
  234. default:
  235. ASSERT(0)
  236. ;
  237. }
  238. success:
  239. // init debug object
  240. DebugObject_Init(&enc->d_obj);
  241. }
  242. void BEncryption_Free (BEncryption *enc)
  243. {
  244. // free debug object
  245. DebugObject_Free(&enc->d_obj);
  246. #ifdef BADVPN_USE_CRYPTODEV
  247. if (enc->use_cryptodev) {
  248. ASSERT_FORCE(ioctl(enc->cryptodev.cfd, CIOCFSESSION, &enc->cryptodev.ses) == 0)
  249. ASSERT_FORCE(close(enc->cryptodev.cfd) == 0)
  250. ASSERT_FORCE(close(enc->cryptodev.fd) == 0)
  251. }
  252. #endif
  253. }
  254. void BEncryption_Encrypt (BEncryption *enc, uint8_t *in, uint8_t *out, int len, uint8_t *iv)
  255. {
  256. ASSERT(enc->mode&BENCRYPTION_MODE_ENCRYPT)
  257. ASSERT(len >= 0)
  258. ASSERT(len % BEncryption_cipher_block_size(enc->cipher) == 0)
  259. #ifdef BADVPN_USE_CRYPTODEV
  260. if (enc->use_cryptodev) {
  261. struct crypt_op cryp;
  262. memset(&cryp, 0, sizeof(cryp));
  263. cryp.ses = enc->cryptodev.ses;
  264. cryp.len = len;
  265. cryp.src = in;
  266. cryp.dst = out;
  267. cryp.iv = iv;
  268. cryp.op = COP_ENCRYPT;
  269. ASSERT_FORCE(ioctl(enc->cryptodev.cfd, CIOCCRYPT, &cryp) == 0)
  270. return;
  271. }
  272. #endif
  273. switch (enc->cipher) {
  274. case BENCRYPTION_CIPHER_BLOWFISH:
  275. BF_cbc_encrypt(in, out, len, &enc->blowfish, iv, BF_ENCRYPT);
  276. break;
  277. case BENCRYPTION_CIPHER_AES:
  278. AES_cbc_encrypt(in, out, len, &enc->aes.encrypt, iv, AES_ENCRYPT);
  279. break;
  280. default:
  281. ASSERT(0);
  282. }
  283. }
  284. void BEncryption_Decrypt (BEncryption *enc, uint8_t *in, uint8_t *out, int len, uint8_t *iv)
  285. {
  286. ASSERT(enc->mode&BENCRYPTION_MODE_DECRYPT)
  287. ASSERT(len >= 0)
  288. ASSERT(len % BEncryption_cipher_block_size(enc->cipher) == 0)
  289. #ifdef BADVPN_USE_CRYPTODEV
  290. if (enc->use_cryptodev) {
  291. struct crypt_op cryp;
  292. memset(&cryp, 0, sizeof(cryp));
  293. cryp.ses = enc->cryptodev.ses;
  294. cryp.len = len;
  295. cryp.src = in;
  296. cryp.dst = out;
  297. cryp.iv = iv;
  298. cryp.op = COP_DECRYPT;
  299. ASSERT_FORCE(ioctl(enc->cryptodev.cfd, CIOCCRYPT, &cryp) == 0)
  300. return;
  301. }
  302. #endif
  303. switch (enc->cipher) {
  304. case BENCRYPTION_CIPHER_BLOWFISH:
  305. BF_cbc_encrypt(in, out, len, &enc->blowfish, iv, BF_DECRYPT);
  306. break;
  307. case BENCRYPTION_CIPHER_AES:
  308. AES_cbc_encrypt(in, out, len, &enc->aes.decrypt, iv, AES_DECRYPT);
  309. break;
  310. default:
  311. ASSERT(0);
  312. }
  313. }
  314. #endif