crypto_openssl.c 26 KB

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