ed448.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. /* ed448.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. /* Implemented to: RFC 8032 */
  22. /* Based On Daniel J Bernstein's ed25519 Public Domain ref10 work.
  23. * Reworked for curve448 by Sean Parkinson.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include <config.h>
  27. #endif
  28. /* in case user set HAVE_ED448 there */
  29. #include <wolfssl/wolfcrypt/settings.h>
  30. #ifdef HAVE_ED448
  31. #include <wolfssl/wolfcrypt/ed448.h>
  32. #include <wolfssl/wolfcrypt/error-crypt.h>
  33. #include <wolfssl/wolfcrypt/hash.h>
  34. #ifdef NO_INLINE
  35. #include <wolfssl/wolfcrypt/misc.h>
  36. #else
  37. #define WOLFSSL_MISC_INCLUDED
  38. #include <wolfcrypt/src/misc.c>
  39. #endif
  40. #if defined(HAVE_ED448_SIGN) || defined(HAVE_ED448_VERIFY)
  41. /* Size of context bytes to use with hash when signing and verifying. */
  42. #define ED448CTX_SIZE 8
  43. /* Context to pass to hash when signing and verifying. */
  44. static const byte ed448Ctx[ED448CTX_SIZE+1] = "SigEd448";
  45. #endif
  46. static int ed448_hash_init(ed448_key* key, wc_Shake *sha)
  47. {
  48. int ret;
  49. ret = wc_InitShake256(sha, key->heap,
  50. #if defined(WOLF_CRYPTO_CB)
  51. key->devId
  52. #else
  53. INVALID_DEVID
  54. #endif
  55. );
  56. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  57. if (ret == 0)
  58. key->sha_clean_flag = 1;
  59. #endif
  60. return ret;
  61. }
  62. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  63. static int ed448_hash_reset(ed448_key* key)
  64. {
  65. int ret;
  66. if (key->sha_clean_flag)
  67. ret = 0;
  68. else {
  69. wc_Shake256_Free(&key->sha);
  70. ret = wc_InitShake256(&key->sha, key->heap,
  71. #if defined(WOLF_CRYPTO_CB)
  72. key->devId
  73. #else
  74. INVALID_DEVID
  75. #endif
  76. );
  77. if (ret == 0)
  78. key->sha_clean_flag = 1;
  79. }
  80. return ret;
  81. }
  82. #endif /* WOLFSSL_ED448_PERSISTENT_SHA */
  83. static int ed448_hash_update(ed448_key* key, wc_Shake *sha, const byte* data,
  84. word32 len)
  85. {
  86. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  87. if (key->sha_clean_flag)
  88. key->sha_clean_flag = 0;
  89. #else
  90. (void)key;
  91. #endif
  92. return wc_Shake256_Update(sha, data, len);
  93. }
  94. static int ed448_hash_final(ed448_key* key, wc_Shake *sha, byte* hash,
  95. word32 hashLen)
  96. {
  97. int ret = wc_Shake256_Final(sha, hash, hashLen);
  98. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  99. if (ret == 0)
  100. key->sha_clean_flag = 1;
  101. #else
  102. (void)key;
  103. #endif
  104. return ret;
  105. }
  106. static void ed448_hash_free(ed448_key* key, wc_Shake *sha)
  107. {
  108. wc_Shake256_Free(sha);
  109. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  110. key->sha_clean_flag = 0;
  111. #else
  112. (void)key;
  113. #endif
  114. }
  115. static int ed448_hash(ed448_key* key, const byte* in, word32 inLen,
  116. byte* hash, word32 hashLen)
  117. {
  118. int ret;
  119. #ifndef WOLFSSL_ED448_PERSISTENT_SHA
  120. wc_Shake sha[1];
  121. #else
  122. wc_Shake *sha;
  123. #endif
  124. if (key == NULL || (in == NULL && inLen > 0) || hash == NULL) {
  125. return BAD_FUNC_ARG;
  126. }
  127. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  128. sha = &key->sha;
  129. ret = ed448_hash_reset(key);
  130. #else
  131. ret = ed448_hash_init(key, sha);
  132. #endif
  133. if (ret < 0)
  134. return ret;
  135. ret = ed448_hash_update(key, sha, in, inLen);
  136. if (ret == 0)
  137. ret = ed448_hash_final(key, sha, hash, hashLen);
  138. #ifndef WOLFSSL_ED448_PERSISTENT_SHA
  139. ed448_hash_free(key, sha);
  140. #endif
  141. return ret;
  142. }
  143. /* Derive the public key for the private key.
  144. *
  145. * key [in] Ed448 key object.
  146. * pubKey [in] Byte array to hold the public key.
  147. * pubKeySz [in] Size of the array in bytes.
  148. * returns BAD_FUNC_ARG when key is NULL or pubKeySz is not equal to
  149. * ED448_PUB_KEY_SIZE,
  150. * other -ve value on hash failure,
  151. * 0 otherwise.
  152. */
  153. int wc_ed448_make_public(ed448_key* key, unsigned char* pubKey, word32 pubKeySz)
  154. {
  155. int ret = 0;
  156. byte az[ED448_PRV_KEY_SIZE];
  157. ge448_p2 A;
  158. if ((key == NULL) || (pubKey == NULL) || (pubKeySz != ED448_PUB_KEY_SIZE)) {
  159. ret = BAD_FUNC_ARG;
  160. }
  161. if ((ret == 0) && (!key->privKeySet)) {
  162. ret = ECC_PRIV_KEY_E;
  163. }
  164. if (ret == 0)
  165. ret = ed448_hash(key, key->k, ED448_KEY_SIZE, az, sizeof(az));
  166. if (ret == 0) {
  167. /* apply clamp */
  168. az[0] &= 0xfc;
  169. az[55] |= 0x80;
  170. az[56] = 0x00;
  171. ge448_scalarmult_base(&A, az);
  172. ge448_to_bytes(pubKey, &A);
  173. key->pubKeySet = 1;
  174. }
  175. return ret;
  176. }
  177. /* Make a new ed448 private/public key.
  178. *
  179. * rng [in] Random number generator.
  180. * keysize [in] Size of the key to generate.
  181. * key [in] Ed448 key object.
  182. * returns BAD_FUNC_ARG when rng or key is NULL or keySz is not equal to
  183. * ED448_KEY_SIZE,
  184. * other -ve value on random number or hash failure,
  185. * 0 otherwise.
  186. */
  187. int wc_ed448_make_key(WC_RNG* rng, int keySz, ed448_key* key)
  188. {
  189. int ret = 0;
  190. if ((rng == NULL) || (key == NULL)) {
  191. ret = BAD_FUNC_ARG;
  192. }
  193. /* ed448 has 57 byte key sizes */
  194. if ((ret == 0) && (keySz != ED448_KEY_SIZE)) {
  195. ret = BAD_FUNC_ARG;
  196. }
  197. if (ret == 0) {
  198. key->pubKeySet = 0;
  199. key->privKeySet = 0;
  200. ret = wc_RNG_GenerateBlock(rng, key->k, ED448_KEY_SIZE);
  201. }
  202. if (ret == 0) {
  203. key->privKeySet = 1;
  204. ret = wc_ed448_make_public(key, key->p, ED448_PUB_KEY_SIZE);
  205. if (ret != 0) {
  206. key->privKeySet = 0;
  207. ForceZero(key->k, ED448_KEY_SIZE);
  208. }
  209. }
  210. if (ret == 0) {
  211. /* put public key after private key, on the same buffer */
  212. XMEMMOVE(key->k + ED448_KEY_SIZE, key->p, ED448_PUB_KEY_SIZE);
  213. }
  214. return ret;
  215. }
  216. #ifdef HAVE_ED448_SIGN
  217. /* Sign the message using the ed448 private key.
  218. *
  219. * in [in] Message to sign.
  220. * inLen [in] Length of the message in bytes.
  221. * out [in] Buffer to write signature into.
  222. * outLen [in/out] On in, size of buffer.
  223. * On out, the length of the signature in bytes.
  224. * key [in] Ed448 key to use when signing
  225. * type [in] Type of signature to perform: Ed448 or Ed448ph
  226. * context [in] Context of signing.
  227. * contextLen [in] Length of context in bytes.
  228. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  229. * context is not NULL or public key not set,
  230. * BUFFER_E when outLen is less than ED448_SIG_SIZE,
  231. * other -ve values when hash fails,
  232. * 0 otherwise.
  233. */
  234. int wc_ed448_sign_msg_ex(const byte* in, word32 inLen, byte* out,
  235. word32 *outLen, ed448_key* key, byte type,
  236. const byte* context, byte contextLen)
  237. {
  238. ge448_p2 R;
  239. byte nonce[ED448_SIG_SIZE];
  240. byte hram[ED448_SIG_SIZE];
  241. byte az[ED448_PRV_KEY_SIZE];
  242. int ret = 0;
  243. /* sanity check on arguments */
  244. if ((in == NULL) || (out == NULL) || (outLen == NULL) || (key == NULL) ||
  245. ((context == NULL) && (contextLen != 0))) {
  246. ret = BAD_FUNC_ARG;
  247. }
  248. if ((ret == 0) && (!key->pubKeySet)) {
  249. ret = BAD_FUNC_ARG;
  250. }
  251. /* check and set up out length */
  252. if ((ret == 0) && (*outLen < ED448_SIG_SIZE)) {
  253. *outLen = ED448_SIG_SIZE;
  254. ret = BUFFER_E;
  255. }
  256. if (ret == 0) {
  257. *outLen = ED448_SIG_SIZE;
  258. /* step 1: create nonce to use where nonce is r in
  259. r = H(h_b, ... ,h_2b-1,M) */
  260. ret = ed448_hash(key, key->k, ED448_KEY_SIZE, az, sizeof(az));
  261. }
  262. if (ret == 0) {
  263. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  264. wc_Shake *sha = &key->sha;
  265. #else
  266. wc_Shake sha[1];
  267. ret = ed448_hash_init(key, sha);
  268. if (ret < 0)
  269. return ret;
  270. #endif
  271. /* apply clamp */
  272. az[0] &= 0xfc;
  273. az[55] |= 0x80;
  274. az[56] = 0x00;
  275. ret = ed448_hash_update(key, sha, ed448Ctx, ED448CTX_SIZE);
  276. if (ret == 0) {
  277. ret = ed448_hash_update(key, sha, &type, sizeof(type));
  278. }
  279. if (ret == 0) {
  280. ret = ed448_hash_update(key, sha, &contextLen, sizeof(contextLen));
  281. }
  282. if ((ret == 0) && (context != NULL)) {
  283. ret = ed448_hash_update(key, sha, context, contextLen);
  284. }
  285. if (ret == 0) {
  286. ret = ed448_hash_update(key, sha, az + ED448_KEY_SIZE, ED448_KEY_SIZE);
  287. }
  288. if (ret == 0) {
  289. ret = ed448_hash_update(key, sha, in, inLen);
  290. }
  291. if (ret == 0) {
  292. ret = ed448_hash_final(key, sha, nonce, sizeof(nonce));
  293. }
  294. #ifndef WOLFSSL_ED448_PERSISTENT_SHA
  295. ed448_hash_free(key, sha);
  296. #endif
  297. }
  298. if (ret == 0) {
  299. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  300. wc_Shake *sha = &key->sha;
  301. #else
  302. wc_Shake sha[1];
  303. ret = ed448_hash_init(key, sha);
  304. if (ret < 0)
  305. return ret;
  306. #endif
  307. sc448_reduce(nonce);
  308. /* step 2: computing R = rB where rB is the scalar multiplication of
  309. r and B */
  310. ge448_scalarmult_base(&R,nonce);
  311. ge448_to_bytes(out,&R);
  312. /* step 3: hash R + public key + message getting H(R,A,M) then
  313. creating S = (r + H(R,A,M)a) mod l */
  314. ret = ed448_hash_update(key, sha, ed448Ctx, ED448CTX_SIZE);
  315. if (ret == 0) {
  316. ret = ed448_hash_update(key, sha, &type, sizeof(type));
  317. }
  318. if (ret == 0) {
  319. ret = ed448_hash_update(key, sha, &contextLen, sizeof(contextLen));
  320. }
  321. if ((ret == 0) && (context != NULL)) {
  322. ret = ed448_hash_update(key, sha, context, contextLen);
  323. }
  324. if (ret == 0) {
  325. ret = ed448_hash_update(key, sha, out, ED448_SIG_SIZE/2);
  326. }
  327. if (ret == 0) {
  328. ret = ed448_hash_update(key, sha, key->p, ED448_PUB_KEY_SIZE);
  329. }
  330. if (ret == 0) {
  331. ret = ed448_hash_update(key, sha, in, inLen);
  332. }
  333. if (ret == 0) {
  334. ret = ed448_hash_final(key, sha, hram, sizeof(hram));
  335. }
  336. #ifndef WOLFSSL_ED448_PERSISTENT_SHA
  337. ed448_hash_free(key, sha);
  338. #endif
  339. }
  340. if (ret == 0) {
  341. sc448_reduce(hram);
  342. sc448_muladd(out + (ED448_SIG_SIZE/2), hram, az, nonce);
  343. }
  344. return ret;
  345. }
  346. /* Sign the message using the ed448 private key.
  347. * Signature type is Ed448.
  348. *
  349. * in [in] Message to sign.
  350. * inLen [in] Length of the message in bytes.
  351. * out [in] Buffer to write signature into.
  352. * outLen [in/out] On in, size of buffer.
  353. * On out, the length of the signature in bytes.
  354. * key [in] Ed448 key to use when signing
  355. * context [in] Context of signing.
  356. * contextLen [in] Length of context in bytes.
  357. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  358. * context is not NULL or public key not set,
  359. * BUFFER_E when outLen is less than ED448_SIG_SIZE,
  360. * other -ve values when hash fails,
  361. * 0 otherwise.
  362. */
  363. int wc_ed448_sign_msg(const byte* in, word32 inLen, byte* out, word32 *outLen,
  364. ed448_key* key, const byte* context, byte contextLen)
  365. {
  366. return wc_ed448_sign_msg_ex(in, inLen, out, outLen, key, Ed448, context,
  367. contextLen);
  368. }
  369. /* Sign the hash using the ed448 private key.
  370. * Signature type is Ed448ph.
  371. *
  372. * hash [in] Hash of message to sign.
  373. * hashLen [in] Length of hash of message in bytes.
  374. * out [in] Buffer to write signature into.
  375. * outLen [in/out] On in, size of buffer.
  376. * On out, the length of the signature in bytes.
  377. * key [in] Ed448 key to use when signing
  378. * context [in] Context of signing.
  379. * contextLen [in] Length of context in bytes.
  380. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  381. * context is not NULL or public key not set,
  382. * BUFFER_E when outLen is less than ED448_SIG_SIZE,
  383. * other -ve values when hash fails,
  384. * 0 otherwise.
  385. */
  386. int wc_ed448ph_sign_hash(const byte* hash, word32 hashLen, byte* out,
  387. word32 *outLen, ed448_key* key,
  388. const byte* context, byte contextLen)
  389. {
  390. return wc_ed448_sign_msg_ex(hash, hashLen, out, outLen, key, Ed448ph,
  391. context, contextLen);
  392. }
  393. /* Sign the message using the ed448 private key.
  394. * Signature type is Ed448ph.
  395. *
  396. * in [in] Message to sign.
  397. * inLen [in] Length of the message to sign in bytes.
  398. * out [in] Buffer to write signature into.
  399. * outLen [in/out] On in, size of buffer.
  400. * On out, the length of the signature in bytes.
  401. * key [in] Ed448 key to use when signing
  402. * context [in] Context of signing.
  403. * contextLen [in] Length of context in bytes.
  404. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  405. * context is not NULL or public key not set,
  406. * BUFFER_E when outLen is less than ED448_SIG_SIZE,
  407. * other -ve values when hash fails,
  408. * 0 otherwise.
  409. */
  410. int wc_ed448ph_sign_msg(const byte* in, word32 inLen, byte* out, word32 *outLen,
  411. ed448_key* key, const byte* context, byte contextLen)
  412. {
  413. int ret;
  414. byte hash[ED448_PREHASH_SIZE];
  415. ret = ed448_hash(key, in, inLen, hash, sizeof(hash));
  416. if (ret == 0) {
  417. ret = wc_ed448ph_sign_hash(hash, sizeof(hash), out, outLen, key,
  418. context, contextLen);
  419. }
  420. return ret;
  421. }
  422. #endif /* HAVE_ED448_SIGN */
  423. #ifdef HAVE_ED448_VERIFY
  424. /* Verify the message using the ed448 public key.
  425. *
  426. * sig [in] Signature to verify.
  427. * sigLen [in] Size of signature in bytes.
  428. * key [in] Ed448 key to use to verify.
  429. * type [in] Type of signature to verify: Ed448 or Ed448ph
  430. * context [in] Context of verification.
  431. * contextLen [in] Length of context in bytes.
  432. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  433. * context is not NULL or public key not set,
  434. * BUFFER_E when sigLen is less than ED448_SIG_SIZE,
  435. * other -ve values when hash fails,
  436. * 0 otherwise.
  437. */
  438. static int ed448_verify_msg_init_with_sha(const byte* sig, word32 sigLen,
  439. ed448_key* key, wc_Shake *sha, byte type,
  440. const byte* context, byte contextLen)
  441. {
  442. int ret;
  443. /* sanity check on arguments */
  444. if ((sig == NULL) || (key == NULL) ||
  445. ((context == NULL) && (contextLen != 0))) {
  446. return BAD_FUNC_ARG;
  447. }
  448. /* check on basics needed to verify signature */
  449. if (sigLen != ED448_SIG_SIZE) {
  450. return BAD_FUNC_ARG;
  451. }
  452. /* find H(R,A,M) and store it as h */
  453. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  454. ret = ed448_hash_reset(key);
  455. if (ret < 0)
  456. return ret;
  457. #endif
  458. ret = ed448_hash_update(key, sha, ed448Ctx, ED448CTX_SIZE);
  459. if (ret == 0) {
  460. ret = ed448_hash_update(key, sha, &type, sizeof(type));
  461. }
  462. if (ret == 0) {
  463. ret = ed448_hash_update(key, sha, &contextLen, sizeof(contextLen));
  464. }
  465. if ((ret == 0) && (context != NULL)) {
  466. ret = ed448_hash_update(key, sha, context, contextLen);
  467. }
  468. if (ret == 0) {
  469. ret = ed448_hash_update(key, sha, sig, ED448_SIG_SIZE/2);
  470. }
  471. if (ret == 0) {
  472. ret = ed448_hash_update(key, sha, key->p, ED448_PUB_KEY_SIZE);
  473. }
  474. return ret;
  475. }
  476. /*
  477. msgSegment an array of bytes containing a message segment
  478. msgSegmentLen length of msgSegment
  479. key Ed448 public key
  480. return 0 on success
  481. */
  482. static int ed448_verify_msg_update_with_sha(const byte* msgSegment,
  483. word32 msgSegmentLen,
  484. ed448_key* key,
  485. wc_Shake *sha)
  486. {
  487. /* sanity check on arguments */
  488. if (msgSegment == NULL || key == NULL)
  489. return BAD_FUNC_ARG;
  490. return ed448_hash_update(key, sha, msgSegment, msgSegmentLen);
  491. }
  492. /* Order of the ed448 curve - little endian. */
  493. static const byte ed448_order[] = {
  494. 0xf3, 0x44, 0x58, 0xab, 0x92, 0xc2, 0x78, 0x23,
  495. 0x55, 0x8f, 0xc5, 0x8d, 0x72, 0xc2, 0x6c, 0x21,
  496. 0x90, 0x36, 0xd6, 0xae, 0x49, 0xdb, 0x4e, 0xc4,
  497. 0xe9, 0x23, 0xca, 0x7c, 0xff, 0xff, 0xff, 0xff,
  498. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  499. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  500. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f,
  501. 0x00
  502. };
  503. /* Verify the message using the ed448 public key.
  504. *
  505. * sig [in] Signature to verify.
  506. * sigLen [in] Size of signature in bytes.
  507. * res [out] *res is set to 1 on successful verification.
  508. * key [in] Ed448 key to use to verify.
  509. * returns BAD_FUNC_ARG when a parameter is NULL or public key not set,
  510. * BUFFER_E when sigLen is less than ED448_SIG_SIZE,
  511. * other -ve values when hash fails,
  512. * 0 otherwise.
  513. */
  514. static int ed448_verify_msg_final_with_sha(const byte* sig, word32 sigLen,
  515. int* res, ed448_key* key, wc_Shake *sha)
  516. {
  517. byte rcheck[ED448_KEY_SIZE];
  518. byte h[ED448_SIG_SIZE];
  519. ge448_p2 A;
  520. ge448_p2 R;
  521. int ret;
  522. int i;
  523. /* sanity check on arguments */
  524. if ((sig == NULL) || (res == NULL) || (key == NULL))
  525. return BAD_FUNC_ARG;
  526. /* set verification failed by default */
  527. *res = 0;
  528. /* check on basics needed to verify signature */
  529. if (sigLen != ED448_SIG_SIZE)
  530. return BAD_FUNC_ARG;
  531. /* Check S is not larger than or equal to order. */
  532. for (i = (int)sizeof(ed448_order) - 1; i >= 0; i--) {
  533. /* Bigger than order. */
  534. if (sig[ED448_SIG_SIZE/2 + i] > ed448_order[i])
  535. return BAD_FUNC_ARG;
  536. /* Less than order. */
  537. if (sig[ED448_SIG_SIZE/2 + i] < ed448_order[i])
  538. break;
  539. }
  540. /* Same value as order. */
  541. if (i == -1)
  542. return BAD_FUNC_ARG;
  543. /* uncompress A (public key), test if valid, and negate it */
  544. if (ge448_from_bytes_negate_vartime(&A, key->p) != 0)
  545. return BAD_FUNC_ARG;
  546. ret = ed448_hash_final(key, sha, h, sizeof(h));
  547. if (ret != 0)
  548. return ret;
  549. sc448_reduce(h);
  550. /* Uses a fast single-signature verification SB = R + H(R,A,M)A becomes
  551. * SB - H(R,A,M)A saving decompression of R
  552. */
  553. ret = ge448_double_scalarmult_vartime(&R, h, &A,
  554. sig + (ED448_SIG_SIZE/2));
  555. if (ret != 0)
  556. return ret;
  557. ge448_to_bytes(rcheck, &R);
  558. /* comparison of R created to R in sig */
  559. if (ConstantCompare(rcheck, sig, ED448_SIG_SIZE/2) != 0) {
  560. ret = SIG_VERIFY_E;
  561. }
  562. else {
  563. /* set the verification status */
  564. *res = 1;
  565. }
  566. return ret;
  567. }
  568. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  569. int wc_ed448_verify_msg_init(const byte* sig, word32 sigLen, ed448_key* key,
  570. byte type, const byte* context, byte contextLen)
  571. {
  572. return ed448_verify_msg_init_with_sha(sig, sigLen, key, &key->sha, type,
  573. context, contextLen);
  574. }
  575. int wc_ed448_verify_msg_update(const byte* msgSegment, word32 msgSegmentLen,
  576. ed448_key* key)
  577. {
  578. return ed448_verify_msg_update_with_sha(msgSegment, msgSegmentLen, key,
  579. &key->sha);
  580. }
  581. int wc_ed448_verify_msg_final(const byte* sig, word32 sigLen,
  582. int* res, ed448_key* key)
  583. {
  584. return ed448_verify_msg_final_with_sha(sig, sigLen, res, key, &key->sha);
  585. }
  586. #endif
  587. /* Verify the message using the ed448 public key.
  588. *
  589. * sig [in] Signature to verify.
  590. * sigLen [in] Size of signature in bytes.
  591. * msg [in] Message to verify.
  592. * msgLen [in] Length of the message in bytes.
  593. * res [out] *res is set to 1 on successful verification.
  594. * key [in] Ed448 key to use to verify.
  595. * type [in] Type of signature to verify: Ed448 or Ed448ph
  596. * context [in] Context of verification.
  597. * contextLen [in] Length of context in bytes.
  598. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  599. * context is not NULL or public key not set,
  600. * BUFFER_E when sigLen is less than ED448_SIG_SIZE,
  601. * other -ve values when hash fails,
  602. * 0 otherwise.
  603. */
  604. int wc_ed448_verify_msg_ex(const byte* sig, word32 sigLen, const byte* msg,
  605. word32 msgLen, int* res, ed448_key* key,
  606. byte type, const byte* context, byte contextLen)
  607. {
  608. int ret;
  609. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  610. wc_Shake *sha;
  611. #else
  612. wc_Shake sha[1];
  613. #endif
  614. if (key == NULL)
  615. return BAD_FUNC_ARG;
  616. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  617. sha = &key->sha;
  618. #else
  619. ret = ed448_hash_init(key, sha);
  620. if (ret < 0)
  621. return ret;
  622. #endif
  623. ret = ed448_verify_msg_init_with_sha(sig, sigLen, key, sha,
  624. type, context, contextLen);
  625. if (ret == 0)
  626. ret = ed448_verify_msg_update_with_sha(msg, msgLen, key, sha);
  627. if (ret == 0)
  628. ret = ed448_verify_msg_final_with_sha(sig, sigLen, res, key, sha);
  629. #ifndef WOLFSSL_ED448_PERSISTENT_SHA
  630. ed448_hash_free(key, sha);
  631. #endif
  632. return ret;
  633. }
  634. /* Verify the message using the ed448 public key.
  635. * Signature type is Ed448.
  636. *
  637. * sig [in] Signature to verify.
  638. * sigLen [in] Size of signature in bytes.
  639. * msg [in] Message to verify.
  640. * msgLen [in] Length of the message in bytes.
  641. * key [in] Ed448 key to use to verify.
  642. * context [in] Context of verification.
  643. * contextLen [in] Length of context in bytes.
  644. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  645. * context is not NULL or public key not set,
  646. * BUFFER_E when sigLen is less than ED448_SIG_SIZE,
  647. * other -ve values when hash fails,
  648. * 0 otherwise.
  649. */
  650. int wc_ed448_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  651. word32 msgLen, int* res, ed448_key* key,
  652. const byte* context, byte contextLen)
  653. {
  654. return wc_ed448_verify_msg_ex(sig, sigLen, msg, msgLen, res, key, Ed448,
  655. context, contextLen);
  656. }
  657. /* Verify the hash using the ed448 public key.
  658. * Signature type is Ed448ph.
  659. *
  660. * sig [in] Signature to verify.
  661. * sigLen [in] Size of signature in bytes.
  662. * hash [in] Hash of message to verify.
  663. * hashLen [in] Length of the hash in bytes.
  664. * key [in] Ed448 key to use to verify.
  665. * context [in] Context of verification.
  666. * contextLen [in] Length of context in bytes.
  667. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  668. * context is not NULL or public key not set,
  669. * BUFFER_E when sigLen is less than ED448_SIG_SIZE,
  670. * other -ve values when hash fails,
  671. * 0 otherwise.
  672. */
  673. int wc_ed448ph_verify_hash(const byte* sig, word32 sigLen, const byte* hash,
  674. word32 hashLen, int* res, ed448_key* key,
  675. const byte* context, byte contextLen)
  676. {
  677. return wc_ed448_verify_msg_ex(sig, sigLen, hash, hashLen, res, key, Ed448ph,
  678. context, contextLen);
  679. }
  680. /* Verify the message using the ed448 public key.
  681. * Signature type is Ed448ph.
  682. *
  683. * sig [in] Signature to verify.
  684. * sigLen [in] Size of signature in bytes.
  685. * msg [in] Message to verify.
  686. * msgLen [in] Length of the message in bytes.
  687. * key [in] Ed448 key to use to verify.
  688. * context [in] Context of verification.
  689. * contextLen [in] Length of context in bytes.
  690. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  691. * context is not NULL or public key not set,
  692. * BUFFER_E when sigLen is less than ED448_SIG_SIZE,
  693. * other -ve values when hash fails,
  694. * 0 otherwise.
  695. */
  696. int wc_ed448ph_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  697. word32 msgLen, int* res, ed448_key* key,
  698. const byte* context, byte contextLen)
  699. {
  700. int ret = 0;
  701. byte hash[ED448_PREHASH_SIZE];
  702. ret = ed448_hash(key, msg, msgLen, hash, sizeof(hash));
  703. if (ret == 0) {
  704. ret = wc_ed448ph_verify_hash(sig, sigLen, hash, sizeof(hash), res, key,
  705. context, contextLen);
  706. }
  707. return ret;
  708. }
  709. #endif /* HAVE_ED448_VERIFY */
  710. /* Initialize the ed448 private/public key.
  711. *
  712. * key [in] Ed448 key.
  713. * heap [in] heap pointer to pass to wc_InitShake256().
  714. * returns BAD_FUNC_ARG when key is NULL
  715. */
  716. int wc_ed448_init_ex(ed448_key* key, void *heap, int devId)
  717. {
  718. if (key == NULL)
  719. return BAD_FUNC_ARG;
  720. XMEMSET(key, 0, sizeof(ed448_key));
  721. #ifdef WOLF_CRYPTO_CB
  722. key->devId = devId;
  723. #else
  724. (void)devId;
  725. #endif
  726. key->heap = heap;
  727. fe448_init();
  728. #ifdef WOLFSSL_CHECK_MEM_ZERO
  729. wc_MemZero_Add("wc_ed448_init_ex key->k", &key->k, sizeof(key->k));
  730. #endif
  731. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  732. return ed448_hash_init(key, &key->sha);
  733. #else /* !WOLFSSL_ED448_PERSISTENT_SHA */
  734. return 0;
  735. #endif /* WOLFSSL_ED448_PERSISTENT_SHA */
  736. }
  737. /* Initialize the ed448 private/public key.
  738. *
  739. * key [in] Ed448 key.
  740. * returns BAD_FUNC_ARG when key is NULL
  741. */
  742. int wc_ed448_init(ed448_key* key) {
  743. return wc_ed448_init_ex(key, NULL, INVALID_DEVID);
  744. }
  745. /* Clears the ed448 key data
  746. *
  747. * key [in] Ed448 key.
  748. */
  749. void wc_ed448_free(ed448_key* key)
  750. {
  751. if (key != NULL) {
  752. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  753. ed448_hash_free(key, &key->sha);
  754. #endif
  755. ForceZero(key, sizeof(ed448_key));
  756. #ifdef WOLFSSL_CHECK_MEM_ZERO
  757. wc_MemZero_Check(key, sizeof(ed448_key));
  758. #endif
  759. }
  760. }
  761. #ifdef HAVE_ED448_KEY_EXPORT
  762. /* Export the ed448 public key.
  763. *
  764. * key [in] Ed448 public key.
  765. * out [in] Array to hold public key.
  766. * outLen [in/out] On in, the number of bytes in array.
  767. * On out, the number bytes put into array.
  768. * returns BAD_FUNC_ARG when a parameter is NULL,
  769. * ECC_BAD_ARG_E when outLen is less than ED448_PUB_KEY_SIZE,
  770. * 0 otherwise.
  771. */
  772. int wc_ed448_export_public(ed448_key* key, byte* out, word32* outLen)
  773. {
  774. int ret = 0;
  775. /* sanity check on arguments */
  776. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  777. ret = BAD_FUNC_ARG;
  778. }
  779. if ((ret == 0) && (*outLen < ED448_PUB_KEY_SIZE)) {
  780. *outLen = ED448_PUB_KEY_SIZE;
  781. ret = BUFFER_E;
  782. }
  783. if (ret == 0) {
  784. *outLen = ED448_PUB_KEY_SIZE;
  785. XMEMCPY(out, key->p, ED448_PUB_KEY_SIZE);
  786. }
  787. return ret;
  788. }
  789. #endif /* HAVE_ED448_KEY_EXPORT */
  790. #ifdef HAVE_ED448_KEY_IMPORT
  791. /* Import a compressed or uncompressed ed448 public key from a byte array.
  792. * Public key encoded in big-endian.
  793. *
  794. * in [in] Array holding public key.
  795. * inLen [in] Number of bytes of data in array.
  796. * key [in] Ed448 public key.
  797. * trusted [in] Indicates whether the public key data is trusted.
  798. * When 0, checks public key matches private key.
  799. * When 1, doesn't check public key matches private key.
  800. * returns BAD_FUNC_ARG when a parameter is NULL or key format is not supported,
  801. * 0 otherwise.
  802. */
  803. int wc_ed448_import_public_ex(const byte* in, word32 inLen, ed448_key* key,
  804. int trusted)
  805. {
  806. int ret = 0;
  807. /* sanity check on arguments */
  808. if ((in == NULL) || (key == NULL)) {
  809. ret = BAD_FUNC_ARG;
  810. }
  811. if (inLen != ED448_PUB_KEY_SIZE) {
  812. ret = BAD_FUNC_ARG;
  813. }
  814. if (ret == 0) {
  815. /* compressed prefix according to draft
  816. * https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-06 */
  817. if (in[0] == 0x40 && inLen > ED448_PUB_KEY_SIZE) {
  818. /* key is stored in compressed format so just copy in */
  819. XMEMCPY(key->p, (in + 1), ED448_PUB_KEY_SIZE);
  820. }
  821. /* importing uncompressed public key */
  822. else if (in[0] == 0x04 && inLen > 2*ED448_PUB_KEY_SIZE) {
  823. /* pass in (x,y) and store compressed key */
  824. ret = ge448_compress_key(key->p, in+1, in+1+ED448_PUB_KEY_SIZE);
  825. }
  826. else if (inLen == ED448_PUB_KEY_SIZE) {
  827. /* if not specified compressed or uncompressed check key size
  828. * if key size is equal to compressed key size copy in key */
  829. XMEMCPY(key->p, in, ED448_PUB_KEY_SIZE);
  830. }
  831. else {
  832. /* bad public key format */
  833. ret = BAD_FUNC_ARG;
  834. }
  835. }
  836. if (ret == 0) {
  837. key->pubKeySet = 1;
  838. if (key->privKeySet && (!trusted)) {
  839. /* Check untrusted public key data matches private key. */
  840. ret = wc_ed448_check_key(key);
  841. }
  842. }
  843. if ((ret != 0) && (key != NULL)) {
  844. /* No public key set on failure. */
  845. key->pubKeySet = 0;
  846. }
  847. return ret;
  848. }
  849. /* Import a compressed or uncompressed ed448 public key from a byte array.
  850. *
  851. * Public key encoded in big-endian.
  852. * Public key is not trusted and is checked against private key if set.
  853. *
  854. * in [in] Array holding public key.
  855. * inLen [in] Number of bytes of data in array.
  856. * key [in] Ed448 public key.
  857. * returns BAD_FUNC_ARG when a parameter is NULL or key format is not supported,
  858. * 0 otherwise.
  859. */
  860. int wc_ed448_import_public(const byte* in, word32 inLen, ed448_key* key)
  861. {
  862. return wc_ed448_import_public_ex(in, inLen, key, 0);
  863. }
  864. /* Import an ed448 private key from a byte array.
  865. *
  866. * priv [in] Array holding private key.
  867. * privSz [in] Number of bytes of data in array.
  868. * key [in] Ed448 private key.
  869. * returns BAD_FUNC_ARG when a parameter is NULL or privSz is less than
  870. * ED448_KEY_SIZE,
  871. * 0 otherwise.
  872. */
  873. int wc_ed448_import_private_only(const byte* priv, word32 privSz,
  874. ed448_key* key)
  875. {
  876. int ret = 0;
  877. /* sanity check on arguments */
  878. if ((priv == NULL) || (key == NULL)) {
  879. ret = BAD_FUNC_ARG;
  880. }
  881. /* key size check */
  882. if ((ret == 0) && (privSz != ED448_KEY_SIZE)) {
  883. ret = BAD_FUNC_ARG;
  884. }
  885. if (ret == 0) {
  886. XMEMCPY(key->k, priv, ED448_KEY_SIZE);
  887. key->privKeySet = 1;
  888. }
  889. if ((ret == 0) && key->pubKeySet) {
  890. /* Validate loaded public key */
  891. ret = wc_ed448_check_key(key);
  892. }
  893. if ((ret != 0) && (key != NULL)) {
  894. /* No private key set on error. */
  895. key->privKeySet = 0;
  896. ForceZero(key->k, ED448_KEY_SIZE);
  897. }
  898. return ret;
  899. }
  900. /* Import an ed448 private and public keys from byte array(s).
  901. *
  902. * priv [in] Array holding private key from wc_ed448_export_private_only(),
  903. * or private+public keys from wc_ed448_export_private().
  904. * privSz [in] Number of bytes of data in private key array.
  905. * pub [in] Array holding public key (or NULL).
  906. * pubSz [in] Number of bytes of data in public key array (or 0).
  907. * key [in] Ed448 private/public key.
  908. * trusted [in] Indicates whether the public key data is trusted.
  909. * When 0, checks public key matches private key.
  910. * When 1, doesn't check public key matches private key.
  911. * returns BAD_FUNC_ARG when a required parameter is NULL or an invalid
  912. * combination of keys/lengths is supplied, 0 otherwise.
  913. */
  914. int wc_ed448_import_private_key_ex(const byte* priv, word32 privSz,
  915. const byte* pub, word32 pubSz, ed448_key* key, int trusted)
  916. {
  917. int ret;
  918. /* sanity check on arguments */
  919. if (priv == NULL || key == NULL)
  920. return BAD_FUNC_ARG;
  921. /* key size check */
  922. if (privSz != ED448_KEY_SIZE && privSz != ED448_PRV_KEY_SIZE)
  923. return BAD_FUNC_ARG;
  924. if (pub == NULL) {
  925. if (pubSz != 0)
  926. return BAD_FUNC_ARG;
  927. if (privSz != ED448_PRV_KEY_SIZE)
  928. return BAD_FUNC_ARG;
  929. pub = priv + ED448_KEY_SIZE;
  930. pubSz = ED448_PUB_KEY_SIZE;
  931. }
  932. else if (pubSz < ED448_PUB_KEY_SIZE) {
  933. return BAD_FUNC_ARG;
  934. }
  935. XMEMCPY(key->k, priv, ED448_KEY_SIZE);
  936. key->privKeySet = 1;
  937. /* import public key */
  938. ret = wc_ed448_import_public_ex(pub, pubSz, key, trusted);
  939. if (ret != 0) {
  940. key->privKeySet = 0;
  941. ForceZero(key->k, ED448_KEY_SIZE);
  942. return ret;
  943. }
  944. /* make the private key (priv + pub) */
  945. XMEMCPY(key->k + ED448_KEY_SIZE, key->p, ED448_PUB_KEY_SIZE);
  946. return ret;
  947. }
  948. /* Import an ed448 private and public keys from byte array(s).
  949. *
  950. * Public key is not trusted and is checked against private key.
  951. *
  952. * priv [in] Array holding private key from wc_ed448_export_private_only(),
  953. * or private+public keys from wc_ed448_export_private().
  954. * privSz [in] Number of bytes of data in private key array.
  955. * pub [in] Array holding public key (or NULL).
  956. * pubSz [in] Number of bytes of data in public key array (or 0).
  957. * key [in] Ed448 private/public key.
  958. * returns BAD_FUNC_ARG when a required parameter is NULL or an invalid
  959. * combination of keys/lengths is supplied, 0 otherwise.
  960. */
  961. int wc_ed448_import_private_key(const byte* priv, word32 privSz,
  962. const byte* pub, word32 pubSz, ed448_key* key)
  963. {
  964. return wc_ed448_import_private_key_ex(priv, privSz, pub, pubSz, key, 0);
  965. }
  966. #endif /* HAVE_ED448_KEY_IMPORT */
  967. #ifdef HAVE_ED448_KEY_EXPORT
  968. /* Export the ed448 private key.
  969. *
  970. * key [in] Ed448 private key.
  971. * out [in] Array to hold private key.
  972. * outLen [in/out] On in, the number of bytes in array.
  973. * On out, the number bytes put into array.
  974. * returns BAD_FUNC_ARG when a parameter is NULL,
  975. * ECC_BAD_ARG_E when outLen is less than ED448_KEY_SIZE,
  976. * 0 otherwise.
  977. */
  978. int wc_ed448_export_private_only(ed448_key* key, byte* out, word32* outLen)
  979. {
  980. int ret = 0;
  981. /* sanity checks on arguments */
  982. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  983. ret = BAD_FUNC_ARG;
  984. }
  985. if ((ret == 0) && (*outLen < ED448_KEY_SIZE)) {
  986. *outLen = ED448_KEY_SIZE;
  987. ret = BUFFER_E;
  988. }
  989. if (ret == 0) {
  990. *outLen = ED448_KEY_SIZE;
  991. XMEMCPY(out, key->k, ED448_KEY_SIZE);
  992. }
  993. return ret;
  994. }
  995. /* Export the ed448 private and public key.
  996. *
  997. * key [in] Ed448 private/public key.
  998. * out [in] Array to hold private and public key.
  999. * outLen [in/out] On in, the number of bytes in array.
  1000. * On out, the number bytes put into array.
  1001. * returns BAD_FUNC_ARG when a parameter is NULL,
  1002. * BUFFER_E when outLen is less than ED448_PRV_KEY_SIZE,
  1003. * 0 otherwise.
  1004. */
  1005. int wc_ed448_export_private(ed448_key* key, byte* out, word32* outLen)
  1006. {
  1007. int ret = 0;
  1008. /* sanity checks on arguments */
  1009. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  1010. ret = BAD_FUNC_ARG;
  1011. }
  1012. if ((ret == 0) && (*outLen < ED448_PRV_KEY_SIZE)) {
  1013. *outLen = ED448_PRV_KEY_SIZE;
  1014. ret = BUFFER_E;
  1015. }
  1016. if (ret == 0) {
  1017. *outLen = ED448_PRV_KEY_SIZE;
  1018. XMEMCPY(out, key->k, ED448_PRV_KEY_SIZE);
  1019. }
  1020. return ret;
  1021. }
  1022. /* Export the ed448 private and public key.
  1023. *
  1024. * key [in] Ed448 private/public key.
  1025. * priv [in] Array to hold private key.
  1026. * privSz [in/out] On in, the number of bytes in private key array.
  1027. * pub [in] Array to hold public key.
  1028. * pubSz [in/out] On in, the number of bytes in public key array.
  1029. * On out, the number bytes put into array.
  1030. * returns BAD_FUNC_ARG when a parameter is NULL,
  1031. * BUFFER_E when privSz is less than ED448_PRV_KEY_SIZE or pubSz is less
  1032. * than ED448_PUB_KEY_SIZE,
  1033. * 0 otherwise.
  1034. */
  1035. int wc_ed448_export_key(ed448_key* key, byte* priv, word32 *privSz,
  1036. byte* pub, word32 *pubSz)
  1037. {
  1038. int ret = 0;
  1039. /* export 'full' private part */
  1040. ret = wc_ed448_export_private(key, priv, privSz);
  1041. if (ret == 0) {
  1042. /* export public part */
  1043. ret = wc_ed448_export_public(key, pub, pubSz);
  1044. }
  1045. return ret;
  1046. }
  1047. #endif /* HAVE_ED448_KEY_EXPORT */
  1048. /* Check the public key of the ed448 key matches the private key.
  1049. *
  1050. * key [in] Ed448 private/public key.
  1051. * returns BAD_FUNC_ARG when key is NULL,
  1052. * PUBLIC_KEY_E when the public key is not set or doesn't match,
  1053. * other -ve value on hash failure,
  1054. * 0 otherwise.
  1055. */
  1056. int wc_ed448_check_key(ed448_key* key)
  1057. {
  1058. int ret = 0;
  1059. unsigned char pubKey[ED448_PUB_KEY_SIZE];
  1060. if (key == NULL) {
  1061. ret = BAD_FUNC_ARG;
  1062. }
  1063. if (ret == 0 && !key->pubKeySet) {
  1064. ret = PUBLIC_KEY_E;
  1065. }
  1066. if (ret == 0) {
  1067. ret = wc_ed448_make_public(key, pubKey, sizeof(pubKey));
  1068. }
  1069. if ((ret == 0) && (XMEMCMP(pubKey, key->p, ED448_PUB_KEY_SIZE) != 0)) {
  1070. ret = PUBLIC_KEY_E;
  1071. }
  1072. return ret;
  1073. }
  1074. /* Returns the size of an ed448 private key.
  1075. *
  1076. * key [in] Ed448 private/public key.
  1077. * returns BAD_FUNC_ARG when key is NULL,
  1078. * ED448_KEY_SIZE otherwise.
  1079. */
  1080. int wc_ed448_size(ed448_key* key)
  1081. {
  1082. int ret = ED448_KEY_SIZE;
  1083. if (key == NULL) {
  1084. ret = BAD_FUNC_ARG;
  1085. }
  1086. return ret;
  1087. }
  1088. /* Returns the size of an ed448 private plus public key.
  1089. *
  1090. * key [in] Ed448 private/public key.
  1091. * returns BAD_FUNC_ARG when key is NULL,
  1092. * ED448_PRV_KEY_SIZE otherwise.
  1093. */
  1094. int wc_ed448_priv_size(ed448_key* key)
  1095. {
  1096. int ret = ED448_PRV_KEY_SIZE;
  1097. if (key == NULL) {
  1098. ret = BAD_FUNC_ARG;
  1099. }
  1100. return ret;
  1101. }
  1102. /* Returns the size of an ed448 public key.
  1103. *
  1104. * key [in] Ed448 private/public key.
  1105. * returns BAD_FUNC_ARG when key is NULL,
  1106. * ED448_PUB_KEY_SIZE otherwise.
  1107. */
  1108. int wc_ed448_pub_size(ed448_key* key)
  1109. {
  1110. int ret = ED448_PUB_KEY_SIZE;
  1111. if (key == NULL) {
  1112. ret = BAD_FUNC_ARG;
  1113. }
  1114. return ret;
  1115. }
  1116. /* Returns the size of an ed448 signature.
  1117. *
  1118. * key [in] Ed448 private/public key.
  1119. * returns BAD_FUNC_ARG when key is NULL,
  1120. * ED448_SIG_SIZE otherwise.
  1121. */
  1122. int wc_ed448_sig_size(ed448_key* key)
  1123. {
  1124. int ret = ED448_SIG_SIZE;
  1125. if (key == NULL) {
  1126. ret = BAD_FUNC_ARG;
  1127. }
  1128. return ret;
  1129. }
  1130. #endif /* HAVE_ED448 */