BEncryption.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * @file BEncryption.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <base/BLog.h>
  30. #include <security/BEncryption.h>
  31. #include <generated/blog_channel_BEncryption.h>
  32. int BEncryption_cipher_valid (int cipher)
  33. {
  34. switch (cipher) {
  35. case BENCRYPTION_CIPHER_BLOWFISH:
  36. case BENCRYPTION_CIPHER_AES:
  37. return 1;
  38. default:
  39. return 0;
  40. }
  41. }
  42. int BEncryption_cipher_block_size (int cipher)
  43. {
  44. switch (cipher) {
  45. case BENCRYPTION_CIPHER_BLOWFISH:
  46. return BENCRYPTION_CIPHER_BLOWFISH_BLOCK_SIZE;
  47. case BENCRYPTION_CIPHER_AES:
  48. return BENCRYPTION_CIPHER_AES_BLOCK_SIZE;
  49. default:
  50. ASSERT(0)
  51. return 0;
  52. }
  53. }
  54. int BEncryption_cipher_key_size (int cipher)
  55. {
  56. switch (cipher) {
  57. case BENCRYPTION_CIPHER_BLOWFISH:
  58. return BENCRYPTION_CIPHER_BLOWFISH_KEY_SIZE;
  59. case BENCRYPTION_CIPHER_AES:
  60. return BENCRYPTION_CIPHER_AES_KEY_SIZE;
  61. default:
  62. ASSERT(0)
  63. return 0;
  64. }
  65. }
  66. void BEncryption_Init (BEncryption *enc, int mode, int cipher, uint8_t *key)
  67. {
  68. ASSERT(!(mode&~(BENCRYPTION_MODE_ENCRYPT|BENCRYPTION_MODE_DECRYPT)))
  69. ASSERT((mode&BENCRYPTION_MODE_ENCRYPT) || (mode&BENCRYPTION_MODE_DECRYPT))
  70. enc->mode = mode;
  71. enc->cipher = cipher;
  72. #ifdef BADVPN_USE_CRYPTODEV
  73. switch (enc->cipher) {
  74. case BENCRYPTION_CIPHER_AES:
  75. enc->cryptodev.cipher = CRYPTO_AES_CBC;
  76. break;
  77. default:
  78. goto fail1;
  79. }
  80. if ((enc->cryptodev.fd = open("/dev/crypto", O_RDWR, 0)) < 0) {
  81. BLog(BLOG_ERROR, "failed to open /dev/crypto");
  82. goto fail1;
  83. }
  84. if (ioctl(enc->cryptodev.fd, CRIOGET, &enc->cryptodev.cfd)) {
  85. BLog(BLOG_ERROR, "failed ioctl(CRIOGET)");
  86. goto fail2;
  87. }
  88. struct session_op sess;
  89. memset(&sess, 0, sizeof(sess));
  90. sess.cipher = enc->cryptodev.cipher;
  91. sess.keylen = BEncryption_cipher_key_size(enc->cipher);
  92. sess.key = key;
  93. if (ioctl(enc->cryptodev.cfd, CIOCGSESSION, &sess)) {
  94. BLog(BLOG_ERROR, "failed ioctl(CIOCGSESSION)");
  95. goto fail3;
  96. }
  97. enc->cryptodev.ses = sess.ses;
  98. enc->use_cryptodev = 1;
  99. goto success;
  100. fail3:
  101. ASSERT_FORCE(close(enc->cryptodev.cfd) == 0)
  102. fail2:
  103. ASSERT_FORCE(close(enc->cryptodev.fd) == 0)
  104. fail1:
  105. enc->use_cryptodev = 0;
  106. #endif
  107. int res;
  108. switch (enc->cipher) {
  109. case BENCRYPTION_CIPHER_BLOWFISH:
  110. BF_set_key(&enc->blowfish, BENCRYPTION_CIPHER_BLOWFISH_KEY_SIZE, key);
  111. break;
  112. case BENCRYPTION_CIPHER_AES:
  113. if (enc->mode&BENCRYPTION_MODE_ENCRYPT) {
  114. res = AES_set_encrypt_key(key, 128, &enc->aes.encrypt);
  115. ASSERT_EXECUTE(res >= 0)
  116. }
  117. if (enc->mode&BENCRYPTION_MODE_DECRYPT) {
  118. res = AES_set_decrypt_key(key, 128, &enc->aes.decrypt);
  119. ASSERT_EXECUTE(res >= 0)
  120. }
  121. break;
  122. default:
  123. ASSERT(0)
  124. ;
  125. }
  126. #ifdef BADVPN_USE_CRYPTODEV
  127. success:
  128. #endif
  129. // init debug object
  130. DebugObject_Init(&enc->d_obj);
  131. }
  132. void BEncryption_Free (BEncryption *enc)
  133. {
  134. // free debug object
  135. DebugObject_Free(&enc->d_obj);
  136. #ifdef BADVPN_USE_CRYPTODEV
  137. if (enc->use_cryptodev) {
  138. ASSERT_FORCE(ioctl(enc->cryptodev.cfd, CIOCFSESSION, &enc->cryptodev.ses) == 0)
  139. ASSERT_FORCE(close(enc->cryptodev.cfd) == 0)
  140. ASSERT_FORCE(close(enc->cryptodev.fd) == 0)
  141. }
  142. #endif
  143. }
  144. void BEncryption_Encrypt (BEncryption *enc, uint8_t *in, uint8_t *out, int len, uint8_t *iv)
  145. {
  146. ASSERT(enc->mode&BENCRYPTION_MODE_ENCRYPT)
  147. ASSERT(len >= 0)
  148. ASSERT(len % BEncryption_cipher_block_size(enc->cipher) == 0)
  149. #ifdef BADVPN_USE_CRYPTODEV
  150. if (enc->use_cryptodev) {
  151. struct crypt_op cryp;
  152. memset(&cryp, 0, sizeof(cryp));
  153. cryp.ses = enc->cryptodev.ses;
  154. cryp.len = len;
  155. cryp.src = in;
  156. cryp.dst = out;
  157. cryp.iv = iv;
  158. cryp.op = COP_ENCRYPT;
  159. ASSERT_FORCE(ioctl(enc->cryptodev.cfd, CIOCCRYPT, &cryp) == 0)
  160. return;
  161. }
  162. #endif
  163. switch (enc->cipher) {
  164. case BENCRYPTION_CIPHER_BLOWFISH:
  165. BF_cbc_encrypt(in, out, len, &enc->blowfish, iv, BF_ENCRYPT);
  166. break;
  167. case BENCRYPTION_CIPHER_AES:
  168. AES_cbc_encrypt(in, out, len, &enc->aes.encrypt, iv, AES_ENCRYPT);
  169. break;
  170. default:
  171. ASSERT(0);
  172. }
  173. }
  174. void BEncryption_Decrypt (BEncryption *enc, uint8_t *in, uint8_t *out, int len, uint8_t *iv)
  175. {
  176. ASSERT(enc->mode&BENCRYPTION_MODE_DECRYPT)
  177. ASSERT(len >= 0)
  178. ASSERT(len % BEncryption_cipher_block_size(enc->cipher) == 0)
  179. #ifdef BADVPN_USE_CRYPTODEV
  180. if (enc->use_cryptodev) {
  181. struct crypt_op cryp;
  182. memset(&cryp, 0, sizeof(cryp));
  183. cryp.ses = enc->cryptodev.ses;
  184. cryp.len = len;
  185. cryp.src = in;
  186. cryp.dst = out;
  187. cryp.iv = iv;
  188. cryp.op = COP_DECRYPT;
  189. ASSERT_FORCE(ioctl(enc->cryptodev.cfd, CIOCCRYPT, &cryp) == 0)
  190. return;
  191. }
  192. #endif
  193. switch (enc->cipher) {
  194. case BENCRYPTION_CIPHER_BLOWFISH:
  195. BF_cbc_encrypt(in, out, len, &enc->blowfish, iv, BF_DECRYPT);
  196. break;
  197. case BENCRYPTION_CIPHER_AES:
  198. AES_cbc_encrypt(in, out, len, &enc->aes.decrypt, iv, AES_DECRYPT);
  199. break;
  200. default:
  201. ASSERT(0);
  202. }
  203. }