crypto_wolfssl.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  1. /*
  2. * Wrapper functions for libwolfssl
  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 "common.h"
  10. #include "crypto.h"
  11. #define WOLFSSL_AES_DIRECT
  12. #define HAVE_AESGCM
  13. #define HAVE_AES_KEYWRAP
  14. #define WOLFSSL_SHA384
  15. #define WOLFSSL_SHA512
  16. #define WOLFSSL_CMAC
  17. #define HAVE_ECC
  18. #define USE_FAST_MATH
  19. #define WOLFSSL_KEY_GEN
  20. #include <wolfssl/options.h>
  21. /* wolfSSL headers */
  22. #include <wolfssl/wolfcrypt/md4.h>
  23. #include <wolfssl/wolfcrypt/md5.h>
  24. #include <wolfssl/wolfcrypt/sha.h>
  25. #include <wolfssl/wolfcrypt/sha256.h>
  26. #include <wolfssl/wolfcrypt/sha512.h>
  27. #include <wolfssl/wolfcrypt/hmac.h>
  28. #include <wolfssl/wolfcrypt/pwdbased.h>
  29. #include <wolfssl/wolfcrypt/arc4.h>
  30. #include <wolfssl/wolfcrypt/des3.h>
  31. #include <wolfssl/wolfcrypt/aes.h>
  32. #include <wolfssl/wolfcrypt/dh.h>
  33. #include <wolfssl/wolfcrypt/cmac.h>
  34. #include <wolfssl/wolfcrypt/ecc.h>
  35. #include <wolfssl/openssl/bn.h>
  36. #ifndef CONFIG_FIPS
  37. int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  38. {
  39. Md4 md4;
  40. size_t i;
  41. if (TEST_FAIL())
  42. return -1;
  43. wc_InitMd4(&md4);
  44. for (i = 0; i < num_elem; i++)
  45. wc_Md4Update(&md4, addr[i], len[i]);
  46. wc_Md4Final(&md4, mac);
  47. return 0;
  48. }
  49. int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  50. {
  51. Md5 md5;
  52. size_t i;
  53. if (TEST_FAIL())
  54. return -1;
  55. wc_InitMd5(&md5);
  56. for (i = 0; i < num_elem; i++)
  57. wc_Md5Update(&md5, addr[i], len[i]);
  58. wc_Md5Final(&md5, mac);
  59. return 0;
  60. }
  61. #endif /* CONFIG_FIPS */
  62. int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  63. {
  64. Sha sha;
  65. size_t i;
  66. if (TEST_FAIL())
  67. return -1;
  68. wc_InitSha(&sha);
  69. for (i = 0; i < num_elem; i++)
  70. wc_ShaUpdate(&sha, addr[i], len[i]);
  71. wc_ShaFinal(&sha, mac);
  72. return 0;
  73. }
  74. #ifndef NO_SHA256_WRAPPER
  75. int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  76. u8 *mac)
  77. {
  78. Sha256 sha256;
  79. size_t i;
  80. if (TEST_FAIL())
  81. return -1;
  82. wc_InitSha256(&sha256);
  83. for (i = 0; i < num_elem; i++)
  84. wc_Sha256Update(&sha256, addr[i], len[i]);
  85. wc_Sha256Final(&sha256, mac);
  86. return 0;
  87. }
  88. #endif /* NO_SHA256_WRAPPER */
  89. #ifdef CONFIG_SHA384
  90. int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  91. u8 *mac)
  92. {
  93. Sha384 sha384;
  94. size_t i;
  95. if (TEST_FAIL())
  96. return -1;
  97. wc_InitSha384(&sha384);
  98. for (i = 0; i < num_elem; i++)
  99. wc_Sha384Update(&sha384, addr[i], len[i]);
  100. wc_Sha384Final(&sha384, mac);
  101. return 0;
  102. }
  103. #endif /* CONFIG_SHA384 */
  104. #ifdef CONFIG_SHA512
  105. int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
  106. u8 *mac)
  107. {
  108. Sha512 sha512;
  109. size_t i;
  110. if (TEST_FAIL())
  111. return -1;
  112. wc_InitSha512(&sha512);
  113. for (i = 0; i < num_elem; i++)
  114. wc_Sha512Update(&sha512, addr[i], len[i]);
  115. wc_Sha512Final(&sha512, mac);
  116. return 0;
  117. }
  118. #endif /* CONFIG_SHA512 */
  119. static int wolfssl_hmac_vector(int type, const u8 *key,
  120. size_t key_len, size_t num_elem,
  121. const u8 *addr[], const size_t *len, u8 *mac,
  122. unsigned int mdlen)
  123. {
  124. Hmac hmac;
  125. size_t i;
  126. (void) mdlen;
  127. if (TEST_FAIL())
  128. return -1;
  129. if (wc_HmacSetKey(&hmac, type, key, (word32) key_len) != 0)
  130. return -1;
  131. for (i = 0; i < num_elem; i++)
  132. if (wc_HmacUpdate(&hmac, addr[i], len[i]) != 0)
  133. return -1;
  134. if (wc_HmacFinal(&hmac, mac) != 0)
  135. return -1;
  136. return 0;
  137. }
  138. #ifndef CONFIG_FIPS
  139. int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
  140. const u8 *addr[], const size_t *len, u8 *mac)
  141. {
  142. return wolfssl_hmac_vector(MD5, key, key_len, num_elem, addr, len, mac,
  143. 16);
  144. }
  145. int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  146. u8 *mac)
  147. {
  148. return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
  149. }
  150. #endif /* CONFIG_FIPS */
  151. int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
  152. const u8 *addr[], const size_t *len, u8 *mac)
  153. {
  154. return wolfssl_hmac_vector(SHA, key, key_len, num_elem, addr, len, mac,
  155. 20);
  156. }
  157. int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
  158. u8 *mac)
  159. {
  160. return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
  161. }
  162. #ifdef CONFIG_SHA256
  163. int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
  164. const u8 *addr[], const size_t *len, u8 *mac)
  165. {
  166. return wolfssl_hmac_vector(SHA256, key, key_len, num_elem, addr, len,
  167. mac, 32);
  168. }
  169. int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
  170. size_t data_len, u8 *mac)
  171. {
  172. return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
  173. }
  174. #endif /* CONFIG_SHA256 */
  175. #ifdef CONFIG_SHA384
  176. int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
  177. const u8 *addr[], const size_t *len, u8 *mac)
  178. {
  179. return wolfssl_hmac_vector(SHA384, key, key_len, num_elem, addr, len,
  180. mac, 48);
  181. }
  182. int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
  183. size_t data_len, u8 *mac)
  184. {
  185. return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
  186. }
  187. #endif /* CONFIG_SHA384 */
  188. #ifdef CONFIG_SHA512
  189. int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem,
  190. const u8 *addr[], const size_t *len, u8 *mac)
  191. {
  192. return wolfssl_hmac_vector(SHA512, key, key_len, num_elem, addr, len,
  193. mac, 64);
  194. }
  195. int hmac_sha512(const u8 *key, size_t key_len, const u8 *data,
  196. size_t data_len, u8 *mac)
  197. {
  198. return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac);
  199. }
  200. #endif /* CONFIG_SHA512 */
  201. int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
  202. int iterations, u8 *buf, size_t buflen)
  203. {
  204. if (wc_PBKDF2(buf, (const byte*)passphrase, os_strlen(passphrase), ssid,
  205. ssid_len, iterations, buflen, SHA) != 0)
  206. return -1;
  207. return 0;
  208. }
  209. #ifdef CONFIG_DES
  210. int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
  211. {
  212. Des des;
  213. u8 pkey[8], next, tmp;
  214. int i;
  215. /* Add parity bits to the key */
  216. next = 0;
  217. for (i = 0; i < 7; i++) {
  218. tmp = key[i];
  219. pkey[i] = (tmp >> i) | next | 1;
  220. next = tmp << (7 - i);
  221. }
  222. pkey[i] = next | 1;
  223. wc_Des_SetKey(&des, pkey, NULL, DES_ENCRYPTION);
  224. wc_Des_EcbEncrypt(&des, cypher, clear, DES_BLOCK_SIZE);
  225. return 0;
  226. }
  227. #endif /* CONFIG_DES */
  228. void * aes_encrypt_init(const u8 *key, size_t len)
  229. {
  230. Aes *aes;
  231. if (TEST_FAIL())
  232. return NULL;
  233. aes = os_malloc(sizeof(Aes));
  234. if (!aes)
  235. return NULL;
  236. if (wc_AesSetKey(aes, key, len, NULL, AES_ENCRYPTION) < 0) {
  237. os_free(aes);
  238. return NULL;
  239. }
  240. return aes;
  241. }
  242. int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
  243. {
  244. wc_AesEncryptDirect(ctx, crypt, plain);
  245. return 0;
  246. }
  247. void aes_encrypt_deinit(void *ctx)
  248. {
  249. os_free(ctx);
  250. }
  251. void * aes_decrypt_init(const u8 *key, size_t len)
  252. {
  253. Aes *aes;
  254. if (TEST_FAIL())
  255. return NULL;
  256. aes = os_malloc(sizeof(Aes));
  257. if (!aes)
  258. return NULL;
  259. if (wc_AesSetKey(aes, key, len, NULL, AES_DECRYPTION) < 0) {
  260. os_free(aes);
  261. return NULL;
  262. }
  263. return aes;
  264. }
  265. int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
  266. {
  267. wc_AesDecryptDirect(ctx, plain, crypt);
  268. return 0;
  269. }
  270. void aes_decrypt_deinit(void *ctx)
  271. {
  272. os_free(ctx);
  273. }
  274. int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
  275. {
  276. Aes aes;
  277. int ret;
  278. if (TEST_FAIL())
  279. return -1;
  280. ret = wc_AesSetKey(&aes, key, 16, iv, AES_ENCRYPTION);
  281. if (ret != 0)
  282. return -1;
  283. ret = wc_AesCbcEncrypt(&aes, data, data, data_len);
  284. if (ret != 0)
  285. return -1;
  286. return 0;
  287. }
  288. int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
  289. {
  290. Aes aes;
  291. int ret;
  292. if (TEST_FAIL())
  293. return -1;
  294. ret = wc_AesSetKey(&aes, key, 16, iv, AES_DECRYPTION);
  295. if (ret != 0)
  296. return -1;
  297. ret = wc_AesCbcDecrypt(&aes, data, data, data_len);
  298. if (ret != 0)
  299. return -1;
  300. return 0;
  301. }
  302. int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
  303. {
  304. int ret;
  305. ret = wc_AesKeyWrap(kek, kek_len, plain, n * 8, cipher, (n + 1) * 8,
  306. NULL);
  307. return ret != (n + 1) * 8 ? -1 : 0;
  308. }
  309. int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
  310. u8 *plain)
  311. {
  312. int ret;
  313. ret = wc_AesKeyUnWrap(kek, kek_len, cipher, (n + 1) * 8, plain, n * 8,
  314. NULL);
  315. return ret != n * 8 ? -1 : 0;
  316. }
  317. #ifndef CONFIG_NO_RC4
  318. int rc4_skip(const u8 *key, size_t keylen, size_t skip, u8 *data,
  319. size_t data_len)
  320. {
  321. #ifndef NO_RC4
  322. Arc4 arc4;
  323. unsigned char skip_buf[16];
  324. wc_Arc4SetKey(&arc4, key, keylen);
  325. while (skip >= sizeof(skip_buf)) {
  326. size_t len = skip;
  327. if (len > sizeof(skip_buf))
  328. len = sizeof(skip_buf);
  329. wc_Arc4Process(&arc4, skip_buf, skip_buf, len);
  330. skip -= len;
  331. }
  332. wc_Arc4Process(&arc4, data, data, data_len);
  333. return 0;
  334. #else /* NO_RC4 */
  335. return -1;
  336. #endif /* NO_RC4 */
  337. }
  338. #endif /* CONFIG_NO_RC4 */
  339. #if defined(EAP_IKEV2) || defined(EAP_IKEV2_DYNAMIC) \
  340. || defined(EAP_SERVER_IKEV2)
  341. union wolfssl_cipher {
  342. Aes aes;
  343. Des3 des3;
  344. Arc4 arc4;
  345. };
  346. struct crypto_cipher {
  347. enum crypto_cipher_alg alg;
  348. union wolfssl_cipher enc;
  349. union wolfssl_cipher dec;
  350. };
  351. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  352. const u8 *iv, const u8 *key,
  353. size_t key_len)
  354. {
  355. struct crypto_cipher *ctx;
  356. ctx = os_zalloc(sizeof(*ctx));
  357. if (!ctx)
  358. return NULL;
  359. switch (alg) {
  360. #ifndef CONFIG_NO_RC4
  361. #ifndef NO_RC4
  362. case CRYPTO_CIPHER_ALG_RC4:
  363. wc_Arc4SetKey(&ctx->enc.arc4, key, key_len);
  364. wc_Arc4SetKey(&ctx->dec.arc4, key, key_len);
  365. break;
  366. #endif /* NO_RC4 */
  367. #endif /* CONFIG_NO_RC4 */
  368. #ifndef NO_AES
  369. case CRYPTO_CIPHER_ALG_AES:
  370. switch (key_len) {
  371. case 16:
  372. case 24:
  373. case 32:
  374. break;
  375. default:
  376. os_free(ctx);
  377. return NULL;
  378. }
  379. if (wc_AesSetKey(&ctx->enc.aes, key, key_len, iv,
  380. AES_ENCRYPTION) ||
  381. wc_AesSetKey(&ctx->dec.aes, key, key_len, iv,
  382. AES_DECRYPTION)) {
  383. os_free(ctx);
  384. return NULL;
  385. }
  386. break;
  387. #endif /* NO_AES */
  388. #ifndef NO_DES3
  389. case CRYPTO_CIPHER_ALG_3DES:
  390. if (key_len != DES3_KEYLEN ||
  391. wc_Des3_SetKey(&ctx->enc.des3, key, iv, DES_ENCRYPTION) ||
  392. wc_Des3_SetKey(&ctx->dec.des3, key, iv, DES_DECRYPTION)) {
  393. os_free(ctx);
  394. return NULL;
  395. }
  396. break;
  397. #endif /* NO_DES3 */
  398. case CRYPTO_CIPHER_ALG_RC2:
  399. case CRYPTO_CIPHER_ALG_DES:
  400. default:
  401. os_free(ctx);
  402. return NULL;
  403. }
  404. ctx->alg = alg;
  405. return ctx;
  406. }
  407. int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
  408. u8 *crypt, size_t len)
  409. {
  410. switch (ctx->alg) {
  411. #ifndef CONFIG_NO_RC4
  412. #ifndef NO_RC4
  413. case CRYPTO_CIPHER_ALG_RC4:
  414. wc_Arc4Process(&ctx->enc.arc4, crypt, plain, len);
  415. return 0;
  416. #endif /* NO_RC4 */
  417. #endif /* CONFIG_NO_RC4 */
  418. #ifndef NO_AES
  419. case CRYPTO_CIPHER_ALG_AES:
  420. if (wc_AesCbcEncrypt(&ctx->enc.aes, crypt, plain, len) != 0)
  421. return -1;
  422. return 0;
  423. #endif /* NO_AES */
  424. #ifndef NO_DES3
  425. case CRYPTO_CIPHER_ALG_3DES:
  426. if (wc_Des3_CbcEncrypt(&ctx->enc.des3, crypt, plain, len) != 0)
  427. return -1;
  428. return 0;
  429. #endif /* NO_DES3 */
  430. default:
  431. return -1;
  432. }
  433. return -1;
  434. }
  435. int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
  436. u8 *plain, size_t len)
  437. {
  438. switch (ctx->alg) {
  439. #ifndef CONFIG_NO_RC4
  440. #ifndef NO_RC4
  441. case CRYPTO_CIPHER_ALG_RC4:
  442. wc_Arc4Process(&ctx->dec.arc4, plain, crypt, len);
  443. return 0;
  444. #endif /* NO_RC4 */
  445. #endif /* CONFIG_NO_RC4 */
  446. #ifndef NO_AES
  447. case CRYPTO_CIPHER_ALG_AES:
  448. if (wc_AesCbcDecrypt(&ctx->dec.aes, plain, crypt, len) != 0)
  449. return -1;
  450. return 0;
  451. #endif /* NO_AES */
  452. #ifndef NO_DES3
  453. case CRYPTO_CIPHER_ALG_3DES:
  454. if (wc_Des3_CbcDecrypt(&ctx->dec.des3, plain, crypt, len) != 0)
  455. return -1;
  456. return 0;
  457. #endif /* NO_DES3 */
  458. default:
  459. return -1;
  460. }
  461. return -1;
  462. }
  463. void crypto_cipher_deinit(struct crypto_cipher *ctx)
  464. {
  465. os_free(ctx);
  466. }
  467. #endif
  468. #ifdef CONFIG_WPS_NFC
  469. static const unsigned char RFC3526_PRIME_1536[] = {
  470. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
  471. 0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1,
  472. 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6,
  473. 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD,
  474. 0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D,
  475. 0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45,
  476. 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9,
  477. 0xA6, 0x37, 0xED, 0x6B, 0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED,
  478. 0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE, 0x9F, 0x24, 0x11,
  479. 0x7C, 0x4B, 0x1F, 0xE6, 0x49, 0x28, 0x66, 0x51, 0xEC, 0xE4, 0x5B, 0x3D,
  480. 0xC2, 0x00, 0x7C, 0xB8, 0xA1, 0x63, 0xBF, 0x05, 0x98, 0xDA, 0x48, 0x36,
  481. 0x1C, 0x55, 0xD3, 0x9A, 0x69, 0x16, 0x3F, 0xA8, 0xFD, 0x24, 0xCF, 0x5F,
  482. 0x83, 0x65, 0x5D, 0x23, 0xDC, 0xA3, 0xAD, 0x96, 0x1C, 0x62, 0xF3, 0x56,
  483. 0x20, 0x85, 0x52, 0xBB, 0x9E, 0xD5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6D,
  484. 0x67, 0x0C, 0x35, 0x4E, 0x4A, 0xBC, 0x98, 0x04, 0xF1, 0x74, 0x6C, 0x08,
  485. 0xCA, 0x23, 0x73, 0x27, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  486. };
  487. static const unsigned char RFC3526_GENERATOR_1536[] = {
  488. 0x02
  489. };
  490. #define RFC3526_LEN sizeof(RFC3526_PRIME_1536)
  491. void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
  492. {
  493. WC_RNG rng;
  494. DhKey *ret = NULL;
  495. DhKey *dh = NULL;
  496. struct wpabuf *privkey = NULL;
  497. struct wpabuf *pubkey = NULL;
  498. word32 priv_sz, pub_sz;
  499. *priv = NULL;
  500. wpabuf_free(*publ);
  501. *publ = NULL;
  502. dh = os_malloc(sizeof(DhKey));
  503. if (!dh)
  504. return NULL;
  505. wc_InitDhKey(dh);
  506. if (wc_InitRng(&rng) != 0) {
  507. os_free(dh);
  508. return NULL;
  509. }
  510. privkey = wpabuf_alloc(RFC3526_LEN);
  511. pubkey = wpabuf_alloc(RFC3526_LEN);
  512. if (!privkey || !pubkey)
  513. goto done;
  514. if (wc_DhSetKey(dh, RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536),
  515. RFC3526_GENERATOR_1536, sizeof(RFC3526_GENERATOR_1536))
  516. != 0)
  517. goto done;
  518. if (wc_DhGenerateKeyPair(dh, &rng, wpabuf_mhead(privkey), &priv_sz,
  519. wpabuf_mhead(pubkey), &pub_sz) != 0)
  520. goto done;
  521. wpabuf_put(privkey, priv_sz);
  522. wpabuf_put(pubkey, pub_sz);
  523. ret = dh;
  524. *priv = privkey;
  525. *publ = pubkey;
  526. dh = NULL;
  527. privkey = NULL;
  528. pubkey = NULL;
  529. done:
  530. wpabuf_clear_free(pubkey);
  531. wpabuf_clear_free(privkey);
  532. if (dh) {
  533. wc_FreeDhKey(dh);
  534. os_free(dh);
  535. }
  536. wc_FreeRng(&rng);
  537. return ret;
  538. }
  539. void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
  540. {
  541. DhKey *ret = NULL;
  542. DhKey *dh;
  543. byte *secret;
  544. word32 secret_sz;
  545. dh = os_malloc(sizeof(DhKey));
  546. if (!dh)
  547. return NULL;
  548. wc_InitDhKey(dh);
  549. secret = os_malloc(RFC3526_LEN);
  550. if (!secret)
  551. goto done;
  552. if (wc_DhSetKey(dh, RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536),
  553. RFC3526_GENERATOR_1536, sizeof(RFC3526_GENERATOR_1536))
  554. != 0)
  555. goto done;
  556. if (wc_DhAgree(dh, secret, &secret_sz, wpabuf_head(priv),
  557. wpabuf_len(priv), RFC3526_GENERATOR_1536,
  558. sizeof(RFC3526_GENERATOR_1536)) != 0)
  559. goto done;
  560. if (secret_sz != wpabuf_len(publ) ||
  561. os_memcmp(secret, wpabuf_head(publ), secret_sz) != 0)
  562. goto done;
  563. ret = dh;
  564. dh = NULL;
  565. done:
  566. if (dh) {
  567. wc_FreeDhKey(dh);
  568. os_free(dh);
  569. }
  570. os_free(secret);
  571. return ret;
  572. }
  573. struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
  574. const struct wpabuf *own_private)
  575. {
  576. struct wpabuf *ret = NULL;
  577. struct wpabuf *secret;
  578. word32 secret_sz;
  579. secret = wpabuf_alloc(RFC3526_LEN);
  580. if (!secret)
  581. goto done;
  582. if (wc_DhAgree(ctx, wpabuf_mhead(secret), &secret_sz,
  583. wpabuf_head(own_private), wpabuf_len(own_private),
  584. wpabuf_head(peer_public), wpabuf_len(peer_public)) != 0)
  585. goto done;
  586. wpabuf_put(secret, secret_sz);
  587. ret = secret;
  588. secret = NULL;
  589. done:
  590. wpabuf_clear_free(secret);
  591. return ret;
  592. }
  593. void dh5_free(void *ctx)
  594. {
  595. if (!ctx)
  596. return;
  597. wc_FreeDhKey(ctx);
  598. os_free(ctx);
  599. }
  600. #endif /* CONFIG_WPS_NFC */
  601. int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey,
  602. u8 *pubkey)
  603. {
  604. int ret = -1;
  605. WC_RNG rng;
  606. DhKey *dh = NULL;
  607. word32 priv_sz, pub_sz;
  608. if (TEST_FAIL())
  609. return -1;
  610. dh = os_malloc(sizeof(DhKey));
  611. if (!dh)
  612. return -1;
  613. wc_InitDhKey(dh);
  614. if (wc_InitRng(&rng) != 0) {
  615. os_free(dh);
  616. return -1;
  617. }
  618. if (wc_DhSetKey(dh, prime, prime_len, &generator, 1) != 0)
  619. goto done;
  620. if (wc_DhGenerateKeyPair(dh, &rng, privkey, &priv_sz, pubkey, &pub_sz)
  621. != 0)
  622. goto done;
  623. if (priv_sz < prime_len) {
  624. size_t pad_sz = prime_len - priv_sz;
  625. os_memmove(privkey + pad_sz, privkey, priv_sz);
  626. os_memset(privkey, 0, pad_sz);
  627. }
  628. if (pub_sz < prime_len) {
  629. size_t pad_sz = prime_len - pub_sz;
  630. os_memmove(pubkey + pad_sz, pubkey, pub_sz);
  631. os_memset(pubkey, 0, pad_sz);
  632. }
  633. ret = 0;
  634. done:
  635. wc_FreeDhKey(dh);
  636. os_free(dh);
  637. wc_FreeRng(&rng);
  638. return ret;
  639. }
  640. int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len,
  641. const u8 *privkey, size_t privkey_len,
  642. const u8 *pubkey, size_t pubkey_len,
  643. u8 *secret, size_t *len)
  644. {
  645. int ret = -1;
  646. DhKey *dh;
  647. word32 secret_sz;
  648. dh = os_malloc(sizeof(DhKey));
  649. if (!dh)
  650. return -1;
  651. wc_InitDhKey(dh);
  652. if (wc_DhSetKey(dh, prime, prime_len, &generator, 1) != 0)
  653. goto done;
  654. if (wc_DhAgree(dh, secret, &secret_sz, privkey, privkey_len, pubkey,
  655. pubkey_len) != 0)
  656. goto done;
  657. *len = secret_sz;
  658. ret = 0;
  659. done:
  660. wc_FreeDhKey(dh);
  661. os_free(dh);
  662. return ret;
  663. }
  664. #ifdef CONFIG_FIPS
  665. int crypto_get_random(void *buf, size_t len)
  666. {
  667. int ret = 0;
  668. WC_RNG rng;
  669. if (wc_InitRng(&rng) != 0)
  670. return -1;
  671. if (wc_RNG_GenerateBlock(&rng, buf, len) != 0)
  672. ret = -1;
  673. wc_FreeRng(&rng);
  674. return ret;
  675. }
  676. #endif /* CONFIG_FIPS */
  677. #if defined(EAP_PWD) || defined(EAP_SERVER_PWD)
  678. struct crypto_hash {
  679. Hmac hmac;
  680. int size;
  681. };
  682. struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
  683. size_t key_len)
  684. {
  685. struct crypto_hash *ret = NULL;
  686. struct crypto_hash *hash;
  687. int type;
  688. hash = os_malloc(sizeof(*hash));
  689. if (!hash)
  690. goto done;
  691. switch (alg) {
  692. #ifndef NO_MD5
  693. case CRYPTO_HASH_ALG_HMAC_MD5:
  694. hash->size = 16;
  695. type = MD5;
  696. break;
  697. #endif /* NO_MD5 */
  698. #ifndef NO_SHA
  699. case CRYPTO_HASH_ALG_HMAC_SHA1:
  700. type = SHA;
  701. hash->size = 20;
  702. break;
  703. #endif /* NO_SHA */
  704. #ifdef CONFIG_SHA256
  705. #ifndef NO_SHA256
  706. case CRYPTO_HASH_ALG_HMAC_SHA256:
  707. type = SHA256;
  708. hash->size = 32;
  709. break;
  710. #endif /* NO_SHA256 */
  711. #endif /* CONFIG_SHA256 */
  712. default:
  713. goto done;
  714. }
  715. if (wc_HmacSetKey(&hash->hmac, type, key, key_len) != 0)
  716. goto done;
  717. ret = hash;
  718. hash = NULL;
  719. done:
  720. os_free(hash);
  721. return ret;
  722. }
  723. void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
  724. {
  725. if (!ctx)
  726. return;
  727. wc_HmacUpdate(&ctx->hmac, data, len);
  728. }
  729. int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
  730. {
  731. int ret = 0;
  732. if (!ctx)
  733. return -2;
  734. if (!mac || !len)
  735. goto done;
  736. if (wc_HmacFinal(&ctx->hmac, mac) != 0) {
  737. ret = -1;
  738. goto done;
  739. }
  740. *len = ctx->size;
  741. ret = 0;
  742. done:
  743. bin_clear_free(ctx, sizeof(*ctx));
  744. return ret;
  745. }
  746. #endif
  747. int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
  748. const u8 *addr[], const size_t *len, u8 *mac)
  749. {
  750. Cmac cmac;
  751. size_t i;
  752. word32 sz;
  753. if (TEST_FAIL())
  754. return -1;
  755. if (wc_InitCmac(&cmac, key, key_len, WC_CMAC_AES, NULL) != 0)
  756. return -1;
  757. for (i = 0; i < num_elem; i++)
  758. if (wc_CmacUpdate(&cmac, addr[i], len[i]) != 0)
  759. return -1;
  760. sz = AES_BLOCK_SIZE;
  761. if (wc_CmacFinal(&cmac, mac, &sz) != 0 || sz != AES_BLOCK_SIZE)
  762. return -1;
  763. return 0;
  764. }
  765. int omac1_aes_128_vector(const u8 *key, size_t num_elem,
  766. const u8 *addr[], const size_t *len, u8 *mac)
  767. {
  768. return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
  769. }
  770. int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
  771. {
  772. return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
  773. }
  774. int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
  775. {
  776. return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
  777. }
  778. struct crypto_bignum * crypto_bignum_init(void)
  779. {
  780. mp_int *a;
  781. if (TEST_FAIL())
  782. return NULL;
  783. a = os_malloc(sizeof(*a));
  784. if (!a || mp_init(a) != MP_OKAY) {
  785. os_free(a);
  786. a = NULL;
  787. }
  788. return (struct crypto_bignum *) a;
  789. }
  790. struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
  791. {
  792. mp_int *a;
  793. if (TEST_FAIL())
  794. return NULL;
  795. a = (mp_int *) crypto_bignum_init();
  796. if (!a)
  797. return NULL;
  798. if (mp_read_unsigned_bin(a, buf, len) != MP_OKAY) {
  799. os_free(a);
  800. a = NULL;
  801. }
  802. return (struct crypto_bignum *) a;
  803. }
  804. void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
  805. {
  806. if (!n)
  807. return;
  808. if (clear)
  809. mp_forcezero((mp_int *) n);
  810. mp_clear((mp_int *) n);
  811. os_free((mp_int *) n);
  812. }
  813. int crypto_bignum_to_bin(const struct crypto_bignum *a,
  814. u8 *buf, size_t buflen, size_t padlen)
  815. {
  816. int num_bytes, offset;
  817. if (TEST_FAIL())
  818. return -1;
  819. if (padlen > buflen)
  820. return -1;
  821. num_bytes = (mp_count_bits((mp_int *) a) + 7) / 8;
  822. if ((size_t) num_bytes > buflen)
  823. return -1;
  824. if (padlen > (size_t) num_bytes)
  825. offset = padlen - num_bytes;
  826. else
  827. offset = 0;
  828. os_memset(buf, 0, offset);
  829. mp_to_unsigned_bin((mp_int *) a, buf + offset);
  830. return num_bytes + offset;
  831. }
  832. int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
  833. {
  834. int ret = 0;
  835. WC_RNG rng;
  836. if (wc_InitRng(&rng) != 0)
  837. return -1;
  838. if (mp_rand_prime((mp_int *) r,
  839. (mp_count_bits((mp_int *) m) + 7) / 8 * 2,
  840. &rng, NULL) != 0)
  841. ret = -1;
  842. if (ret == 0 &&
  843. mp_mod((mp_int *) r, (mp_int *) m, (mp_int *) r) != 0)
  844. ret = -1;
  845. wc_FreeRng(&rng);
  846. return ret;
  847. }
  848. int crypto_bignum_add(const struct crypto_bignum *a,
  849. const struct crypto_bignum *b,
  850. struct crypto_bignum *r)
  851. {
  852. return mp_add((mp_int *) a, (mp_int *) b,
  853. (mp_int *) r) == MP_OKAY ? 0 : -1;
  854. }
  855. int crypto_bignum_mod(const struct crypto_bignum *a,
  856. const struct crypto_bignum *m,
  857. struct crypto_bignum *r)
  858. {
  859. return mp_mod((mp_int *) a, (mp_int *) m,
  860. (mp_int *) r) == MP_OKAY ? 0 : -1;
  861. }
  862. int crypto_bignum_exptmod(const struct crypto_bignum *b,
  863. const struct crypto_bignum *e,
  864. const struct crypto_bignum *m,
  865. struct crypto_bignum *r)
  866. {
  867. if (TEST_FAIL())
  868. return -1;
  869. return mp_exptmod((mp_int *) b, (mp_int *) e, (mp_int *) m,
  870. (mp_int *) r) == MP_OKAY ? 0 : -1;
  871. }
  872. int crypto_bignum_inverse(const struct crypto_bignum *a,
  873. const struct crypto_bignum *m,
  874. struct crypto_bignum *r)
  875. {
  876. if (TEST_FAIL())
  877. return -1;
  878. return mp_invmod((mp_int *) a, (mp_int *) m,
  879. (mp_int *) r) == MP_OKAY ? 0 : -1;
  880. }
  881. int crypto_bignum_sub(const struct crypto_bignum *a,
  882. const struct crypto_bignum *b,
  883. struct crypto_bignum *r)
  884. {
  885. if (TEST_FAIL())
  886. return -1;
  887. return mp_add((mp_int *) a, (mp_int *) b,
  888. (mp_int *) r) == MP_OKAY ? 0 : -1;
  889. }
  890. int crypto_bignum_div(const struct crypto_bignum *a,
  891. const struct crypto_bignum *b,
  892. struct crypto_bignum *d)
  893. {
  894. if (TEST_FAIL())
  895. return -1;
  896. return mp_div((mp_int *) a, (mp_int *) b, (mp_int *) d,
  897. NULL) == MP_OKAY ? 0 : -1;
  898. }
  899. int crypto_bignum_mulmod(const struct crypto_bignum *a,
  900. const struct crypto_bignum *b,
  901. const struct crypto_bignum *m,
  902. struct crypto_bignum *d)
  903. {
  904. if (TEST_FAIL())
  905. return -1;
  906. return mp_mulmod((mp_int *) a, (mp_int *) b, (mp_int *) m,
  907. (mp_int *) d) == MP_OKAY ? 0 : -1;
  908. }
  909. int crypto_bignum_rshift(const struct crypto_bignum *a, int n,
  910. struct crypto_bignum *r)
  911. {
  912. if (mp_copy((mp_int *) a, (mp_int *) r) != MP_OKAY)
  913. return -1;
  914. mp_rshd((mp_int *) r, n);
  915. return 0;
  916. }
  917. int crypto_bignum_cmp(const struct crypto_bignum *a,
  918. const struct crypto_bignum *b)
  919. {
  920. return mp_cmp((mp_int *) a, (mp_int *) b);
  921. }
  922. int crypto_bignum_bits(const struct crypto_bignum *a)
  923. {
  924. return mp_count_bits((mp_int *) a);
  925. }
  926. int crypto_bignum_is_zero(const struct crypto_bignum *a)
  927. {
  928. return mp_iszero((mp_int *) a);
  929. }
  930. int crypto_bignum_is_one(const struct crypto_bignum *a)
  931. {
  932. return mp_isone((const mp_int *) a);
  933. }
  934. int crypto_bignum_is_odd(const struct crypto_bignum *a)
  935. {
  936. return mp_isodd((mp_int *) a);
  937. }
  938. int crypto_bignum_legendre(const struct crypto_bignum *a,
  939. const struct crypto_bignum *p)
  940. {
  941. mp_int t;
  942. int ret;
  943. int res = -2;
  944. if (TEST_FAIL())
  945. return -2;
  946. if (mp_init(&t) != MP_OKAY)
  947. return -2;
  948. /* t = (p-1) / 2 */
  949. ret = mp_sub_d((mp_int *) p, 1, &t);
  950. if (ret == MP_OKAY)
  951. mp_rshb(&t, 1);
  952. if (ret == MP_OKAY)
  953. ret = mp_exptmod((mp_int *) a, &t, (mp_int *) p, &t);
  954. if (ret == MP_OKAY) {
  955. if (mp_isone(&t))
  956. res = 1;
  957. else if (mp_iszero(&t))
  958. res = 0;
  959. else
  960. res = -1;
  961. }
  962. mp_clear(&t);
  963. return res;
  964. }
  965. #ifdef CONFIG_ECC
  966. int ecc_map(ecc_point *, mp_int *, mp_digit);
  967. int ecc_projective_add_point(ecc_point *P, ecc_point *Q, ecc_point *R,
  968. mp_int *a, mp_int *modulus, mp_digit mp);
  969. struct crypto_ec {
  970. ecc_key key;
  971. mp_int a;
  972. mp_int prime;
  973. mp_int order;
  974. mp_digit mont_b;
  975. mp_int b;
  976. };
  977. struct crypto_ec * crypto_ec_init(int group)
  978. {
  979. int built = 0;
  980. struct crypto_ec *e;
  981. int curve_id;
  982. /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
  983. switch (group) {
  984. case 19:
  985. curve_id = ECC_SECP256R1;
  986. break;
  987. case 20:
  988. curve_id = ECC_SECP384R1;
  989. break;
  990. case 21:
  991. curve_id = ECC_SECP521R1;
  992. break;
  993. case 25:
  994. curve_id = ECC_SECP192R1;
  995. break;
  996. case 26:
  997. curve_id = ECC_SECP224R1;
  998. break;
  999. #ifdef HAVE_ECC_BRAINPOOL
  1000. case 27:
  1001. curve_id = ECC_BRAINPOOLP224R1;
  1002. break;
  1003. case 28:
  1004. curve_id = ECC_BRAINPOOLP256R1;
  1005. break;
  1006. case 29:
  1007. curve_id = ECC_BRAINPOOLP384R1;
  1008. break;
  1009. case 30:
  1010. curve_id = ECC_BRAINPOOLP512R1;
  1011. break;
  1012. #endif /* HAVE_ECC_BRAINPOOL */
  1013. default:
  1014. return NULL;
  1015. }
  1016. e = os_zalloc(sizeof(*e));
  1017. if (!e)
  1018. return NULL;
  1019. if (wc_ecc_init(&e->key) != 0 ||
  1020. wc_ecc_set_curve(&e->key, 0, curve_id) != 0 ||
  1021. mp_init(&e->a) != MP_OKAY ||
  1022. mp_init(&e->prime) != MP_OKAY ||
  1023. mp_init(&e->order) != MP_OKAY ||
  1024. mp_init(&e->b) != MP_OKAY ||
  1025. mp_read_radix(&e->a, e->key.dp->Af, 16) != MP_OKAY ||
  1026. mp_read_radix(&e->b, e->key.dp->Bf, 16) != MP_OKAY ||
  1027. mp_read_radix(&e->prime, e->key.dp->prime, 16) != MP_OKAY ||
  1028. mp_read_radix(&e->order, e->key.dp->order, 16) != MP_OKAY ||
  1029. mp_montgomery_setup(&e->prime, &e->mont_b) != MP_OKAY)
  1030. goto done;
  1031. built = 1;
  1032. done:
  1033. if (!built) {
  1034. crypto_ec_deinit(e);
  1035. e = NULL;
  1036. }
  1037. return e;
  1038. }
  1039. void crypto_ec_deinit(struct crypto_ec* e)
  1040. {
  1041. if (!e)
  1042. return;
  1043. mp_clear(&e->b);
  1044. mp_clear(&e->order);
  1045. mp_clear(&e->prime);
  1046. mp_clear(&e->a);
  1047. wc_ecc_free(&e->key);
  1048. os_free(e);
  1049. }
  1050. int crypto_ec_cofactor(struct crypto_ec *e, struct crypto_bignum *cofactor)
  1051. {
  1052. if (!e || !cofactor)
  1053. return -1;
  1054. mp_set((mp_int *) cofactor, e->key.dp->cofactor);
  1055. return 0;
  1056. }
  1057. struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
  1058. {
  1059. if (TEST_FAIL())
  1060. return NULL;
  1061. if (!e)
  1062. return NULL;
  1063. return (struct crypto_ec_point *) wc_ecc_new_point();
  1064. }
  1065. size_t crypto_ec_prime_len(struct crypto_ec *e)
  1066. {
  1067. return (mp_count_bits(&e->prime) + 7) / 8;
  1068. }
  1069. size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
  1070. {
  1071. return mp_count_bits(&e->prime);
  1072. }
  1073. size_t crypto_ec_order_len(struct crypto_ec *e)
  1074. {
  1075. return (mp_count_bits(&e->order) + 7) / 8;
  1076. }
  1077. const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
  1078. {
  1079. return (const struct crypto_bignum *) &e->prime;
  1080. }
  1081. const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
  1082. {
  1083. return (const struct crypto_bignum *) &e->order;
  1084. }
  1085. void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
  1086. {
  1087. ecc_point *point = (ecc_point *) p;
  1088. if (!p)
  1089. return;
  1090. if (clear) {
  1091. mp_forcezero(point->x);
  1092. mp_forcezero(point->y);
  1093. mp_forcezero(point->z);
  1094. }
  1095. wc_ecc_del_point(point);
  1096. }
  1097. int crypto_ec_point_x(struct crypto_ec *e, const struct crypto_ec_point *p,
  1098. struct crypto_bignum *x)
  1099. {
  1100. return mp_copy(((ecc_point *) p)->x, (mp_int *) x) == MP_OKAY ? 0 : -1;
  1101. }
  1102. int crypto_ec_point_to_bin(struct crypto_ec *e,
  1103. const struct crypto_ec_point *point, u8 *x, u8 *y)
  1104. {
  1105. ecc_point *p = (ecc_point *) point;
  1106. if (TEST_FAIL())
  1107. return -1;
  1108. if (!mp_isone(p->z)) {
  1109. if (ecc_map(p, &e->prime, e->mont_b) != MP_OKAY)
  1110. return -1;
  1111. }
  1112. if (x) {
  1113. if (crypto_bignum_to_bin((struct crypto_bignum *)p->x, x,
  1114. e->key.dp->size,
  1115. e->key.dp->size) <= 0)
  1116. return -1;
  1117. }
  1118. if (y) {
  1119. if (crypto_bignum_to_bin((struct crypto_bignum *) p->y, y,
  1120. e->key.dp->size,
  1121. e->key.dp->size) <= 0)
  1122. return -1;
  1123. }
  1124. return 0;
  1125. }
  1126. struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
  1127. const u8 *val)
  1128. {
  1129. ecc_point *point = NULL;
  1130. int loaded = 0;
  1131. if (TEST_FAIL())
  1132. return NULL;
  1133. point = wc_ecc_new_point();
  1134. if (!point)
  1135. goto done;
  1136. if (mp_read_unsigned_bin(point->x, val, e->key.dp->size) != MP_OKAY)
  1137. goto done;
  1138. val += e->key.dp->size;
  1139. if (mp_read_unsigned_bin(point->y, val, e->key.dp->size) != MP_OKAY)
  1140. goto done;
  1141. mp_set(point->z, 1);
  1142. loaded = 1;
  1143. done:
  1144. if (!loaded) {
  1145. wc_ecc_del_point(point);
  1146. point = NULL;
  1147. }
  1148. return (struct crypto_ec_point *) point;
  1149. }
  1150. int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
  1151. const struct crypto_ec_point *b,
  1152. struct crypto_ec_point *c)
  1153. {
  1154. mp_int mu;
  1155. ecc_point *ta = NULL, *tb = NULL;
  1156. ecc_point *pa = (ecc_point *) a, *pb = (ecc_point *) b;
  1157. mp_int *modulus = &e->prime;
  1158. int ret;
  1159. if (TEST_FAIL())
  1160. return -1;
  1161. ret = mp_init(&mu);
  1162. if (ret != MP_OKAY)
  1163. return -1;
  1164. ret = mp_montgomery_calc_normalization(&mu, modulus);
  1165. if (ret != MP_OKAY) {
  1166. mp_clear(&mu);
  1167. return -1;
  1168. }
  1169. if (!mp_isone(&mu)) {
  1170. ta = wc_ecc_new_point();
  1171. if (!ta) {
  1172. mp_clear(&mu);
  1173. return -1;
  1174. }
  1175. tb = wc_ecc_new_point();
  1176. if (!tb) {
  1177. wc_ecc_del_point(ta);
  1178. mp_clear(&mu);
  1179. return -1;
  1180. }
  1181. if (mp_mulmod(pa->x, &mu, modulus, ta->x) != MP_OKAY ||
  1182. mp_mulmod(pa->y, &mu, modulus, ta->y) != MP_OKAY ||
  1183. mp_mulmod(pa->z, &mu, modulus, ta->z) != MP_OKAY ||
  1184. mp_mulmod(pb->x, &mu, modulus, tb->x) != MP_OKAY ||
  1185. mp_mulmod(pb->y, &mu, modulus, tb->y) != MP_OKAY ||
  1186. mp_mulmod(pb->z, &mu, modulus, tb->z) != MP_OKAY) {
  1187. ret = -1;
  1188. goto end;
  1189. }
  1190. pa = ta;
  1191. pb = tb;
  1192. }
  1193. ret = ecc_projective_add_point(pa, pb, (ecc_point *) c, &e->a,
  1194. &e->prime, e->mont_b);
  1195. if (ret != 0) {
  1196. ret = -1;
  1197. goto end;
  1198. }
  1199. if (ecc_map((ecc_point *) c, &e->prime, e->mont_b) != MP_OKAY)
  1200. ret = -1;
  1201. else
  1202. ret = 0;
  1203. end:
  1204. wc_ecc_del_point(tb);
  1205. wc_ecc_del_point(ta);
  1206. mp_clear(&mu);
  1207. return ret;
  1208. }
  1209. int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
  1210. const struct crypto_bignum *b,
  1211. struct crypto_ec_point *res)
  1212. {
  1213. int ret;
  1214. if (TEST_FAIL())
  1215. return -1;
  1216. ret = wc_ecc_mulmod((mp_int *) b, (ecc_point *) p, (ecc_point *) res,
  1217. &e->a, &e->prime, 1);
  1218. return ret == 0 ? 0 : -1;
  1219. }
  1220. int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
  1221. {
  1222. ecc_point *point = (ecc_point *) p;
  1223. if (TEST_FAIL())
  1224. return -1;
  1225. if (mp_sub(&e->prime, point->y, point->y) != MP_OKAY)
  1226. return -1;
  1227. return 0;
  1228. }
  1229. int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
  1230. struct crypto_ec_point *p,
  1231. const struct crypto_bignum *x, int y_bit)
  1232. {
  1233. byte buf[MAX_ECC_BYTES + 1];
  1234. int ret;
  1235. int prime_len = crypto_ec_prime_len(e);
  1236. if (TEST_FAIL())
  1237. return -1;
  1238. buf[0] = 0x2 + (byte) y_bit;
  1239. ret = crypto_bignum_to_bin(x, buf + 1, prime_len, prime_len);
  1240. if (ret <= 0)
  1241. return -1;
  1242. ret = wc_ecc_import_point_der(buf, ret + 1, e->key.idx,
  1243. (ecc_point *) p);
  1244. if (ret != 0)
  1245. return -1;
  1246. return 0;
  1247. }
  1248. struct crypto_bignum *
  1249. crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
  1250. const struct crypto_bignum *x)
  1251. {
  1252. mp_int *y2 = NULL;
  1253. mp_int t;
  1254. int calced = 0;
  1255. if (TEST_FAIL())
  1256. return NULL;
  1257. if (mp_init(&t) != MP_OKAY)
  1258. return NULL;
  1259. y2 = (mp_int *) crypto_bignum_init();
  1260. if (!y2)
  1261. goto done;
  1262. if (mp_sqrmod((mp_int *) x, &e->prime, y2) != 0 ||
  1263. mp_mulmod((mp_int *) x, &t, &e->prime, y2) != 0 ||
  1264. mp_mulmod((mp_int *) x, &e->a, &e->prime, &t) != 0 ||
  1265. mp_addmod(y2, &t, &e->prime, y2) != 0 ||
  1266. mp_addmod(y2, &e->b, &e->prime, y2) != 0)
  1267. goto done;
  1268. calced = 1;
  1269. done:
  1270. if (!calced) {
  1271. if (y2) {
  1272. mp_clear(y2);
  1273. os_free(y2);
  1274. }
  1275. mp_clear(&t);
  1276. }
  1277. return (struct crypto_bignum *) y2;
  1278. }
  1279. int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
  1280. const struct crypto_ec_point *p)
  1281. {
  1282. return wc_ecc_point_is_at_infinity((ecc_point *) p);
  1283. }
  1284. int crypto_ec_point_is_on_curve(struct crypto_ec *e,
  1285. const struct crypto_ec_point *p)
  1286. {
  1287. return wc_ecc_is_point((ecc_point *) p, &e->a, &e->b, &e->prime) ==
  1288. MP_OKAY;
  1289. }
  1290. int crypto_ec_point_cmp(const struct crypto_ec *e,
  1291. const struct crypto_ec_point *a,
  1292. const struct crypto_ec_point *b)
  1293. {
  1294. return wc_ecc_cmp_point((ecc_point *) a, (ecc_point *) b);
  1295. }
  1296. #endif /* CONFIG_ECC */