crypto_openssl.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  1. /*
  2. * Wrapper functions for OpenSSL libcrypto
  3. * Copyright (c) 2004-2017, 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 "md5.h"
  31. #include "aes_wrap.h"
  32. #include "crypto.h"
  33. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  34. /* Compatibility wrappers for older versions. */
  35. static HMAC_CTX * HMAC_CTX_new(void)
  36. {
  37. HMAC_CTX *ctx;
  38. ctx = os_zalloc(sizeof(*ctx));
  39. if (ctx)
  40. HMAC_CTX_init(ctx);
  41. return ctx;
  42. }
  43. static void HMAC_CTX_free(HMAC_CTX *ctx)
  44. {
  45. if (!ctx)
  46. return;
  47. HMAC_CTX_cleanup(ctx);
  48. bin_clear_free(ctx, sizeof(*ctx));
  49. }
  50. static EVP_MD_CTX * EVP_MD_CTX_new(void)
  51. {
  52. EVP_MD_CTX *ctx;
  53. ctx = os_zalloc(sizeof(*ctx));
  54. if (ctx)
  55. EVP_MD_CTX_init(ctx);
  56. return ctx;
  57. }
  58. static void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
  59. {
  60. if (!ctx)
  61. return;
  62. EVP_MD_CTX_cleanup(ctx);
  63. bin_clear_free(ctx, sizeof(*ctx));
  64. }
  65. #endif /* OpenSSL version < 1.1.0 */
  66. static BIGNUM * get_group5_prime(void)
  67. {
  68. #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  69. return BN_get_rfc3526_prime_1536(NULL);
  70. #elif !defined(OPENSSL_IS_BORINGSSL)
  71. return get_rfc3526_prime_1536(NULL);
  72. #else
  73. static const unsigned char RFC3526_PRIME_1536[] = {
  74. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
  75. 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  76. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
  77. 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  78. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
  79. 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  80. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
  81. 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  82. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
  83. 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  84. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
  85. 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  86. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
  87. 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  88. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
  89. 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  90. };
  91. return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
  92. #endif
  93. }
  94. #ifdef OPENSSL_NO_SHA256
  95. #define NO_SHA256_WRAPPER
  96. #endif
  97. #ifdef OPENSSL_NO_SHA512
  98. #define NO_SHA384_WRAPPER
  99. #endif
  100. static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
  101. const u8 *addr[], const size_t *len, u8 *mac)
  102. {
  103. EVP_MD_CTX *ctx;
  104. size_t i;
  105. unsigned int mac_len;
  106. if (TEST_FAIL())
  107. return -1;
  108. ctx = EVP_MD_CTX_new();
  109. if (!ctx)
  110. return -1;
  111. if (!EVP_DigestInit_ex(ctx, type, NULL)) {
  112. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
  113. ERR_error_string(ERR_get_error(), NULL));
  114. EVP_MD_CTX_free(ctx);
  115. return -1;
  116. }
  117. for (i = 0; i < num_elem; i++) {
  118. if (!EVP_DigestUpdate(ctx, addr[i], len[i])) {
  119. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
  120. "failed: %s",
  121. ERR_error_string(ERR_get_error(), NULL));
  122. EVP_MD_CTX_free(ctx);
  123. return -1;
  124. }
  125. }
  126. if (!EVP_DigestFinal(ctx, mac, &mac_len)) {
  127. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
  128. ERR_error_string(ERR_get_error(), NULL));
  129. EVP_MD_CTX_free(ctx);
  130. return -1;
  131. }
  132. EVP_MD_CTX_free(ctx);
  133. return 0;
  134. }
  135. #ifndef CONFIG_FIPS
  136. int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  137. {
  138. return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
  139. }
  140. #endif /* CONFIG_FIPS */
  141. int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
  142. {
  143. u8 pkey[8], next, tmp;
  144. int i;
  145. DES_key_schedule ks;
  146. /* Add parity bits to the key */
  147. next = 0;
  148. for (i = 0; i < 7; i++) {
  149. tmp = key[i];
  150. pkey[i] = (tmp >> i) | next | 1;
  151. next = tmp << (7 - i);
  152. }
  153. pkey[i] = next | 1;
  154. DES_set_key((DES_cblock *) &pkey, &ks);
  155. DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
  156. DES_ENCRYPT);
  157. return 0;
  158. }
  159. #ifndef CONFIG_NO_RC4
  160. int rc4_skip(const u8 *key, size_t keylen, size_t skip,
  161. u8 *data, size_t data_len)
  162. {
  163. #ifdef OPENSSL_NO_RC4
  164. return -1;
  165. #else /* OPENSSL_NO_RC4 */
  166. EVP_CIPHER_CTX *ctx;
  167. int outl;
  168. int res = -1;
  169. unsigned char skip_buf[16];
  170. ctx = EVP_CIPHER_CTX_new();
  171. if (!ctx ||
  172. !EVP_CIPHER_CTX_set_padding(ctx, 0) ||
  173. !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
  174. !EVP_CIPHER_CTX_set_key_length(ctx, keylen) ||
  175. !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1))
  176. goto out;
  177. while (skip >= sizeof(skip_buf)) {
  178. size_t len = skip;
  179. if (len > sizeof(skip_buf))
  180. len = sizeof(skip_buf);
  181. if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len))
  182. goto out;
  183. skip -= len;
  184. }
  185. if (EVP_CipherUpdate(ctx, data, &outl, data, data_len))
  186. res = 0;
  187. out:
  188. if (ctx)
  189. EVP_CIPHER_CTX_free(ctx);
  190. return res;
  191. #endif /* OPENSSL_NO_RC4 */
  192. }
  193. #endif /* CONFIG_NO_RC4 */
  194. #ifndef CONFIG_FIPS
  195. int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  196. {
  197. return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
  198. }
  199. #endif /* CONFIG_FIPS */
  200. int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  201. {
  202. return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
  203. }
  204. #ifndef NO_SHA256_WRAPPER
  205. int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  206. u8 *mac)
  207. {
  208. return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
  209. }
  210. #endif /* NO_SHA256_WRAPPER */
  211. #ifndef NO_SHA384_WRAPPER
  212. int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  213. u8 *mac)
  214. {
  215. return openssl_digest_vector(EVP_sha384(), num_elem, addr, len, mac);
  216. }
  217. #endif /* NO_SHA384_WRAPPER */
  218. static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
  219. {
  220. switch (keylen) {
  221. case 16:
  222. return EVP_aes_128_ecb();
  223. #ifndef OPENSSL_IS_BORINGSSL
  224. case 24:
  225. return EVP_aes_192_ecb();
  226. #endif /* OPENSSL_IS_BORINGSSL */
  227. case 32:
  228. return EVP_aes_256_ecb();
  229. }
  230. return NULL;
  231. }
  232. void * aes_encrypt_init(const u8 *key, size_t len)
  233. {
  234. EVP_CIPHER_CTX *ctx;
  235. const EVP_CIPHER *type;
  236. if (TEST_FAIL())
  237. return NULL;
  238. type = aes_get_evp_cipher(len);
  239. if (type == NULL)
  240. return NULL;
  241. ctx = EVP_CIPHER_CTX_new();
  242. if (ctx == NULL)
  243. return NULL;
  244. if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
  245. os_free(ctx);
  246. return NULL;
  247. }
  248. EVP_CIPHER_CTX_set_padding(ctx, 0);
  249. return ctx;
  250. }
  251. int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
  252. {
  253. EVP_CIPHER_CTX *c = ctx;
  254. int clen = 16;
  255. if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
  256. wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
  257. ERR_error_string(ERR_get_error(), NULL));
  258. return -1;
  259. }
  260. return 0;
  261. }
  262. void aes_encrypt_deinit(void *ctx)
  263. {
  264. EVP_CIPHER_CTX *c = ctx;
  265. u8 buf[16];
  266. int len = sizeof(buf);
  267. if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
  268. wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
  269. "%s", ERR_error_string(ERR_get_error(), NULL));
  270. }
  271. if (len != 0) {
  272. wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
  273. "in AES encrypt", len);
  274. }
  275. EVP_CIPHER_CTX_free(c);
  276. }
  277. void * aes_decrypt_init(const u8 *key, size_t len)
  278. {
  279. EVP_CIPHER_CTX *ctx;
  280. const EVP_CIPHER *type;
  281. if (TEST_FAIL())
  282. return NULL;
  283. type = aes_get_evp_cipher(len);
  284. if (type == NULL)
  285. return NULL;
  286. ctx = EVP_CIPHER_CTX_new();
  287. if (ctx == NULL)
  288. return NULL;
  289. if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
  290. EVP_CIPHER_CTX_free(ctx);
  291. return NULL;
  292. }
  293. EVP_CIPHER_CTX_set_padding(ctx, 0);
  294. return ctx;
  295. }
  296. int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
  297. {
  298. EVP_CIPHER_CTX *c = ctx;
  299. int plen = 16;
  300. if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
  301. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
  302. ERR_error_string(ERR_get_error(), NULL));
  303. return -1;
  304. }
  305. return 0;
  306. }
  307. void aes_decrypt_deinit(void *ctx)
  308. {
  309. EVP_CIPHER_CTX *c = ctx;
  310. u8 buf[16];
  311. int len = sizeof(buf);
  312. if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
  313. wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
  314. "%s", ERR_error_string(ERR_get_error(), NULL));
  315. }
  316. if (len != 0) {
  317. wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
  318. "in AES decrypt", len);
  319. }
  320. EVP_CIPHER_CTX_free(c);
  321. }
  322. #ifndef CONFIG_FIPS
  323. #ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP
  324. int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
  325. {
  326. AES_KEY actx;
  327. int res;
  328. if (TEST_FAIL())
  329. return -1;
  330. if (AES_set_encrypt_key(kek, kek_len << 3, &actx))
  331. return -1;
  332. res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8);
  333. OPENSSL_cleanse(&actx, sizeof(actx));
  334. return res <= 0 ? -1 : 0;
  335. }
  336. int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
  337. u8 *plain)
  338. {
  339. AES_KEY actx;
  340. int res;
  341. if (TEST_FAIL())
  342. return -1;
  343. if (AES_set_decrypt_key(kek, kek_len << 3, &actx))
  344. return -1;
  345. res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8);
  346. OPENSSL_cleanse(&actx, sizeof(actx));
  347. return res <= 0 ? -1 : 0;
  348. }
  349. #endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */
  350. #endif /* CONFIG_FIPS */
  351. int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
  352. {
  353. EVP_CIPHER_CTX *ctx;
  354. int clen, len;
  355. u8 buf[16];
  356. int res = -1;
  357. if (TEST_FAIL())
  358. return -1;
  359. ctx = EVP_CIPHER_CTX_new();
  360. if (!ctx)
  361. return -1;
  362. clen = data_len;
  363. len = sizeof(buf);
  364. if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
  365. EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
  366. EVP_EncryptUpdate(ctx, data, &clen, data, data_len) == 1 &&
  367. clen == (int) data_len &&
  368. EVP_EncryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
  369. res = 0;
  370. EVP_CIPHER_CTX_free(ctx);
  371. return res;
  372. }
  373. int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
  374. {
  375. EVP_CIPHER_CTX *ctx;
  376. int plen, len;
  377. u8 buf[16];
  378. int res = -1;
  379. if (TEST_FAIL())
  380. return -1;
  381. ctx = EVP_CIPHER_CTX_new();
  382. if (!ctx)
  383. return -1;
  384. plen = data_len;
  385. len = sizeof(buf);
  386. if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
  387. EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
  388. EVP_DecryptUpdate(ctx, data, &plen, data, data_len) == 1 &&
  389. plen == (int) data_len &&
  390. EVP_DecryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
  391. res = 0;
  392. EVP_CIPHER_CTX_free(ctx);
  393. return res;
  394. }
  395. int crypto_mod_exp(const u8 *base, size_t base_len,
  396. const u8 *power, size_t power_len,
  397. const u8 *modulus, size_t modulus_len,
  398. u8 *result, size_t *result_len)
  399. {
  400. BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
  401. int ret = -1;
  402. BN_CTX *ctx;
  403. ctx = BN_CTX_new();
  404. if (ctx == NULL)
  405. return -1;
  406. bn_base = BN_bin2bn(base, base_len, NULL);
  407. bn_exp = BN_bin2bn(power, power_len, NULL);
  408. bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
  409. bn_result = BN_new();
  410. if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
  411. bn_result == NULL)
  412. goto error;
  413. if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
  414. goto error;
  415. *result_len = BN_bn2bin(bn_result, result);
  416. ret = 0;
  417. error:
  418. BN_clear_free(bn_base);
  419. BN_clear_free(bn_exp);
  420. BN_clear_free(bn_modulus);
  421. BN_clear_free(bn_result);
  422. BN_CTX_free(ctx);
  423. return ret;
  424. }
  425. struct crypto_cipher {
  426. EVP_CIPHER_CTX *enc;
  427. EVP_CIPHER_CTX *dec;
  428. };
  429. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  430. const u8 *iv, const u8 *key,
  431. size_t key_len)
  432. {
  433. struct crypto_cipher *ctx;
  434. const EVP_CIPHER *cipher;
  435. ctx = os_zalloc(sizeof(*ctx));
  436. if (ctx == NULL)
  437. return NULL;
  438. switch (alg) {
  439. #ifndef CONFIG_NO_RC4
  440. #ifndef OPENSSL_NO_RC4
  441. case CRYPTO_CIPHER_ALG_RC4:
  442. cipher = EVP_rc4();
  443. break;
  444. #endif /* OPENSSL_NO_RC4 */
  445. #endif /* CONFIG_NO_RC4 */
  446. #ifndef OPENSSL_NO_AES
  447. case CRYPTO_CIPHER_ALG_AES:
  448. switch (key_len) {
  449. case 16:
  450. cipher = EVP_aes_128_cbc();
  451. break;
  452. #ifndef OPENSSL_IS_BORINGSSL
  453. case 24:
  454. cipher = EVP_aes_192_cbc();
  455. break;
  456. #endif /* OPENSSL_IS_BORINGSSL */
  457. case 32:
  458. cipher = EVP_aes_256_cbc();
  459. break;
  460. default:
  461. os_free(ctx);
  462. return NULL;
  463. }
  464. break;
  465. #endif /* OPENSSL_NO_AES */
  466. #ifndef OPENSSL_NO_DES
  467. case CRYPTO_CIPHER_ALG_3DES:
  468. cipher = EVP_des_ede3_cbc();
  469. break;
  470. case CRYPTO_CIPHER_ALG_DES:
  471. cipher = EVP_des_cbc();
  472. break;
  473. #endif /* OPENSSL_NO_DES */
  474. #ifndef OPENSSL_NO_RC2
  475. case CRYPTO_CIPHER_ALG_RC2:
  476. cipher = EVP_rc2_ecb();
  477. break;
  478. #endif /* OPENSSL_NO_RC2 */
  479. default:
  480. os_free(ctx);
  481. return NULL;
  482. }
  483. if (!(ctx->enc = EVP_CIPHER_CTX_new()) ||
  484. !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) ||
  485. !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) ||
  486. !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) ||
  487. !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) {
  488. if (ctx->enc)
  489. EVP_CIPHER_CTX_free(ctx->enc);
  490. os_free(ctx);
  491. return NULL;
  492. }
  493. if (!(ctx->dec = EVP_CIPHER_CTX_new()) ||
  494. !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) ||
  495. !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) ||
  496. !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) ||
  497. !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) {
  498. EVP_CIPHER_CTX_free(ctx->enc);
  499. if (ctx->dec)
  500. EVP_CIPHER_CTX_free(ctx->dec);
  501. os_free(ctx);
  502. return NULL;
  503. }
  504. return ctx;
  505. }
  506. int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
  507. u8 *crypt, size_t len)
  508. {
  509. int outl;
  510. if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len))
  511. return -1;
  512. return 0;
  513. }
  514. int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
  515. u8 *plain, size_t len)
  516. {
  517. int outl;
  518. outl = len;
  519. if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len))
  520. return -1;
  521. return 0;
  522. }
  523. void crypto_cipher_deinit(struct crypto_cipher *ctx)
  524. {
  525. EVP_CIPHER_CTX_free(ctx->enc);
  526. EVP_CIPHER_CTX_free(ctx->dec);
  527. os_free(ctx);
  528. }
  529. void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
  530. {
  531. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  532. DH *dh;
  533. struct wpabuf *pubkey = NULL, *privkey = NULL;
  534. size_t publen, privlen;
  535. *priv = NULL;
  536. wpabuf_free(*publ);
  537. *publ = NULL;
  538. dh = DH_new();
  539. if (dh == NULL)
  540. return NULL;
  541. dh->g = BN_new();
  542. if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
  543. goto err;
  544. dh->p = get_group5_prime();
  545. if (dh->p == NULL)
  546. goto err;
  547. if (DH_generate_key(dh) != 1)
  548. goto err;
  549. publen = BN_num_bytes(dh->pub_key);
  550. pubkey = wpabuf_alloc(publen);
  551. if (pubkey == NULL)
  552. goto err;
  553. privlen = BN_num_bytes(dh->priv_key);
  554. privkey = wpabuf_alloc(privlen);
  555. if (privkey == NULL)
  556. goto err;
  557. BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
  558. BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
  559. *priv = privkey;
  560. *publ = pubkey;
  561. return dh;
  562. err:
  563. wpabuf_clear_free(pubkey);
  564. wpabuf_clear_free(privkey);
  565. DH_free(dh);
  566. return NULL;
  567. #else
  568. DH *dh;
  569. struct wpabuf *pubkey = NULL, *privkey = NULL;
  570. size_t publen, privlen;
  571. BIGNUM *p = NULL, *g;
  572. const BIGNUM *priv_key = NULL, *pub_key = NULL;
  573. *priv = NULL;
  574. wpabuf_free(*publ);
  575. *publ = NULL;
  576. dh = DH_new();
  577. if (dh == NULL)
  578. return NULL;
  579. g = BN_new();
  580. p = get_group5_prime();
  581. if (!g || BN_set_word(g, 2) != 1 || !p ||
  582. DH_set0_pqg(dh, p, NULL, g) != 1)
  583. goto err;
  584. p = NULL;
  585. g = NULL;
  586. if (DH_generate_key(dh) != 1)
  587. goto err;
  588. DH_get0_key(dh, &pub_key, &priv_key);
  589. publen = BN_num_bytes(pub_key);
  590. pubkey = wpabuf_alloc(publen);
  591. if (!pubkey)
  592. goto err;
  593. privlen = BN_num_bytes(priv_key);
  594. privkey = wpabuf_alloc(privlen);
  595. if (!privkey)
  596. goto err;
  597. BN_bn2bin(pub_key, wpabuf_put(pubkey, publen));
  598. BN_bn2bin(priv_key, wpabuf_put(privkey, privlen));
  599. *priv = privkey;
  600. *publ = pubkey;
  601. return dh;
  602. err:
  603. BN_free(p);
  604. BN_free(g);
  605. wpabuf_clear_free(pubkey);
  606. wpabuf_clear_free(privkey);
  607. DH_free(dh);
  608. return NULL;
  609. #endif
  610. }
  611. void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
  612. {
  613. #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
  614. DH *dh;
  615. dh = DH_new();
  616. if (dh == NULL)
  617. return NULL;
  618. dh->g = BN_new();
  619. if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
  620. goto err;
  621. dh->p = get_group5_prime();
  622. if (dh->p == NULL)
  623. goto err;
  624. dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
  625. if (dh->priv_key == NULL)
  626. goto err;
  627. dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
  628. if (dh->pub_key == NULL)
  629. goto err;
  630. if (DH_generate_key(dh) != 1)
  631. goto err;
  632. return dh;
  633. err:
  634. DH_free(dh);
  635. return NULL;
  636. #else
  637. DH *dh;
  638. BIGNUM *p = NULL, *g, *priv_key = NULL, *pub_key = NULL;
  639. dh = DH_new();
  640. if (dh == NULL)
  641. return NULL;
  642. g = BN_new();
  643. p = get_group5_prime();
  644. if (!g || BN_set_word(g, 2) != 1 || !p ||
  645. DH_set0_pqg(dh, p, NULL, g) != 1)
  646. goto err;
  647. p = NULL;
  648. g = NULL;
  649. priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
  650. pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
  651. if (!priv_key || !pub_key || DH_set0_key(dh, pub_key, priv_key) != 1)
  652. goto err;
  653. pub_key = NULL;
  654. priv_key = NULL;
  655. if (DH_generate_key(dh) != 1)
  656. goto err;
  657. return dh;
  658. err:
  659. BN_free(p);
  660. BN_free(g);
  661. BN_free(pub_key);
  662. BN_clear_free(priv_key);
  663. DH_free(dh);
  664. return NULL;
  665. #endif
  666. }
  667. struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
  668. const struct wpabuf *own_private)
  669. {
  670. BIGNUM *pub_key;
  671. struct wpabuf *res = NULL;
  672. size_t rlen;
  673. DH *dh = ctx;
  674. int keylen;
  675. if (ctx == NULL)
  676. return NULL;
  677. pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
  678. NULL);
  679. if (pub_key == NULL)
  680. return NULL;
  681. rlen = DH_size(dh);
  682. res = wpabuf_alloc(rlen);
  683. if (res == NULL)
  684. goto err;
  685. keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
  686. if (keylen < 0)
  687. goto err;
  688. wpabuf_put(res, keylen);
  689. BN_clear_free(pub_key);
  690. return res;
  691. err:
  692. BN_clear_free(pub_key);
  693. wpabuf_clear_free(res);
  694. return NULL;
  695. }
  696. void dh5_free(void *ctx)
  697. {
  698. DH *dh;
  699. if (ctx == NULL)
  700. return;
  701. dh = ctx;
  702. DH_free(dh);
  703. }
  704. struct crypto_hash {
  705. HMAC_CTX *ctx;
  706. };
  707. struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
  708. size_t key_len)
  709. {
  710. struct crypto_hash *ctx;
  711. const EVP_MD *md;
  712. switch (alg) {
  713. #ifndef OPENSSL_NO_MD5
  714. case CRYPTO_HASH_ALG_HMAC_MD5:
  715. md = EVP_md5();
  716. break;
  717. #endif /* OPENSSL_NO_MD5 */
  718. #ifndef OPENSSL_NO_SHA
  719. case CRYPTO_HASH_ALG_HMAC_SHA1:
  720. md = EVP_sha1();
  721. break;
  722. #endif /* OPENSSL_NO_SHA */
  723. #ifndef OPENSSL_NO_SHA256
  724. #ifdef CONFIG_SHA256
  725. case CRYPTO_HASH_ALG_HMAC_SHA256:
  726. md = EVP_sha256();
  727. break;
  728. #endif /* CONFIG_SHA256 */
  729. #endif /* OPENSSL_NO_SHA256 */
  730. default:
  731. return NULL;
  732. }
  733. ctx = os_zalloc(sizeof(*ctx));
  734. if (ctx == NULL)
  735. return NULL;
  736. ctx->ctx = HMAC_CTX_new();
  737. if (!ctx->ctx) {
  738. os_free(ctx);
  739. return NULL;
  740. }
  741. if (HMAC_Init_ex(ctx->ctx, key, key_len, md, NULL) != 1) {
  742. HMAC_CTX_free(ctx->ctx);
  743. bin_clear_free(ctx, sizeof(*ctx));
  744. return NULL;
  745. }
  746. return ctx;
  747. }
  748. void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
  749. {
  750. if (ctx == NULL)
  751. return;
  752. HMAC_Update(ctx->ctx, data, len);
  753. }
  754. int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
  755. {
  756. unsigned int mdlen;
  757. int res;
  758. if (ctx == NULL)
  759. return -2;
  760. if (mac == NULL || len == NULL) {
  761. HMAC_CTX_free(ctx->ctx);
  762. bin_clear_free(ctx, sizeof(*ctx));
  763. return 0;
  764. }
  765. mdlen = *len;
  766. res = HMAC_Final(ctx->ctx, mac, &mdlen);
  767. HMAC_CTX_free(ctx->ctx);
  768. bin_clear_free(ctx, sizeof(*ctx));
  769. if (res == 1) {
  770. *len = mdlen;
  771. return 0;
  772. }
  773. return -1;
  774. }
  775. static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
  776. size_t key_len, size_t num_elem,
  777. const u8 *addr[], const size_t *len, u8 *mac,
  778. unsigned int mdlen)
  779. {
  780. HMAC_CTX *ctx;
  781. size_t i;
  782. int res;
  783. if (TEST_FAIL())
  784. return -1;
  785. ctx = HMAC_CTX_new();
  786. if (!ctx)
  787. return -1;
  788. res = HMAC_Init_ex(ctx, key, key_len, type, NULL);
  789. if (res != 1)
  790. goto done;
  791. for (i = 0; i < num_elem; i++)
  792. HMAC_Update(ctx, addr[i], len[i]);
  793. res = HMAC_Final(ctx, mac, &mdlen);
  794. done:
  795. HMAC_CTX_free(ctx);
  796. return res == 1 ? 0 : -1;
  797. }
  798. #ifndef CONFIG_FIPS
  799. int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
  800. const u8 *addr[], const size_t *len, u8 *mac)
  801. {
  802. return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
  803. mac, 16);
  804. }
  805. int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  806. u8 *mac)
  807. {
  808. return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
  809. }
  810. #endif /* CONFIG_FIPS */
  811. int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
  812. int iterations, u8 *buf, size_t buflen)
  813. {
  814. if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
  815. ssid_len, iterations, buflen, buf) != 1)
  816. return -1;
  817. return 0;
  818. }
  819. int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
  820. const u8 *addr[], const size_t *len, u8 *mac)
  821. {
  822. return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr,
  823. len, mac, 20);
  824. }
  825. int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  826. u8 *mac)
  827. {
  828. return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
  829. }
  830. #ifdef CONFIG_SHA256
  831. int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
  832. const u8 *addr[], const size_t *len, u8 *mac)
  833. {
  834. return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
  835. len, mac, 32);
  836. }
  837. int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
  838. size_t data_len, u8 *mac)
  839. {
  840. return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
  841. }
  842. #endif /* CONFIG_SHA256 */
  843. #ifdef CONFIG_SHA384
  844. int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
  845. const u8 *addr[], const size_t *len, u8 *mac)
  846. {
  847. return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
  848. len, mac, 48);
  849. }
  850. int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
  851. size_t data_len, u8 *mac)
  852. {
  853. return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
  854. }
  855. #endif /* CONFIG_SHA384 */
  856. int crypto_get_random(void *buf, size_t len)
  857. {
  858. if (RAND_bytes(buf, len) != 1)
  859. return -1;
  860. return 0;
  861. }
  862. #ifdef CONFIG_OPENSSL_CMAC
  863. int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
  864. const u8 *addr[], const size_t *len, u8 *mac)
  865. {
  866. CMAC_CTX *ctx;
  867. int ret = -1;
  868. size_t outlen, i;
  869. if (TEST_FAIL())
  870. return -1;
  871. ctx = CMAC_CTX_new();
  872. if (ctx == NULL)
  873. return -1;
  874. if (key_len == 32) {
  875. if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
  876. goto fail;
  877. } else if (key_len == 16) {
  878. if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
  879. goto fail;
  880. } else {
  881. goto fail;
  882. }
  883. for (i = 0; i < num_elem; i++) {
  884. if (!CMAC_Update(ctx, addr[i], len[i]))
  885. goto fail;
  886. }
  887. if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
  888. goto fail;
  889. ret = 0;
  890. fail:
  891. CMAC_CTX_free(ctx);
  892. return ret;
  893. }
  894. int omac1_aes_128_vector(const u8 *key, size_t num_elem,
  895. const u8 *addr[], const size_t *len, u8 *mac)
  896. {
  897. return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
  898. }
  899. int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
  900. {
  901. return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
  902. }
  903. int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
  904. {
  905. return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
  906. }
  907. #endif /* CONFIG_OPENSSL_CMAC */
  908. struct crypto_bignum * crypto_bignum_init(void)
  909. {
  910. if (TEST_FAIL())
  911. return NULL;
  912. return (struct crypto_bignum *) BN_new();
  913. }
  914. struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
  915. {
  916. BIGNUM *bn;
  917. if (TEST_FAIL())
  918. return NULL;
  919. bn = BN_bin2bn(buf, len, NULL);
  920. return (struct crypto_bignum *) bn;
  921. }
  922. void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
  923. {
  924. if (clear)
  925. BN_clear_free((BIGNUM *) n);
  926. else
  927. BN_free((BIGNUM *) n);
  928. }
  929. int crypto_bignum_to_bin(const struct crypto_bignum *a,
  930. u8 *buf, size_t buflen, size_t padlen)
  931. {
  932. int num_bytes, offset;
  933. if (TEST_FAIL())
  934. return -1;
  935. if (padlen > buflen)
  936. return -1;
  937. num_bytes = BN_num_bytes((const BIGNUM *) a);
  938. if ((size_t) num_bytes > buflen)
  939. return -1;
  940. if (padlen > (size_t) num_bytes)
  941. offset = padlen - num_bytes;
  942. else
  943. offset = 0;
  944. os_memset(buf, 0, offset);
  945. BN_bn2bin((const BIGNUM *) a, buf + offset);
  946. return num_bytes + offset;
  947. }
  948. int crypto_bignum_add(const struct crypto_bignum *a,
  949. const struct crypto_bignum *b,
  950. struct crypto_bignum *c)
  951. {
  952. return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
  953. 0 : -1;
  954. }
  955. int crypto_bignum_mod(const struct crypto_bignum *a,
  956. const struct crypto_bignum *b,
  957. struct crypto_bignum *c)
  958. {
  959. int res;
  960. BN_CTX *bnctx;
  961. bnctx = BN_CTX_new();
  962. if (bnctx == NULL)
  963. return -1;
  964. res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
  965. bnctx);
  966. BN_CTX_free(bnctx);
  967. return res ? 0 : -1;
  968. }
  969. int crypto_bignum_exptmod(const struct crypto_bignum *a,
  970. const struct crypto_bignum *b,
  971. const struct crypto_bignum *c,
  972. struct crypto_bignum *d)
  973. {
  974. int res;
  975. BN_CTX *bnctx;
  976. if (TEST_FAIL())
  977. return -1;
  978. bnctx = BN_CTX_new();
  979. if (bnctx == NULL)
  980. return -1;
  981. res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
  982. (const BIGNUM *) c, bnctx);
  983. BN_CTX_free(bnctx);
  984. return res ? 0 : -1;
  985. }
  986. int crypto_bignum_inverse(const struct crypto_bignum *a,
  987. const struct crypto_bignum *b,
  988. struct crypto_bignum *c)
  989. {
  990. BIGNUM *res;
  991. BN_CTX *bnctx;
  992. if (TEST_FAIL())
  993. return -1;
  994. bnctx = BN_CTX_new();
  995. if (bnctx == NULL)
  996. return -1;
  997. res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
  998. (const BIGNUM *) b, bnctx);
  999. BN_CTX_free(bnctx);
  1000. return res ? 0 : -1;
  1001. }
  1002. int crypto_bignum_sub(const struct crypto_bignum *a,
  1003. const struct crypto_bignum *b,
  1004. struct crypto_bignum *c)
  1005. {
  1006. if (TEST_FAIL())
  1007. return -1;
  1008. return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
  1009. 0 : -1;
  1010. }
  1011. int crypto_bignum_div(const struct crypto_bignum *a,
  1012. const struct crypto_bignum *b,
  1013. struct crypto_bignum *c)
  1014. {
  1015. int res;
  1016. BN_CTX *bnctx;
  1017. if (TEST_FAIL())
  1018. return -1;
  1019. bnctx = BN_CTX_new();
  1020. if (bnctx == NULL)
  1021. return -1;
  1022. res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
  1023. (const BIGNUM *) b, bnctx);
  1024. BN_CTX_free(bnctx);
  1025. return res ? 0 : -1;
  1026. }
  1027. int crypto_bignum_mulmod(const struct crypto_bignum *a,
  1028. const struct crypto_bignum *b,
  1029. const struct crypto_bignum *c,
  1030. struct crypto_bignum *d)
  1031. {
  1032. int res;
  1033. BN_CTX *bnctx;
  1034. if (TEST_FAIL())
  1035. return -1;
  1036. bnctx = BN_CTX_new();
  1037. if (bnctx == NULL)
  1038. return -1;
  1039. res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
  1040. (const BIGNUM *) c, bnctx);
  1041. BN_CTX_free(bnctx);
  1042. return res ? 0 : -1;
  1043. }
  1044. int crypto_bignum_cmp(const struct crypto_bignum *a,
  1045. const struct crypto_bignum *b)
  1046. {
  1047. return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
  1048. }
  1049. int crypto_bignum_bits(const struct crypto_bignum *a)
  1050. {
  1051. return BN_num_bits((const BIGNUM *) a);
  1052. }
  1053. int crypto_bignum_is_zero(const struct crypto_bignum *a)
  1054. {
  1055. return BN_is_zero((const BIGNUM *) a);
  1056. }
  1057. int crypto_bignum_is_one(const struct crypto_bignum *a)
  1058. {
  1059. return BN_is_one((const BIGNUM *) a);
  1060. }
  1061. int crypto_bignum_legendre(const struct crypto_bignum *a,
  1062. const struct crypto_bignum *p)
  1063. {
  1064. BN_CTX *bnctx;
  1065. BIGNUM *exp = NULL, *tmp = NULL;
  1066. int res = -2;
  1067. if (TEST_FAIL())
  1068. return -2;
  1069. bnctx = BN_CTX_new();
  1070. if (bnctx == NULL)
  1071. return -2;
  1072. exp = BN_new();
  1073. tmp = BN_new();
  1074. if (!exp || !tmp ||
  1075. /* exp = (p-1) / 2 */
  1076. !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
  1077. !BN_rshift1(exp, exp) ||
  1078. !BN_mod_exp(tmp, (const BIGNUM *) a, exp, (const BIGNUM *) p,
  1079. bnctx))
  1080. goto fail;
  1081. if (BN_is_word(tmp, 1))
  1082. res = 1;
  1083. else if (BN_is_zero(tmp))
  1084. res = 0;
  1085. else
  1086. res = -1;
  1087. fail:
  1088. BN_clear_free(tmp);
  1089. BN_clear_free(exp);
  1090. BN_CTX_free(bnctx);
  1091. return res;
  1092. }
  1093. #ifdef CONFIG_ECC
  1094. struct crypto_ec {
  1095. EC_GROUP *group;
  1096. int nid;
  1097. BN_CTX *bnctx;
  1098. BIGNUM *prime;
  1099. BIGNUM *order;
  1100. BIGNUM *a;
  1101. BIGNUM *b;
  1102. };
  1103. struct crypto_ec * crypto_ec_init(int group)
  1104. {
  1105. struct crypto_ec *e;
  1106. int nid;
  1107. /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
  1108. switch (group) {
  1109. case 19:
  1110. nid = NID_X9_62_prime256v1;
  1111. break;
  1112. case 20:
  1113. nid = NID_secp384r1;
  1114. break;
  1115. case 21:
  1116. nid = NID_secp521r1;
  1117. break;
  1118. case 25:
  1119. nid = NID_X9_62_prime192v1;
  1120. break;
  1121. case 26:
  1122. nid = NID_secp224r1;
  1123. break;
  1124. #ifdef NID_brainpoolP224r1
  1125. case 27:
  1126. nid = NID_brainpoolP224r1;
  1127. break;
  1128. #endif /* NID_brainpoolP224r1 */
  1129. #ifdef NID_brainpoolP256r1
  1130. case 28:
  1131. nid = NID_brainpoolP256r1;
  1132. break;
  1133. #endif /* NID_brainpoolP256r1 */
  1134. #ifdef NID_brainpoolP384r1
  1135. case 29:
  1136. nid = NID_brainpoolP384r1;
  1137. break;
  1138. #endif /* NID_brainpoolP384r1 */
  1139. #ifdef NID_brainpoolP512r1
  1140. case 30:
  1141. nid = NID_brainpoolP512r1;
  1142. break;
  1143. #endif /* NID_brainpoolP512r1 */
  1144. default:
  1145. return NULL;
  1146. }
  1147. e = os_zalloc(sizeof(*e));
  1148. if (e == NULL)
  1149. return NULL;
  1150. e->nid = nid;
  1151. e->bnctx = BN_CTX_new();
  1152. e->group = EC_GROUP_new_by_curve_name(nid);
  1153. e->prime = BN_new();
  1154. e->order = BN_new();
  1155. e->a = BN_new();
  1156. e->b = BN_new();
  1157. if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
  1158. e->order == NULL || e->a == NULL || e->b == NULL ||
  1159. !EC_GROUP_get_curve_GFp(e->group, e->prime, e->a, e->b, e->bnctx) ||
  1160. !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
  1161. crypto_ec_deinit(e);
  1162. e = NULL;
  1163. }
  1164. return e;
  1165. }
  1166. void crypto_ec_deinit(struct crypto_ec *e)
  1167. {
  1168. if (e == NULL)
  1169. return;
  1170. BN_clear_free(e->b);
  1171. BN_clear_free(e->a);
  1172. BN_clear_free(e->order);
  1173. BN_clear_free(e->prime);
  1174. EC_GROUP_free(e->group);
  1175. BN_CTX_free(e->bnctx);
  1176. os_free(e);
  1177. }
  1178. struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
  1179. {
  1180. if (TEST_FAIL())
  1181. return NULL;
  1182. if (e == NULL)
  1183. return NULL;
  1184. return (struct crypto_ec_point *) EC_POINT_new(e->group);
  1185. }
  1186. size_t crypto_ec_prime_len(struct crypto_ec *e)
  1187. {
  1188. return BN_num_bytes(e->prime);
  1189. }
  1190. size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
  1191. {
  1192. return BN_num_bits(e->prime);
  1193. }
  1194. const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
  1195. {
  1196. return (const struct crypto_bignum *) e->prime;
  1197. }
  1198. const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
  1199. {
  1200. return (const struct crypto_bignum *) e->order;
  1201. }
  1202. void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
  1203. {
  1204. if (clear)
  1205. EC_POINT_clear_free((EC_POINT *) p);
  1206. else
  1207. EC_POINT_free((EC_POINT *) p);
  1208. }
  1209. int crypto_ec_point_to_bin(struct crypto_ec *e,
  1210. const struct crypto_ec_point *point, u8 *x, u8 *y)
  1211. {
  1212. BIGNUM *x_bn, *y_bn;
  1213. int ret = -1;
  1214. int len = BN_num_bytes(e->prime);
  1215. if (TEST_FAIL())
  1216. return -1;
  1217. x_bn = BN_new();
  1218. y_bn = BN_new();
  1219. if (x_bn && y_bn &&
  1220. EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
  1221. x_bn, y_bn, e->bnctx)) {
  1222. if (x) {
  1223. crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
  1224. x, len, len);
  1225. }
  1226. if (y) {
  1227. crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
  1228. y, len, len);
  1229. }
  1230. ret = 0;
  1231. }
  1232. BN_clear_free(x_bn);
  1233. BN_clear_free(y_bn);
  1234. return ret;
  1235. }
  1236. struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
  1237. const u8 *val)
  1238. {
  1239. BIGNUM *x, *y;
  1240. EC_POINT *elem;
  1241. int len = BN_num_bytes(e->prime);
  1242. if (TEST_FAIL())
  1243. return NULL;
  1244. x = BN_bin2bn(val, len, NULL);
  1245. y = BN_bin2bn(val + len, len, NULL);
  1246. elem = EC_POINT_new(e->group);
  1247. if (x == NULL || y == NULL || elem == NULL) {
  1248. BN_clear_free(x);
  1249. BN_clear_free(y);
  1250. EC_POINT_clear_free(elem);
  1251. return NULL;
  1252. }
  1253. if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
  1254. e->bnctx)) {
  1255. EC_POINT_clear_free(elem);
  1256. elem = NULL;
  1257. }
  1258. BN_clear_free(x);
  1259. BN_clear_free(y);
  1260. return (struct crypto_ec_point *) elem;
  1261. }
  1262. int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
  1263. const struct crypto_ec_point *b,
  1264. struct crypto_ec_point *c)
  1265. {
  1266. if (TEST_FAIL())
  1267. return -1;
  1268. return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
  1269. (const EC_POINT *) b, e->bnctx) ? 0 : -1;
  1270. }
  1271. int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
  1272. const struct crypto_bignum *b,
  1273. struct crypto_ec_point *res)
  1274. {
  1275. if (TEST_FAIL())
  1276. return -1;
  1277. return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
  1278. (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
  1279. ? 0 : -1;
  1280. }
  1281. int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
  1282. {
  1283. if (TEST_FAIL())
  1284. return -1;
  1285. return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
  1286. }
  1287. int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
  1288. struct crypto_ec_point *p,
  1289. const struct crypto_bignum *x, int y_bit)
  1290. {
  1291. if (TEST_FAIL())
  1292. return -1;
  1293. if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
  1294. (const BIGNUM *) x, y_bit,
  1295. e->bnctx) ||
  1296. !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
  1297. return -1;
  1298. return 0;
  1299. }
  1300. struct crypto_bignum *
  1301. crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
  1302. const struct crypto_bignum *x)
  1303. {
  1304. BIGNUM *tmp, *tmp2, *y_sqr = NULL;
  1305. if (TEST_FAIL())
  1306. return NULL;
  1307. tmp = BN_new();
  1308. tmp2 = BN_new();
  1309. /* y^2 = x^3 + ax + b */
  1310. if (tmp && tmp2 &&
  1311. BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
  1312. BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
  1313. BN_mod_mul(tmp2, e->a, (const BIGNUM *) x, e->prime, e->bnctx) &&
  1314. BN_mod_add_quick(tmp2, tmp2, tmp, e->prime) &&
  1315. BN_mod_add_quick(tmp2, tmp2, e->b, e->prime)) {
  1316. y_sqr = tmp2;
  1317. tmp2 = NULL;
  1318. }
  1319. BN_clear_free(tmp);
  1320. BN_clear_free(tmp2);
  1321. return (struct crypto_bignum *) y_sqr;
  1322. }
  1323. int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
  1324. const struct crypto_ec_point *p)
  1325. {
  1326. return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
  1327. }
  1328. int crypto_ec_point_is_on_curve(struct crypto_ec *e,
  1329. const struct crypto_ec_point *p)
  1330. {
  1331. return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
  1332. e->bnctx) == 1;
  1333. }
  1334. int crypto_ec_point_cmp(const struct crypto_ec *e,
  1335. const struct crypto_ec_point *a,
  1336. const struct crypto_ec_point *b)
  1337. {
  1338. return EC_POINT_cmp(e->group, (const EC_POINT *) a,
  1339. (const EC_POINT *) b, e->bnctx);
  1340. }
  1341. struct crypto_ecdh {
  1342. struct crypto_ec *ec;
  1343. EVP_PKEY *pkey;
  1344. };
  1345. struct crypto_ecdh * crypto_ecdh_init(int group)
  1346. {
  1347. struct crypto_ecdh *ecdh;
  1348. EVP_PKEY *params = NULL;
  1349. EVP_PKEY_CTX *pctx = NULL;
  1350. EVP_PKEY_CTX *kctx = NULL;
  1351. ecdh = os_zalloc(sizeof(*ecdh));
  1352. if (!ecdh)
  1353. goto fail;
  1354. ecdh->ec = crypto_ec_init(group);
  1355. if (!ecdh->ec)
  1356. goto fail;
  1357. pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
  1358. if (!pctx)
  1359. goto fail;
  1360. if (EVP_PKEY_paramgen_init(pctx) != 1) {
  1361. wpa_printf(MSG_ERROR,
  1362. "OpenSSL: EVP_PKEY_paramgen_init failed: %s",
  1363. ERR_error_string(ERR_get_error(), NULL));
  1364. goto fail;
  1365. }
  1366. if (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, ecdh->ec->nid) != 1) {
  1367. wpa_printf(MSG_ERROR,
  1368. "OpenSSL: EVP_PKEY_CTX_set_ec_paramgen_curve_nid failed: %s",
  1369. ERR_error_string(ERR_get_error(), NULL));
  1370. goto fail;
  1371. }
  1372. if (EVP_PKEY_paramgen(pctx, &params) != 1) {
  1373. wpa_printf(MSG_ERROR, "OpenSSL: EVP_PKEY_paramgen failed: %s",
  1374. ERR_error_string(ERR_get_error(), NULL));
  1375. goto fail;
  1376. }
  1377. kctx = EVP_PKEY_CTX_new(params, NULL);
  1378. if (!kctx)
  1379. goto fail;
  1380. if (EVP_PKEY_keygen_init(kctx) != 1) {
  1381. wpa_printf(MSG_ERROR,
  1382. "OpenSSL: EVP_PKEY_keygen_init failed: %s",
  1383. ERR_error_string(ERR_get_error(), NULL));
  1384. goto fail;
  1385. }
  1386. if (EVP_PKEY_keygen(kctx, &ecdh->pkey) != 1) {
  1387. wpa_printf(MSG_ERROR, "OpenSSL: EVP_PKEY_keygen failed: %s",
  1388. ERR_error_string(ERR_get_error(), NULL));
  1389. goto fail;
  1390. }
  1391. done:
  1392. EVP_PKEY_free(params);
  1393. EVP_PKEY_CTX_free(pctx);
  1394. EVP_PKEY_CTX_free(kctx);
  1395. return ecdh;
  1396. fail:
  1397. crypto_ecdh_deinit(ecdh);
  1398. ecdh = NULL;
  1399. goto done;
  1400. }
  1401. struct wpabuf * crypto_ecdh_get_pubkey(struct crypto_ecdh *ecdh, int inc_y)
  1402. {
  1403. struct wpabuf *buf = NULL;
  1404. EC_KEY *eckey;
  1405. const EC_POINT *pubkey;
  1406. BIGNUM *x, *y = NULL;
  1407. int len = BN_num_bytes(ecdh->ec->prime);
  1408. int res;
  1409. eckey = EVP_PKEY_get1_EC_KEY(ecdh->pkey);
  1410. if (!eckey)
  1411. return NULL;
  1412. pubkey = EC_KEY_get0_public_key(eckey);
  1413. if (!pubkey)
  1414. return NULL;
  1415. x = BN_new();
  1416. if (inc_y) {
  1417. y = BN_new();
  1418. if (!y)
  1419. goto fail;
  1420. }
  1421. buf = wpabuf_alloc(inc_y ? 2 * len : len);
  1422. if (!x || !buf)
  1423. goto fail;
  1424. if (EC_POINT_get_affine_coordinates_GFp(ecdh->ec->group, pubkey,
  1425. x, y, ecdh->ec->bnctx) != 1) {
  1426. wpa_printf(MSG_ERROR,
  1427. "OpenSSL: EC_POINT_get_affine_coordinates_GFp failed: %s",
  1428. ERR_error_string(ERR_get_error(), NULL));
  1429. goto fail;
  1430. }
  1431. res = crypto_bignum_to_bin((struct crypto_bignum *) x,
  1432. wpabuf_put(buf, len), len, len);
  1433. if (res < 0)
  1434. goto fail;
  1435. if (inc_y) {
  1436. res = crypto_bignum_to_bin((struct crypto_bignum *) y,
  1437. wpabuf_put(buf, len), len, len);
  1438. if (res < 0)
  1439. goto fail;
  1440. }
  1441. done:
  1442. BN_clear_free(x);
  1443. BN_clear_free(y);
  1444. EC_KEY_free(eckey);
  1445. return buf;
  1446. fail:
  1447. wpabuf_free(buf);
  1448. buf = NULL;
  1449. goto done;
  1450. }
  1451. struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
  1452. const u8 *key, size_t len)
  1453. {
  1454. BIGNUM *x, *y = NULL;
  1455. EVP_PKEY_CTX *ctx = NULL;
  1456. EVP_PKEY *peerkey = NULL;
  1457. struct wpabuf *secret = NULL;
  1458. size_t secret_len;
  1459. EC_POINT *pub;
  1460. EC_KEY *eckey = NULL;
  1461. x = BN_bin2bn(key, inc_y ? len / 2 : len, NULL);
  1462. pub = EC_POINT_new(ecdh->ec->group);
  1463. if (!x || !pub)
  1464. goto fail;
  1465. if (inc_y) {
  1466. y = BN_bin2bn(key + len / 2, len / 2, NULL);
  1467. if (!y)
  1468. goto fail;
  1469. if (!EC_POINT_set_affine_coordinates_GFp(ecdh->ec->group, pub,
  1470. x, y,
  1471. ecdh->ec->bnctx)) {
  1472. wpa_printf(MSG_ERROR,
  1473. "OpenSSL: EC_POINT_set_affine_coordinates_GFp failed: %s",
  1474. ERR_error_string(ERR_get_error(), NULL));
  1475. goto fail;
  1476. }
  1477. } else if (!EC_POINT_set_compressed_coordinates_GFp(ecdh->ec->group,
  1478. pub, x, 0,
  1479. ecdh->ec->bnctx)) {
  1480. wpa_printf(MSG_ERROR,
  1481. "OpenSSL: EC_POINT_set_compressed_coordinates_GFp failed: %s",
  1482. ERR_error_string(ERR_get_error(), NULL));
  1483. goto fail;
  1484. }
  1485. if (!EC_POINT_is_on_curve(ecdh->ec->group, pub, ecdh->ec->bnctx)) {
  1486. wpa_printf(MSG_ERROR,
  1487. "OpenSSL: ECDH peer public key is not on curve");
  1488. goto fail;
  1489. }
  1490. eckey = EC_KEY_new_by_curve_name(ecdh->ec->nid);
  1491. if (!eckey || EC_KEY_set_public_key(eckey, pub) != 1) {
  1492. wpa_printf(MSG_ERROR,
  1493. "OpenSSL: EC_KEY_set_public_key failed: %s",
  1494. ERR_error_string(ERR_get_error(), NULL));
  1495. goto fail;
  1496. }
  1497. peerkey = EVP_PKEY_new();
  1498. if (!peerkey || EVP_PKEY_set1_EC_KEY(peerkey, eckey) != 1)
  1499. goto fail;
  1500. ctx = EVP_PKEY_CTX_new(ecdh->pkey, NULL);
  1501. if (!ctx || EVP_PKEY_derive_init(ctx) != 1 ||
  1502. EVP_PKEY_derive_set_peer(ctx, peerkey) != 1 ||
  1503. EVP_PKEY_derive(ctx, NULL, &secret_len) != 1) {
  1504. wpa_printf(MSG_ERROR,
  1505. "OpenSSL: EVP_PKEY_derive(1) failed: %s",
  1506. ERR_error_string(ERR_get_error(), NULL));
  1507. goto fail;
  1508. }
  1509. secret = wpabuf_alloc(secret_len);
  1510. if (!secret)
  1511. goto fail;
  1512. if (EVP_PKEY_derive(ctx, wpabuf_put(secret, secret_len),
  1513. &secret_len) != 1) {
  1514. wpa_printf(MSG_ERROR,
  1515. "OpenSSL: EVP_PKEY_derive(2) failed: %s",
  1516. ERR_error_string(ERR_get_error(), NULL));
  1517. goto fail;
  1518. }
  1519. done:
  1520. BN_free(x);
  1521. BN_free(y);
  1522. EC_KEY_free(eckey);
  1523. EC_POINT_free(pub);
  1524. EVP_PKEY_CTX_free(ctx);
  1525. EVP_PKEY_free(peerkey);
  1526. return secret;
  1527. fail:
  1528. wpabuf_free(secret);
  1529. secret = NULL;
  1530. goto done;
  1531. }
  1532. void crypto_ecdh_deinit(struct crypto_ecdh *ecdh)
  1533. {
  1534. if (ecdh) {
  1535. crypto_ec_deinit(ecdh->ec);
  1536. EVP_PKEY_free(ecdh->pkey);
  1537. os_free(ecdh);
  1538. }
  1539. }
  1540. #endif /* CONFIG_ECC */