ed25519.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. /* ed25519.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* Based On Daniel J Bernstein's ed25519 Public Domain ref10 work. */
  22. /* Possible Ed25519 enable options:
  23. * WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN Default: OFF
  24. * Check that the private key didn't change during the signing operations.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include <config.h>
  28. #endif
  29. /* in case user set HAVE_ED25519 there */
  30. #include <wolfssl/wolfcrypt/settings.h>
  31. #ifdef HAVE_ED25519
  32. #if FIPS_VERSION3_GE(6,0,0)
  33. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  34. #define FIPS_NO_WRAPPERS
  35. #ifdef USE_WINDOWS_API
  36. #pragma code_seg(".fipsA$f")
  37. #pragma const_seg(".fipsB$f")
  38. #endif
  39. #endif
  40. #include <wolfssl/wolfcrypt/ed25519.h>
  41. #include <wolfssl/wolfcrypt/ge_operations.h>
  42. #include <wolfssl/wolfcrypt/error-crypt.h>
  43. #include <wolfssl/wolfcrypt/hash.h>
  44. #ifdef NO_INLINE
  45. #include <wolfssl/wolfcrypt/misc.h>
  46. #else
  47. #define WOLFSSL_MISC_INCLUDED
  48. #include <wolfcrypt/src/misc.c>
  49. #endif
  50. #if FIPS_VERSION3_GE(6,0,0)
  51. const unsigned int wolfCrypt_FIPS_ed25519_ro_sanity[2] =
  52. { 0x1a2b3c4d, 0x00000006 };
  53. int wolfCrypt_FIPS_ED25519_sanity(void)
  54. {
  55. return 0;
  56. }
  57. #endif
  58. #ifdef FREESCALE_LTC_ECC
  59. #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
  60. #endif
  61. #ifdef WOLFSSL_SE050
  62. #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
  63. #endif
  64. #ifdef WOLF_CRYPTO_CB
  65. #include <wolfssl/wolfcrypt/cryptocb.h>
  66. #endif
  67. #if defined(HAVE_ED25519_SIGN) || defined(HAVE_ED25519_VERIFY)
  68. /* Set a static message string for "Sig No Collisions Message SNC".
  69. ** Note this is a static string per spec, see:
  70. ** https://datatracker.ietf.org/doc/rfc8032/
  71. */
  72. #define ED25519CTX_SNC_MESSAGE "SigEd25519 no Ed25519 collisions"
  73. #define ED25519CTX_SIZE 32 /* 32 chars: fixed length of SNC Message. */
  74. /* The 32 bytes of ED25519CTX_SIZE is used elsewhere, but we need one
  75. ** more char for saving the line ending in our ed25519Ctx[] here: */
  76. static const byte ed25519Ctx[ED25519CTX_SIZE + 1] = ED25519CTX_SNC_MESSAGE;
  77. #endif
  78. static int ed25519_hash_init(ed25519_key* key, wc_Sha512 *sha)
  79. {
  80. int ret;
  81. #ifndef WOLFSSL_ED25519_PERSISTENT_SHA
  82. /* when not using persistent SHA, we'll zero the sha param */
  83. XMEMSET(sha, 0, sizeof(wc_Sha512));
  84. #endif
  85. ret = wc_InitSha512_ex(sha, key->heap,
  86. #if defined(WOLF_CRYPTO_CB)
  87. key->devId
  88. #else
  89. INVALID_DEVID
  90. #endif
  91. );
  92. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  93. if (ret == 0)
  94. key->sha_clean_flag = 1;
  95. #endif
  96. return ret;
  97. }
  98. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  99. static int ed25519_hash_reset(ed25519_key* key)
  100. {
  101. int ret;
  102. if (key->sha_clean_flag)
  103. ret = 0;
  104. else {
  105. wc_Sha512Free(&key->sha);
  106. ret = wc_InitSha512_ex(&key->sha, key->heap,
  107. #if defined(WOLF_CRYPTO_CB)
  108. key->devId
  109. #else
  110. INVALID_DEVID
  111. #endif
  112. );
  113. if (ret == 0)
  114. key->sha_clean_flag = 1;
  115. }
  116. return ret;
  117. }
  118. #endif /* WOLFSSL_ED25519_PERSISTENT_SHA */
  119. static int ed25519_hash_update(ed25519_key* key, wc_Sha512 *sha,
  120. const byte* data, word32 len)
  121. {
  122. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  123. if (key->sha_clean_flag)
  124. key->sha_clean_flag = 0;
  125. #else
  126. (void)key;
  127. #endif
  128. return wc_Sha512Update(sha, data, len);
  129. }
  130. static int ed25519_hash_final(ed25519_key* key, wc_Sha512 *sha, byte* hash)
  131. {
  132. int ret = wc_Sha512Final(sha, hash);
  133. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  134. if (ret == 0)
  135. key->sha_clean_flag = 1;
  136. #else
  137. (void)key;
  138. #endif
  139. return ret;
  140. }
  141. static void ed25519_hash_free(ed25519_key* key, wc_Sha512 *sha)
  142. {
  143. wc_Sha512Free(sha);
  144. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  145. key->sha_clean_flag = 0;
  146. #else
  147. (void)key;
  148. #endif
  149. }
  150. static int ed25519_hash(ed25519_key* key, const byte* in, word32 inLen,
  151. byte* hash)
  152. {
  153. int ret;
  154. #ifndef WOLFSSL_ED25519_PERSISTENT_SHA
  155. wc_Sha512 sha[1];
  156. #else
  157. wc_Sha512 *sha;
  158. #endif
  159. if (key == NULL || (in == NULL && inLen > 0) || hash == NULL) {
  160. return BAD_FUNC_ARG;
  161. }
  162. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  163. sha = &key->sha;
  164. ret = ed25519_hash_reset(key);
  165. #else
  166. ret = ed25519_hash_init(key, sha);
  167. #endif
  168. if (ret < 0)
  169. return ret;
  170. ret = ed25519_hash_update(key, sha, in, inLen);
  171. if (ret == 0)
  172. ret = ed25519_hash_final(key, sha, hash);
  173. #ifndef WOLFSSL_ED25519_PERSISTENT_SHA
  174. ed25519_hash_free(key, sha);
  175. #endif
  176. return ret;
  177. }
  178. #ifdef HAVE_ED25519_MAKE_KEY
  179. int wc_ed25519_make_public(ed25519_key* key, unsigned char* pubKey,
  180. word32 pubKeySz)
  181. {
  182. int ret = 0;
  183. ALIGN16 byte az[ED25519_PRV_KEY_SIZE];
  184. #if !defined(FREESCALE_LTC_ECC)
  185. ge_p3 A;
  186. #endif
  187. if (key == NULL || pubKey == NULL || pubKeySz != ED25519_PUB_KEY_SIZE)
  188. ret = BAD_FUNC_ARG;
  189. if ((ret == 0) && (!key->privKeySet)) {
  190. ret = ECC_PRIV_KEY_E;
  191. }
  192. if (ret == 0)
  193. ret = ed25519_hash(key, key->k, ED25519_KEY_SIZE, az);
  194. if (ret == 0) {
  195. /* apply clamp */
  196. az[0] &= 248;
  197. az[31] &= 63; /* same than az[31] &= 127 because of az[31] |= 64 */
  198. az[31] |= 64;
  199. #ifdef FREESCALE_LTC_ECC
  200. ltc_pkha_ecc_point_t publicKey = {0};
  201. publicKey.X = key->pointX;
  202. publicKey.Y = key->pointY;
  203. LTC_PKHA_Ed25519_PointMul(LTC_PKHA_Ed25519_BasePoint(), az,
  204. ED25519_KEY_SIZE, &publicKey, kLTC_Ed25519 /* result on Ed25519 */);
  205. LTC_PKHA_Ed25519_Compress(&publicKey, pubKey);
  206. #else
  207. ge_scalarmult_base(&A, az);
  208. ge_p3_tobytes(pubKey, &A);
  209. #endif
  210. key->pubKeySet = 1;
  211. }
  212. return ret;
  213. }
  214. /* generate an ed25519 key pair.
  215. * returns 0 on success
  216. */
  217. int wc_ed25519_make_key(WC_RNG* rng, int keySz, ed25519_key* key)
  218. {
  219. int ret;
  220. if (rng == NULL || key == NULL)
  221. return BAD_FUNC_ARG;
  222. /* ed25519 has 32 byte key sizes */
  223. if (keySz != ED25519_KEY_SIZE)
  224. return BAD_FUNC_ARG;
  225. key->privKeySet = 0;
  226. key->pubKeySet = 0;
  227. #ifdef WOLF_CRYPTO_CB
  228. if (key->devId != INVALID_DEVID) {
  229. ret = wc_CryptoCb_Ed25519Gen(rng, keySz, key);
  230. if (ret != CRYPTOCB_UNAVAILABLE)
  231. return ret;
  232. /* fall-through when unavailable */
  233. }
  234. #endif
  235. ret = wc_RNG_GenerateBlock(rng, key->k, ED25519_KEY_SIZE);
  236. if (ret != 0)
  237. return ret;
  238. key->privKeySet = 1;
  239. ret = wc_ed25519_make_public(key, key->p, ED25519_PUB_KEY_SIZE);
  240. if (ret != 0) {
  241. key->privKeySet = 0;
  242. ForceZero(key->k, ED25519_KEY_SIZE);
  243. return ret;
  244. }
  245. /* put public key after private key, on the same buffer */
  246. XMEMMOVE(key->k + ED25519_KEY_SIZE, key->p, ED25519_PUB_KEY_SIZE);
  247. return ret;
  248. }
  249. #endif /* HAVE_ED25519_MAKE_KEY */
  250. #ifdef HAVE_ED25519_SIGN
  251. /*
  252. in contains the message to sign
  253. inLen is the length of the message to sign
  254. out is the buffer to write the signature
  255. outLen [in/out] input size of out buf
  256. output gets set as the final length of out
  257. key is the ed25519 key to use when signing
  258. type one of Ed25519, Ed25519ctx or Ed25519ph
  259. context extra signing data
  260. contextLen length of extra signing data
  261. return 0 on success
  262. */
  263. int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out,
  264. word32 *outLen, ed25519_key* key, byte type,
  265. const byte* context, byte contextLen)
  266. {
  267. int ret;
  268. #ifdef WOLFSSL_SE050
  269. (void)context;
  270. (void)contextLen;
  271. (void)type;
  272. ret = se050_ed25519_sign_msg(in, inLen, out, outLen, key);
  273. #else
  274. #ifdef FREESCALE_LTC_ECC
  275. ALIGN16 byte tempBuf[ED25519_PRV_KEY_SIZE];
  276. ltc_pkha_ecc_point_t ltcPoint = {0};
  277. #else
  278. ge_p3 R;
  279. #endif
  280. ALIGN16 byte nonce[WC_SHA512_DIGEST_SIZE];
  281. ALIGN16 byte hram[WC_SHA512_DIGEST_SIZE];
  282. ALIGN16 byte az[ED25519_PRV_KEY_SIZE];
  283. #ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN
  284. byte orig_k[ED25519_KEY_SIZE];
  285. #endif
  286. /* sanity check on arguments */
  287. if (in == NULL || out == NULL || outLen == NULL || key == NULL ||
  288. (context == NULL && contextLen != 0)) {
  289. return BAD_FUNC_ARG;
  290. }
  291. #ifdef WOLF_CRYPTO_CB
  292. if (key->devId != INVALID_DEVID) {
  293. ret = wc_CryptoCb_Ed25519Sign(in, inLen, out, outLen, key, type,
  294. context, contextLen);
  295. if (ret != CRYPTOCB_UNAVAILABLE)
  296. return ret;
  297. /* fall-through when unavailable */
  298. }
  299. #endif
  300. if (!key->pubKeySet)
  301. return BAD_FUNC_ARG;
  302. /* check and set up out length */
  303. if (*outLen < ED25519_SIG_SIZE) {
  304. *outLen = ED25519_SIG_SIZE;
  305. return BUFFER_E;
  306. }
  307. *outLen = ED25519_SIG_SIZE;
  308. #ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN
  309. XMEMCPY(orig_k, key->k, ED25519_KEY_SIZE);
  310. #endif
  311. /* step 1: create nonce to use where nonce is r in
  312. r = H(h_b, ... ,h_2b-1,M) */
  313. ret = ed25519_hash(key, key->k, ED25519_KEY_SIZE, az);
  314. if (ret != 0)
  315. return ret;
  316. /* apply clamp */
  317. az[0] &= 248;
  318. az[31] &= 63; /* same than az[31] &= 127 because of az[31] |= 64 */
  319. az[31] |= 64;
  320. {
  321. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  322. wc_Sha512 *sha = &key->sha;
  323. #else
  324. wc_Sha512 sha[1];
  325. ret = ed25519_hash_init(key, sha);
  326. if (ret < 0) {
  327. return ret;
  328. }
  329. #endif
  330. if (type == Ed25519ctx || type == Ed25519ph) {
  331. ret = ed25519_hash_update(key, sha, ed25519Ctx, ED25519CTX_SIZE);
  332. if (ret == 0)
  333. ret = ed25519_hash_update(key, sha, &type, sizeof(type));
  334. if (ret == 0)
  335. ret = ed25519_hash_update(key, sha, &contextLen,
  336. sizeof(contextLen));
  337. if (ret == 0 && context != NULL)
  338. ret = ed25519_hash_update(key, sha, context, contextLen);
  339. }
  340. if (ret == 0)
  341. ret = ed25519_hash_update(key, sha, az + ED25519_KEY_SIZE,
  342. ED25519_KEY_SIZE);
  343. if (ret == 0)
  344. ret = ed25519_hash_update(key, sha, in, inLen);
  345. if (ret == 0)
  346. ret = ed25519_hash_final(key, sha, nonce);
  347. #ifndef WOLFSSL_ED25519_PERSISTENT_SHA
  348. ed25519_hash_free(key, sha);
  349. #endif
  350. }
  351. if (ret != 0)
  352. return ret;
  353. #ifdef FREESCALE_LTC_ECC
  354. ltcPoint.X = &tempBuf[0];
  355. ltcPoint.Y = &tempBuf[32];
  356. LTC_PKHA_sc_reduce(nonce);
  357. LTC_PKHA_Ed25519_PointMul(LTC_PKHA_Ed25519_BasePoint(), nonce,
  358. ED25519_KEY_SIZE, &ltcPoint, kLTC_Ed25519 /* result on Ed25519 */);
  359. LTC_PKHA_Ed25519_Compress(&ltcPoint, out);
  360. #else
  361. sc_reduce(nonce);
  362. /* step 2: computing R = rB where rB is the scalar multiplication of
  363. r and B */
  364. ge_scalarmult_base(&R,nonce);
  365. ge_p3_tobytes(out,&R);
  366. #endif
  367. /* step 3: hash R + public key + message getting H(R,A,M) then
  368. creating S = (r + H(R,A,M)a) mod l */
  369. {
  370. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  371. wc_Sha512 *sha = &key->sha;
  372. #else
  373. wc_Sha512 sha[1];
  374. ret = ed25519_hash_init(key, sha);
  375. if (ret < 0)
  376. return ret;
  377. #endif
  378. if (type == Ed25519ctx || type == Ed25519ph) {
  379. ret = ed25519_hash_update(key, sha, ed25519Ctx, ED25519CTX_SIZE);
  380. if (ret == 0)
  381. ret = ed25519_hash_update(key, sha, &type, sizeof(type));
  382. if (ret == 0)
  383. ret = ed25519_hash_update(key, sha, &contextLen,
  384. sizeof(contextLen));
  385. if (ret == 0 && context != NULL)
  386. ret = ed25519_hash_update(key, sha, context, contextLen);
  387. }
  388. if (ret == 0)
  389. ret = ed25519_hash_update(key, sha, out, ED25519_SIG_SIZE/2);
  390. if (ret == 0)
  391. ret = ed25519_hash_update(key, sha, key->p, ED25519_PUB_KEY_SIZE);
  392. if (ret == 0)
  393. ret = ed25519_hash_update(key, sha, in, inLen);
  394. if (ret == 0)
  395. ret = ed25519_hash_final(key, sha, hram);
  396. #ifndef WOLFSSL_ED25519_PERSISTENT_SHA
  397. ed25519_hash_free(key, sha);
  398. #endif
  399. }
  400. if (ret != 0)
  401. return ret;
  402. #ifdef FREESCALE_LTC_ECC
  403. LTC_PKHA_sc_reduce(hram);
  404. LTC_PKHA_sc_muladd(out + (ED25519_SIG_SIZE/2), hram, az, nonce);
  405. #else
  406. sc_reduce(hram);
  407. sc_muladd(out + (ED25519_SIG_SIZE/2), hram, az, nonce);
  408. #endif
  409. #endif /* WOLFSSL_SE050 */
  410. #ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN
  411. {
  412. int i;
  413. byte c = 0;
  414. for (i = 0; i < ED25519_KEY_SIZE; i++) {
  415. c |= key->k[i] ^ orig_k[i];
  416. }
  417. ret = ctMaskGT(c, 0) & SIG_VERIFY_E;
  418. }
  419. #endif
  420. return ret;
  421. }
  422. /*
  423. in contains the message to sign
  424. inLen is the length of the message to sign
  425. out is the buffer to write the signature
  426. outLen [in/out] input size of out buf
  427. output gets set as the final length of out
  428. key is the ed25519 key to use when signing
  429. return 0 on success
  430. */
  431. int wc_ed25519_sign_msg(const byte* in, word32 inLen, byte* out,
  432. word32 *outLen, ed25519_key* key)
  433. {
  434. return wc_ed25519_sign_msg_ex(in, inLen, out, outLen, key, (byte)Ed25519,
  435. NULL, 0);
  436. }
  437. /*
  438. in contains the message to sign
  439. inLen is the length of the message to sign
  440. out is the buffer to write the signature
  441. outLen [in/out] input size of out buf
  442. output gets set as the final length of out
  443. key is the ed25519 key to use when signing
  444. context extra signing data
  445. contextLen length of extra signing data
  446. return 0 on success
  447. */
  448. int wc_ed25519ctx_sign_msg(const byte* in, word32 inLen, byte* out,
  449. word32 *outLen, ed25519_key* key,
  450. const byte* context, byte contextLen)
  451. {
  452. return wc_ed25519_sign_msg_ex(in, inLen, out, outLen, key, Ed25519ctx,
  453. context, contextLen);
  454. }
  455. /*
  456. hash contains the SHA-512 hash of the message to sign
  457. hashLen is the length of the SHA-512 hash of the message to sign
  458. out is the buffer to write the signature
  459. outLen [in/out] input size of out buf
  460. output gets set as the final length of out
  461. key is the ed25519 key to use when signing
  462. context extra signing data
  463. contextLen length of extra signing data
  464. return 0 on success
  465. */
  466. int wc_ed25519ph_sign_hash(const byte* hash, word32 hashLen, byte* out,
  467. word32 *outLen, ed25519_key* key,
  468. const byte* context, byte contextLen)
  469. {
  470. return wc_ed25519_sign_msg_ex(hash, hashLen, out, outLen, key, Ed25519ph,
  471. context, contextLen);
  472. }
  473. /*
  474. in contains the message to sign
  475. inLen is the length of the message to sign
  476. out is the buffer to write the signature
  477. outLen [in/out] input size of out buf
  478. output gets set as the final length of out
  479. key is the ed25519 key to use when signing
  480. context extra signing data
  481. contextLen length of extra signing data
  482. return 0 on success
  483. */
  484. int wc_ed25519ph_sign_msg(const byte* in, word32 inLen, byte* out,
  485. word32 *outLen, ed25519_key* key,
  486. const byte* context, byte contextLen)
  487. {
  488. int ret;
  489. byte hash[WC_SHA512_DIGEST_SIZE];
  490. ret = ed25519_hash(key, in, inLen, hash);
  491. if (ret != 0)
  492. return ret;
  493. return wc_ed25519_sign_msg_ex(hash, sizeof(hash), out, outLen, key,
  494. Ed25519ph, context, contextLen);
  495. }
  496. #endif /* HAVE_ED25519_SIGN */
  497. #ifdef HAVE_ED25519_VERIFY
  498. #ifndef WOLFSSL_SE050
  499. /*
  500. sig is array of bytes containing the signature
  501. sigLen is the length of sig byte array
  502. key Ed25519 public key
  503. return 0 on success
  504. type variant to use -- Ed25519, Ed25519ctx, or Ed25519ph
  505. context extra signing data
  506. contextLen length of extra signing data
  507. */
  508. static int ed25519_verify_msg_init_with_sha(const byte* sig, word32 sigLen,
  509. ed25519_key* key, wc_Sha512 *sha,
  510. byte type, const byte* context,
  511. byte contextLen)
  512. {
  513. int ret;
  514. /* sanity check on arguments */
  515. if (sig == NULL || key == NULL ||
  516. (context == NULL && contextLen != 0)) {
  517. return BAD_FUNC_ARG;
  518. }
  519. /* check on basics needed to verify signature */
  520. if (sigLen != ED25519_SIG_SIZE || (sig[ED25519_SIG_SIZE-1] & 224))
  521. return BAD_FUNC_ARG;
  522. /* find H(R,A,M) and store it as h */
  523. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  524. ret = ed25519_hash_reset(key);
  525. if (ret != 0)
  526. return ret;
  527. #else
  528. ret = 0;
  529. #endif
  530. if (type == Ed25519ctx || type == Ed25519ph) {
  531. ret = ed25519_hash_update(key, sha, ed25519Ctx, ED25519CTX_SIZE);
  532. if (ret == 0)
  533. ret = ed25519_hash_update(key, sha, &type, sizeof(type));
  534. if (ret == 0)
  535. ret = ed25519_hash_update(key, sha, &contextLen, sizeof(contextLen));
  536. if (ret == 0 && context != NULL)
  537. ret = ed25519_hash_update(key, sha, context, contextLen);
  538. }
  539. if (ret == 0)
  540. ret = ed25519_hash_update(key, sha, sig, ED25519_SIG_SIZE/2);
  541. if (ret == 0)
  542. ret = ed25519_hash_update(key, sha, key->p, ED25519_PUB_KEY_SIZE);
  543. return ret;
  544. }
  545. /*
  546. msgSegment an array of bytes containing a message segment
  547. msgSegmentLen length of msgSegment
  548. key Ed25519 public key
  549. return 0 on success
  550. */
  551. static int ed25519_verify_msg_update_with_sha(const byte* msgSegment,
  552. word32 msgSegmentLen,
  553. ed25519_key* key,
  554. wc_Sha512 *sha) {
  555. /* sanity check on arguments */
  556. if (msgSegment == NULL || key == NULL)
  557. return BAD_FUNC_ARG;
  558. return ed25519_hash_update(key, sha, msgSegment, msgSegmentLen);
  559. }
  560. /* Low part of order in big endian. */
  561. static const byte ed25519_low_order[] = {
  562. 0x14, 0xde, 0xf9, 0xde, 0xa2, 0xf7, 0x9c, 0xd6,
  563. 0x58, 0x12, 0x63, 0x1a, 0x5c, 0xf5, 0xd3, 0xed
  564. };
  565. #define ED25519_SIG_LOW_ORDER_IDX \
  566. ((int)(ED25519_SIG_SIZE/2 + sizeof(ed25519_low_order) - 1))
  567. /*
  568. sig is array of bytes containing the signature
  569. sigLen is the length of sig byte array
  570. res will be 1 on successful verify and 0 on unsuccessful
  571. key Ed25519 public key
  572. return 0 and res of 1 on success
  573. */
  574. static int ed25519_verify_msg_final_with_sha(const byte* sig, word32 sigLen,
  575. int* res, ed25519_key* key,
  576. wc_Sha512 *sha)
  577. {
  578. ALIGN16 byte rcheck[ED25519_KEY_SIZE];
  579. ALIGN16 byte h[WC_SHA512_DIGEST_SIZE];
  580. #ifndef FREESCALE_LTC_ECC
  581. ge_p3 A;
  582. ge_p2 R;
  583. #endif
  584. int ret;
  585. /* sanity check on arguments */
  586. if (sig == NULL || res == NULL || key == NULL)
  587. return BAD_FUNC_ARG;
  588. /* set verification failed by default */
  589. *res = 0;
  590. /* check on basics needed to verify signature */
  591. if (sigLen != ED25519_SIG_SIZE)
  592. return BAD_FUNC_ARG;
  593. /* S is not larger or equal to the order:
  594. * 2^252 + 0x14def9dea2f79cd65812631a5cf5d3ed
  595. * = 0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed
  596. */
  597. if (sig[ED25519_SIG_SIZE-1] > 0x10)
  598. return BAD_FUNC_ARG;
  599. if (sig[ED25519_SIG_SIZE-1] == 0x10) {
  600. int i = ED25519_SIG_SIZE-1;
  601. int j;
  602. /* Check high zeros. */
  603. for (--i; i > ED25519_SIG_LOW_ORDER_IDX; i--) {
  604. if (sig[i] > 0x00)
  605. break;
  606. }
  607. /* Did we see all zeros up to lower order index? */
  608. if (i == ED25519_SIG_LOW_ORDER_IDX) {
  609. /* Check lower part. */
  610. for (j = 0; j < (int)sizeof(ed25519_low_order); j++, i--) {
  611. /* Check smaller. */
  612. if (sig[i] < ed25519_low_order[j])
  613. break;
  614. /* Check bigger. */
  615. if (sig[i] > ed25519_low_order[j])
  616. return BAD_FUNC_ARG;
  617. }
  618. /* Check equal - all bytes match. */
  619. if (i == ED25519_SIG_SIZE/2 - 1)
  620. return BAD_FUNC_ARG;
  621. }
  622. }
  623. /* uncompress A (public key), test if valid, and negate it */
  624. #ifndef FREESCALE_LTC_ECC
  625. if (ge_frombytes_negate_vartime(&A, key->p) != 0)
  626. return BAD_FUNC_ARG;
  627. #endif
  628. /* find H(R,A,M) and store it as h */
  629. ret = ed25519_hash_final(key, sha, h);
  630. if (ret != 0)
  631. return ret;
  632. #ifdef FREESCALE_LTC_ECC
  633. ret = LTC_PKHA_sc_reduce(h);
  634. if (ret != kStatus_Success)
  635. return ret;
  636. ret = LTC_PKHA_SignatureForVerify(rcheck, h, sig + (ED25519_SIG_SIZE/2), key);
  637. if (ret != kStatus_Success)
  638. return ret;
  639. #else
  640. sc_reduce(h);
  641. /*
  642. Uses a fast single-signature verification SB = R + H(R,A,M)A becomes
  643. SB - H(R,A,M)A saving decompression of R
  644. */
  645. ret = ge_double_scalarmult_vartime(&R, h, &A, sig + (ED25519_SIG_SIZE/2));
  646. if (ret != 0)
  647. return ret;
  648. ge_tobytes(rcheck, &R);
  649. #endif /* FREESCALE_LTC_ECC */
  650. /* comparison of R created to R in sig */
  651. ret = ConstantCompare(rcheck, sig, ED25519_SIG_SIZE/2);
  652. if (ret != 0) {
  653. ret = SIG_VERIFY_E;
  654. } else {
  655. /* set the verification status */
  656. *res = 1;
  657. }
  658. return ret;
  659. }
  660. #endif /* WOLFSSL_SE050 */
  661. #if defined(WOLFSSL_ED25519_STREAMING_VERIFY) && !defined(WOLFSSL_SE050)
  662. int wc_ed25519_verify_msg_init(const byte* sig, word32 sigLen, ed25519_key* key,
  663. byte type, const byte* context, byte contextLen) {
  664. return ed25519_verify_msg_init_with_sha(sig, sigLen, key, &key->sha,
  665. type, context, contextLen);
  666. }
  667. int wc_ed25519_verify_msg_update(const byte* msgSegment, word32 msgSegmentLen,
  668. ed25519_key* key) {
  669. return ed25519_verify_msg_update_with_sha(msgSegment, msgSegmentLen,
  670. key, &key->sha);
  671. }
  672. int wc_ed25519_verify_msg_final(const byte* sig, word32 sigLen, int* res,
  673. ed25519_key* key) {
  674. return ed25519_verify_msg_final_with_sha(sig, sigLen, res,
  675. key, &key->sha);
  676. }
  677. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY && !WOLFSSL_SE050 */
  678. /*
  679. sig is array of bytes containing the signature
  680. sigLen is the length of sig byte array
  681. msg the array of bytes containing the message
  682. msgLen length of msg array
  683. res will be 1 on successful verify and 0 on unsuccessful
  684. key Ed25519 public key
  685. return 0 and res of 1 on success
  686. */
  687. int wc_ed25519_verify_msg_ex(const byte* sig, word32 sigLen, const byte* msg,
  688. word32 msgLen, int* res, ed25519_key* key,
  689. byte type, const byte* context, byte contextLen)
  690. {
  691. int ret;
  692. #ifdef WOLFSSL_SE050
  693. (void)type;
  694. (void)context;
  695. (void)contextLen;
  696. (void)ed25519Ctx;
  697. ret = se050_ed25519_verify_msg(sig, sigLen, msg, msgLen, key, res);
  698. #else
  699. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  700. wc_Sha512 *sha;
  701. #else
  702. wc_Sha512 sha[1];
  703. #endif
  704. /* sanity check on arguments */
  705. if (sig == NULL || msg == NULL || res == NULL || key == NULL ||
  706. (context == NULL && contextLen != 0))
  707. return BAD_FUNC_ARG;
  708. #ifdef WOLF_CRYPTO_CB
  709. if (key->devId != INVALID_DEVID) {
  710. ret = wc_CryptoCb_Ed25519Verify(sig, sigLen, msg, msgLen, res, key,
  711. type, context, contextLen);
  712. if (ret != CRYPTOCB_UNAVAILABLE)
  713. return ret;
  714. /* fall-through when unavailable */
  715. }
  716. #endif
  717. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  718. sha = &key->sha;
  719. #else
  720. ret = ed25519_hash_init(key, sha);
  721. if (ret < 0) {
  722. return ret;
  723. }
  724. #endif /* WOLFSSL_ED25519_PERSISTENT_SHA */
  725. ret = ed25519_verify_msg_init_with_sha(sig, sigLen, key, sha, type, context,
  726. contextLen);
  727. if (ret == 0)
  728. ret = ed25519_verify_msg_update_with_sha(msg, msgLen, key, sha);
  729. if (ret == 0)
  730. ret = ed25519_verify_msg_final_with_sha(sig, sigLen, res, key, sha);
  731. #ifndef WOLFSSL_ED25519_PERSISTENT_SHA
  732. ed25519_hash_free(key, sha);
  733. #endif
  734. #endif /* WOLFSSL_SE050 */
  735. return ret;
  736. }
  737. /*
  738. sig is array of bytes containing the signature
  739. sigLen is the length of sig byte array
  740. msg the array of bytes containing the message
  741. msgLen length of msg array
  742. res will be 1 on successful verify and 0 on unsuccessful
  743. key Ed25519 public key
  744. return 0 and res of 1 on success
  745. */
  746. int wc_ed25519_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  747. word32 msgLen, int* res, ed25519_key* key)
  748. {
  749. return wc_ed25519_verify_msg_ex(sig, sigLen, msg, msgLen, res, key,
  750. (byte)Ed25519, NULL, 0);
  751. }
  752. /*
  753. sig is array of bytes containing the signature
  754. sigLen is the length of sig byte array
  755. msg the array of bytes containing the message
  756. msgLen length of msg array
  757. res will be 1 on successful verify and 0 on unsuccessful
  758. key Ed25519 public key
  759. context extra signing data
  760. contextLen length of extra signing data
  761. return 0 and res of 1 on success
  762. */
  763. int wc_ed25519ctx_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  764. word32 msgLen, int* res, ed25519_key* key,
  765. const byte* context, byte contextLen)
  766. {
  767. return wc_ed25519_verify_msg_ex(sig, sigLen, msg, msgLen, res, key,
  768. Ed25519ctx, context, contextLen);
  769. }
  770. /*
  771. sig is array of bytes containing the signature
  772. sigLen is the length of sig byte array
  773. hash the array of bytes containing the SHA-512 hash of the message
  774. hashLen length of hash array
  775. res will be 1 on successful verify and 0 on unsuccessful
  776. key Ed25519 public key
  777. context extra signing data
  778. contextLen length of extra signing data
  779. return 0 and res of 1 on success
  780. */
  781. int wc_ed25519ph_verify_hash(const byte* sig, word32 sigLen, const byte* hash,
  782. word32 hashLen, int* res, ed25519_key* key,
  783. const byte* context, byte contextLen)
  784. {
  785. return wc_ed25519_verify_msg_ex(sig, sigLen, hash, hashLen, res, key,
  786. Ed25519ph, context, contextLen);
  787. }
  788. /*
  789. sig is array of bytes containing the signature
  790. sigLen is the length of sig byte array
  791. msg the array of bytes containing the message
  792. msgLen length of msg array
  793. res will be 1 on successful verify and 0 on unsuccessful
  794. key Ed25519 public key
  795. context extra signing data
  796. contextLen length of extra signing data
  797. return 0 and res of 1 on success
  798. */
  799. int wc_ed25519ph_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  800. word32 msgLen, int* res, ed25519_key* key,
  801. const byte* context, byte contextLen)
  802. {
  803. int ret;
  804. byte hash[WC_SHA512_DIGEST_SIZE];
  805. ret = ed25519_hash(key, msg, msgLen, hash);
  806. if (ret != 0)
  807. return ret;
  808. return wc_ed25519_verify_msg_ex(sig, sigLen, hash, sizeof(hash), res, key,
  809. Ed25519ph, context, contextLen);
  810. }
  811. #endif /* HAVE_ED25519_VERIFY */
  812. /* initialize information and memory for key */
  813. int wc_ed25519_init_ex(ed25519_key* key, void* heap, int devId)
  814. {
  815. if (key == NULL)
  816. return BAD_FUNC_ARG;
  817. /* for init, ensure the key is zeroed*/
  818. XMEMSET(key, 0, sizeof(ed25519_key));
  819. #ifdef WOLF_CRYPTO_CB
  820. key->devId = devId;
  821. #else
  822. (void)devId;
  823. #endif
  824. key->heap = heap;
  825. #ifndef FREESCALE_LTC_ECC
  826. fe_init();
  827. #endif
  828. #ifdef WOLFSSL_CHECK_MEM_ZERO
  829. wc_MemZero_Add("wc_ed25519_init_ex key->k", &key->k, sizeof(key->k));
  830. #endif
  831. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  832. return ed25519_hash_init(key, &key->sha);
  833. #else /* !WOLFSSL_ED25519_PERSISTENT_SHA */
  834. return 0;
  835. #endif /* WOLFSSL_ED25519_PERSISTENT_SHA */
  836. }
  837. int wc_ed25519_init(ed25519_key* key)
  838. {
  839. return wc_ed25519_init_ex(key, NULL, INVALID_DEVID);
  840. }
  841. /* clear memory of key */
  842. void wc_ed25519_free(ed25519_key* key)
  843. {
  844. if (key == NULL)
  845. return;
  846. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  847. ed25519_hash_free(key, &key->sha);
  848. #endif
  849. #ifdef WOLFSSL_SE050
  850. se050_ed25519_free_key(key);
  851. #endif
  852. ForceZero(key, sizeof(ed25519_key));
  853. #ifdef WOLFSSL_CHECK_MEM_ZERO
  854. wc_MemZero_Check(key, sizeof(ed25519_key));
  855. #endif
  856. }
  857. #ifdef HAVE_ED25519_KEY_EXPORT
  858. /*
  859. outLen should contain the size of out buffer when input. outLen is than set
  860. to the final output length.
  861. returns 0 on success
  862. */
  863. int wc_ed25519_export_public(ed25519_key* key, byte* out, word32* outLen)
  864. {
  865. /* sanity check on arguments */
  866. if (key == NULL || out == NULL || outLen == NULL)
  867. return BAD_FUNC_ARG;
  868. if (*outLen < ED25519_PUB_KEY_SIZE) {
  869. *outLen = ED25519_PUB_KEY_SIZE;
  870. return BUFFER_E;
  871. }
  872. *outLen = ED25519_PUB_KEY_SIZE;
  873. XMEMCPY(out, key->p, ED25519_PUB_KEY_SIZE);
  874. return 0;
  875. }
  876. #endif /* HAVE_ED25519_KEY_EXPORT */
  877. #ifdef HAVE_ED25519_KEY_IMPORT
  878. /*
  879. Imports a compressed/uncompressed public key.
  880. in the byte array containing the public key
  881. inLen the length of the byte array being passed in
  882. key ed25519 key struct to put the public key in
  883. trusted whether the public key is trusted to match private key if set
  884. */
  885. int wc_ed25519_import_public_ex(const byte* in, word32 inLen, ed25519_key* key,
  886. int trusted)
  887. {
  888. int ret = 0;
  889. /* sanity check on arguments */
  890. if (in == NULL || key == NULL)
  891. return BAD_FUNC_ARG;
  892. if (inLen < ED25519_PUB_KEY_SIZE)
  893. return BAD_FUNC_ARG;
  894. /* compressed prefix according to draft
  895. http://www.ietf.org/id/draft-koch-eddsa-for-openpgp-02.txt */
  896. if (in[0] == 0x40 && inLen == ED25519_PUB_KEY_SIZE + 1) {
  897. /* key is stored in compressed format so just copy in */
  898. XMEMCPY(key->p, (in + 1), ED25519_PUB_KEY_SIZE);
  899. #ifdef FREESCALE_LTC_ECC
  900. /* recover X coordinate */
  901. ltc_pkha_ecc_point_t pubKey;
  902. pubKey.X = key->pointX;
  903. pubKey.Y = key->pointY;
  904. LTC_PKHA_Ed25519_PointDecompress(key->p, ED25519_PUB_KEY_SIZE, &pubKey);
  905. #endif
  906. }
  907. /* importing uncompressed public key */
  908. else if (in[0] == 0x04 && inLen > 2*ED25519_PUB_KEY_SIZE) {
  909. #ifdef FREESCALE_LTC_ECC
  910. /* reverse bytes for little endian byte order */
  911. for (int i = 0; i < ED25519_KEY_SIZE; i++)
  912. {
  913. key->pointX[i] = *(in + ED25519_KEY_SIZE - i);
  914. key->pointY[i] = *(in + 2*ED25519_KEY_SIZE - i);
  915. }
  916. XMEMCPY(key->p, key->pointY, ED25519_KEY_SIZE);
  917. #else
  918. /* pass in (x,y) and store compressed key */
  919. ret = ge_compress_key(key->p, in+1,
  920. in+1+ED25519_PUB_KEY_SIZE, ED25519_PUB_KEY_SIZE);
  921. #endif /* FREESCALE_LTC_ECC */
  922. }
  923. /* if not specified compressed or uncompressed check key size
  924. if key size is equal to compressed key size copy in key */
  925. else if (inLen == ED25519_PUB_KEY_SIZE) {
  926. XMEMCPY(key->p, in, ED25519_PUB_KEY_SIZE);
  927. #ifdef FREESCALE_LTC_ECC
  928. /* recover X coordinate */
  929. ltc_pkha_ecc_point_t pubKey;
  930. pubKey.X = key->pointX;
  931. pubKey.Y = key->pointY;
  932. LTC_PKHA_Ed25519_PointDecompress(key->p, ED25519_PUB_KEY_SIZE, &pubKey);
  933. #endif
  934. }
  935. else {
  936. ret = BAD_FUNC_ARG;
  937. }
  938. if (ret == 0) {
  939. key->pubKeySet = 1;
  940. if (key->privKeySet && (!trusted)) {
  941. ret = wc_ed25519_check_key(key);
  942. }
  943. }
  944. if (ret != 0) {
  945. key->pubKeySet = 0;
  946. }
  947. /* bad public key format */
  948. return ret;
  949. }
  950. /*
  951. Imports a compressed/uncompressed public key.
  952. in the byte array containing the public key
  953. inLen the length of the byte array being passed in
  954. key ed25519 key struct to put the public key in
  955. */
  956. int wc_ed25519_import_public(const byte* in, word32 inLen, ed25519_key* key)
  957. {
  958. return wc_ed25519_import_public_ex(in, inLen, key, 0);
  959. }
  960. /*
  961. For importing a private key.
  962. */
  963. int wc_ed25519_import_private_only(const byte* priv, word32 privSz,
  964. ed25519_key* key)
  965. {
  966. int ret = 0;
  967. /* sanity check on arguments */
  968. if (priv == NULL || key == NULL)
  969. return BAD_FUNC_ARG;
  970. /* key size check */
  971. if (privSz != ED25519_KEY_SIZE)
  972. return BAD_FUNC_ARG;
  973. XMEMCPY(key->k, priv, ED25519_KEY_SIZE);
  974. key->privKeySet = 1;
  975. if (key->pubKeySet) {
  976. /* Validate loaded public key */
  977. ret = wc_ed25519_check_key(key);
  978. }
  979. if (ret != 0) {
  980. key->privKeySet = 0;
  981. ForceZero(key->k, ED25519_KEY_SIZE);
  982. }
  983. return ret;
  984. }
  985. /* Import an ed25519 private and public keys from byte array(s).
  986. *
  987. * priv [in] Array holding private key from
  988. * wc_ed25519_export_private_only(), or private+public keys from
  989. * wc_ed25519_export_private().
  990. * privSz [in] Number of bytes of data in private key array.
  991. * pub [in] Array holding public key (or NULL).
  992. * pubSz [in] Number of bytes of data in public key array (or 0).
  993. * key [in] Ed25519 private/public key.
  994. * trusted [in] Indicates whether the public key data is trusted.
  995. * When 0, checks public key matches private key.
  996. * When 1, doesn't check public key matches private key.
  997. * returns BAD_FUNC_ARG when a required parameter is NULL or an invalid
  998. * combination of keys/lengths is supplied, 0 otherwise.
  999. */
  1000. int wc_ed25519_import_private_key_ex(const byte* priv, word32 privSz,
  1001. const byte* pub, word32 pubSz, ed25519_key* key, int trusted)
  1002. {
  1003. int ret;
  1004. /* sanity check on arguments */
  1005. if (priv == NULL || key == NULL)
  1006. return BAD_FUNC_ARG;
  1007. /* key size check */
  1008. if (privSz != ED25519_KEY_SIZE && privSz != ED25519_PRV_KEY_SIZE)
  1009. return BAD_FUNC_ARG;
  1010. if (pub == NULL) {
  1011. if (pubSz != 0)
  1012. return BAD_FUNC_ARG;
  1013. if (privSz != ED25519_PRV_KEY_SIZE)
  1014. return BAD_FUNC_ARG;
  1015. pub = priv + ED25519_KEY_SIZE;
  1016. pubSz = ED25519_PUB_KEY_SIZE;
  1017. }
  1018. else if (pubSz < ED25519_PUB_KEY_SIZE) {
  1019. return BAD_FUNC_ARG;
  1020. }
  1021. XMEMCPY(key->k, priv, ED25519_KEY_SIZE);
  1022. key->privKeySet = 1;
  1023. /* import public key */
  1024. ret = wc_ed25519_import_public_ex(pub, pubSz, key, trusted);
  1025. if (ret != 0) {
  1026. key->privKeySet = 0;
  1027. ForceZero(key->k, ED25519_KEY_SIZE);
  1028. return ret;
  1029. }
  1030. /* make the private key (priv + pub) */
  1031. XMEMCPY(key->k + ED25519_KEY_SIZE, key->p, ED25519_PUB_KEY_SIZE);
  1032. return ret;
  1033. }
  1034. /* Import an ed25519 private and public keys from byte array(s).
  1035. *
  1036. * priv [in] Array holding private key from wc_ed25519_export_private_only(),
  1037. * or private+public keys from wc_ed25519_export_private().
  1038. * privSz [in] Number of bytes of data in private key array.
  1039. * pub [in] Array holding public key (or NULL).
  1040. * pubSz [in] Number of bytes of data in public key array (or 0).
  1041. * key [in] Ed25519 private/public key.
  1042. * returns BAD_FUNC_ARG when a required parameter is NULL or an invalid
  1043. * combination of keys/lengths is supplied, 0 otherwise.
  1044. */
  1045. int wc_ed25519_import_private_key(const byte* priv, word32 privSz,
  1046. const byte* pub, word32 pubSz, ed25519_key* key)
  1047. {
  1048. return wc_ed25519_import_private_key_ex(priv, privSz, pub, pubSz, key, 0);
  1049. }
  1050. #endif /* HAVE_ED25519_KEY_IMPORT */
  1051. #ifdef HAVE_ED25519_KEY_EXPORT
  1052. /*
  1053. export private key only (secret part so 32 bytes)
  1054. outLen should contain the size of out buffer when input. outLen is than set
  1055. to the final output length.
  1056. returns 0 on success
  1057. */
  1058. int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen)
  1059. {
  1060. /* sanity checks on arguments */
  1061. if (key == NULL || out == NULL || outLen == NULL)
  1062. return BAD_FUNC_ARG;
  1063. if (*outLen < ED25519_KEY_SIZE) {
  1064. *outLen = ED25519_KEY_SIZE;
  1065. return BUFFER_E;
  1066. }
  1067. *outLen = ED25519_KEY_SIZE;
  1068. XMEMCPY(out, key->k, ED25519_KEY_SIZE);
  1069. return 0;
  1070. }
  1071. /*
  1072. export private key, including public part
  1073. outLen should contain the size of out buffer when input. outLen is than set
  1074. to the final output length.
  1075. returns 0 on success
  1076. */
  1077. int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen)
  1078. {
  1079. /* sanity checks on arguments */
  1080. if (key == NULL || out == NULL || outLen == NULL)
  1081. return BAD_FUNC_ARG;
  1082. if (*outLen < ED25519_PRV_KEY_SIZE) {
  1083. *outLen = ED25519_PRV_KEY_SIZE;
  1084. return BUFFER_E;
  1085. }
  1086. *outLen = ED25519_PRV_KEY_SIZE;
  1087. XMEMCPY(out, key->k, ED25519_PRV_KEY_SIZE);
  1088. return 0;
  1089. }
  1090. /* export full private key and public key
  1091. return 0 on success
  1092. */
  1093. int wc_ed25519_export_key(ed25519_key* key,
  1094. byte* priv, word32 *privSz,
  1095. byte* pub, word32 *pubSz)
  1096. {
  1097. int ret;
  1098. /* export 'full' private part */
  1099. ret = wc_ed25519_export_private(key, priv, privSz);
  1100. if (ret != 0)
  1101. return ret;
  1102. /* export public part */
  1103. ret = wc_ed25519_export_public(key, pub, pubSz);
  1104. return ret;
  1105. }
  1106. #endif /* HAVE_ED25519_KEY_EXPORT */
  1107. /* check the private and public keys match */
  1108. int wc_ed25519_check_key(ed25519_key* key)
  1109. {
  1110. int ret = 0;
  1111. #ifdef HAVE_ED25519_MAKE_KEY
  1112. ALIGN16 unsigned char pubKey[ED25519_PUB_KEY_SIZE];
  1113. if (!key->pubKeySet)
  1114. ret = PUBLIC_KEY_E;
  1115. if (ret == 0)
  1116. ret = wc_ed25519_make_public(key, pubKey, sizeof(pubKey));
  1117. if (ret == 0 && XMEMCMP(pubKey, key->p, ED25519_PUB_KEY_SIZE) != 0)
  1118. ret = PUBLIC_KEY_E;
  1119. #else
  1120. (void)key;
  1121. #endif /* HAVE_ED25519_MAKE_KEY */
  1122. return ret;
  1123. }
  1124. /* returns the private key size (secret only) in bytes */
  1125. int wc_ed25519_size(ed25519_key* key)
  1126. {
  1127. if (key == NULL)
  1128. return BAD_FUNC_ARG;
  1129. return ED25519_KEY_SIZE;
  1130. }
  1131. /* returns the private key size (secret + public) in bytes */
  1132. int wc_ed25519_priv_size(ed25519_key* key)
  1133. {
  1134. if (key == NULL)
  1135. return BAD_FUNC_ARG;
  1136. return ED25519_PRV_KEY_SIZE;
  1137. }
  1138. /* returns the compressed key size in bytes (public key) */
  1139. int wc_ed25519_pub_size(ed25519_key* key)
  1140. {
  1141. if (key == NULL)
  1142. return BAD_FUNC_ARG;
  1143. return ED25519_PUB_KEY_SIZE;
  1144. }
  1145. /* returns the size of signature in bytes */
  1146. int wc_ed25519_sig_size(ed25519_key* key)
  1147. {
  1148. if (key == NULL)
  1149. return BAD_FUNC_ARG;
  1150. return ED25519_SIG_SIZE;
  1151. }
  1152. #endif /* HAVE_ED25519 */