ed448.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. /* ed448.c
  2. *
  3. * Copyright (C) 2006-2021 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 te 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)
  162. ret = ed448_hash(key, key->k, ED448_KEY_SIZE, az, sizeof(az));
  163. if (ret == 0) {
  164. /* apply clamp */
  165. az[0] &= 0xfc;
  166. az[55] |= 0x80;
  167. az[56] = 0x00;
  168. ge448_scalarmult_base(&A, az);
  169. ge448_to_bytes(pubKey, &A);
  170. }
  171. return ret;
  172. }
  173. /* Make a new ed448 private/public key.
  174. *
  175. * rng [in] Random number generator.
  176. * keysize [in] Size of the key to generate.
  177. * key [in] Ed448 key object.
  178. * returns BAD_FUNC_ARG when rng or key is NULL or keySz is not equal to
  179. * ED448_KEY_SIZE,
  180. * other -ve value on random number or hash failure,
  181. * 0 otherwise.
  182. */
  183. int wc_ed448_make_key(WC_RNG* rng, int keySz, ed448_key* key)
  184. {
  185. int ret = 0;
  186. if ((rng == NULL) || (key == NULL)) {
  187. ret = BAD_FUNC_ARG;
  188. }
  189. /* ed448 has 57 byte key sizes */
  190. if ((ret == 0) && (keySz != ED448_KEY_SIZE)) {
  191. ret = BAD_FUNC_ARG;
  192. }
  193. if (ret == 0) {
  194. ret = wc_RNG_GenerateBlock(rng, key->k, ED448_KEY_SIZE);
  195. }
  196. if (ret == 0) {
  197. ret = wc_ed448_make_public(key, key->p, ED448_PUB_KEY_SIZE);
  198. if (ret != 0) {
  199. ForceZero(key->k, ED448_KEY_SIZE);
  200. }
  201. }
  202. if (ret == 0) {
  203. /* put public key after private key, on the same buffer */
  204. XMEMMOVE(key->k + ED448_KEY_SIZE, key->p, ED448_PUB_KEY_SIZE);
  205. key->pubKeySet = 1;
  206. }
  207. return ret;
  208. }
  209. #ifdef HAVE_ED448_SIGN
  210. /* Sign the message using the ed448 private key.
  211. *
  212. * in [in] Message to sign.
  213. * inLen [in] Length of the message in bytes.
  214. * out [in] Buffer to write signature into.
  215. * outLen [in/out] On in, size of buffer.
  216. * On out, the length of the signature in bytes.
  217. * key [in] Ed448 key to use when signing
  218. * type [in] Type of signature to perform: Ed448 or Ed448ph
  219. * context [in] Context of signing.
  220. * contextLen [in] Length of context in bytes.
  221. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  222. * context is not NULL or public key not set,
  223. * BUFFER_E when outLen is less than ED448_SIG_SIZE,
  224. * other -ve values when hash fails,
  225. * 0 otherwise.
  226. */
  227. int wc_ed448_sign_msg_ex(const byte* in, word32 inLen, byte* out,
  228. word32 *outLen, ed448_key* key, byte type,
  229. const byte* context, byte contextLen)
  230. {
  231. ge448_p2 R;
  232. byte nonce[ED448_SIG_SIZE];
  233. byte hram[ED448_SIG_SIZE];
  234. byte az[ED448_PRV_KEY_SIZE];
  235. int ret = 0;
  236. /* sanity check on arguments */
  237. if ((in == NULL) || (out == NULL) || (outLen == NULL) || (key == NULL) ||
  238. ((context == NULL) && (contextLen != 0))) {
  239. ret = BAD_FUNC_ARG;
  240. }
  241. if ((ret == 0) && (!key->pubKeySet)) {
  242. ret = BAD_FUNC_ARG;
  243. }
  244. /* check and set up out length */
  245. if ((ret == 0) && (*outLen < ED448_SIG_SIZE)) {
  246. *outLen = ED448_SIG_SIZE;
  247. ret = BUFFER_E;
  248. }
  249. if (ret == 0) {
  250. *outLen = ED448_SIG_SIZE;
  251. /* step 1: create nonce to use where nonce is r in
  252. r = H(h_b, ... ,h_2b-1,M) */
  253. ret = ed448_hash(key, key->k, ED448_KEY_SIZE, az, sizeof(az));
  254. }
  255. if (ret == 0) {
  256. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  257. wc_Shake *sha = &key->sha;
  258. #else
  259. wc_Shake sha[1];
  260. ret = ed448_hash_init(key, sha);
  261. if (ret < 0)
  262. return ret;
  263. #endif
  264. /* apply clamp */
  265. az[0] &= 0xfc;
  266. az[55] |= 0x80;
  267. az[56] = 0x00;
  268. ret = ed448_hash_update(key, sha, ed448Ctx, ED448CTX_SIZE);
  269. if (ret == 0) {
  270. ret = ed448_hash_update(key, sha, &type, sizeof(type));
  271. }
  272. if (ret == 0) {
  273. ret = ed448_hash_update(key, sha, &contextLen, sizeof(contextLen));
  274. }
  275. if ((ret == 0) && (context != NULL)) {
  276. ret = ed448_hash_update(key, sha, context, contextLen);
  277. }
  278. if (ret == 0) {
  279. ret = ed448_hash_update(key, sha, az + ED448_KEY_SIZE, ED448_KEY_SIZE);
  280. }
  281. if (ret == 0) {
  282. ret = ed448_hash_update(key, sha, in, inLen);
  283. }
  284. if (ret == 0) {
  285. ret = ed448_hash_final(key, sha, nonce, sizeof(nonce));
  286. }
  287. #ifndef WOLFSSL_ED448_PERSISTENT_SHA
  288. ed448_hash_free(key, sha);
  289. #endif
  290. }
  291. if (ret == 0) {
  292. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  293. wc_Shake *sha = &key->sha;
  294. #else
  295. wc_Shake sha[1];
  296. ret = ed448_hash_init(key, sha);
  297. if (ret < 0)
  298. return ret;
  299. #endif
  300. sc448_reduce(nonce);
  301. /* step 2: computing R = rB where rB is the scalar multiplication of
  302. r and B */
  303. ge448_scalarmult_base(&R,nonce);
  304. ge448_to_bytes(out,&R);
  305. /* step 3: hash R + public key + message getting H(R,A,M) then
  306. creating S = (r + H(R,A,M)a) mod l */
  307. ret = ed448_hash_update(key, sha, ed448Ctx, ED448CTX_SIZE);
  308. if (ret == 0) {
  309. ret = ed448_hash_update(key, sha, &type, sizeof(type));
  310. }
  311. if (ret == 0) {
  312. ret = ed448_hash_update(key, sha, &contextLen, sizeof(contextLen));
  313. }
  314. if ((ret == 0) && (context != NULL)) {
  315. ret = ed448_hash_update(key, sha, context, contextLen);
  316. }
  317. if (ret == 0) {
  318. ret = ed448_hash_update(key, sha, out, ED448_SIG_SIZE/2);
  319. }
  320. if (ret == 0) {
  321. ret = ed448_hash_update(key, sha, key->p, ED448_PUB_KEY_SIZE);
  322. }
  323. if (ret == 0) {
  324. ret = ed448_hash_update(key, sha, in, inLen);
  325. }
  326. if (ret == 0) {
  327. ret = ed448_hash_final(key, sha, hram, sizeof(hram));
  328. }
  329. #ifndef WOLFSSL_ED448_PERSISTENT_SHA
  330. ed448_hash_free(key, sha);
  331. #endif
  332. }
  333. if (ret == 0) {
  334. sc448_reduce(hram);
  335. sc448_muladd(out + (ED448_SIG_SIZE/2), hram, az, nonce);
  336. }
  337. return ret;
  338. }
  339. /* Sign the message using the ed448 private key.
  340. * Signature type is Ed448.
  341. *
  342. * in [in] Message to sign.
  343. * inLen [in] Length of the message in bytes.
  344. * out [in] Buffer to write signature into.
  345. * outLen [in/out] On in, size of buffer.
  346. * On out, the length of the signature in bytes.
  347. * key [in] Ed448 key to use when signing
  348. * context [in] Context of signing.
  349. * contextLen [in] Length of context in bytes.
  350. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  351. * context is not NULL or public key not set,
  352. * BUFFER_E when outLen is less than ED448_SIG_SIZE,
  353. * other -ve values when hash fails,
  354. * 0 otherwise.
  355. */
  356. int wc_ed448_sign_msg(const byte* in, word32 inLen, byte* out, word32 *outLen,
  357. ed448_key* key, const byte* context, byte contextLen)
  358. {
  359. return wc_ed448_sign_msg_ex(in, inLen, out, outLen, key, Ed448, context,
  360. contextLen);
  361. }
  362. /* Sign the hash using the ed448 private key.
  363. * Signature type is Ed448ph.
  364. *
  365. * hash [in] Hash of message to sign.
  366. * hashLen [in] Length of hash of message in bytes.
  367. * out [in] Buffer to write signature into.
  368. * outLen [in/out] On in, size of buffer.
  369. * On out, the length of the signature in bytes.
  370. * key [in] Ed448 key to use when signing
  371. * context [in] Context of signing.
  372. * contextLen [in] Length of context in bytes.
  373. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  374. * context is not NULL or public key not set,
  375. * BUFFER_E when outLen is less than ED448_SIG_SIZE,
  376. * other -ve values when hash fails,
  377. * 0 otherwise.
  378. */
  379. int wc_ed448ph_sign_hash(const byte* hash, word32 hashLen, byte* out,
  380. word32 *outLen, ed448_key* key,
  381. const byte* context, byte contextLen)
  382. {
  383. return wc_ed448_sign_msg_ex(hash, hashLen, out, outLen, key, Ed448ph,
  384. context, contextLen);
  385. }
  386. /* Sign the message using the ed448 private key.
  387. * Signature type is Ed448ph.
  388. *
  389. * in [in] Message to sign.
  390. * inLen [in] Length of the message to sign in bytes.
  391. * out [in] Buffer to write signature into.
  392. * outLen [in/out] On in, size of buffer.
  393. * On out, the length of the signature in bytes.
  394. * key [in] Ed448 key to use when signing
  395. * context [in] Context of signing.
  396. * contextLen [in] Length of context in bytes.
  397. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  398. * context is not NULL or public key not set,
  399. * BUFFER_E when outLen is less than ED448_SIG_SIZE,
  400. * other -ve values when hash fails,
  401. * 0 otherwise.
  402. */
  403. int wc_ed448ph_sign_msg(const byte* in, word32 inLen, byte* out, word32 *outLen,
  404. ed448_key* key, const byte* context, byte contextLen)
  405. {
  406. int ret;
  407. byte hash[ED448_PREHASH_SIZE];
  408. ret = ed448_hash(key, in, inLen, hash, sizeof(hash));
  409. if (ret == 0) {
  410. ret = wc_ed448ph_sign_hash(hash, sizeof(hash), out, outLen, key,
  411. context, contextLen);
  412. }
  413. return ret;
  414. }
  415. #endif /* HAVE_ED448_SIGN */
  416. #ifdef HAVE_ED448_VERIFY
  417. /* Verify the message using the ed448 public key.
  418. *
  419. * sig [in] Signature to verify.
  420. * sigLen [in] Size of signature in bytes.
  421. * key [in] Ed448 key to use to verify.
  422. * type [in] Type of signature to verify: Ed448 or Ed448ph
  423. * context [in] Context of verification.
  424. * contextLen [in] Length of context in bytes.
  425. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  426. * context is not NULL or public key not set,
  427. * BUFFER_E when sigLen is less than ED448_SIG_SIZE,
  428. * other -ve values when hash fails,
  429. * 0 otherwise.
  430. */
  431. static int ed448_verify_msg_init_with_sha(const byte* sig, word32 sigLen,
  432. ed448_key* key, wc_Shake *sha, byte type,
  433. const byte* context, byte contextLen)
  434. {
  435. int ret;
  436. /* sanity check on arguments */
  437. if ((sig == NULL) || (key == NULL) ||
  438. ((context == NULL) && (contextLen != 0))) {
  439. return BAD_FUNC_ARG;
  440. }
  441. /* check on basics needed to verify signature */
  442. if (sigLen != ED448_SIG_SIZE) {
  443. return BAD_FUNC_ARG;
  444. }
  445. /* find H(R,A,M) and store it as h */
  446. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  447. ret = ed448_hash_reset(key);
  448. if (ret < 0)
  449. return ret;
  450. #endif
  451. ret = ed448_hash_update(key, sha, ed448Ctx, ED448CTX_SIZE);
  452. if (ret == 0) {
  453. ret = ed448_hash_update(key, sha, &type, sizeof(type));
  454. }
  455. if (ret == 0) {
  456. ret = ed448_hash_update(key, sha, &contextLen, sizeof(contextLen));
  457. }
  458. if ((ret == 0) && (context != NULL)) {
  459. ret = ed448_hash_update(key, sha, context, contextLen);
  460. }
  461. if (ret == 0) {
  462. ret = ed448_hash_update(key, sha, sig, ED448_SIG_SIZE/2);
  463. }
  464. if (ret == 0) {
  465. ret = ed448_hash_update(key, sha, key->p, ED448_PUB_KEY_SIZE);
  466. }
  467. return ret;
  468. }
  469. /*
  470. msgSegment an array of bytes containing a message segment
  471. msgSegmentLen length of msgSegment
  472. key Ed448 public key
  473. return 0 on success
  474. */
  475. static int ed448_verify_msg_update_with_sha(const byte* msgSegment,
  476. word32 msgSegmentLen,
  477. ed448_key* key,
  478. wc_Shake *sha)
  479. {
  480. /* sanity check on arguments */
  481. if (msgSegment == NULL || key == NULL)
  482. return BAD_FUNC_ARG;
  483. return ed448_hash_update(key, sha, msgSegment, msgSegmentLen);
  484. }
  485. /* Verify the message using the ed448 public key.
  486. *
  487. * sig [in] Signature to verify.
  488. * sigLen [in] Size of signature in bytes.
  489. * res [out] *res is set to 1 on successful verification.
  490. * key [in] Ed448 key to use to verify.
  491. * returns BAD_FUNC_ARG when a parameter is NULL or public key not set,
  492. * BUFFER_E when sigLen is less than ED448_SIG_SIZE,
  493. * other -ve values when hash fails,
  494. * 0 otherwise.
  495. */
  496. static int ed448_verify_msg_final_with_sha(const byte* sig, word32 sigLen,
  497. int* res, ed448_key* key, wc_Shake *sha)
  498. {
  499. byte rcheck[ED448_KEY_SIZE];
  500. byte h[ED448_SIG_SIZE];
  501. ge448_p2 A;
  502. ge448_p2 R;
  503. int ret;
  504. /* sanity check on arguments */
  505. if ((sig == NULL) || (res == NULL) || (key == NULL))
  506. return BAD_FUNC_ARG;
  507. /* set verification failed by default */
  508. *res = 0;
  509. /* check on basics needed to verify signature */
  510. if (sigLen != ED448_SIG_SIZE)
  511. return BAD_FUNC_ARG;
  512. /* uncompress A (public key), test if valid, and negate it */
  513. if (ge448_from_bytes_negate_vartime(&A, key->p) != 0)
  514. return BAD_FUNC_ARG;
  515. ret = ed448_hash_final(key, sha, h, sizeof(h));
  516. if (ret != 0)
  517. return ret;
  518. sc448_reduce(h);
  519. /* Uses a fast single-signature verification SB = R + H(R,A,M)A becomes
  520. * SB - H(R,A,M)A saving decompression of R
  521. */
  522. ret = ge448_double_scalarmult_vartime(&R, h, &A,
  523. sig + (ED448_SIG_SIZE/2));
  524. if (ret != 0)
  525. return ret;
  526. ge448_to_bytes(rcheck, &R);
  527. /* comparison of R created to R in sig */
  528. if (ConstantCompare(rcheck, sig, ED448_SIG_SIZE/2) != 0) {
  529. ret = SIG_VERIFY_E;
  530. }
  531. else {
  532. /* set the verification status */
  533. *res = 1;
  534. }
  535. return ret;
  536. }
  537. #ifdef WOLFSSL_ED448_STREAMING_VERIFY
  538. int wc_ed448_verify_msg_init(const byte* sig, word32 sigLen, ed448_key* key,
  539. byte type, const byte* context, byte contextLen)
  540. {
  541. return ed448_verify_msg_init_with_sha(sig, sigLen, key, &key->sha, type,
  542. context, contextLen);
  543. }
  544. int wc_ed448_verify_msg_update(const byte* msgSegment, word32 msgSegmentLen,
  545. ed448_key* key)
  546. {
  547. return ed448_verify_msg_update_with_sha(msgSegment, msgSegmentLen, key,
  548. &key->sha);
  549. }
  550. int wc_ed448_verify_msg_final(const byte* sig, word32 sigLen,
  551. int* res, ed448_key* key)
  552. {
  553. return ed448_verify_msg_final_with_sha(sig, sigLen, res, key, &key->sha);
  554. }
  555. #endif
  556. /* Verify the message using the ed448 public key.
  557. *
  558. * sig [in] Signature to verify.
  559. * sigLen [in] Size of signature in bytes.
  560. * msg [in] Message to verify.
  561. * msgLen [in] Length of the message in bytes.
  562. * res [out] *res is set to 1 on successful verification.
  563. * key [in] Ed448 key to use to verify.
  564. * type [in] Type of signature to verify: Ed448 or Ed448ph
  565. * context [in] Context of verification.
  566. * contextLen [in] Length of context in bytes.
  567. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  568. * context is not NULL or public key not set,
  569. * BUFFER_E when sigLen is less than ED448_SIG_SIZE,
  570. * other -ve values when hash fails,
  571. * 0 otherwise.
  572. */
  573. int wc_ed448_verify_msg_ex(const byte* sig, word32 sigLen, const byte* msg,
  574. word32 msgLen, int* res, ed448_key* key,
  575. byte type, const byte* context, byte contextLen)
  576. {
  577. int ret;
  578. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  579. wc_Shake *sha;
  580. #else
  581. wc_Shake sha[1];
  582. #endif
  583. if (key == NULL)
  584. return BAD_FUNC_ARG;
  585. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  586. sha = &key->sha;
  587. #else
  588. ret = ed448_hash_init(key, sha);
  589. if (ret < 0)
  590. return ret;
  591. #endif
  592. ret = ed448_verify_msg_init_with_sha(sig, sigLen, key, sha,
  593. type, context, contextLen);
  594. if (ret == 0)
  595. ret = ed448_verify_msg_update_with_sha(msg, msgLen, key, sha);
  596. if (ret == 0)
  597. ret = ed448_verify_msg_final_with_sha(sig, sigLen, res, key, sha);
  598. #ifndef WOLFSSL_ED448_PERSISTENT_SHA
  599. ed448_hash_free(key, sha);
  600. #endif
  601. return ret;
  602. }
  603. /* Verify the message using the ed448 public key.
  604. * Signature type is Ed448.
  605. *
  606. * sig [in] Signature to verify.
  607. * sigLen [in] Size of signature in bytes.
  608. * msg [in] Message to verify.
  609. * msgLen [in] Length of the message in bytes.
  610. * key [in] Ed448 key to use to verify.
  611. * context [in] Context of verification.
  612. * contextLen [in] Length of context in bytes.
  613. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  614. * context is not NULL or public key not set,
  615. * BUFFER_E when sigLen is less than ED448_SIG_SIZE,
  616. * other -ve values when hash fails,
  617. * 0 otherwise.
  618. */
  619. int wc_ed448_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  620. word32 msgLen, int* res, ed448_key* key,
  621. const byte* context, byte contextLen)
  622. {
  623. return wc_ed448_verify_msg_ex(sig, sigLen, msg, msgLen, res, key, Ed448,
  624. context, contextLen);
  625. }
  626. /* Verify the hash using the ed448 public key.
  627. * Signature type is Ed448ph.
  628. *
  629. * sig [in] Signature to verify.
  630. * sigLen [in] Size of signature in bytes.
  631. * hash [in] Hash of message to verify.
  632. * hashLen [in] Length of the hash in bytes.
  633. * key [in] Ed448 key to use to verify.
  634. * context [in] Context of verification.
  635. * contextLen [in] Length of context in bytes.
  636. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  637. * context is not NULL or public key not set,
  638. * BUFFER_E when sigLen is less than ED448_SIG_SIZE,
  639. * other -ve values when hash fails,
  640. * 0 otherwise.
  641. */
  642. int wc_ed448ph_verify_hash(const byte* sig, word32 sigLen, const byte* hash,
  643. word32 hashLen, int* res, ed448_key* key,
  644. const byte* context, byte contextLen)
  645. {
  646. return wc_ed448_verify_msg_ex(sig, sigLen, hash, hashLen, res, key, Ed448ph,
  647. context, contextLen);
  648. }
  649. /* Verify the message using the ed448 public key.
  650. * Signature type is Ed448ph.
  651. *
  652. * sig [in] Signature to verify.
  653. * sigLen [in] Size of signature in bytes.
  654. * msg [in] Message to verify.
  655. * msgLen [in] Length of the message in bytes.
  656. * key [in] Ed448 key to use to verify.
  657. * context [in] Context of verification.
  658. * contextLen [in] Length of context in bytes.
  659. * returns BAD_FUNC_ARG when a parameter is NULL or contextLen is zero when and
  660. * context is not NULL or public key not set,
  661. * BUFFER_E when sigLen is less than ED448_SIG_SIZE,
  662. * other -ve values when hash fails,
  663. * 0 otherwise.
  664. */
  665. int wc_ed448ph_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  666. word32 msgLen, int* res, ed448_key* key,
  667. const byte* context, byte contextLen)
  668. {
  669. int ret = 0;
  670. byte hash[ED448_PREHASH_SIZE];
  671. ret = ed448_hash(key, msg, msgLen, hash, sizeof(hash));
  672. if (ret == 0) {
  673. ret = wc_ed448ph_verify_hash(sig, sigLen, hash, sizeof(hash), res, key,
  674. context, contextLen);
  675. }
  676. return ret;
  677. }
  678. #endif /* HAVE_ED448_VERIFY */
  679. /* Initialize the ed448 private/public key.
  680. *
  681. * key [in] Ed448 key.
  682. * heap [in] heap pointer to pass to wc_InitShake256().
  683. * returns BAD_FUNC_ARG when key is NULL
  684. */
  685. int wc_ed448_init_ex(ed448_key* key, void *heap, int devId)
  686. {
  687. if (key == NULL)
  688. return BAD_FUNC_ARG;
  689. XMEMSET(key, 0, sizeof(ed448_key));
  690. #ifdef WOLF_CRYPTO_CB
  691. key->devId = devId;
  692. #else
  693. (void)devId;
  694. #endif
  695. key->heap = heap;
  696. fe448_init();
  697. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  698. return ed448_hash_init(key, &key->sha);
  699. #else /* !WOLFSSL_ED448_PERSISTENT_SHA */
  700. return 0;
  701. #endif /* WOLFSSL_ED448_PERSISTENT_SHA */
  702. }
  703. /* Initialize the ed448 private/public key.
  704. *
  705. * key [in] Ed448 key.
  706. * returns BAD_FUNC_ARG when key is NULL
  707. */
  708. int wc_ed448_init(ed448_key* key) {
  709. return wc_ed448_init_ex(key, NULL, INVALID_DEVID);
  710. }
  711. /* Clears the ed448 key data
  712. *
  713. * key [in] Ed448 key.
  714. */
  715. void wc_ed448_free(ed448_key* key)
  716. {
  717. if (key != NULL) {
  718. #ifdef WOLFSSL_ED448_PERSISTENT_SHA
  719. ed448_hash_free(key, &key->sha);
  720. #endif
  721. ForceZero(key, sizeof(ed448_key));
  722. }
  723. }
  724. #ifdef HAVE_ED448_KEY_EXPORT
  725. /* Export the ed448 public key.
  726. *
  727. * key [in] Ed448 public key.
  728. * out [in] Array to hold public key.
  729. * outLen [in/out] On in, the number of bytes in array.
  730. * On out, the number bytes put into array.
  731. * returns BAD_FUNC_ARG when a parameter is NULL,
  732. * ECC_BAD_ARG_E when outLen is less than ED448_PUB_KEY_SIZE,
  733. * 0 otherwise.
  734. */
  735. int wc_ed448_export_public(ed448_key* key, byte* out, word32* outLen)
  736. {
  737. int ret = 0;
  738. /* sanity check on arguments */
  739. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  740. ret = BAD_FUNC_ARG;
  741. }
  742. if ((ret == 0) && (*outLen < ED448_PUB_KEY_SIZE)) {
  743. *outLen = ED448_PUB_KEY_SIZE;
  744. ret = BUFFER_E;
  745. }
  746. if (ret == 0) {
  747. *outLen = ED448_PUB_KEY_SIZE;
  748. XMEMCPY(out, key->p, ED448_PUB_KEY_SIZE);
  749. }
  750. return ret;
  751. }
  752. #endif /* HAVE_ED448_KEY_EXPORT */
  753. #ifdef HAVE_ED448_KEY_IMPORT
  754. /* Import a compressed or uncompressed ed448 public key from a byte array.
  755. * Public key encoded in big-endian.
  756. *
  757. * in [in] Array holding public key.
  758. * inLen [in] Number of bytes of data in array.
  759. * key [in] Ed448 public key.
  760. * returns BAD_FUNC_ARG when a parameter is NULL or key format is not supported,
  761. * 0 otherwise.
  762. */
  763. int wc_ed448_import_public(const byte* in, word32 inLen, ed448_key* key)
  764. {
  765. int ret = 0;
  766. /* sanity check on arguments */
  767. if ((in == NULL) || (key == NULL)) {
  768. ret = BAD_FUNC_ARG;
  769. }
  770. if (inLen < ED448_PUB_KEY_SIZE) {
  771. ret = BAD_FUNC_ARG;
  772. }
  773. if (ret == 0) {
  774. /* compressed prefix according to draft
  775. * https://tools.ietf.org/html/draft-ietf-openpgp-rfc4880bis-06 */
  776. if (in[0] == 0x40 && inLen > ED448_PUB_KEY_SIZE) {
  777. /* key is stored in compressed format so just copy in */
  778. XMEMCPY(key->p, (in + 1), ED448_PUB_KEY_SIZE);
  779. key->pubKeySet = 1;
  780. }
  781. /* importing uncompressed public key */
  782. else if (in[0] == 0x04 && inLen > 2*ED448_PUB_KEY_SIZE) {
  783. /* pass in (x,y) and store compressed key */
  784. ret = ge448_compress_key(key->p, in+1, in+1+ED448_PUB_KEY_SIZE);
  785. if (ret == 0)
  786. key->pubKeySet = 1;
  787. }
  788. else if (inLen == ED448_PUB_KEY_SIZE) {
  789. /* if not specified compressed or uncompressed check key size
  790. * if key size is equal to compressed key size copy in key */
  791. XMEMCPY(key->p, in, ED448_PUB_KEY_SIZE);
  792. key->pubKeySet = 1;
  793. }
  794. else {
  795. /* bad public key format */
  796. ret = BAD_FUNC_ARG;
  797. }
  798. }
  799. return ret;
  800. }
  801. /* Import an ed448 private key from a byte array.
  802. *
  803. * priv [in] Array holding private key.
  804. * privSz [in] Number of bytes of data in array.
  805. * key [in] Ed448 private key.
  806. * returns BAD_FUNC_ARG when a parameter is NULL or privSz is less than
  807. * ED448_KEY_SIZE,
  808. * 0 otherwise.
  809. */
  810. int wc_ed448_import_private_only(const byte* priv, word32 privSz,
  811. ed448_key* key)
  812. {
  813. int ret = 0;
  814. /* sanity check on arguments */
  815. if ((priv == NULL) || (key == NULL)) {
  816. ret = BAD_FUNC_ARG;
  817. }
  818. /* key size check */
  819. if ((ret == 0) && (privSz < ED448_KEY_SIZE)) {
  820. ret = BAD_FUNC_ARG;
  821. }
  822. if (ret == 0) {
  823. XMEMCPY(key->k, priv, ED448_KEY_SIZE);
  824. }
  825. return ret;
  826. }
  827. /* Import an ed448 private and public keys from byte array(s).
  828. *
  829. * priv [in] Array holding private key from wc_ed448_export_private_only(),
  830. * or private+public keys from wc_ed448_export_private().
  831. * privSz [in] Number of bytes of data in private key array.
  832. * pub [in] Array holding public key (or NULL).
  833. * pubSz [in] Number of bytes of data in public key array (or 0).
  834. * key [in] Ed448 private/public key.
  835. * returns BAD_FUNC_ARG when a required parameter is NULL or an invalid
  836. * combination of keys/lengths is supplied, 0 otherwise.
  837. */
  838. int wc_ed448_import_private_key(const byte* priv, word32 privSz,
  839. const byte* pub, word32 pubSz, ed448_key* key)
  840. {
  841. int ret;
  842. /* sanity check on arguments */
  843. if (priv == NULL || key == NULL)
  844. return BAD_FUNC_ARG;
  845. /* key size check */
  846. if (privSz < ED448_KEY_SIZE)
  847. return BAD_FUNC_ARG;
  848. if (pub == NULL) {
  849. if (pubSz != 0)
  850. return BAD_FUNC_ARG;
  851. if (privSz < ED448_PRV_KEY_SIZE)
  852. return BAD_FUNC_ARG;
  853. pub = priv + ED448_KEY_SIZE;
  854. pubSz = ED448_PUB_KEY_SIZE;
  855. } else if (pubSz < ED448_PUB_KEY_SIZE) {
  856. return BAD_FUNC_ARG;
  857. }
  858. /* import public key */
  859. ret = wc_ed448_import_public(pub, pubSz, key);
  860. if (ret != 0)
  861. return ret;
  862. /* make the private key (priv + pub) */
  863. XMEMCPY(key->k, priv, ED448_KEY_SIZE);
  864. XMEMCPY(key->k + ED448_KEY_SIZE, key->p, ED448_PUB_KEY_SIZE);
  865. return ret;
  866. }
  867. #endif /* HAVE_ED448_KEY_IMPORT */
  868. #ifdef HAVE_ED448_KEY_EXPORT
  869. /* Export the ed448 private key.
  870. *
  871. * key [in] Ed448 private key.
  872. * out [in] Array to hold private key.
  873. * outLen [in/out] On in, the number of bytes in array.
  874. * On out, the number bytes put into array.
  875. * returns BAD_FUNC_ARG when a parameter is NULL,
  876. * ECC_BAD_ARG_E when outLen is less than ED448_KEY_SIZE,
  877. * 0 otherwise.
  878. */
  879. int wc_ed448_export_private_only(ed448_key* key, byte* out, word32* outLen)
  880. {
  881. int ret = 0;
  882. /* sanity checks on arguments */
  883. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  884. ret = BAD_FUNC_ARG;
  885. }
  886. if ((ret == 0) && (*outLen < ED448_KEY_SIZE)) {
  887. *outLen = ED448_KEY_SIZE;
  888. ret = BUFFER_E;
  889. }
  890. if (ret == 0) {
  891. *outLen = ED448_KEY_SIZE;
  892. XMEMCPY(out, key->k, ED448_KEY_SIZE);
  893. }
  894. return ret;
  895. }
  896. /* Export the ed448 private and public key.
  897. *
  898. * key [in] Ed448 private/public key.
  899. * out [in] Array to hold private and public key.
  900. * outLen [in/out] On in, the number of bytes in array.
  901. * On out, the number bytes put into array.
  902. * returns BAD_FUNC_ARG when a parameter is NULL,
  903. * BUFFER_E when outLen is less than ED448_PRV_KEY_SIZE,
  904. * 0 otherwise.
  905. */
  906. int wc_ed448_export_private(ed448_key* key, byte* out, word32* outLen)
  907. {
  908. int ret = 0;
  909. /* sanity checks on arguments */
  910. if ((key == NULL) || (out == NULL) || (outLen == NULL)) {
  911. ret = BAD_FUNC_ARG;
  912. }
  913. if ((ret == 0) && (*outLen < ED448_PRV_KEY_SIZE)) {
  914. *outLen = ED448_PRV_KEY_SIZE;
  915. ret = BUFFER_E;
  916. }
  917. if (ret == 0) {
  918. *outLen = ED448_PRV_KEY_SIZE;
  919. XMEMCPY(out, key->k, ED448_PRV_KEY_SIZE);
  920. }
  921. return ret;
  922. }
  923. /* Export the ed448 private and public key.
  924. *
  925. * key [in] Ed448 private/public key.
  926. * priv [in] Array to hold private key.
  927. * privSz [in/out] On in, the number of bytes in private key array.
  928. * pub [in] Array to hold public key.
  929. * pubSz [in/out] On in, the number of bytes in public key array.
  930. * On out, the number bytes put into array.
  931. * returns BAD_FUNC_ARG when a parameter is NULL,
  932. * BUFFER_E when privSz is less than ED448_PRV_KEY_SIZE or pubSz is less
  933. * than ED448_PUB_KEY_SIZE,
  934. * 0 otherwise.
  935. */
  936. int wc_ed448_export_key(ed448_key* key, byte* priv, word32 *privSz,
  937. byte* pub, word32 *pubSz)
  938. {
  939. int ret = 0;
  940. /* export 'full' private part */
  941. ret = wc_ed448_export_private(key, priv, privSz);
  942. if (ret == 0) {
  943. /* export public part */
  944. ret = wc_ed448_export_public(key, pub, pubSz);
  945. }
  946. return ret;
  947. }
  948. #endif /* HAVE_ED448_KEY_EXPORT */
  949. /* Check the public key of the ed448 key matches the private key.
  950. *
  951. * key [in] Ed448 private/public key.
  952. * returns BAD_FUNC_ARG when key is NULL,
  953. * PUBLIC_KEY_E when the public key is not set or doesn't match,
  954. * other -ve value on hash failure,
  955. * 0 otherwise.
  956. */
  957. int wc_ed448_check_key(ed448_key* key)
  958. {
  959. int ret = 0;
  960. unsigned char pubKey[ED448_PUB_KEY_SIZE];
  961. if (key == NULL) {
  962. ret = BAD_FUNC_ARG;
  963. }
  964. if (ret == 0 && !key->pubKeySet) {
  965. ret = PUBLIC_KEY_E;
  966. }
  967. if (ret == 0) {
  968. ret = wc_ed448_make_public(key, pubKey, sizeof(pubKey));
  969. }
  970. if ((ret == 0) && (XMEMCMP(pubKey, key->p, ED448_PUB_KEY_SIZE) != 0)) {
  971. ret = PUBLIC_KEY_E;
  972. }
  973. return ret;
  974. }
  975. /* Returns the size of an ed448 private key.
  976. *
  977. * key [in] Ed448 private/public key.
  978. * returns BAD_FUNC_ARG when key is NULL,
  979. * ED448_KEY_SIZE otherwise.
  980. */
  981. int wc_ed448_size(ed448_key* key)
  982. {
  983. int ret = ED448_KEY_SIZE;
  984. if (key == NULL) {
  985. ret = BAD_FUNC_ARG;
  986. }
  987. return ret;
  988. }
  989. /* Returns the size of an ed448 private plus public key.
  990. *
  991. * key [in] Ed448 private/public key.
  992. * returns BAD_FUNC_ARG when key is NULL,
  993. * ED448_PRV_KEY_SIZE otherwise.
  994. */
  995. int wc_ed448_priv_size(ed448_key* key)
  996. {
  997. int ret = ED448_PRV_KEY_SIZE;
  998. if (key == NULL) {
  999. ret = BAD_FUNC_ARG;
  1000. }
  1001. return ret;
  1002. }
  1003. /* Returns the size of an ed448 public key.
  1004. *
  1005. * key [in] Ed448 private/public key.
  1006. * returns BAD_FUNC_ARG when key is NULL,
  1007. * ED448_PUB_KEY_SIZE otherwise.
  1008. */
  1009. int wc_ed448_pub_size(ed448_key* key)
  1010. {
  1011. int ret = ED448_PUB_KEY_SIZE;
  1012. if (key == NULL) {
  1013. ret = BAD_FUNC_ARG;
  1014. }
  1015. return ret;
  1016. }
  1017. /* Returns the size of an ed448 signature.
  1018. *
  1019. * key [in] Ed448 private/public key.
  1020. * returns BAD_FUNC_ARG when key is NULL,
  1021. * ED448_SIG_SIZE otherwise.
  1022. */
  1023. int wc_ed448_sig_size(ed448_key* key)
  1024. {
  1025. int ret = ED448_SIG_SIZE;
  1026. if (key == NULL) {
  1027. ret = BAD_FUNC_ARG;
  1028. }
  1029. return ret;
  1030. }
  1031. #endif /* HAVE_ED448 */