crypto_openssl.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*
  2. * Wrapper functions for OpenSSL libcrypto
  3. * Copyright (c) 2004-2015, 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 <openssl/opensslv.h>
  10. #include <openssl/err.h>
  11. #include <openssl/des.h>
  12. #include <openssl/aes.h>
  13. #include <openssl/bn.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/dh.h>
  16. #include <openssl/hmac.h>
  17. #include <openssl/rand.h>
  18. #ifdef CONFIG_OPENSSL_CMAC
  19. #include <openssl/cmac.h>
  20. #endif /* CONFIG_OPENSSL_CMAC */
  21. #ifdef CONFIG_ECC
  22. #include <openssl/ec.h>
  23. #endif /* CONFIG_ECC */
  24. #include "common.h"
  25. #include "wpabuf.h"
  26. #include "dh_group5.h"
  27. #include "sha1.h"
  28. #include "sha256.h"
  29. #include "sha384.h"
  30. #include "crypto.h"
  31. #if OPENSSL_VERSION_NUMBER < 0x00907000
  32. #define DES_key_schedule des_key_schedule
  33. #define DES_cblock des_cblock
  34. #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
  35. #define DES_ecb_encrypt(input, output, ks, enc) \
  36. des_ecb_encrypt((input), (output), *(ks), (enc))
  37. #endif /* openssl < 0.9.7 */
  38. static BIGNUM * get_group5_prime(void)
  39. {
  40. #if OPENSSL_VERSION_NUMBER < 0x00908000 || defined(OPENSSL_IS_BORINGSSL)
  41. static const unsigned char RFC3526_PRIME_1536[] = {
  42. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
  43. 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  44. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
  45. 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  46. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
  47. 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  48. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
  49. 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  50. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
  51. 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  52. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
  53. 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  54. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
  55. 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  56. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
  57. 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  58. };
  59. return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
  60. #else /* openssl < 0.9.8 */
  61. return get_rfc3526_prime_1536(NULL);
  62. #endif /* openssl < 0.9.8 */
  63. }
  64. #if OPENSSL_VERSION_NUMBER < 0x00908000
  65. #ifndef OPENSSL_NO_SHA256
  66. #ifndef OPENSSL_FIPS
  67. #define NO_SHA256_WRAPPER
  68. #endif
  69. #endif
  70. #endif /* openssl < 0.9.8 */
  71. #ifdef OPENSSL_NO_SHA256
  72. #define NO_SHA256_WRAPPER
  73. #endif
  74. static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
  75. const u8 *addr[], const size_t *len, u8 *mac)
  76. {
  77. EVP_MD_CTX ctx;
  78. size_t i;
  79. unsigned int mac_len;
  80. EVP_MD_CTX_init(&ctx);
  81. if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
  82. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
  83. ERR_error_string(ERR_get_error(), NULL));
  84. return -1;
  85. }
  86. for (i = 0; i < num_elem; i++) {
  87. if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
  88. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
  89. "failed: %s",
  90. ERR_error_string(ERR_get_error(), NULL));
  91. return -1;
  92. }
  93. }
  94. if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
  95. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
  96. ERR_error_string(ERR_get_error(), NULL));
  97. return -1;
  98. }
  99. return 0;
  100. }
  101. int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  102. {
  103. return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
  104. }
  105. void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
  106. {
  107. u8 pkey[8], next, tmp;
  108. int i;
  109. DES_key_schedule ks;
  110. /* Add parity bits to the key */
  111. next = 0;
  112. for (i = 0; i < 7; i++) {
  113. tmp = key[i];
  114. pkey[i] = (tmp >> i) | next | 1;
  115. next = tmp << (7 - i);
  116. }
  117. pkey[i] = next | 1;
  118. DES_set_key((DES_cblock *) &pkey, &ks);
  119. DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
  120. DES_ENCRYPT);
  121. }
  122. int rc4_skip(const u8 *key, size_t keylen, size_t skip,
  123. u8 *data, size_t data_len)
  124. {
  125. #ifdef OPENSSL_NO_RC4
  126. return -1;
  127. #else /* OPENSSL_NO_RC4 */
  128. EVP_CIPHER_CTX ctx;
  129. int outl;
  130. int res = -1;
  131. unsigned char skip_buf[16];
  132. EVP_CIPHER_CTX_init(&ctx);
  133. if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
  134. !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
  135. !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
  136. !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
  137. goto out;
  138. while (skip >= sizeof(skip_buf)) {
  139. size_t len = skip;
  140. if (len > sizeof(skip_buf))
  141. len = sizeof(skip_buf);
  142. if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
  143. goto out;
  144. skip -= len;
  145. }
  146. if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
  147. res = 0;
  148. out:
  149. EVP_CIPHER_CTX_cleanup(&ctx);
  150. return res;
  151. #endif /* OPENSSL_NO_RC4 */
  152. }
  153. int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  154. {
  155. return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
  156. }
  157. int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  158. {
  159. return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
  160. }
  161. #ifndef NO_SHA256_WRAPPER
  162. int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  163. u8 *mac)
  164. {
  165. return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
  166. }
  167. #endif /* NO_SHA256_WRAPPER */
  168. static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
  169. {
  170. switch (keylen) {
  171. case 16:
  172. return EVP_aes_128_ecb();
  173. #ifndef OPENSSL_IS_BORINGSSL
  174. case 24:
  175. return EVP_aes_192_ecb();
  176. #endif /* OPENSSL_IS_BORINGSSL */
  177. case 32:
  178. return EVP_aes_256_ecb();
  179. }
  180. return NULL;
  181. }
  182. void * aes_encrypt_init(const u8 *key, size_t len)
  183. {
  184. EVP_CIPHER_CTX *ctx;
  185. const EVP_CIPHER *type;
  186. type = aes_get_evp_cipher(len);
  187. if (type == NULL)
  188. return NULL;
  189. ctx = os_malloc(sizeof(*ctx));
  190. if (ctx == NULL)
  191. return NULL;
  192. EVP_CIPHER_CTX_init(ctx);
  193. if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
  194. os_free(ctx);
  195. return NULL;
  196. }
  197. EVP_CIPHER_CTX_set_padding(ctx, 0);
  198. return ctx;
  199. }
  200. void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
  201. {
  202. EVP_CIPHER_CTX *c = ctx;
  203. int clen = 16;
  204. if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
  205. wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
  206. ERR_error_string(ERR_get_error(), NULL));
  207. }
  208. }
  209. void aes_encrypt_deinit(void *ctx)
  210. {
  211. EVP_CIPHER_CTX *c = ctx;
  212. u8 buf[16];
  213. int len = sizeof(buf);
  214. if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
  215. wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
  216. "%s", ERR_error_string(ERR_get_error(), NULL));
  217. }
  218. if (len != 0) {
  219. wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
  220. "in AES encrypt", len);
  221. }
  222. EVP_CIPHER_CTX_cleanup(c);
  223. bin_clear_free(c, sizeof(*c));
  224. }
  225. void * aes_decrypt_init(const u8 *key, size_t len)
  226. {
  227. EVP_CIPHER_CTX *ctx;
  228. const EVP_CIPHER *type;
  229. type = aes_get_evp_cipher(len);
  230. if (type == NULL)
  231. return NULL;
  232. ctx = os_malloc(sizeof(*ctx));
  233. if (ctx == NULL)
  234. return NULL;
  235. EVP_CIPHER_CTX_init(ctx);
  236. if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
  237. os_free(ctx);
  238. return NULL;
  239. }
  240. EVP_CIPHER_CTX_set_padding(ctx, 0);
  241. return ctx;
  242. }
  243. void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
  244. {
  245. EVP_CIPHER_CTX *c = ctx;
  246. int plen = 16;
  247. if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
  248. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
  249. ERR_error_string(ERR_get_error(), NULL));
  250. }
  251. }
  252. void aes_decrypt_deinit(void *ctx)
  253. {
  254. EVP_CIPHER_CTX *c = ctx;
  255. u8 buf[16];
  256. int len = sizeof(buf);
  257. if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
  258. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
  259. "%s", ERR_error_string(ERR_get_error(), NULL));
  260. }
  261. if (len != 0) {
  262. wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
  263. "in AES decrypt", len);
  264. }
  265. EVP_CIPHER_CTX_cleanup(c);
  266. bin_clear_free(c, sizeof(*c));
  267. }
  268. int crypto_mod_exp(const u8 *base, size_t base_len,
  269. const u8 *power, size_t power_len,
  270. const u8 *modulus, size_t modulus_len,
  271. u8 *result, size_t *result_len)
  272. {
  273. BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
  274. int ret = -1;
  275. BN_CTX *ctx;
  276. ctx = BN_CTX_new();
  277. if (ctx == NULL)
  278. return -1;
  279. bn_base = BN_bin2bn(base, base_len, NULL);
  280. bn_exp = BN_bin2bn(power, power_len, NULL);
  281. bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
  282. bn_result = BN_new();
  283. if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
  284. bn_result == NULL)
  285. goto error;
  286. if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
  287. goto error;
  288. *result_len = BN_bn2bin(bn_result, result);
  289. ret = 0;
  290. error:
  291. BN_clear_free(bn_base);
  292. BN_clear_free(bn_exp);
  293. BN_clear_free(bn_modulus);
  294. BN_clear_free(bn_result);
  295. BN_CTX_free(ctx);
  296. return ret;
  297. }
  298. struct crypto_cipher {
  299. EVP_CIPHER_CTX enc;
  300. EVP_CIPHER_CTX dec;
  301. };
  302. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  303. const u8 *iv, const u8 *key,
  304. size_t key_len)
  305. {
  306. struct crypto_cipher *ctx;
  307. const EVP_CIPHER *cipher;
  308. ctx = os_zalloc(sizeof(*ctx));
  309. if (ctx == NULL)
  310. return NULL;
  311. switch (alg) {
  312. #ifndef OPENSSL_NO_RC4
  313. case CRYPTO_CIPHER_ALG_RC4:
  314. cipher = EVP_rc4();
  315. break;
  316. #endif /* OPENSSL_NO_RC4 */
  317. #ifndef OPENSSL_NO_AES
  318. case CRYPTO_CIPHER_ALG_AES:
  319. switch (key_len) {
  320. case 16:
  321. cipher = EVP_aes_128_cbc();
  322. break;
  323. #ifndef OPENSSL_IS_BORINGSSL
  324. case 24:
  325. cipher = EVP_aes_192_cbc();
  326. break;
  327. #endif /* OPENSSL_IS_BORINGSSL */
  328. case 32:
  329. cipher = EVP_aes_256_cbc();
  330. break;
  331. default:
  332. os_free(ctx);
  333. return NULL;
  334. }
  335. break;
  336. #endif /* OPENSSL_NO_AES */
  337. #ifndef OPENSSL_NO_DES
  338. case CRYPTO_CIPHER_ALG_3DES:
  339. cipher = EVP_des_ede3_cbc();
  340. break;
  341. case CRYPTO_CIPHER_ALG_DES:
  342. cipher = EVP_des_cbc();
  343. break;
  344. #endif /* OPENSSL_NO_DES */
  345. #ifndef OPENSSL_NO_RC2
  346. case CRYPTO_CIPHER_ALG_RC2:
  347. cipher = EVP_rc2_ecb();
  348. break;
  349. #endif /* OPENSSL_NO_RC2 */
  350. default:
  351. os_free(ctx);
  352. return NULL;
  353. }
  354. EVP_CIPHER_CTX_init(&ctx->enc);
  355. EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
  356. if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
  357. !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
  358. !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
  359. EVP_CIPHER_CTX_cleanup(&ctx->enc);
  360. os_free(ctx);
  361. return NULL;
  362. }
  363. EVP_CIPHER_CTX_init(&ctx->dec);
  364. EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
  365. if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
  366. !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
  367. !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
  368. EVP_CIPHER_CTX_cleanup(&ctx->enc);
  369. EVP_CIPHER_CTX_cleanup(&ctx->dec);
  370. os_free(ctx);
  371. return NULL;
  372. }
  373. return ctx;
  374. }
  375. int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
  376. u8 *crypt, size_t len)
  377. {
  378. int outl;
  379. if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
  380. return -1;
  381. return 0;
  382. }
  383. int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
  384. u8 *plain, size_t len)
  385. {
  386. int outl;
  387. outl = len;
  388. if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
  389. return -1;
  390. return 0;
  391. }
  392. void crypto_cipher_deinit(struct crypto_cipher *ctx)
  393. {
  394. EVP_CIPHER_CTX_cleanup(&ctx->enc);
  395. EVP_CIPHER_CTX_cleanup(&ctx->dec);
  396. os_free(ctx);
  397. }
  398. void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
  399. {
  400. DH *dh;
  401. struct wpabuf *pubkey = NULL, *privkey = NULL;
  402. size_t publen, privlen;
  403. *priv = NULL;
  404. *publ = NULL;
  405. dh = DH_new();
  406. if (dh == NULL)
  407. return NULL;
  408. dh->g = BN_new();
  409. if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
  410. goto err;
  411. dh->p = get_group5_prime();
  412. if (dh->p == NULL)
  413. goto err;
  414. if (DH_generate_key(dh) != 1)
  415. goto err;
  416. publen = BN_num_bytes(dh->pub_key);
  417. pubkey = wpabuf_alloc(publen);
  418. if (pubkey == NULL)
  419. goto err;
  420. privlen = BN_num_bytes(dh->priv_key);
  421. privkey = wpabuf_alloc(privlen);
  422. if (privkey == NULL)
  423. goto err;
  424. BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
  425. BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
  426. *priv = privkey;
  427. *publ = pubkey;
  428. return dh;
  429. err:
  430. wpabuf_clear_free(pubkey);
  431. wpabuf_clear_free(privkey);
  432. DH_free(dh);
  433. return NULL;
  434. }
  435. void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
  436. {
  437. DH *dh;
  438. dh = DH_new();
  439. if (dh == NULL)
  440. return NULL;
  441. dh->g = BN_new();
  442. if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
  443. goto err;
  444. dh->p = get_group5_prime();
  445. if (dh->p == NULL)
  446. goto err;
  447. dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
  448. if (dh->priv_key == NULL)
  449. goto err;
  450. dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
  451. if (dh->pub_key == NULL)
  452. goto err;
  453. if (DH_generate_key(dh) != 1)
  454. goto err;
  455. return dh;
  456. err:
  457. DH_free(dh);
  458. return NULL;
  459. }
  460. struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
  461. const struct wpabuf *own_private)
  462. {
  463. BIGNUM *pub_key;
  464. struct wpabuf *res = NULL;
  465. size_t rlen;
  466. DH *dh = ctx;
  467. int keylen;
  468. if (ctx == NULL)
  469. return NULL;
  470. pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
  471. NULL);
  472. if (pub_key == NULL)
  473. return NULL;
  474. rlen = DH_size(dh);
  475. res = wpabuf_alloc(rlen);
  476. if (res == NULL)
  477. goto err;
  478. keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
  479. if (keylen < 0)
  480. goto err;
  481. wpabuf_put(res, keylen);
  482. BN_clear_free(pub_key);
  483. return res;
  484. err:
  485. BN_clear_free(pub_key);
  486. wpabuf_clear_free(res);
  487. return NULL;
  488. }
  489. void dh5_free(void *ctx)
  490. {
  491. DH *dh;
  492. if (ctx == NULL)
  493. return;
  494. dh = ctx;
  495. DH_free(dh);
  496. }
  497. struct crypto_hash {
  498. HMAC_CTX ctx;
  499. };
  500. struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
  501. size_t key_len)
  502. {
  503. struct crypto_hash *ctx;
  504. const EVP_MD *md;
  505. switch (alg) {
  506. #ifndef OPENSSL_NO_MD5
  507. case CRYPTO_HASH_ALG_HMAC_MD5:
  508. md = EVP_md5();
  509. break;
  510. #endif /* OPENSSL_NO_MD5 */
  511. #ifndef OPENSSL_NO_SHA
  512. case CRYPTO_HASH_ALG_HMAC_SHA1:
  513. md = EVP_sha1();
  514. break;
  515. #endif /* OPENSSL_NO_SHA */
  516. #ifndef OPENSSL_NO_SHA256
  517. #ifdef CONFIG_SHA256
  518. case CRYPTO_HASH_ALG_HMAC_SHA256:
  519. md = EVP_sha256();
  520. break;
  521. #endif /* CONFIG_SHA256 */
  522. #endif /* OPENSSL_NO_SHA256 */
  523. default:
  524. return NULL;
  525. }
  526. ctx = os_zalloc(sizeof(*ctx));
  527. if (ctx == NULL)
  528. return NULL;
  529. HMAC_CTX_init(&ctx->ctx);
  530. #if OPENSSL_VERSION_NUMBER < 0x00909000
  531. HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL);
  532. #else /* openssl < 0.9.9 */
  533. if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
  534. bin_clear_free(ctx, sizeof(*ctx));
  535. return NULL;
  536. }
  537. #endif /* openssl < 0.9.9 */
  538. return ctx;
  539. }
  540. void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
  541. {
  542. if (ctx == NULL)
  543. return;
  544. HMAC_Update(&ctx->ctx, data, len);
  545. }
  546. int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
  547. {
  548. unsigned int mdlen;
  549. int res;
  550. if (ctx == NULL)
  551. return -2;
  552. if (mac == NULL || len == NULL) {
  553. bin_clear_free(ctx, sizeof(*ctx));
  554. return 0;
  555. }
  556. mdlen = *len;
  557. #if OPENSSL_VERSION_NUMBER < 0x00909000
  558. HMAC_Final(&ctx->ctx, mac, &mdlen);
  559. res = 1;
  560. #else /* openssl < 0.9.9 */
  561. res = HMAC_Final(&ctx->ctx, mac, &mdlen);
  562. #endif /* openssl < 0.9.9 */
  563. HMAC_CTX_cleanup(&ctx->ctx);
  564. bin_clear_free(ctx, sizeof(*ctx));
  565. if (res == 1) {
  566. *len = mdlen;
  567. return 0;
  568. }
  569. return -1;
  570. }
  571. static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
  572. size_t key_len, size_t num_elem,
  573. const u8 *addr[], const size_t *len, u8 *mac,
  574. unsigned int mdlen)
  575. {
  576. HMAC_CTX ctx;
  577. size_t i;
  578. int res;
  579. HMAC_CTX_init(&ctx);
  580. #if OPENSSL_VERSION_NUMBER < 0x00909000
  581. HMAC_Init_ex(&ctx, key, key_len, type, NULL);
  582. #else /* openssl < 0.9.9 */
  583. if (HMAC_Init_ex(&ctx, key, key_len, type, NULL) != 1)
  584. return -1;
  585. #endif /* openssl < 0.9.9 */
  586. for (i = 0; i < num_elem; i++)
  587. HMAC_Update(&ctx, addr[i], len[i]);
  588. #if OPENSSL_VERSION_NUMBER < 0x00909000
  589. HMAC_Final(&ctx, mac, &mdlen);
  590. res = 1;
  591. #else /* openssl < 0.9.9 */
  592. res = HMAC_Final(&ctx, mac, &mdlen);
  593. #endif /* openssl < 0.9.9 */
  594. HMAC_CTX_cleanup(&ctx);
  595. return res == 1 ? 0 : -1;
  596. }
  597. #ifndef CONFIG_FIPS
  598. int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
  599. const u8 *addr[], const size_t *len, u8 *mac)
  600. {
  601. return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
  602. mac, 16);
  603. }
  604. int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  605. u8 *mac)
  606. {
  607. return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
  608. }
  609. #endif /* CONFIG_FIPS */
  610. int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
  611. int iterations, u8 *buf, size_t buflen)
  612. {
  613. #if OPENSSL_VERSION_NUMBER < 0x00908000
  614. if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase),
  615. (unsigned char *) ssid,
  616. ssid_len, iterations, buflen, buf) != 1)
  617. return -1;
  618. #else /* openssl < 0.9.8 */
  619. if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
  620. ssid_len, iterations, buflen, buf) != 1)
  621. return -1;
  622. #endif /* openssl < 0.9.8 */
  623. return 0;
  624. }
  625. int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
  626. const u8 *addr[], const size_t *len, u8 *mac)
  627. {
  628. return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr,
  629. len, mac, 20);
  630. }
  631. int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  632. u8 *mac)
  633. {
  634. return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
  635. }
  636. #ifdef CONFIG_SHA256
  637. int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
  638. const u8 *addr[], const size_t *len, u8 *mac)
  639. {
  640. return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
  641. len, mac, 32);
  642. }
  643. int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
  644. size_t data_len, u8 *mac)
  645. {
  646. return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
  647. }
  648. #endif /* CONFIG_SHA256 */
  649. #ifdef CONFIG_SHA384
  650. int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
  651. const u8 *addr[], const size_t *len, u8 *mac)
  652. {
  653. return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
  654. len, mac, 32);
  655. }
  656. int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
  657. size_t data_len, u8 *mac)
  658. {
  659. return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
  660. }
  661. #endif /* CONFIG_SHA384 */
  662. int crypto_get_random(void *buf, size_t len)
  663. {
  664. if (RAND_bytes(buf, len) != 1)
  665. return -1;
  666. return 0;
  667. }
  668. #ifdef CONFIG_OPENSSL_CMAC
  669. int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
  670. const u8 *addr[], const size_t *len, u8 *mac)
  671. {
  672. CMAC_CTX *ctx;
  673. int ret = -1;
  674. size_t outlen, i;
  675. ctx = CMAC_CTX_new();
  676. if (ctx == NULL)
  677. return -1;
  678. if (key_len == 32) {
  679. if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
  680. goto fail;
  681. } else if (key_len == 16) {
  682. if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
  683. goto fail;
  684. } else {
  685. goto fail;
  686. }
  687. for (i = 0; i < num_elem; i++) {
  688. if (!CMAC_Update(ctx, addr[i], len[i]))
  689. goto fail;
  690. }
  691. if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
  692. goto fail;
  693. ret = 0;
  694. fail:
  695. CMAC_CTX_free(ctx);
  696. return ret;
  697. }
  698. int omac1_aes_128_vector(const u8 *key, size_t num_elem,
  699. const u8 *addr[], const size_t *len, u8 *mac)
  700. {
  701. return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
  702. }
  703. int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
  704. {
  705. return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
  706. }
  707. int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
  708. {
  709. return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
  710. }
  711. #endif /* CONFIG_OPENSSL_CMAC */
  712. struct crypto_bignum * crypto_bignum_init(void)
  713. {
  714. return (struct crypto_bignum *) BN_new();
  715. }
  716. struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
  717. {
  718. BIGNUM *bn = BN_bin2bn(buf, len, NULL);
  719. return (struct crypto_bignum *) bn;
  720. }
  721. void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
  722. {
  723. if (clear)
  724. BN_clear_free((BIGNUM *) n);
  725. else
  726. BN_free((BIGNUM *) n);
  727. }
  728. int crypto_bignum_to_bin(const struct crypto_bignum *a,
  729. u8 *buf, size_t buflen, size_t padlen)
  730. {
  731. int num_bytes, offset;
  732. if (padlen > buflen)
  733. return -1;
  734. num_bytes = BN_num_bytes((const BIGNUM *) a);
  735. if ((size_t) num_bytes > buflen)
  736. return -1;
  737. if (padlen > (size_t) num_bytes)
  738. offset = padlen - num_bytes;
  739. else
  740. offset = 0;
  741. os_memset(buf, 0, offset);
  742. BN_bn2bin((const BIGNUM *) a, buf + offset);
  743. return num_bytes + offset;
  744. }
  745. int crypto_bignum_add(const struct crypto_bignum *a,
  746. const struct crypto_bignum *b,
  747. struct crypto_bignum *c)
  748. {
  749. return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
  750. 0 : -1;
  751. }
  752. int crypto_bignum_mod(const struct crypto_bignum *a,
  753. const struct crypto_bignum *b,
  754. struct crypto_bignum *c)
  755. {
  756. int res;
  757. BN_CTX *bnctx;
  758. bnctx = BN_CTX_new();
  759. if (bnctx == NULL)
  760. return -1;
  761. res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
  762. bnctx);
  763. BN_CTX_free(bnctx);
  764. return res ? 0 : -1;
  765. }
  766. int crypto_bignum_exptmod(const struct crypto_bignum *a,
  767. const struct crypto_bignum *b,
  768. const struct crypto_bignum *c,
  769. struct crypto_bignum *d)
  770. {
  771. int res;
  772. BN_CTX *bnctx;
  773. bnctx = BN_CTX_new();
  774. if (bnctx == NULL)
  775. return -1;
  776. res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
  777. (const BIGNUM *) c, bnctx);
  778. BN_CTX_free(bnctx);
  779. return res ? 0 : -1;
  780. }
  781. int crypto_bignum_inverse(const struct crypto_bignum *a,
  782. const struct crypto_bignum *b,
  783. struct crypto_bignum *c)
  784. {
  785. BIGNUM *res;
  786. BN_CTX *bnctx;
  787. bnctx = BN_CTX_new();
  788. if (bnctx == NULL)
  789. return -1;
  790. res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
  791. (const BIGNUM *) b, bnctx);
  792. BN_CTX_free(bnctx);
  793. return res ? 0 : -1;
  794. }
  795. int crypto_bignum_sub(const struct crypto_bignum *a,
  796. const struct crypto_bignum *b,
  797. struct crypto_bignum *c)
  798. {
  799. return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
  800. 0 : -1;
  801. }
  802. int crypto_bignum_div(const struct crypto_bignum *a,
  803. const struct crypto_bignum *b,
  804. struct crypto_bignum *c)
  805. {
  806. int res;
  807. BN_CTX *bnctx;
  808. bnctx = BN_CTX_new();
  809. if (bnctx == NULL)
  810. return -1;
  811. res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
  812. (const BIGNUM *) b, bnctx);
  813. BN_CTX_free(bnctx);
  814. return res ? 0 : -1;
  815. }
  816. int crypto_bignum_mulmod(const struct crypto_bignum *a,
  817. const struct crypto_bignum *b,
  818. const struct crypto_bignum *c,
  819. struct crypto_bignum *d)
  820. {
  821. int res;
  822. BN_CTX *bnctx;
  823. bnctx = BN_CTX_new();
  824. if (bnctx == NULL)
  825. return -1;
  826. res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
  827. (const BIGNUM *) c, bnctx);
  828. BN_CTX_free(bnctx);
  829. return res ? 0 : -1;
  830. }
  831. int crypto_bignum_cmp(const struct crypto_bignum *a,
  832. const struct crypto_bignum *b)
  833. {
  834. return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
  835. }
  836. int crypto_bignum_bits(const struct crypto_bignum *a)
  837. {
  838. return BN_num_bits((const BIGNUM *) a);
  839. }
  840. int crypto_bignum_is_zero(const struct crypto_bignum *a)
  841. {
  842. return BN_is_zero((const BIGNUM *) a);
  843. }
  844. int crypto_bignum_is_one(const struct crypto_bignum *a)
  845. {
  846. return BN_is_one((const BIGNUM *) a);
  847. }
  848. #ifdef CONFIG_ECC
  849. struct crypto_ec {
  850. EC_GROUP *group;
  851. BN_CTX *bnctx;
  852. BIGNUM *prime;
  853. BIGNUM *order;
  854. };
  855. struct crypto_ec * crypto_ec_init(int group)
  856. {
  857. struct crypto_ec *e;
  858. int nid;
  859. /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
  860. switch (group) {
  861. case 19:
  862. nid = NID_X9_62_prime256v1;
  863. break;
  864. case 20:
  865. nid = NID_secp384r1;
  866. break;
  867. case 21:
  868. nid = NID_secp521r1;
  869. break;
  870. case 25:
  871. nid = NID_X9_62_prime192v1;
  872. break;
  873. case 26:
  874. nid = NID_secp224r1;
  875. break;
  876. default:
  877. return NULL;
  878. }
  879. e = os_zalloc(sizeof(*e));
  880. if (e == NULL)
  881. return NULL;
  882. e->bnctx = BN_CTX_new();
  883. e->group = EC_GROUP_new_by_curve_name(nid);
  884. e->prime = BN_new();
  885. e->order = BN_new();
  886. if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
  887. e->order == NULL ||
  888. !EC_GROUP_get_curve_GFp(e->group, e->prime, NULL, NULL, e->bnctx) ||
  889. !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
  890. crypto_ec_deinit(e);
  891. e = NULL;
  892. }
  893. return e;
  894. }
  895. void crypto_ec_deinit(struct crypto_ec *e)
  896. {
  897. if (e == NULL)
  898. return;
  899. BN_clear_free(e->order);
  900. BN_clear_free(e->prime);
  901. EC_GROUP_free(e->group);
  902. BN_CTX_free(e->bnctx);
  903. os_free(e);
  904. }
  905. struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
  906. {
  907. if (e == NULL)
  908. return NULL;
  909. return (struct crypto_ec_point *) EC_POINT_new(e->group);
  910. }
  911. size_t crypto_ec_prime_len(struct crypto_ec *e)
  912. {
  913. return BN_num_bytes(e->prime);
  914. }
  915. size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
  916. {
  917. return BN_num_bits(e->prime);
  918. }
  919. const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
  920. {
  921. return (const struct crypto_bignum *) e->prime;
  922. }
  923. const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
  924. {
  925. return (const struct crypto_bignum *) e->order;
  926. }
  927. void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
  928. {
  929. if (clear)
  930. EC_POINT_clear_free((EC_POINT *) p);
  931. else
  932. EC_POINT_free((EC_POINT *) p);
  933. }
  934. int crypto_ec_point_to_bin(struct crypto_ec *e,
  935. const struct crypto_ec_point *point, u8 *x, u8 *y)
  936. {
  937. BIGNUM *x_bn, *y_bn;
  938. int ret = -1;
  939. int len = BN_num_bytes(e->prime);
  940. x_bn = BN_new();
  941. y_bn = BN_new();
  942. if (x_bn && y_bn &&
  943. EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
  944. x_bn, y_bn, e->bnctx)) {
  945. if (x) {
  946. crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
  947. x, len, len);
  948. }
  949. if (y) {
  950. crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
  951. y, len, len);
  952. }
  953. ret = 0;
  954. }
  955. BN_clear_free(x_bn);
  956. BN_clear_free(y_bn);
  957. return ret;
  958. }
  959. struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
  960. const u8 *val)
  961. {
  962. BIGNUM *x, *y;
  963. EC_POINT *elem;
  964. int len = BN_num_bytes(e->prime);
  965. x = BN_bin2bn(val, len, NULL);
  966. y = BN_bin2bn(val + len, len, NULL);
  967. elem = EC_POINT_new(e->group);
  968. if (x == NULL || y == NULL || elem == NULL) {
  969. BN_clear_free(x);
  970. BN_clear_free(y);
  971. EC_POINT_clear_free(elem);
  972. return NULL;
  973. }
  974. if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
  975. e->bnctx)) {
  976. EC_POINT_clear_free(elem);
  977. elem = NULL;
  978. }
  979. BN_clear_free(x);
  980. BN_clear_free(y);
  981. return (struct crypto_ec_point *) elem;
  982. }
  983. int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
  984. const struct crypto_ec_point *b,
  985. struct crypto_ec_point *c)
  986. {
  987. return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
  988. (const EC_POINT *) b, e->bnctx) ? 0 : -1;
  989. }
  990. int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
  991. const struct crypto_bignum *b,
  992. struct crypto_ec_point *res)
  993. {
  994. return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
  995. (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
  996. ? 0 : -1;
  997. }
  998. int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
  999. {
  1000. return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
  1001. }
  1002. int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
  1003. struct crypto_ec_point *p,
  1004. const struct crypto_bignum *x, int y_bit)
  1005. {
  1006. if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
  1007. (const BIGNUM *) x, y_bit,
  1008. e->bnctx) ||
  1009. !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
  1010. return -1;
  1011. return 0;
  1012. }
  1013. int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
  1014. const struct crypto_ec_point *p)
  1015. {
  1016. return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
  1017. }
  1018. int crypto_ec_point_is_on_curve(struct crypto_ec *e,
  1019. const struct crypto_ec_point *p)
  1020. {
  1021. return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p, e->bnctx);
  1022. }
  1023. #endif /* CONFIG_ECC */