crypto_openssl.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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 < 0x10100000L
  32. /* Compatibility wrapper for older versions. */
  33. static int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
  34. {
  35. return EVP_CIPHER_CTX_cleanup(ctx);
  36. }
  37. #endif /* OpenSSL version < 1.1.0 */
  38. static BIGNUM * get_group5_prime(void)
  39. {
  40. #ifdef 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_IS_BORINGSSL */
  61. return get_rfc3526_prime_1536(NULL);
  62. #endif /* OPENSSL_IS_BORINGSSL */
  63. }
  64. #ifdef OPENSSL_NO_SHA256
  65. #define NO_SHA256_WRAPPER
  66. #endif
  67. static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
  68. const u8 *addr[], const size_t *len, u8 *mac)
  69. {
  70. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  71. EVP_MD_CTX *ctx;
  72. size_t i;
  73. unsigned int mac_len;
  74. if (TEST_FAIL())
  75. return -1;
  76. ctx = EVP_MD_CTX_new();
  77. if (!ctx)
  78. return -1;
  79. if (!EVP_DigestInit_ex(ctx, type, NULL)) {
  80. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
  81. ERR_error_string(ERR_get_error(), NULL));
  82. EVP_MD_CTX_free(ctx);
  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. EVP_MD_CTX_free(ctx);
  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. EVP_MD_CTX_free(ctx);
  98. return -1;
  99. }
  100. EVP_MD_CTX_free(ctx);
  101. return 0;
  102. #else
  103. EVP_MD_CTX ctx;
  104. size_t i;
  105. unsigned int mac_len;
  106. if (TEST_FAIL())
  107. return -1;
  108. EVP_MD_CTX_init(&ctx);
  109. if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
  110. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
  111. ERR_error_string(ERR_get_error(), NULL));
  112. return -1;
  113. }
  114. for (i = 0; i < num_elem; i++) {
  115. if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
  116. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
  117. "failed: %s",
  118. ERR_error_string(ERR_get_error(), NULL));
  119. return -1;
  120. }
  121. }
  122. if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
  123. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
  124. ERR_error_string(ERR_get_error(), NULL));
  125. return -1;
  126. }
  127. return 0;
  128. #endif
  129. }
  130. #ifndef CONFIG_FIPS
  131. int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  132. {
  133. return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
  134. }
  135. #endif /* CONFIG_FIPS */
  136. void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
  137. {
  138. u8 pkey[8], next, tmp;
  139. int i;
  140. DES_key_schedule ks;
  141. /* Add parity bits to the key */
  142. next = 0;
  143. for (i = 0; i < 7; i++) {
  144. tmp = key[i];
  145. pkey[i] = (tmp >> i) | next | 1;
  146. next = tmp << (7 - i);
  147. }
  148. pkey[i] = next | 1;
  149. DES_set_key((DES_cblock *) &pkey, &ks);
  150. DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
  151. DES_ENCRYPT);
  152. }
  153. #ifndef CONFIG_NO_RC4
  154. int rc4_skip(const u8 *key, size_t keylen, size_t skip,
  155. u8 *data, size_t data_len)
  156. {
  157. #ifdef OPENSSL_NO_RC4
  158. return -1;
  159. #else /* OPENSSL_NO_RC4 */
  160. EVP_CIPHER_CTX *ctx;
  161. int outl;
  162. int res = -1;
  163. unsigned char skip_buf[16];
  164. ctx = EVP_CIPHER_CTX_new();
  165. if (!ctx ||
  166. !EVP_CIPHER_CTX_set_padding(ctx, 0) ||
  167. !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
  168. !EVP_CIPHER_CTX_set_key_length(ctx, keylen) ||
  169. !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1))
  170. goto out;
  171. while (skip >= sizeof(skip_buf)) {
  172. size_t len = skip;
  173. if (len > sizeof(skip_buf))
  174. len = sizeof(skip_buf);
  175. if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len))
  176. goto out;
  177. skip -= len;
  178. }
  179. if (EVP_CipherUpdate(ctx, data, &outl, data, data_len))
  180. res = 0;
  181. out:
  182. if (ctx)
  183. EVP_CIPHER_CTX_reset(ctx);
  184. return res;
  185. #endif /* OPENSSL_NO_RC4 */
  186. }
  187. #endif /* CONFIG_NO_RC4 */
  188. #ifndef CONFIG_FIPS
  189. int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  190. {
  191. return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
  192. }
  193. #endif /* CONFIG_FIPS */
  194. int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  195. {
  196. return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
  197. }
  198. #ifndef NO_SHA256_WRAPPER
  199. int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  200. u8 *mac)
  201. {
  202. return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
  203. }
  204. #endif /* NO_SHA256_WRAPPER */
  205. static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
  206. {
  207. switch (keylen) {
  208. case 16:
  209. return EVP_aes_128_ecb();
  210. #ifndef OPENSSL_IS_BORINGSSL
  211. case 24:
  212. return EVP_aes_192_ecb();
  213. #endif /* OPENSSL_IS_BORINGSSL */
  214. case 32:
  215. return EVP_aes_256_ecb();
  216. }
  217. return NULL;
  218. }
  219. void * aes_encrypt_init(const u8 *key, size_t len)
  220. {
  221. EVP_CIPHER_CTX *ctx;
  222. const EVP_CIPHER *type;
  223. if (TEST_FAIL())
  224. return NULL;
  225. type = aes_get_evp_cipher(len);
  226. if (type == NULL)
  227. return NULL;
  228. ctx = EVP_CIPHER_CTX_new();
  229. if (ctx == NULL)
  230. return NULL;
  231. if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
  232. os_free(ctx);
  233. return NULL;
  234. }
  235. EVP_CIPHER_CTX_set_padding(ctx, 0);
  236. return ctx;
  237. }
  238. void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
  239. {
  240. EVP_CIPHER_CTX *c = ctx;
  241. int clen = 16;
  242. if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
  243. wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
  244. ERR_error_string(ERR_get_error(), NULL));
  245. }
  246. }
  247. void aes_encrypt_deinit(void *ctx)
  248. {
  249. EVP_CIPHER_CTX *c = ctx;
  250. u8 buf[16];
  251. int len = sizeof(buf);
  252. if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
  253. wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
  254. "%s", ERR_error_string(ERR_get_error(), NULL));
  255. }
  256. if (len != 0) {
  257. wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
  258. "in AES encrypt", len);
  259. }
  260. EVP_CIPHER_CTX_reset(c);
  261. EVP_CIPHER_CTX_free(c);
  262. }
  263. void * aes_decrypt_init(const u8 *key, size_t len)
  264. {
  265. EVP_CIPHER_CTX *ctx;
  266. const EVP_CIPHER *type;
  267. if (TEST_FAIL())
  268. return NULL;
  269. type = aes_get_evp_cipher(len);
  270. if (type == NULL)
  271. return NULL;
  272. ctx = EVP_CIPHER_CTX_new();
  273. if (ctx == NULL)
  274. return NULL;
  275. if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
  276. EVP_CIPHER_CTX_free(ctx);
  277. return NULL;
  278. }
  279. EVP_CIPHER_CTX_set_padding(ctx, 0);
  280. return ctx;
  281. }
  282. void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
  283. {
  284. EVP_CIPHER_CTX *c = ctx;
  285. int plen = 16;
  286. if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
  287. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
  288. ERR_error_string(ERR_get_error(), NULL));
  289. }
  290. }
  291. void aes_decrypt_deinit(void *ctx)
  292. {
  293. EVP_CIPHER_CTX *c = ctx;
  294. u8 buf[16];
  295. int len = sizeof(buf);
  296. if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
  297. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
  298. "%s", ERR_error_string(ERR_get_error(), NULL));
  299. }
  300. if (len != 0) {
  301. wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
  302. "in AES decrypt", len);
  303. }
  304. EVP_CIPHER_CTX_reset(c);
  305. EVP_CIPHER_CTX_free(c);
  306. }
  307. #ifndef CONFIG_FIPS
  308. #ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP
  309. int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
  310. {
  311. AES_KEY actx;
  312. int res;
  313. if (AES_set_encrypt_key(kek, kek_len << 3, &actx))
  314. return -1;
  315. res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8);
  316. OPENSSL_cleanse(&actx, sizeof(actx));
  317. return res <= 0 ? -1 : 0;
  318. }
  319. int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
  320. u8 *plain)
  321. {
  322. AES_KEY actx;
  323. int res;
  324. if (AES_set_decrypt_key(kek, kek_len << 3, &actx))
  325. return -1;
  326. res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8);
  327. OPENSSL_cleanse(&actx, sizeof(actx));
  328. return res <= 0 ? -1 : 0;
  329. }
  330. #endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */
  331. #endif /* CONFIG_FIPS */
  332. int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
  333. {
  334. EVP_CIPHER_CTX *ctx;
  335. int clen, len;
  336. u8 buf[16];
  337. int res = -1;
  338. if (TEST_FAIL())
  339. return -1;
  340. ctx = EVP_CIPHER_CTX_new();
  341. if (!ctx)
  342. return -1;
  343. clen = data_len;
  344. len = sizeof(buf);
  345. if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
  346. EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
  347. EVP_EncryptUpdate(ctx, data, &clen, data, data_len) == 1 &&
  348. clen == (int) data_len &&
  349. EVP_EncryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
  350. res = 0;
  351. EVP_CIPHER_CTX_reset(ctx);
  352. return res;
  353. }
  354. int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
  355. {
  356. EVP_CIPHER_CTX *ctx;
  357. int plen, len;
  358. u8 buf[16];
  359. int res = -1;
  360. if (TEST_FAIL())
  361. return -1;
  362. ctx = EVP_CIPHER_CTX_new();
  363. if (!ctx)
  364. return -1;
  365. plen = data_len;
  366. len = sizeof(buf);
  367. if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
  368. EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
  369. EVP_DecryptUpdate(ctx, data, &plen, data, data_len) == 1 &&
  370. plen == (int) data_len &&
  371. EVP_DecryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
  372. res = 0;
  373. EVP_CIPHER_CTX_reset(ctx);
  374. return res;
  375. }
  376. int crypto_mod_exp(const u8 *base, size_t base_len,
  377. const u8 *power, size_t power_len,
  378. const u8 *modulus, size_t modulus_len,
  379. u8 *result, size_t *result_len)
  380. {
  381. BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
  382. int ret = -1;
  383. BN_CTX *ctx;
  384. ctx = BN_CTX_new();
  385. if (ctx == NULL)
  386. return -1;
  387. bn_base = BN_bin2bn(base, base_len, NULL);
  388. bn_exp = BN_bin2bn(power, power_len, NULL);
  389. bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
  390. bn_result = BN_new();
  391. if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
  392. bn_result == NULL)
  393. goto error;
  394. if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
  395. goto error;
  396. *result_len = BN_bn2bin(bn_result, result);
  397. ret = 0;
  398. error:
  399. BN_clear_free(bn_base);
  400. BN_clear_free(bn_exp);
  401. BN_clear_free(bn_modulus);
  402. BN_clear_free(bn_result);
  403. BN_CTX_free(ctx);
  404. return ret;
  405. }
  406. struct crypto_cipher {
  407. EVP_CIPHER_CTX *enc;
  408. EVP_CIPHER_CTX *dec;
  409. };
  410. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  411. const u8 *iv, const u8 *key,
  412. size_t key_len)
  413. {
  414. struct crypto_cipher *ctx;
  415. const EVP_CIPHER *cipher;
  416. ctx = os_zalloc(sizeof(*ctx));
  417. if (ctx == NULL)
  418. return NULL;
  419. switch (alg) {
  420. #ifndef CONFIG_NO_RC4
  421. #ifndef OPENSSL_NO_RC4
  422. case CRYPTO_CIPHER_ALG_RC4:
  423. cipher = EVP_rc4();
  424. break;
  425. #endif /* OPENSSL_NO_RC4 */
  426. #endif /* CONFIG_NO_RC4 */
  427. #ifndef OPENSSL_NO_AES
  428. case CRYPTO_CIPHER_ALG_AES:
  429. switch (key_len) {
  430. case 16:
  431. cipher = EVP_aes_128_cbc();
  432. break;
  433. #ifndef OPENSSL_IS_BORINGSSL
  434. case 24:
  435. cipher = EVP_aes_192_cbc();
  436. break;
  437. #endif /* OPENSSL_IS_BORINGSSL */
  438. case 32:
  439. cipher = EVP_aes_256_cbc();
  440. break;
  441. default:
  442. os_free(ctx);
  443. return NULL;
  444. }
  445. break;
  446. #endif /* OPENSSL_NO_AES */
  447. #ifndef OPENSSL_NO_DES
  448. case CRYPTO_CIPHER_ALG_3DES:
  449. cipher = EVP_des_ede3_cbc();
  450. break;
  451. case CRYPTO_CIPHER_ALG_DES:
  452. cipher = EVP_des_cbc();
  453. break;
  454. #endif /* OPENSSL_NO_DES */
  455. #ifndef OPENSSL_NO_RC2
  456. case CRYPTO_CIPHER_ALG_RC2:
  457. cipher = EVP_rc2_ecb();
  458. break;
  459. #endif /* OPENSSL_NO_RC2 */
  460. default:
  461. os_free(ctx);
  462. return NULL;
  463. }
  464. if (!(ctx->enc = EVP_CIPHER_CTX_new()) ||
  465. !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) ||
  466. !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) ||
  467. !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) ||
  468. !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) {
  469. if (ctx->enc)
  470. EVP_CIPHER_CTX_reset(ctx->enc);
  471. os_free(ctx);
  472. return NULL;
  473. }
  474. if (!(ctx->dec = EVP_CIPHER_CTX_new()) ||
  475. !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) ||
  476. !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) ||
  477. !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) ||
  478. !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) {
  479. EVP_CIPHER_CTX_reset(ctx->enc);
  480. if (ctx->dec)
  481. EVP_CIPHER_CTX_reset(ctx->dec);
  482. os_free(ctx);
  483. return NULL;
  484. }
  485. return ctx;
  486. }
  487. int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
  488. u8 *crypt, size_t len)
  489. {
  490. int outl;
  491. if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len))
  492. return -1;
  493. return 0;
  494. }
  495. int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
  496. u8 *plain, size_t len)
  497. {
  498. int outl;
  499. outl = len;
  500. if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len))
  501. return -1;
  502. return 0;
  503. }
  504. void crypto_cipher_deinit(struct crypto_cipher *ctx)
  505. {
  506. EVP_CIPHER_CTX_reset(ctx->enc);
  507. EVP_CIPHER_CTX_reset(ctx->dec);
  508. os_free(ctx);
  509. }
  510. void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
  511. {
  512. DH *dh;
  513. struct wpabuf *pubkey = NULL, *privkey = NULL;
  514. size_t publen, privlen;
  515. *priv = NULL;
  516. *publ = NULL;
  517. dh = DH_new();
  518. if (dh == NULL)
  519. return NULL;
  520. dh->g = BN_new();
  521. if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
  522. goto err;
  523. dh->p = get_group5_prime();
  524. if (dh->p == NULL)
  525. goto err;
  526. if (DH_generate_key(dh) != 1)
  527. goto err;
  528. publen = BN_num_bytes(dh->pub_key);
  529. pubkey = wpabuf_alloc(publen);
  530. if (pubkey == NULL)
  531. goto err;
  532. privlen = BN_num_bytes(dh->priv_key);
  533. privkey = wpabuf_alloc(privlen);
  534. if (privkey == NULL)
  535. goto err;
  536. BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
  537. BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
  538. *priv = privkey;
  539. *publ = pubkey;
  540. return dh;
  541. err:
  542. wpabuf_clear_free(pubkey);
  543. wpabuf_clear_free(privkey);
  544. DH_free(dh);
  545. return NULL;
  546. }
  547. void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
  548. {
  549. DH *dh;
  550. dh = DH_new();
  551. if (dh == NULL)
  552. return NULL;
  553. dh->g = BN_new();
  554. if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
  555. goto err;
  556. dh->p = get_group5_prime();
  557. if (dh->p == NULL)
  558. goto err;
  559. dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
  560. if (dh->priv_key == NULL)
  561. goto err;
  562. dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
  563. if (dh->pub_key == NULL)
  564. goto err;
  565. if (DH_generate_key(dh) != 1)
  566. goto err;
  567. return dh;
  568. err:
  569. DH_free(dh);
  570. return NULL;
  571. }
  572. struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
  573. const struct wpabuf *own_private)
  574. {
  575. BIGNUM *pub_key;
  576. struct wpabuf *res = NULL;
  577. size_t rlen;
  578. DH *dh = ctx;
  579. int keylen;
  580. if (ctx == NULL)
  581. return NULL;
  582. pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
  583. NULL);
  584. if (pub_key == NULL)
  585. return NULL;
  586. rlen = DH_size(dh);
  587. res = wpabuf_alloc(rlen);
  588. if (res == NULL)
  589. goto err;
  590. keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
  591. if (keylen < 0)
  592. goto err;
  593. wpabuf_put(res, keylen);
  594. BN_clear_free(pub_key);
  595. return res;
  596. err:
  597. BN_clear_free(pub_key);
  598. wpabuf_clear_free(res);
  599. return NULL;
  600. }
  601. void dh5_free(void *ctx)
  602. {
  603. DH *dh;
  604. if (ctx == NULL)
  605. return;
  606. dh = ctx;
  607. DH_free(dh);
  608. }
  609. struct crypto_hash {
  610. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  611. HMAC_CTX *ctx;
  612. #else
  613. HMAC_CTX ctx;
  614. #endif
  615. };
  616. struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
  617. size_t key_len)
  618. {
  619. struct crypto_hash *ctx;
  620. const EVP_MD *md;
  621. switch (alg) {
  622. #ifndef OPENSSL_NO_MD5
  623. case CRYPTO_HASH_ALG_HMAC_MD5:
  624. md = EVP_md5();
  625. break;
  626. #endif /* OPENSSL_NO_MD5 */
  627. #ifndef OPENSSL_NO_SHA
  628. case CRYPTO_HASH_ALG_HMAC_SHA1:
  629. md = EVP_sha1();
  630. break;
  631. #endif /* OPENSSL_NO_SHA */
  632. #ifndef OPENSSL_NO_SHA256
  633. #ifdef CONFIG_SHA256
  634. case CRYPTO_HASH_ALG_HMAC_SHA256:
  635. md = EVP_sha256();
  636. break;
  637. #endif /* CONFIG_SHA256 */
  638. #endif /* OPENSSL_NO_SHA256 */
  639. default:
  640. return NULL;
  641. }
  642. ctx = os_zalloc(sizeof(*ctx));
  643. if (ctx == NULL)
  644. return NULL;
  645. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  646. ctx->ctx = HMAC_CTX_new();
  647. if (!ctx->ctx) {
  648. os_free(ctx);
  649. return NULL;
  650. }
  651. if (HMAC_Init_ex(ctx->ctx, key, key_len, md, NULL) != 1) {
  652. HMAC_CTX_free(ctx->ctx);
  653. bin_clear_free(ctx, sizeof(*ctx));
  654. return NULL;
  655. }
  656. #else
  657. HMAC_CTX_init(&ctx->ctx);
  658. if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
  659. bin_clear_free(ctx, sizeof(*ctx));
  660. return NULL;
  661. }
  662. #endif
  663. return ctx;
  664. }
  665. void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
  666. {
  667. if (ctx == NULL)
  668. return;
  669. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  670. HMAC_Update(ctx->ctx, data, len);
  671. #else
  672. HMAC_Update(&ctx->ctx, data, len);
  673. #endif
  674. }
  675. int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
  676. {
  677. unsigned int mdlen;
  678. int res;
  679. if (ctx == NULL)
  680. return -2;
  681. if (mac == NULL || len == NULL) {
  682. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  683. HMAC_CTX_free(ctx->ctx);
  684. #endif
  685. bin_clear_free(ctx, sizeof(*ctx));
  686. return 0;
  687. }
  688. mdlen = *len;
  689. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  690. res = HMAC_Final(ctx->ctx, mac, &mdlen);
  691. HMAC_CTX_free(ctx->ctx);
  692. #else
  693. res = HMAC_Final(&ctx->ctx, mac, &mdlen);
  694. HMAC_CTX_cleanup(&ctx->ctx);
  695. #endif
  696. bin_clear_free(ctx, sizeof(*ctx));
  697. if (res == 1) {
  698. *len = mdlen;
  699. return 0;
  700. }
  701. return -1;
  702. }
  703. static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
  704. size_t key_len, size_t num_elem,
  705. const u8 *addr[], const size_t *len, u8 *mac,
  706. unsigned int mdlen)
  707. {
  708. #if OPENSSL_VERSION_NUMBER >= 0x10100000L
  709. HMAC_CTX *ctx;
  710. size_t i;
  711. int res;
  712. if (TEST_FAIL())
  713. return -1;
  714. ctx = HMAC_CTX_new();
  715. if (!ctx)
  716. return -1;
  717. res = HMAC_Init_ex(ctx, key, key_len, type, NULL);
  718. if (res != 1)
  719. goto done;
  720. for (i = 0; i < num_elem; i++)
  721. HMAC_Update(ctx, addr[i], len[i]);
  722. res = HMAC_Final(ctx, mac, &mdlen);
  723. done:
  724. HMAC_CTX_free(ctx);
  725. return res == 1 ? 0 : -1;
  726. #else
  727. HMAC_CTX ctx;
  728. size_t i;
  729. int res;
  730. if (TEST_FAIL())
  731. return -1;
  732. HMAC_CTX_init(&ctx);
  733. if (HMAC_Init_ex(&ctx, key, key_len, type, NULL) != 1)
  734. return -1;
  735. for (i = 0; i < num_elem; i++)
  736. HMAC_Update(&ctx, addr[i], len[i]);
  737. res = HMAC_Final(&ctx, mac, &mdlen);
  738. HMAC_CTX_cleanup(&ctx);
  739. return res == 1 ? 0 : -1;
  740. #endif
  741. }
  742. #ifndef CONFIG_FIPS
  743. int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
  744. const u8 *addr[], const size_t *len, u8 *mac)
  745. {
  746. return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
  747. mac, 16);
  748. }
  749. int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  750. u8 *mac)
  751. {
  752. return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
  753. }
  754. #endif /* CONFIG_FIPS */
  755. int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
  756. int iterations, u8 *buf, size_t buflen)
  757. {
  758. if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
  759. ssid_len, iterations, buflen, buf) != 1)
  760. return -1;
  761. return 0;
  762. }
  763. int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
  764. const u8 *addr[], const size_t *len, u8 *mac)
  765. {
  766. return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr,
  767. len, mac, 20);
  768. }
  769. int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  770. u8 *mac)
  771. {
  772. return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
  773. }
  774. #ifdef CONFIG_SHA256
  775. int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
  776. const u8 *addr[], const size_t *len, u8 *mac)
  777. {
  778. return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
  779. len, mac, 32);
  780. }
  781. int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
  782. size_t data_len, u8 *mac)
  783. {
  784. return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
  785. }
  786. #endif /* CONFIG_SHA256 */
  787. #ifdef CONFIG_SHA384
  788. int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
  789. const u8 *addr[], const size_t *len, u8 *mac)
  790. {
  791. return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
  792. len, mac, 32);
  793. }
  794. int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
  795. size_t data_len, u8 *mac)
  796. {
  797. return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
  798. }
  799. #endif /* CONFIG_SHA384 */
  800. int crypto_get_random(void *buf, size_t len)
  801. {
  802. if (RAND_bytes(buf, len) != 1)
  803. return -1;
  804. return 0;
  805. }
  806. #ifdef CONFIG_OPENSSL_CMAC
  807. int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
  808. const u8 *addr[], const size_t *len, u8 *mac)
  809. {
  810. CMAC_CTX *ctx;
  811. int ret = -1;
  812. size_t outlen, i;
  813. if (TEST_FAIL())
  814. return -1;
  815. ctx = CMAC_CTX_new();
  816. if (ctx == NULL)
  817. return -1;
  818. if (key_len == 32) {
  819. if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
  820. goto fail;
  821. } else if (key_len == 16) {
  822. if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
  823. goto fail;
  824. } else {
  825. goto fail;
  826. }
  827. for (i = 0; i < num_elem; i++) {
  828. if (!CMAC_Update(ctx, addr[i], len[i]))
  829. goto fail;
  830. }
  831. if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
  832. goto fail;
  833. ret = 0;
  834. fail:
  835. CMAC_CTX_free(ctx);
  836. return ret;
  837. }
  838. int omac1_aes_128_vector(const u8 *key, size_t num_elem,
  839. const u8 *addr[], const size_t *len, u8 *mac)
  840. {
  841. return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
  842. }
  843. int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
  844. {
  845. return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
  846. }
  847. int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
  848. {
  849. return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
  850. }
  851. #endif /* CONFIG_OPENSSL_CMAC */
  852. struct crypto_bignum * crypto_bignum_init(void)
  853. {
  854. return (struct crypto_bignum *) BN_new();
  855. }
  856. struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
  857. {
  858. BIGNUM *bn = BN_bin2bn(buf, len, NULL);
  859. return (struct crypto_bignum *) bn;
  860. }
  861. void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
  862. {
  863. if (clear)
  864. BN_clear_free((BIGNUM *) n);
  865. else
  866. BN_free((BIGNUM *) n);
  867. }
  868. int crypto_bignum_to_bin(const struct crypto_bignum *a,
  869. u8 *buf, size_t buflen, size_t padlen)
  870. {
  871. int num_bytes, offset;
  872. if (padlen > buflen)
  873. return -1;
  874. num_bytes = BN_num_bytes((const BIGNUM *) a);
  875. if ((size_t) num_bytes > buflen)
  876. return -1;
  877. if (padlen > (size_t) num_bytes)
  878. offset = padlen - num_bytes;
  879. else
  880. offset = 0;
  881. os_memset(buf, 0, offset);
  882. BN_bn2bin((const BIGNUM *) a, buf + offset);
  883. return num_bytes + offset;
  884. }
  885. int crypto_bignum_add(const struct crypto_bignum *a,
  886. const struct crypto_bignum *b,
  887. struct crypto_bignum *c)
  888. {
  889. return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
  890. 0 : -1;
  891. }
  892. int crypto_bignum_mod(const struct crypto_bignum *a,
  893. const struct crypto_bignum *b,
  894. struct crypto_bignum *c)
  895. {
  896. int res;
  897. BN_CTX *bnctx;
  898. bnctx = BN_CTX_new();
  899. if (bnctx == NULL)
  900. return -1;
  901. res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
  902. bnctx);
  903. BN_CTX_free(bnctx);
  904. return res ? 0 : -1;
  905. }
  906. int crypto_bignum_exptmod(const struct crypto_bignum *a,
  907. const struct crypto_bignum *b,
  908. const struct crypto_bignum *c,
  909. struct crypto_bignum *d)
  910. {
  911. int res;
  912. BN_CTX *bnctx;
  913. bnctx = BN_CTX_new();
  914. if (bnctx == NULL)
  915. return -1;
  916. res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
  917. (const BIGNUM *) c, bnctx);
  918. BN_CTX_free(bnctx);
  919. return res ? 0 : -1;
  920. }
  921. int crypto_bignum_inverse(const struct crypto_bignum *a,
  922. const struct crypto_bignum *b,
  923. struct crypto_bignum *c)
  924. {
  925. BIGNUM *res;
  926. BN_CTX *bnctx;
  927. bnctx = BN_CTX_new();
  928. if (bnctx == NULL)
  929. return -1;
  930. res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
  931. (const BIGNUM *) b, bnctx);
  932. BN_CTX_free(bnctx);
  933. return res ? 0 : -1;
  934. }
  935. int crypto_bignum_sub(const struct crypto_bignum *a,
  936. const struct crypto_bignum *b,
  937. struct crypto_bignum *c)
  938. {
  939. return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
  940. 0 : -1;
  941. }
  942. int crypto_bignum_div(const struct crypto_bignum *a,
  943. const struct crypto_bignum *b,
  944. struct crypto_bignum *c)
  945. {
  946. int res;
  947. BN_CTX *bnctx;
  948. bnctx = BN_CTX_new();
  949. if (bnctx == NULL)
  950. return -1;
  951. res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
  952. (const BIGNUM *) b, bnctx);
  953. BN_CTX_free(bnctx);
  954. return res ? 0 : -1;
  955. }
  956. int crypto_bignum_mulmod(const struct crypto_bignum *a,
  957. const struct crypto_bignum *b,
  958. const struct crypto_bignum *c,
  959. struct crypto_bignum *d)
  960. {
  961. int res;
  962. BN_CTX *bnctx;
  963. bnctx = BN_CTX_new();
  964. if (bnctx == NULL)
  965. return -1;
  966. res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
  967. (const BIGNUM *) c, bnctx);
  968. BN_CTX_free(bnctx);
  969. return res ? 0 : -1;
  970. }
  971. int crypto_bignum_cmp(const struct crypto_bignum *a,
  972. const struct crypto_bignum *b)
  973. {
  974. return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
  975. }
  976. int crypto_bignum_bits(const struct crypto_bignum *a)
  977. {
  978. return BN_num_bits((const BIGNUM *) a);
  979. }
  980. int crypto_bignum_is_zero(const struct crypto_bignum *a)
  981. {
  982. return BN_is_zero((const BIGNUM *) a);
  983. }
  984. int crypto_bignum_is_one(const struct crypto_bignum *a)
  985. {
  986. return BN_is_one((const BIGNUM *) a);
  987. }
  988. int crypto_bignum_legendre(const struct crypto_bignum *a,
  989. const struct crypto_bignum *p)
  990. {
  991. BN_CTX *bnctx;
  992. BIGNUM *exp = NULL, *tmp = NULL;
  993. int res = -2;
  994. bnctx = BN_CTX_new();
  995. if (bnctx == NULL)
  996. return -2;
  997. exp = BN_new();
  998. tmp = BN_new();
  999. if (!exp || !tmp ||
  1000. /* exp = (p-1) / 2 */
  1001. !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
  1002. !BN_rshift1(exp, exp) ||
  1003. !BN_mod_exp(tmp, (const BIGNUM *) a, exp, (const BIGNUM *) p,
  1004. bnctx))
  1005. goto fail;
  1006. if (BN_is_word(tmp, 1))
  1007. res = 1;
  1008. else if (BN_is_zero(tmp))
  1009. res = 0;
  1010. else
  1011. res = -1;
  1012. fail:
  1013. BN_clear_free(tmp);
  1014. BN_clear_free(exp);
  1015. BN_CTX_free(bnctx);
  1016. return res;
  1017. }
  1018. #ifdef CONFIG_ECC
  1019. struct crypto_ec {
  1020. EC_GROUP *group;
  1021. BN_CTX *bnctx;
  1022. BIGNUM *prime;
  1023. BIGNUM *order;
  1024. BIGNUM *a;
  1025. BIGNUM *b;
  1026. };
  1027. struct crypto_ec * crypto_ec_init(int group)
  1028. {
  1029. struct crypto_ec *e;
  1030. int nid;
  1031. /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
  1032. switch (group) {
  1033. case 19:
  1034. nid = NID_X9_62_prime256v1;
  1035. break;
  1036. case 20:
  1037. nid = NID_secp384r1;
  1038. break;
  1039. case 21:
  1040. nid = NID_secp521r1;
  1041. break;
  1042. case 25:
  1043. nid = NID_X9_62_prime192v1;
  1044. break;
  1045. case 26:
  1046. nid = NID_secp224r1;
  1047. break;
  1048. #ifdef NID_brainpoolP224r1
  1049. case 27:
  1050. nid = NID_brainpoolP224r1;
  1051. break;
  1052. #endif /* NID_brainpoolP224r1 */
  1053. #ifdef NID_brainpoolP256r1
  1054. case 28:
  1055. nid = NID_brainpoolP256r1;
  1056. break;
  1057. #endif /* NID_brainpoolP256r1 */
  1058. #ifdef NID_brainpoolP384r1
  1059. case 29:
  1060. nid = NID_brainpoolP384r1;
  1061. break;
  1062. #endif /* NID_brainpoolP384r1 */
  1063. #ifdef NID_brainpoolP512r1
  1064. case 30:
  1065. nid = NID_brainpoolP512r1;
  1066. break;
  1067. #endif /* NID_brainpoolP512r1 */
  1068. default:
  1069. return NULL;
  1070. }
  1071. e = os_zalloc(sizeof(*e));
  1072. if (e == NULL)
  1073. return NULL;
  1074. e->bnctx = BN_CTX_new();
  1075. e->group = EC_GROUP_new_by_curve_name(nid);
  1076. e->prime = BN_new();
  1077. e->order = BN_new();
  1078. e->a = BN_new();
  1079. e->b = BN_new();
  1080. if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
  1081. e->order == NULL || e->a == NULL || e->b == NULL ||
  1082. !EC_GROUP_get_curve_GFp(e->group, e->prime, e->a, e->b, e->bnctx) ||
  1083. !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
  1084. crypto_ec_deinit(e);
  1085. e = NULL;
  1086. }
  1087. return e;
  1088. }
  1089. void crypto_ec_deinit(struct crypto_ec *e)
  1090. {
  1091. if (e == NULL)
  1092. return;
  1093. BN_clear_free(e->b);
  1094. BN_clear_free(e->a);
  1095. BN_clear_free(e->order);
  1096. BN_clear_free(e->prime);
  1097. EC_GROUP_free(e->group);
  1098. BN_CTX_free(e->bnctx);
  1099. os_free(e);
  1100. }
  1101. struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
  1102. {
  1103. if (e == NULL)
  1104. return NULL;
  1105. return (struct crypto_ec_point *) EC_POINT_new(e->group);
  1106. }
  1107. size_t crypto_ec_prime_len(struct crypto_ec *e)
  1108. {
  1109. return BN_num_bytes(e->prime);
  1110. }
  1111. size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
  1112. {
  1113. return BN_num_bits(e->prime);
  1114. }
  1115. const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
  1116. {
  1117. return (const struct crypto_bignum *) e->prime;
  1118. }
  1119. const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
  1120. {
  1121. return (const struct crypto_bignum *) e->order;
  1122. }
  1123. void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
  1124. {
  1125. if (clear)
  1126. EC_POINT_clear_free((EC_POINT *) p);
  1127. else
  1128. EC_POINT_free((EC_POINT *) p);
  1129. }
  1130. int crypto_ec_point_to_bin(struct crypto_ec *e,
  1131. const struct crypto_ec_point *point, u8 *x, u8 *y)
  1132. {
  1133. BIGNUM *x_bn, *y_bn;
  1134. int ret = -1;
  1135. int len = BN_num_bytes(e->prime);
  1136. x_bn = BN_new();
  1137. y_bn = BN_new();
  1138. if (x_bn && y_bn &&
  1139. EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
  1140. x_bn, y_bn, e->bnctx)) {
  1141. if (x) {
  1142. crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
  1143. x, len, len);
  1144. }
  1145. if (y) {
  1146. crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
  1147. y, len, len);
  1148. }
  1149. ret = 0;
  1150. }
  1151. BN_clear_free(x_bn);
  1152. BN_clear_free(y_bn);
  1153. return ret;
  1154. }
  1155. struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
  1156. const u8 *val)
  1157. {
  1158. BIGNUM *x, *y;
  1159. EC_POINT *elem;
  1160. int len = BN_num_bytes(e->prime);
  1161. x = BN_bin2bn(val, len, NULL);
  1162. y = BN_bin2bn(val + len, len, NULL);
  1163. elem = EC_POINT_new(e->group);
  1164. if (x == NULL || y == NULL || elem == NULL) {
  1165. BN_clear_free(x);
  1166. BN_clear_free(y);
  1167. EC_POINT_clear_free(elem);
  1168. return NULL;
  1169. }
  1170. if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
  1171. e->bnctx)) {
  1172. EC_POINT_clear_free(elem);
  1173. elem = NULL;
  1174. }
  1175. BN_clear_free(x);
  1176. BN_clear_free(y);
  1177. return (struct crypto_ec_point *) elem;
  1178. }
  1179. int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
  1180. const struct crypto_ec_point *b,
  1181. struct crypto_ec_point *c)
  1182. {
  1183. return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
  1184. (const EC_POINT *) b, e->bnctx) ? 0 : -1;
  1185. }
  1186. int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
  1187. const struct crypto_bignum *b,
  1188. struct crypto_ec_point *res)
  1189. {
  1190. return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
  1191. (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
  1192. ? 0 : -1;
  1193. }
  1194. int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
  1195. {
  1196. return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
  1197. }
  1198. int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
  1199. struct crypto_ec_point *p,
  1200. const struct crypto_bignum *x, int y_bit)
  1201. {
  1202. if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
  1203. (const BIGNUM *) x, y_bit,
  1204. e->bnctx) ||
  1205. !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
  1206. return -1;
  1207. return 0;
  1208. }
  1209. struct crypto_bignum *
  1210. crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
  1211. const struct crypto_bignum *x)
  1212. {
  1213. BIGNUM *tmp, *tmp2, *y_sqr = NULL;
  1214. tmp = BN_new();
  1215. tmp2 = BN_new();
  1216. /* y^2 = x^3 + ax + b */
  1217. if (tmp && tmp2 &&
  1218. BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
  1219. BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
  1220. BN_mod_mul(tmp2, e->a, (const BIGNUM *) x, e->prime, e->bnctx) &&
  1221. BN_mod_add_quick(tmp2, tmp2, tmp, e->prime) &&
  1222. BN_mod_add_quick(tmp2, tmp2, e->b, e->prime)) {
  1223. y_sqr = tmp2;
  1224. tmp2 = NULL;
  1225. }
  1226. BN_clear_free(tmp);
  1227. BN_clear_free(tmp2);
  1228. return (struct crypto_bignum *) y_sqr;
  1229. }
  1230. int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
  1231. const struct crypto_ec_point *p)
  1232. {
  1233. return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
  1234. }
  1235. int crypto_ec_point_is_on_curve(struct crypto_ec *e,
  1236. const struct crypto_ec_point *p)
  1237. {
  1238. return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
  1239. e->bnctx) == 1;
  1240. }
  1241. int crypto_ec_point_cmp(const struct crypto_ec *e,
  1242. const struct crypto_ec_point *a,
  1243. const struct crypto_ec_point *b)
  1244. {
  1245. return EC_POINT_cmp(e->group, (const EC_POINT *) a,
  1246. (const EC_POINT *) b, e->bnctx);
  1247. }
  1248. #endif /* CONFIG_ECC */