crypto_internal-cipher.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Crypto wrapper for internal crypto implementation - Cipher wrappers
  3. * Copyright (c) 2006-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto.h"
  11. #include "aes.h"
  12. #include "des_i.h"
  13. struct crypto_cipher {
  14. enum crypto_cipher_alg alg;
  15. union {
  16. struct {
  17. size_t used_bytes;
  18. u8 key[16];
  19. size_t keylen;
  20. } rc4;
  21. struct {
  22. u8 cbc[32];
  23. void *ctx_enc;
  24. void *ctx_dec;
  25. } aes;
  26. struct {
  27. struct des3_key_s key;
  28. u8 cbc[8];
  29. } des3;
  30. struct {
  31. u32 ek[32];
  32. u32 dk[32];
  33. u8 cbc[8];
  34. } des;
  35. } u;
  36. };
  37. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  38. const u8 *iv, const u8 *key,
  39. size_t key_len)
  40. {
  41. struct crypto_cipher *ctx;
  42. ctx = os_zalloc(sizeof(*ctx));
  43. if (ctx == NULL)
  44. return NULL;
  45. ctx->alg = alg;
  46. switch (alg) {
  47. case CRYPTO_CIPHER_ALG_RC4:
  48. if (key_len > sizeof(ctx->u.rc4.key)) {
  49. os_free(ctx);
  50. return NULL;
  51. }
  52. ctx->u.rc4.keylen = key_len;
  53. os_memcpy(ctx->u.rc4.key, key, key_len);
  54. break;
  55. case CRYPTO_CIPHER_ALG_AES:
  56. ctx->u.aes.ctx_enc = aes_encrypt_init(key, key_len);
  57. if (ctx->u.aes.ctx_enc == NULL) {
  58. os_free(ctx);
  59. return NULL;
  60. }
  61. ctx->u.aes.ctx_dec = aes_decrypt_init(key, key_len);
  62. if (ctx->u.aes.ctx_dec == NULL) {
  63. aes_encrypt_deinit(ctx->u.aes.ctx_enc);
  64. os_free(ctx);
  65. return NULL;
  66. }
  67. os_memcpy(ctx->u.aes.cbc, iv, AES_BLOCK_SIZE);
  68. break;
  69. case CRYPTO_CIPHER_ALG_3DES:
  70. if (key_len != 24) {
  71. os_free(ctx);
  72. return NULL;
  73. }
  74. des3_key_setup(key, &ctx->u.des3.key);
  75. os_memcpy(ctx->u.des3.cbc, iv, 8);
  76. break;
  77. case CRYPTO_CIPHER_ALG_DES:
  78. if (key_len != 8) {
  79. os_free(ctx);
  80. return NULL;
  81. }
  82. des_key_setup(key, ctx->u.des.ek, ctx->u.des.dk);
  83. os_memcpy(ctx->u.des.cbc, iv, 8);
  84. break;
  85. default:
  86. os_free(ctx);
  87. return NULL;
  88. }
  89. return ctx;
  90. }
  91. int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
  92. u8 *crypt, size_t len)
  93. {
  94. size_t i, j, blocks;
  95. switch (ctx->alg) {
  96. case CRYPTO_CIPHER_ALG_RC4:
  97. if (plain != crypt)
  98. os_memcpy(crypt, plain, len);
  99. rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
  100. ctx->u.rc4.used_bytes, crypt, len);
  101. ctx->u.rc4.used_bytes += len;
  102. break;
  103. case CRYPTO_CIPHER_ALG_AES:
  104. if (len % AES_BLOCK_SIZE)
  105. return -1;
  106. blocks = len / AES_BLOCK_SIZE;
  107. for (i = 0; i < blocks; i++) {
  108. for (j = 0; j < AES_BLOCK_SIZE; j++)
  109. ctx->u.aes.cbc[j] ^= plain[j];
  110. aes_encrypt(ctx->u.aes.ctx_enc, ctx->u.aes.cbc,
  111. ctx->u.aes.cbc);
  112. os_memcpy(crypt, ctx->u.aes.cbc, AES_BLOCK_SIZE);
  113. plain += AES_BLOCK_SIZE;
  114. crypt += AES_BLOCK_SIZE;
  115. }
  116. break;
  117. case CRYPTO_CIPHER_ALG_3DES:
  118. if (len % 8)
  119. return -1;
  120. blocks = len / 8;
  121. for (i = 0; i < blocks; i++) {
  122. for (j = 0; j < 8; j++)
  123. ctx->u.des3.cbc[j] ^= plain[j];
  124. des3_encrypt(ctx->u.des3.cbc, &ctx->u.des3.key,
  125. ctx->u.des3.cbc);
  126. os_memcpy(crypt, ctx->u.des3.cbc, 8);
  127. plain += 8;
  128. crypt += 8;
  129. }
  130. break;
  131. case CRYPTO_CIPHER_ALG_DES:
  132. if (len % 8)
  133. return -1;
  134. blocks = len / 8;
  135. for (i = 0; i < blocks; i++) {
  136. for (j = 0; j < 8; j++)
  137. ctx->u.des3.cbc[j] ^= plain[j];
  138. des_block_encrypt(ctx->u.des.cbc, ctx->u.des.ek,
  139. ctx->u.des.cbc);
  140. os_memcpy(crypt, ctx->u.des.cbc, 8);
  141. plain += 8;
  142. crypt += 8;
  143. }
  144. break;
  145. default:
  146. return -1;
  147. }
  148. return 0;
  149. }
  150. int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
  151. u8 *plain, size_t len)
  152. {
  153. size_t i, j, blocks;
  154. u8 tmp[32];
  155. switch (ctx->alg) {
  156. case CRYPTO_CIPHER_ALG_RC4:
  157. if (plain != crypt)
  158. os_memcpy(plain, crypt, len);
  159. rc4_skip(ctx->u.rc4.key, ctx->u.rc4.keylen,
  160. ctx->u.rc4.used_bytes, plain, len);
  161. ctx->u.rc4.used_bytes += len;
  162. break;
  163. case CRYPTO_CIPHER_ALG_AES:
  164. if (len % AES_BLOCK_SIZE)
  165. return -1;
  166. blocks = len / AES_BLOCK_SIZE;
  167. for (i = 0; i < blocks; i++) {
  168. os_memcpy(tmp, crypt, AES_BLOCK_SIZE);
  169. aes_decrypt(ctx->u.aes.ctx_dec, crypt, plain);
  170. for (j = 0; j < AES_BLOCK_SIZE; j++)
  171. plain[j] ^= ctx->u.aes.cbc[j];
  172. os_memcpy(ctx->u.aes.cbc, tmp, AES_BLOCK_SIZE);
  173. plain += AES_BLOCK_SIZE;
  174. crypt += AES_BLOCK_SIZE;
  175. }
  176. break;
  177. case CRYPTO_CIPHER_ALG_3DES:
  178. if (len % 8)
  179. return -1;
  180. blocks = len / 8;
  181. for (i = 0; i < blocks; i++) {
  182. os_memcpy(tmp, crypt, 8);
  183. des3_decrypt(crypt, &ctx->u.des3.key, plain);
  184. for (j = 0; j < 8; j++)
  185. plain[j] ^= ctx->u.des3.cbc[j];
  186. os_memcpy(ctx->u.des3.cbc, tmp, 8);
  187. plain += 8;
  188. crypt += 8;
  189. }
  190. break;
  191. case CRYPTO_CIPHER_ALG_DES:
  192. if (len % 8)
  193. return -1;
  194. blocks = len / 8;
  195. for (i = 0; i < blocks; i++) {
  196. os_memcpy(tmp, crypt, 8);
  197. des_block_decrypt(crypt, ctx->u.des.dk, plain);
  198. for (j = 0; j < 8; j++)
  199. plain[j] ^= ctx->u.des.cbc[j];
  200. os_memcpy(ctx->u.des.cbc, tmp, 8);
  201. plain += 8;
  202. crypt += 8;
  203. }
  204. break;
  205. default:
  206. return -1;
  207. }
  208. return 0;
  209. }
  210. void crypto_cipher_deinit(struct crypto_cipher *ctx)
  211. {
  212. switch (ctx->alg) {
  213. case CRYPTO_CIPHER_ALG_AES:
  214. aes_encrypt_deinit(ctx->u.aes.ctx_enc);
  215. aes_decrypt_deinit(ctx->u.aes.ctx_dec);
  216. break;
  217. case CRYPTO_CIPHER_ALG_3DES:
  218. break;
  219. default:
  220. break;
  221. }
  222. os_free(ctx);
  223. }