crypto_openssl.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  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
  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(&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. case 24:
  173. return EVP_aes_192_ecb();
  174. case 32:
  175. return EVP_aes_256_ecb();
  176. }
  177. return NULL;
  178. }
  179. void * aes_encrypt_init(const u8 *key, size_t len)
  180. {
  181. EVP_CIPHER_CTX *ctx;
  182. const EVP_CIPHER *type;
  183. type = aes_get_evp_cipher(len);
  184. if (type == NULL)
  185. return NULL;
  186. ctx = os_malloc(sizeof(*ctx));
  187. if (ctx == NULL)
  188. return NULL;
  189. EVP_CIPHER_CTX_init(ctx);
  190. if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
  191. os_free(ctx);
  192. return NULL;
  193. }
  194. EVP_CIPHER_CTX_set_padding(ctx, 0);
  195. return ctx;
  196. }
  197. void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
  198. {
  199. EVP_CIPHER_CTX *c = ctx;
  200. int clen = 16;
  201. if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
  202. wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
  203. ERR_error_string(ERR_get_error(), NULL));
  204. }
  205. }
  206. void aes_encrypt_deinit(void *ctx)
  207. {
  208. EVP_CIPHER_CTX *c = ctx;
  209. u8 buf[16];
  210. int len = sizeof(buf);
  211. if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
  212. wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
  213. "%s", ERR_error_string(ERR_get_error(), NULL));
  214. }
  215. if (len != 0) {
  216. wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
  217. "in AES encrypt", len);
  218. }
  219. EVP_CIPHER_CTX_cleanup(c);
  220. os_free(c);
  221. }
  222. void * aes_decrypt_init(const u8 *key, size_t len)
  223. {
  224. EVP_CIPHER_CTX *ctx;
  225. const EVP_CIPHER *type;
  226. type = aes_get_evp_cipher(len);
  227. if (type == NULL)
  228. return NULL;
  229. ctx = os_malloc(sizeof(*ctx));
  230. if (ctx == NULL)
  231. return NULL;
  232. EVP_CIPHER_CTX_init(ctx);
  233. if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
  234. os_free(ctx);
  235. return NULL;
  236. }
  237. EVP_CIPHER_CTX_set_padding(ctx, 0);
  238. return ctx;
  239. }
  240. void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
  241. {
  242. EVP_CIPHER_CTX *c = ctx;
  243. int plen = 16;
  244. if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
  245. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
  246. ERR_error_string(ERR_get_error(), NULL));
  247. }
  248. }
  249. void aes_decrypt_deinit(void *ctx)
  250. {
  251. EVP_CIPHER_CTX *c = ctx;
  252. u8 buf[16];
  253. int len = sizeof(buf);
  254. if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
  255. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
  256. "%s", ERR_error_string(ERR_get_error(), NULL));
  257. }
  258. if (len != 0) {
  259. wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
  260. "in AES decrypt", len);
  261. }
  262. EVP_CIPHER_CTX_cleanup(c);
  263. os_free(ctx);
  264. }
  265. int crypto_mod_exp(const u8 *base, size_t base_len,
  266. const u8 *power, size_t power_len,
  267. const u8 *modulus, size_t modulus_len,
  268. u8 *result, size_t *result_len)
  269. {
  270. BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
  271. int ret = -1;
  272. BN_CTX *ctx;
  273. ctx = BN_CTX_new();
  274. if (ctx == NULL)
  275. return -1;
  276. bn_base = BN_bin2bn(base, base_len, NULL);
  277. bn_exp = BN_bin2bn(power, power_len, NULL);
  278. bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
  279. bn_result = BN_new();
  280. if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
  281. bn_result == NULL)
  282. goto error;
  283. if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
  284. goto error;
  285. *result_len = BN_bn2bin(bn_result, result);
  286. ret = 0;
  287. error:
  288. BN_free(bn_base);
  289. BN_free(bn_exp);
  290. BN_free(bn_modulus);
  291. BN_free(bn_result);
  292. BN_CTX_free(ctx);
  293. return ret;
  294. }
  295. struct crypto_cipher {
  296. EVP_CIPHER_CTX enc;
  297. EVP_CIPHER_CTX dec;
  298. };
  299. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  300. const u8 *iv, const u8 *key,
  301. size_t key_len)
  302. {
  303. struct crypto_cipher *ctx;
  304. const EVP_CIPHER *cipher;
  305. ctx = os_zalloc(sizeof(*ctx));
  306. if (ctx == NULL)
  307. return NULL;
  308. switch (alg) {
  309. #ifndef OPENSSL_NO_RC4
  310. case CRYPTO_CIPHER_ALG_RC4:
  311. cipher = EVP_rc4();
  312. break;
  313. #endif /* OPENSSL_NO_RC4 */
  314. #ifndef OPENSSL_NO_AES
  315. case CRYPTO_CIPHER_ALG_AES:
  316. switch (key_len) {
  317. case 16:
  318. cipher = EVP_aes_128_cbc();
  319. break;
  320. case 24:
  321. cipher = EVP_aes_192_cbc();
  322. break;
  323. case 32:
  324. cipher = EVP_aes_256_cbc();
  325. break;
  326. default:
  327. os_free(ctx);
  328. return NULL;
  329. }
  330. break;
  331. #endif /* OPENSSL_NO_AES */
  332. #ifndef OPENSSL_NO_DES
  333. case CRYPTO_CIPHER_ALG_3DES:
  334. cipher = EVP_des_ede3_cbc();
  335. break;
  336. case CRYPTO_CIPHER_ALG_DES:
  337. cipher = EVP_des_cbc();
  338. break;
  339. #endif /* OPENSSL_NO_DES */
  340. #ifndef OPENSSL_NO_RC2
  341. case CRYPTO_CIPHER_ALG_RC2:
  342. cipher = EVP_rc2_ecb();
  343. break;
  344. #endif /* OPENSSL_NO_RC2 */
  345. default:
  346. os_free(ctx);
  347. return NULL;
  348. }
  349. EVP_CIPHER_CTX_init(&ctx->enc);
  350. EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
  351. if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
  352. !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
  353. !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
  354. EVP_CIPHER_CTX_cleanup(&ctx->enc);
  355. os_free(ctx);
  356. return NULL;
  357. }
  358. EVP_CIPHER_CTX_init(&ctx->dec);
  359. EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
  360. if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
  361. !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
  362. !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
  363. EVP_CIPHER_CTX_cleanup(&ctx->enc);
  364. EVP_CIPHER_CTX_cleanup(&ctx->dec);
  365. os_free(ctx);
  366. return NULL;
  367. }
  368. return ctx;
  369. }
  370. int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
  371. u8 *crypt, size_t len)
  372. {
  373. int outl;
  374. if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
  375. return -1;
  376. return 0;
  377. }
  378. int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
  379. u8 *plain, size_t len)
  380. {
  381. int outl;
  382. outl = len;
  383. if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
  384. return -1;
  385. return 0;
  386. }
  387. void crypto_cipher_deinit(struct crypto_cipher *ctx)
  388. {
  389. EVP_CIPHER_CTX_cleanup(&ctx->enc);
  390. EVP_CIPHER_CTX_cleanup(&ctx->dec);
  391. os_free(ctx);
  392. }
  393. void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
  394. {
  395. DH *dh;
  396. struct wpabuf *pubkey = NULL, *privkey = NULL;
  397. size_t publen, privlen;
  398. *priv = NULL;
  399. *publ = NULL;
  400. dh = DH_new();
  401. if (dh == NULL)
  402. return NULL;
  403. dh->g = BN_new();
  404. if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
  405. goto err;
  406. dh->p = get_group5_prime();
  407. if (dh->p == NULL)
  408. goto err;
  409. if (DH_generate_key(dh) != 1)
  410. goto err;
  411. publen = BN_num_bytes(dh->pub_key);
  412. pubkey = wpabuf_alloc(publen);
  413. if (pubkey == NULL)
  414. goto err;
  415. privlen = BN_num_bytes(dh->priv_key);
  416. privkey = wpabuf_alloc(privlen);
  417. if (privkey == NULL)
  418. goto err;
  419. BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
  420. BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
  421. *priv = privkey;
  422. *publ = pubkey;
  423. return dh;
  424. err:
  425. wpabuf_free(pubkey);
  426. wpabuf_free(privkey);
  427. DH_free(dh);
  428. return NULL;
  429. }
  430. void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
  431. {
  432. DH *dh;
  433. dh = DH_new();
  434. if (dh == NULL)
  435. return NULL;
  436. dh->g = BN_new();
  437. if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
  438. goto err;
  439. dh->p = get_group5_prime();
  440. if (dh->p == NULL)
  441. goto err;
  442. dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
  443. if (dh->priv_key == NULL)
  444. goto err;
  445. dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
  446. if (dh->pub_key == NULL)
  447. goto err;
  448. if (DH_generate_key(dh) != 1)
  449. goto err;
  450. return dh;
  451. err:
  452. DH_free(dh);
  453. return NULL;
  454. }
  455. struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
  456. const struct wpabuf *own_private)
  457. {
  458. BIGNUM *pub_key;
  459. struct wpabuf *res = NULL;
  460. size_t rlen;
  461. DH *dh = ctx;
  462. int keylen;
  463. if (ctx == NULL)
  464. return NULL;
  465. pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
  466. NULL);
  467. if (pub_key == NULL)
  468. return NULL;
  469. rlen = DH_size(dh);
  470. res = wpabuf_alloc(rlen);
  471. if (res == NULL)
  472. goto err;
  473. keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
  474. if (keylen < 0)
  475. goto err;
  476. wpabuf_put(res, keylen);
  477. BN_free(pub_key);
  478. return res;
  479. err:
  480. BN_free(pub_key);
  481. wpabuf_free(res);
  482. return NULL;
  483. }
  484. void dh5_free(void *ctx)
  485. {
  486. DH *dh;
  487. if (ctx == NULL)
  488. return;
  489. dh = ctx;
  490. DH_free(dh);
  491. }
  492. struct crypto_hash {
  493. HMAC_CTX ctx;
  494. };
  495. struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
  496. size_t key_len)
  497. {
  498. struct crypto_hash *ctx;
  499. const EVP_MD *md;
  500. switch (alg) {
  501. #ifndef OPENSSL_NO_MD5
  502. case CRYPTO_HASH_ALG_HMAC_MD5:
  503. md = EVP_md5();
  504. break;
  505. #endif /* OPENSSL_NO_MD5 */
  506. #ifndef OPENSSL_NO_SHA
  507. case CRYPTO_HASH_ALG_HMAC_SHA1:
  508. md = EVP_sha1();
  509. break;
  510. #endif /* OPENSSL_NO_SHA */
  511. #ifndef OPENSSL_NO_SHA256
  512. #ifdef CONFIG_SHA256
  513. case CRYPTO_HASH_ALG_HMAC_SHA256:
  514. md = EVP_sha256();
  515. break;
  516. #endif /* CONFIG_SHA256 */
  517. #endif /* OPENSSL_NO_SHA256 */
  518. default:
  519. return NULL;
  520. }
  521. ctx = os_zalloc(sizeof(*ctx));
  522. if (ctx == NULL)
  523. return NULL;
  524. HMAC_CTX_init(&ctx->ctx);
  525. #if OPENSSL_VERSION_NUMBER < 0x00909000
  526. HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL);
  527. #else /* openssl < 0.9.9 */
  528. if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
  529. os_free(ctx);
  530. return NULL;
  531. }
  532. #endif /* openssl < 0.9.9 */
  533. return ctx;
  534. }
  535. void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
  536. {
  537. if (ctx == NULL)
  538. return;
  539. HMAC_Update(&ctx->ctx, data, len);
  540. }
  541. int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
  542. {
  543. unsigned int mdlen;
  544. int res;
  545. if (ctx == NULL)
  546. return -2;
  547. if (mac == NULL || len == NULL) {
  548. os_free(ctx);
  549. return 0;
  550. }
  551. mdlen = *len;
  552. #if OPENSSL_VERSION_NUMBER < 0x00909000
  553. HMAC_Final(&ctx->ctx, mac, &mdlen);
  554. res = 1;
  555. #else /* openssl < 0.9.9 */
  556. res = HMAC_Final(&ctx->ctx, mac, &mdlen);
  557. #endif /* openssl < 0.9.9 */
  558. HMAC_CTX_cleanup(&ctx->ctx);
  559. os_free(ctx);
  560. if (res == 1) {
  561. *len = mdlen;
  562. return 0;
  563. }
  564. return -1;
  565. }
  566. int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
  567. int iterations, u8 *buf, size_t buflen)
  568. {
  569. #if OPENSSL_VERSION_NUMBER < 0x00908000
  570. if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase),
  571. (unsigned char *) ssid,
  572. ssid_len, 4096, buflen, buf) != 1)
  573. return -1;
  574. #else /* openssl < 0.9.8 */
  575. if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
  576. ssid_len, 4096, buflen, buf) != 1)
  577. return -1;
  578. #endif /* openssl < 0.9.8 */
  579. return 0;
  580. }
  581. int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
  582. const u8 *addr[], const size_t *len, u8 *mac)
  583. {
  584. HMAC_CTX ctx;
  585. size_t i;
  586. unsigned int mdlen;
  587. int res;
  588. HMAC_CTX_init(&ctx);
  589. #if OPENSSL_VERSION_NUMBER < 0x00909000
  590. HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL);
  591. #else /* openssl < 0.9.9 */
  592. if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL) != 1)
  593. return -1;
  594. #endif /* openssl < 0.9.9 */
  595. for (i = 0; i < num_elem; i++)
  596. HMAC_Update(&ctx, addr[i], len[i]);
  597. mdlen = 20;
  598. #if OPENSSL_VERSION_NUMBER < 0x00909000
  599. HMAC_Final(&ctx, mac, &mdlen);
  600. res = 1;
  601. #else /* openssl < 0.9.9 */
  602. res = HMAC_Final(&ctx, mac, &mdlen);
  603. #endif /* openssl < 0.9.9 */
  604. HMAC_CTX_cleanup(&ctx);
  605. return res == 1 ? 0 : -1;
  606. }
  607. int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  608. u8 *mac)
  609. {
  610. return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
  611. }
  612. #ifdef CONFIG_SHA256
  613. int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
  614. const u8 *addr[], const size_t *len, u8 *mac)
  615. {
  616. HMAC_CTX ctx;
  617. size_t i;
  618. unsigned int mdlen;
  619. int res;
  620. HMAC_CTX_init(&ctx);
  621. #if OPENSSL_VERSION_NUMBER < 0x00909000
  622. HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL);
  623. #else /* openssl < 0.9.9 */
  624. if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL) != 1)
  625. return -1;
  626. #endif /* openssl < 0.9.9 */
  627. for (i = 0; i < num_elem; i++)
  628. HMAC_Update(&ctx, addr[i], len[i]);
  629. mdlen = 32;
  630. #if OPENSSL_VERSION_NUMBER < 0x00909000
  631. HMAC_Final(&ctx, mac, &mdlen);
  632. res = 1;
  633. #else /* openssl < 0.9.9 */
  634. res = HMAC_Final(&ctx, mac, &mdlen);
  635. #endif /* openssl < 0.9.9 */
  636. HMAC_CTX_cleanup(&ctx);
  637. return res == 1 ? 0 : -1;
  638. }
  639. int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
  640. size_t data_len, u8 *mac)
  641. {
  642. return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
  643. }
  644. #endif /* CONFIG_SHA256 */
  645. int crypto_get_random(void *buf, size_t len)
  646. {
  647. if (RAND_bytes(buf, len) != 1)
  648. return -1;
  649. return 0;
  650. }
  651. #ifdef CONFIG_OPENSSL_CMAC
  652. int omac1_aes_128_vector(const u8 *key, size_t num_elem,
  653. const u8 *addr[], const size_t *len, u8 *mac)
  654. {
  655. CMAC_CTX *ctx;
  656. int ret = -1;
  657. size_t outlen, i;
  658. ctx = CMAC_CTX_new();
  659. if (ctx == NULL)
  660. return -1;
  661. if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
  662. goto fail;
  663. for (i = 0; i < num_elem; i++) {
  664. if (!CMAC_Update(ctx, addr[i], len[i]))
  665. goto fail;
  666. }
  667. if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
  668. goto fail;
  669. ret = 0;
  670. fail:
  671. CMAC_CTX_free(ctx);
  672. return ret;
  673. }
  674. int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
  675. {
  676. return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
  677. }
  678. #endif /* CONFIG_OPENSSL_CMAC */
  679. struct crypto_bignum * crypto_bignum_init(void)
  680. {
  681. return (struct crypto_bignum *) BN_new();
  682. }
  683. struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
  684. {
  685. BIGNUM *bn = BN_bin2bn(buf, len, NULL);
  686. return (struct crypto_bignum *) bn;
  687. }
  688. void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
  689. {
  690. if (clear)
  691. BN_clear_free((BIGNUM *) n);
  692. else
  693. BN_free((BIGNUM *) n);
  694. }
  695. int crypto_bignum_to_bin(const struct crypto_bignum *a,
  696. u8 *buf, size_t buflen, size_t padlen)
  697. {
  698. int num_bytes, offset;
  699. if (padlen > buflen)
  700. return -1;
  701. num_bytes = BN_num_bytes((const BIGNUM *) a);
  702. if ((size_t) num_bytes > buflen)
  703. return -1;
  704. if (padlen > (size_t) num_bytes)
  705. offset = padlen - num_bytes;
  706. else
  707. offset = 0;
  708. os_memset(buf, 0, offset);
  709. BN_bn2bin((const BIGNUM *) a, buf + offset);
  710. return num_bytes + offset;
  711. }
  712. int crypto_bignum_add(const struct crypto_bignum *a,
  713. const struct crypto_bignum *b,
  714. struct crypto_bignum *c)
  715. {
  716. return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
  717. 0 : -1;
  718. }
  719. int crypto_bignum_mod(const struct crypto_bignum *a,
  720. const struct crypto_bignum *b,
  721. struct crypto_bignum *c)
  722. {
  723. int res;
  724. BN_CTX *bnctx;
  725. bnctx = BN_CTX_new();
  726. if (bnctx == NULL)
  727. return -1;
  728. res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
  729. bnctx);
  730. BN_CTX_free(bnctx);
  731. return res ? 0 : -1;
  732. }
  733. int crypto_bignum_exptmod(const struct crypto_bignum *a,
  734. const struct crypto_bignum *b,
  735. const struct crypto_bignum *c,
  736. struct crypto_bignum *d)
  737. {
  738. int res;
  739. BN_CTX *bnctx;
  740. bnctx = BN_CTX_new();
  741. if (bnctx == NULL)
  742. return -1;
  743. res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
  744. (const BIGNUM *) c, bnctx);
  745. BN_CTX_free(bnctx);
  746. return res ? 0 : -1;
  747. }
  748. int crypto_bignum_rshift(const struct crypto_bignum *a, int n,
  749. struct crypto_bignum *b)
  750. {
  751. return BN_rshift((BIGNUM *) b, (const BIGNUM *) a, n) ? 0 : -1;
  752. }
  753. int crypto_bignum_inverse(const struct crypto_bignum *a,
  754. const struct crypto_bignum *b,
  755. struct crypto_bignum *c)
  756. {
  757. BIGNUM *res;
  758. BN_CTX *bnctx;
  759. bnctx = BN_CTX_new();
  760. if (bnctx == NULL)
  761. return -1;
  762. res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
  763. (const BIGNUM *) b, bnctx);
  764. BN_CTX_free(bnctx);
  765. return res ? 0 : -1;
  766. }
  767. int crypto_bignum_sub(const struct crypto_bignum *a,
  768. const struct crypto_bignum *b,
  769. struct crypto_bignum *c)
  770. {
  771. return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
  772. 0 : -1;
  773. }
  774. int crypto_bignum_div(const struct crypto_bignum *a,
  775. const struct crypto_bignum *b,
  776. struct crypto_bignum *c)
  777. {
  778. int res;
  779. BN_CTX *bnctx;
  780. bnctx = BN_CTX_new();
  781. if (bnctx == NULL)
  782. return -1;
  783. res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
  784. (const BIGNUM *) b, bnctx);
  785. BN_CTX_free(bnctx);
  786. return res ? 0 : -1;
  787. }
  788. int crypto_bignum_mulmod(const struct crypto_bignum *a,
  789. const struct crypto_bignum *b,
  790. const struct crypto_bignum *c,
  791. struct crypto_bignum *d)
  792. {
  793. int res;
  794. BN_CTX *bnctx;
  795. bnctx = BN_CTX_new();
  796. if (bnctx == NULL)
  797. return -1;
  798. res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
  799. (const BIGNUM *) c, bnctx);
  800. BN_CTX_free(bnctx);
  801. return res ? 0 : -1;
  802. }
  803. int crypto_bignum_cmp(const struct crypto_bignum *a,
  804. const struct crypto_bignum *b)
  805. {
  806. return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
  807. }
  808. int crypto_bignum_bits(const struct crypto_bignum *a)
  809. {
  810. return BN_num_bits((const BIGNUM *) a);
  811. }
  812. int crypto_bignum_is_zero(const struct crypto_bignum *a)
  813. {
  814. return BN_is_zero((const BIGNUM *) a);
  815. }
  816. int crypto_bignum_is_one(const struct crypto_bignum *a)
  817. {
  818. return BN_is_one((const BIGNUM *) a);
  819. }
  820. #ifdef CONFIG_ECC
  821. struct crypto_ec {
  822. EC_GROUP *group;
  823. BN_CTX *bnctx;
  824. BIGNUM *prime;
  825. BIGNUM *order;
  826. };
  827. struct crypto_ec * crypto_ec_init(int group)
  828. {
  829. struct crypto_ec *e;
  830. int nid;
  831. /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
  832. switch (group) {
  833. case 19:
  834. nid = NID_X9_62_prime256v1;
  835. break;
  836. case 20:
  837. nid = NID_secp384r1;
  838. break;
  839. case 21:
  840. nid = NID_secp521r1;
  841. break;
  842. case 25:
  843. nid = NID_X9_62_prime192v1;
  844. break;
  845. case 26:
  846. nid = NID_secp224r1;
  847. break;
  848. default:
  849. return NULL;
  850. }
  851. e = os_zalloc(sizeof(*e));
  852. if (e == NULL)
  853. return NULL;
  854. e->bnctx = BN_CTX_new();
  855. e->group = EC_GROUP_new_by_curve_name(nid);
  856. e->prime = BN_new();
  857. e->order = BN_new();
  858. if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
  859. e->order == NULL ||
  860. !EC_GROUP_get_curve_GFp(e->group, e->prime, NULL, NULL, e->bnctx) ||
  861. !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
  862. crypto_ec_deinit(e);
  863. e = NULL;
  864. }
  865. return e;
  866. }
  867. void crypto_ec_deinit(struct crypto_ec *e)
  868. {
  869. if (e == NULL)
  870. return;
  871. BN_free(e->order);
  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_free(x_bn);
  927. BN_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_free(x);
  941. BN_free(y);
  942. EC_POINT_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_free(elem);
  948. elem = NULL;
  949. }
  950. BN_free(x);
  951. BN_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 */