crypto_openssl.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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. #ifndef CONFIG_FIPS
  572. int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
  573. const u8 *addr[], const size_t *len, u8 *mac)
  574. {
  575. HMAC_CTX ctx;
  576. size_t i;
  577. unsigned int mdlen;
  578. int res;
  579. HMAC_CTX_init(&ctx);
  580. #if OPENSSL_VERSION_NUMBER < 0x00909000
  581. HMAC_Init_ex(&ctx, key, key_len, EVP_md5(), NULL);
  582. #else /* openssl < 0.9.9 */
  583. if (HMAC_Init_ex(&ctx, key, key_len, EVP_md5(), 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. mdlen = 16;
  589. #if OPENSSL_VERSION_NUMBER < 0x00909000
  590. HMAC_Final(&ctx, mac, &mdlen);
  591. res = 1;
  592. #else /* openssl < 0.9.9 */
  593. res = HMAC_Final(&ctx, mac, &mdlen);
  594. #endif /* openssl < 0.9.9 */
  595. HMAC_CTX_cleanup(&ctx);
  596. return res == 1 ? 0 : -1;
  597. }
  598. int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  599. u8 *mac)
  600. {
  601. return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
  602. }
  603. #endif /* CONFIG_FIPS */
  604. int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
  605. int iterations, u8 *buf, size_t buflen)
  606. {
  607. #if OPENSSL_VERSION_NUMBER < 0x00908000
  608. if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase),
  609. (unsigned char *) ssid,
  610. ssid_len, iterations, buflen, buf) != 1)
  611. return -1;
  612. #else /* openssl < 0.9.8 */
  613. if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
  614. ssid_len, iterations, buflen, buf) != 1)
  615. return -1;
  616. #endif /* openssl < 0.9.8 */
  617. return 0;
  618. }
  619. int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
  620. const u8 *addr[], const size_t *len, u8 *mac)
  621. {
  622. HMAC_CTX ctx;
  623. size_t i;
  624. unsigned int mdlen;
  625. int res;
  626. HMAC_CTX_init(&ctx);
  627. #if OPENSSL_VERSION_NUMBER < 0x00909000
  628. HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL);
  629. #else /* openssl < 0.9.9 */
  630. if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL) != 1)
  631. return -1;
  632. #endif /* openssl < 0.9.9 */
  633. for (i = 0; i < num_elem; i++)
  634. HMAC_Update(&ctx, addr[i], len[i]);
  635. mdlen = 20;
  636. #if OPENSSL_VERSION_NUMBER < 0x00909000
  637. HMAC_Final(&ctx, mac, &mdlen);
  638. res = 1;
  639. #else /* openssl < 0.9.9 */
  640. res = HMAC_Final(&ctx, mac, &mdlen);
  641. #endif /* openssl < 0.9.9 */
  642. HMAC_CTX_cleanup(&ctx);
  643. return res == 1 ? 0 : -1;
  644. }
  645. int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  646. u8 *mac)
  647. {
  648. return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
  649. }
  650. #ifdef CONFIG_SHA256
  651. int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
  652. const u8 *addr[], const size_t *len, u8 *mac)
  653. {
  654. HMAC_CTX ctx;
  655. size_t i;
  656. unsigned int mdlen;
  657. int res;
  658. HMAC_CTX_init(&ctx);
  659. #if OPENSSL_VERSION_NUMBER < 0x00909000
  660. HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL);
  661. #else /* openssl < 0.9.9 */
  662. if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL) != 1)
  663. return -1;
  664. #endif /* openssl < 0.9.9 */
  665. for (i = 0; i < num_elem; i++)
  666. HMAC_Update(&ctx, addr[i], len[i]);
  667. mdlen = 32;
  668. #if OPENSSL_VERSION_NUMBER < 0x00909000
  669. HMAC_Final(&ctx, mac, &mdlen);
  670. res = 1;
  671. #else /* openssl < 0.9.9 */
  672. res = HMAC_Final(&ctx, mac, &mdlen);
  673. #endif /* openssl < 0.9.9 */
  674. HMAC_CTX_cleanup(&ctx);
  675. return res == 1 ? 0 : -1;
  676. }
  677. int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
  678. size_t data_len, u8 *mac)
  679. {
  680. return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
  681. }
  682. #endif /* CONFIG_SHA256 */
  683. #ifdef CONFIG_SHA384
  684. int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
  685. const u8 *addr[], const size_t *len, u8 *mac)
  686. {
  687. HMAC_CTX ctx;
  688. size_t i;
  689. unsigned int mdlen;
  690. int res;
  691. HMAC_CTX_init(&ctx);
  692. if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha384(), NULL) != 1)
  693. return -1;
  694. for (i = 0; i < num_elem; i++)
  695. HMAC_Update(&ctx, addr[i], len[i]);
  696. mdlen = 32;
  697. res = HMAC_Final(&ctx, mac, &mdlen);
  698. HMAC_CTX_cleanup(&ctx);
  699. return res == 1 ? 0 : -1;
  700. }
  701. int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
  702. size_t data_len, u8 *mac)
  703. {
  704. return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
  705. }
  706. #endif /* CONFIG_SHA384 */
  707. int crypto_get_random(void *buf, size_t len)
  708. {
  709. if (RAND_bytes(buf, len) != 1)
  710. return -1;
  711. return 0;
  712. }
  713. #ifdef CONFIG_OPENSSL_CMAC
  714. int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
  715. const u8 *addr[], const size_t *len, u8 *mac)
  716. {
  717. CMAC_CTX *ctx;
  718. int ret = -1;
  719. size_t outlen, i;
  720. ctx = CMAC_CTX_new();
  721. if (ctx == NULL)
  722. return -1;
  723. if (key_len == 32) {
  724. if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
  725. goto fail;
  726. } else if (key_len == 16) {
  727. if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
  728. goto fail;
  729. } else {
  730. goto fail;
  731. }
  732. for (i = 0; i < num_elem; i++) {
  733. if (!CMAC_Update(ctx, addr[i], len[i]))
  734. goto fail;
  735. }
  736. if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
  737. goto fail;
  738. ret = 0;
  739. fail:
  740. CMAC_CTX_free(ctx);
  741. return ret;
  742. }
  743. int omac1_aes_128_vector(const u8 *key, size_t num_elem,
  744. const u8 *addr[], const size_t *len, u8 *mac)
  745. {
  746. return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
  747. }
  748. int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
  749. {
  750. return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
  751. }
  752. int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
  753. {
  754. return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
  755. }
  756. #endif /* CONFIG_OPENSSL_CMAC */
  757. struct crypto_bignum * crypto_bignum_init(void)
  758. {
  759. return (struct crypto_bignum *) BN_new();
  760. }
  761. struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
  762. {
  763. BIGNUM *bn = BN_bin2bn(buf, len, NULL);
  764. return (struct crypto_bignum *) bn;
  765. }
  766. void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
  767. {
  768. if (clear)
  769. BN_clear_free((BIGNUM *) n);
  770. else
  771. BN_free((BIGNUM *) n);
  772. }
  773. int crypto_bignum_to_bin(const struct crypto_bignum *a,
  774. u8 *buf, size_t buflen, size_t padlen)
  775. {
  776. int num_bytes, offset;
  777. if (padlen > buflen)
  778. return -1;
  779. num_bytes = BN_num_bytes((const BIGNUM *) a);
  780. if ((size_t) num_bytes > buflen)
  781. return -1;
  782. if (padlen > (size_t) num_bytes)
  783. offset = padlen - num_bytes;
  784. else
  785. offset = 0;
  786. os_memset(buf, 0, offset);
  787. BN_bn2bin((const BIGNUM *) a, buf + offset);
  788. return num_bytes + offset;
  789. }
  790. int crypto_bignum_add(const struct crypto_bignum *a,
  791. const struct crypto_bignum *b,
  792. struct crypto_bignum *c)
  793. {
  794. return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
  795. 0 : -1;
  796. }
  797. int crypto_bignum_mod(const struct crypto_bignum *a,
  798. const struct crypto_bignum *b,
  799. struct crypto_bignum *c)
  800. {
  801. int res;
  802. BN_CTX *bnctx;
  803. bnctx = BN_CTX_new();
  804. if (bnctx == NULL)
  805. return -1;
  806. res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
  807. bnctx);
  808. BN_CTX_free(bnctx);
  809. return res ? 0 : -1;
  810. }
  811. int crypto_bignum_exptmod(const struct crypto_bignum *a,
  812. const struct crypto_bignum *b,
  813. const struct crypto_bignum *c,
  814. struct crypto_bignum *d)
  815. {
  816. int res;
  817. BN_CTX *bnctx;
  818. bnctx = BN_CTX_new();
  819. if (bnctx == NULL)
  820. return -1;
  821. res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
  822. (const BIGNUM *) c, bnctx);
  823. BN_CTX_free(bnctx);
  824. return res ? 0 : -1;
  825. }
  826. int crypto_bignum_inverse(const struct crypto_bignum *a,
  827. const struct crypto_bignum *b,
  828. struct crypto_bignum *c)
  829. {
  830. BIGNUM *res;
  831. BN_CTX *bnctx;
  832. bnctx = BN_CTX_new();
  833. if (bnctx == NULL)
  834. return -1;
  835. res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
  836. (const BIGNUM *) b, bnctx);
  837. BN_CTX_free(bnctx);
  838. return res ? 0 : -1;
  839. }
  840. int crypto_bignum_sub(const struct crypto_bignum *a,
  841. const struct crypto_bignum *b,
  842. struct crypto_bignum *c)
  843. {
  844. return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
  845. 0 : -1;
  846. }
  847. int crypto_bignum_div(const struct crypto_bignum *a,
  848. const struct crypto_bignum *b,
  849. struct crypto_bignum *c)
  850. {
  851. int res;
  852. BN_CTX *bnctx;
  853. bnctx = BN_CTX_new();
  854. if (bnctx == NULL)
  855. return -1;
  856. res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
  857. (const BIGNUM *) b, bnctx);
  858. BN_CTX_free(bnctx);
  859. return res ? 0 : -1;
  860. }
  861. int crypto_bignum_mulmod(const struct crypto_bignum *a,
  862. const struct crypto_bignum *b,
  863. const struct crypto_bignum *c,
  864. struct crypto_bignum *d)
  865. {
  866. int res;
  867. BN_CTX *bnctx;
  868. bnctx = BN_CTX_new();
  869. if (bnctx == NULL)
  870. return -1;
  871. res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
  872. (const BIGNUM *) c, bnctx);
  873. BN_CTX_free(bnctx);
  874. return res ? 0 : -1;
  875. }
  876. int crypto_bignum_cmp(const struct crypto_bignum *a,
  877. const struct crypto_bignum *b)
  878. {
  879. return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
  880. }
  881. int crypto_bignum_bits(const struct crypto_bignum *a)
  882. {
  883. return BN_num_bits((const BIGNUM *) a);
  884. }
  885. int crypto_bignum_is_zero(const struct crypto_bignum *a)
  886. {
  887. return BN_is_zero((const BIGNUM *) a);
  888. }
  889. int crypto_bignum_is_one(const struct crypto_bignum *a)
  890. {
  891. return BN_is_one((const BIGNUM *) a);
  892. }
  893. #ifdef CONFIG_ECC
  894. struct crypto_ec {
  895. EC_GROUP *group;
  896. BN_CTX *bnctx;
  897. BIGNUM *prime;
  898. BIGNUM *order;
  899. };
  900. struct crypto_ec * crypto_ec_init(int group)
  901. {
  902. struct crypto_ec *e;
  903. int nid;
  904. /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
  905. switch (group) {
  906. case 19:
  907. nid = NID_X9_62_prime256v1;
  908. break;
  909. case 20:
  910. nid = NID_secp384r1;
  911. break;
  912. case 21:
  913. nid = NID_secp521r1;
  914. break;
  915. case 25:
  916. nid = NID_X9_62_prime192v1;
  917. break;
  918. case 26:
  919. nid = NID_secp224r1;
  920. break;
  921. default:
  922. return NULL;
  923. }
  924. e = os_zalloc(sizeof(*e));
  925. if (e == NULL)
  926. return NULL;
  927. e->bnctx = BN_CTX_new();
  928. e->group = EC_GROUP_new_by_curve_name(nid);
  929. e->prime = BN_new();
  930. e->order = BN_new();
  931. if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
  932. e->order == NULL ||
  933. !EC_GROUP_get_curve_GFp(e->group, e->prime, NULL, NULL, e->bnctx) ||
  934. !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
  935. crypto_ec_deinit(e);
  936. e = NULL;
  937. }
  938. return e;
  939. }
  940. void crypto_ec_deinit(struct crypto_ec *e)
  941. {
  942. if (e == NULL)
  943. return;
  944. BN_clear_free(e->order);
  945. BN_clear_free(e->prime);
  946. EC_GROUP_free(e->group);
  947. BN_CTX_free(e->bnctx);
  948. os_free(e);
  949. }
  950. struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
  951. {
  952. if (e == NULL)
  953. return NULL;
  954. return (struct crypto_ec_point *) EC_POINT_new(e->group);
  955. }
  956. size_t crypto_ec_prime_len(struct crypto_ec *e)
  957. {
  958. return BN_num_bytes(e->prime);
  959. }
  960. size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
  961. {
  962. return BN_num_bits(e->prime);
  963. }
  964. const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
  965. {
  966. return (const struct crypto_bignum *) e->prime;
  967. }
  968. const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
  969. {
  970. return (const struct crypto_bignum *) e->order;
  971. }
  972. void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
  973. {
  974. if (clear)
  975. EC_POINT_clear_free((EC_POINT *) p);
  976. else
  977. EC_POINT_free((EC_POINT *) p);
  978. }
  979. int crypto_ec_point_to_bin(struct crypto_ec *e,
  980. const struct crypto_ec_point *point, u8 *x, u8 *y)
  981. {
  982. BIGNUM *x_bn, *y_bn;
  983. int ret = -1;
  984. int len = BN_num_bytes(e->prime);
  985. x_bn = BN_new();
  986. y_bn = BN_new();
  987. if (x_bn && y_bn &&
  988. EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
  989. x_bn, y_bn, e->bnctx)) {
  990. if (x) {
  991. crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
  992. x, len, len);
  993. }
  994. if (y) {
  995. crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
  996. y, len, len);
  997. }
  998. ret = 0;
  999. }
  1000. BN_clear_free(x_bn);
  1001. BN_clear_free(y_bn);
  1002. return ret;
  1003. }
  1004. struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
  1005. const u8 *val)
  1006. {
  1007. BIGNUM *x, *y;
  1008. EC_POINT *elem;
  1009. int len = BN_num_bytes(e->prime);
  1010. x = BN_bin2bn(val, len, NULL);
  1011. y = BN_bin2bn(val + len, len, NULL);
  1012. elem = EC_POINT_new(e->group);
  1013. if (x == NULL || y == NULL || elem == NULL) {
  1014. BN_clear_free(x);
  1015. BN_clear_free(y);
  1016. EC_POINT_clear_free(elem);
  1017. return NULL;
  1018. }
  1019. if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
  1020. e->bnctx)) {
  1021. EC_POINT_clear_free(elem);
  1022. elem = NULL;
  1023. }
  1024. BN_clear_free(x);
  1025. BN_clear_free(y);
  1026. return (struct crypto_ec_point *) elem;
  1027. }
  1028. int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
  1029. const struct crypto_ec_point *b,
  1030. struct crypto_ec_point *c)
  1031. {
  1032. return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
  1033. (const EC_POINT *) b, e->bnctx) ? 0 : -1;
  1034. }
  1035. int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
  1036. const struct crypto_bignum *b,
  1037. struct crypto_ec_point *res)
  1038. {
  1039. return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
  1040. (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
  1041. ? 0 : -1;
  1042. }
  1043. int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
  1044. {
  1045. return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
  1046. }
  1047. int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
  1048. struct crypto_ec_point *p,
  1049. const struct crypto_bignum *x, int y_bit)
  1050. {
  1051. if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
  1052. (const BIGNUM *) x, y_bit,
  1053. e->bnctx) ||
  1054. !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
  1055. return -1;
  1056. return 0;
  1057. }
  1058. int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
  1059. const struct crypto_ec_point *p)
  1060. {
  1061. return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
  1062. }
  1063. int crypto_ec_point_is_on_curve(struct crypto_ec *e,
  1064. const struct crypto_ec_point *p)
  1065. {
  1066. return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p, e->bnctx);
  1067. }
  1068. #endif /* CONFIG_ECC */