crypto_openssl.c 28 KB

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