crypto_openssl.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * WPA Supplicant / wrapper functions for libcrypto
  3. * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include <openssl/opensslv.h>
  16. #include <openssl/err.h>
  17. #include <openssl/des.h>
  18. #include <openssl/aes.h>
  19. #include <openssl/bn.h>
  20. #include <openssl/evp.h>
  21. #include "common.h"
  22. #include "crypto.h"
  23. #if OPENSSL_VERSION_NUMBER < 0x00907000
  24. #define DES_key_schedule des_key_schedule
  25. #define DES_cblock des_cblock
  26. #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
  27. #define DES_ecb_encrypt(input, output, ks, enc) \
  28. des_ecb_encrypt((input), (output), *(ks), (enc))
  29. #endif /* openssl < 0.9.7 */
  30. int openssl_digest_vector(const EVP_MD *type, int non_fips, size_t num_elem,
  31. const u8 *addr[], const size_t *len, u8 *mac)
  32. {
  33. EVP_MD_CTX ctx;
  34. size_t i;
  35. unsigned int mac_len;
  36. EVP_MD_CTX_init(&ctx);
  37. #ifdef CONFIG_FIPS
  38. #ifdef OPENSSL_FIPS
  39. if (non_fips)
  40. EVP_MD_CTX_set_flags(&ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  41. #endif /* OPENSSL_FIPS */
  42. #endif /* CONFIG_FIPS */
  43. if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
  44. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
  45. ERR_error_string(ERR_get_error(), NULL));
  46. return -1;
  47. }
  48. for (i = 0; i < num_elem; i++) {
  49. if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
  50. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
  51. "failed: %s",
  52. ERR_error_string(ERR_get_error(), NULL));
  53. return -1;
  54. }
  55. }
  56. if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
  57. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
  58. ERR_error_string(ERR_get_error(), NULL));
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  64. {
  65. return openssl_digest_vector(EVP_md4(), 0, num_elem, addr, len, mac);
  66. }
  67. void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
  68. {
  69. u8 pkey[8], next, tmp;
  70. int i;
  71. DES_key_schedule ks;
  72. /* Add parity bits to the key */
  73. next = 0;
  74. for (i = 0; i < 7; i++) {
  75. tmp = key[i];
  76. pkey[i] = (tmp >> i) | next | 1;
  77. next = tmp << (7 - i);
  78. }
  79. pkey[i] = next | 1;
  80. DES_set_key(&pkey, &ks);
  81. DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
  82. DES_ENCRYPT);
  83. }
  84. int rc4_skip(const u8 *key, size_t keylen, size_t skip,
  85. u8 *data, size_t data_len)
  86. {
  87. #ifdef OPENSSL_NO_RC4
  88. return -1;
  89. #else /* OPENSSL_NO_RC4 */
  90. EVP_CIPHER_CTX ctx;
  91. int outl;
  92. int res = -1;
  93. unsigned char skip_buf[16];
  94. EVP_CIPHER_CTX_init(&ctx);
  95. if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
  96. !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
  97. !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
  98. !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
  99. goto out;
  100. while (skip >= sizeof(skip_buf)) {
  101. size_t len = skip;
  102. if (len > sizeof(skip_buf))
  103. len = sizeof(skip_buf);
  104. if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
  105. goto out;
  106. skip -= len;
  107. }
  108. if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
  109. res = 0;
  110. out:
  111. EVP_CIPHER_CTX_cleanup(&ctx);
  112. return res;
  113. #endif /* OPENSSL_NO_RC4 */
  114. }
  115. int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  116. {
  117. return openssl_digest_vector(EVP_md5(), 0, num_elem, addr, len, mac);
  118. }
  119. #ifdef CONFIG_FIPS
  120. int md5_vector_non_fips_allow(size_t num_elem, const u8 *addr[],
  121. const size_t *len, u8 *mac)
  122. {
  123. return openssl_digest_vector(EVP_md5(), 1, num_elem, addr, len, mac);
  124. }
  125. #endif /* CONFIG_FIPS */
  126. int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  127. {
  128. return openssl_digest_vector(EVP_sha1(), 0, num_elem, addr, len, mac);
  129. }
  130. int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  131. u8 *mac)
  132. {
  133. return openssl_digest_vector(EVP_sha256(), 0, num_elem, addr, len,
  134. mac);
  135. }
  136. void * aes_encrypt_init(const u8 *key, size_t len)
  137. {
  138. AES_KEY *ak;
  139. ak = os_malloc(sizeof(*ak));
  140. if (ak == NULL)
  141. return NULL;
  142. if (AES_set_encrypt_key(key, 8 * len, ak) < 0) {
  143. os_free(ak);
  144. return NULL;
  145. }
  146. return ak;
  147. }
  148. void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
  149. {
  150. AES_encrypt(plain, crypt, ctx);
  151. }
  152. void aes_encrypt_deinit(void *ctx)
  153. {
  154. os_free(ctx);
  155. }
  156. void * aes_decrypt_init(const u8 *key, size_t len)
  157. {
  158. AES_KEY *ak;
  159. ak = os_malloc(sizeof(*ak));
  160. if (ak == NULL)
  161. return NULL;
  162. if (AES_set_decrypt_key(key, 8 * len, ak) < 0) {
  163. os_free(ak);
  164. return NULL;
  165. }
  166. return ak;
  167. }
  168. void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
  169. {
  170. AES_decrypt(crypt, plain, ctx);
  171. }
  172. void aes_decrypt_deinit(void *ctx)
  173. {
  174. os_free(ctx);
  175. }
  176. int crypto_mod_exp(const u8 *base, size_t base_len,
  177. const u8 *power, size_t power_len,
  178. const u8 *modulus, size_t modulus_len,
  179. u8 *result, size_t *result_len)
  180. {
  181. BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
  182. int ret = -1;
  183. BN_CTX *ctx;
  184. ctx = BN_CTX_new();
  185. if (ctx == NULL)
  186. return -1;
  187. bn_base = BN_bin2bn(base, base_len, NULL);
  188. bn_exp = BN_bin2bn(power, power_len, NULL);
  189. bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
  190. bn_result = BN_new();
  191. if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
  192. bn_result == NULL)
  193. goto error;
  194. if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
  195. goto error;
  196. *result_len = BN_bn2bin(bn_result, result);
  197. ret = 0;
  198. error:
  199. BN_free(bn_base);
  200. BN_free(bn_exp);
  201. BN_free(bn_modulus);
  202. BN_free(bn_result);
  203. BN_CTX_free(ctx);
  204. return ret;
  205. }
  206. struct crypto_cipher {
  207. EVP_CIPHER_CTX enc;
  208. EVP_CIPHER_CTX dec;
  209. };
  210. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  211. const u8 *iv, const u8 *key,
  212. size_t key_len)
  213. {
  214. struct crypto_cipher *ctx;
  215. const EVP_CIPHER *cipher;
  216. ctx = os_zalloc(sizeof(*ctx));
  217. if (ctx == NULL)
  218. return NULL;
  219. switch (alg) {
  220. #ifndef OPENSSL_NO_RC4
  221. case CRYPTO_CIPHER_ALG_RC4:
  222. cipher = EVP_rc4();
  223. break;
  224. #endif /* OPENSSL_NO_RC4 */
  225. #ifndef OPENSSL_NO_AES
  226. case CRYPTO_CIPHER_ALG_AES:
  227. switch (key_len) {
  228. case 16:
  229. cipher = EVP_aes_128_cbc();
  230. break;
  231. case 24:
  232. cipher = EVP_aes_192_cbc();
  233. break;
  234. case 32:
  235. cipher = EVP_aes_256_cbc();
  236. break;
  237. default:
  238. os_free(ctx);
  239. return NULL;
  240. }
  241. break;
  242. #endif /* OPENSSL_NO_AES */
  243. #ifndef OPENSSL_NO_DES
  244. case CRYPTO_CIPHER_ALG_3DES:
  245. cipher = EVP_des_ede3_cbc();
  246. break;
  247. case CRYPTO_CIPHER_ALG_DES:
  248. cipher = EVP_des_cbc();
  249. break;
  250. #endif /* OPENSSL_NO_DES */
  251. #ifndef OPENSSL_NO_RC2
  252. case CRYPTO_CIPHER_ALG_RC2:
  253. cipher = EVP_rc2_ecb();
  254. break;
  255. #endif /* OPENSSL_NO_RC2 */
  256. default:
  257. os_free(ctx);
  258. return NULL;
  259. }
  260. EVP_CIPHER_CTX_init(&ctx->enc);
  261. EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
  262. if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
  263. !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
  264. !EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, key, iv)) {
  265. EVP_CIPHER_CTX_cleanup(&ctx->enc);
  266. os_free(ctx);
  267. return NULL;
  268. }
  269. EVP_CIPHER_CTX_init(&ctx->dec);
  270. EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
  271. if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
  272. !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
  273. !EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, key, iv)) {
  274. EVP_CIPHER_CTX_cleanup(&ctx->enc);
  275. EVP_CIPHER_CTX_cleanup(&ctx->dec);
  276. os_free(ctx);
  277. return NULL;
  278. }
  279. return ctx;
  280. }
  281. int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
  282. u8 *crypt, size_t len)
  283. {
  284. int outl;
  285. if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
  286. return -1;
  287. return 0;
  288. }
  289. int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
  290. u8 *plain, size_t len)
  291. {
  292. int outl;
  293. outl = len;
  294. if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
  295. return -1;
  296. return 0;
  297. }
  298. void crypto_cipher_deinit(struct crypto_cipher *ctx)
  299. {
  300. EVP_CIPHER_CTX_cleanup(&ctx->enc);
  301. EVP_CIPHER_CTX_cleanup(&ctx->dec);
  302. os_free(ctx);
  303. }