crypto_openssl.c 39 KB

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