ed448.c 38 KB

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