eccsi.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267
  1. /* eccsi.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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifdef NO_INLINE
  26. #include <wolfssl/wolfcrypt/misc.h>
  27. #else
  28. #define WOLFSSL_MISC_INCLUDED
  29. #include <wolfcrypt/src/misc.c>
  30. #endif
  31. #ifdef WOLFCRYPT_HAVE_ECCSI
  32. #include <wolfssl/wolfcrypt/error-crypt.h>
  33. #include <wolfssl/wolfcrypt/eccsi.h>
  34. #include <wolfssl/wolfcrypt/asn_public.h>
  35. #ifdef WOLFSSL_HAVE_SP_ECC
  36. #include <wolfssl/wolfcrypt/sp.h>
  37. #endif
  38. /**
  39. * Initialize the components of the ECCSI key and use the specified curve.
  40. *
  41. * Must be called before performing any operations.
  42. * Free the ECCSI key with wc_FreeEccsiKey() when no longer needed.
  43. *
  44. * @param [in] key ECCSI key to initialize.
  45. * @param [in] heap Heap hint.
  46. * @param [in] devId Device identifier.
  47. * Use INVALID_DEVID when no device used.
  48. * @return 0 on success.
  49. * @return BAD_FUNC_ARG when key is NULL.
  50. * @return MEMORY_E when dynamic memory allocation fails.
  51. */
  52. int wc_InitEccsiKey_ex(EccsiKey* key, int keySz, int curveId, void* heap,
  53. int devId)
  54. {
  55. int err = 0;
  56. ecc_key* ecc = NULL;
  57. ecc_key* pubkey = NULL;
  58. EccsiKeyParams* params = NULL;
  59. if (key == NULL) {
  60. err = BAD_FUNC_ARG;
  61. }
  62. if (err == 0) {
  63. XMEMSET(key, 0, sizeof(*key));
  64. key->heap = heap;
  65. params = &key->params;
  66. err = wc_ecc_init_ex(&key->ecc, heap, devId);
  67. }
  68. if (err == 0) {
  69. ecc = &key->ecc;
  70. err = wc_ecc_init_ex(&key->pubkey, heap, devId);
  71. }
  72. if (err == 0) {
  73. key->pvt = wc_ecc_new_point_h(heap);
  74. if (key->pvt == NULL) {
  75. err = MEMORY_E;
  76. }
  77. }
  78. if (err == 0) {
  79. pubkey = &key->pubkey;
  80. err = mp_init_multi(&params->order,
  81. #ifdef WOLFCRYPT_ECCSI_CLIENT
  82. &params->a, &params->b, &params->prime, &key->tmp, &key->ssk
  83. #else
  84. NULL, NULL, NULL, NULL, NULL
  85. #endif
  86. );
  87. }
  88. if (err == 0) {
  89. err = wc_ecc_set_curve(&key->ecc, keySz, curveId);
  90. }
  91. if (err == 0) {
  92. err = wc_ecc_set_curve(&key->pubkey, keySz, curveId);
  93. }
  94. if (err != 0) {
  95. wc_ecc_free(pubkey);
  96. wc_ecc_free(ecc);
  97. }
  98. return err;
  99. }
  100. /**
  101. * Initialize the components of the ECCSI key.
  102. * Default curve used: NIST_P256 (ECC_SECP256R1)
  103. *
  104. * Must be called before performing any operations.
  105. * Free the ECCSI key with wc_FreeEccsiKey() when no longer needed.
  106. *
  107. * @param [in] key ECCSI key to initialize.
  108. * @param [in] heap Heap hint.
  109. * @param [in] devId Device identifier.
  110. * Use INVALID_DEVID when no device used.
  111. * @return 0 on success.
  112. * @return BAD_FUNC_ARG when key is NULL.
  113. * @return MEMORY_E when dynamic memory allocation fails.
  114. */
  115. int wc_InitEccsiKey(EccsiKey* key, void* heap, int devId)
  116. {
  117. return wc_InitEccsiKey_ex(key, 32, ECC_SECP256R1, heap, devId);
  118. }
  119. /**
  120. * Frees memory associated with components of the ECCIS key.
  121. *
  122. * Must be called when finished with the ECCIS key.
  123. *
  124. * @param [in] key ECCIS key.
  125. */
  126. void wc_FreeEccsiKey(EccsiKey* key)
  127. {
  128. if (key != NULL) {
  129. EccsiKeyParams* params = &key->params;
  130. wc_ecc_del_point_h(params->base, key->heap);
  131. #ifdef WOLFCRYPT_ECCSI_CLIENT
  132. mp_free(&key->ssk);
  133. mp_free(&key->tmp);
  134. mp_free(&params->prime);
  135. mp_free(&params->b);
  136. mp_free(&params->a);
  137. #endif
  138. mp_free(&params->order);
  139. wc_ecc_del_point_h(key->pvt, key->heap);
  140. wc_ecc_free(&key->pubkey);
  141. wc_ecc_free(&key->ecc);
  142. XMEMSET(key, 0, sizeof(*key));
  143. }
  144. }
  145. /*
  146. * Order, as a hex string in the ECC object, loaded into mp_int in key.
  147. * Flags that the order is available so it isn't loaded multiple times.
  148. *
  149. * @param [in] key ECCSI key.
  150. * @return 0 on success.
  151. * @return MEMORY_E when dynamic memory allocation fails.
  152. */
  153. static int eccsi_load_order(EccsiKey* key)
  154. {
  155. int err = 0;
  156. if (!key->params.haveOrder) {
  157. err = mp_read_radix(&key->params.order, key->ecc.dp->order,
  158. MP_RADIX_HEX);
  159. if (err == 0) {
  160. key->params.haveOrder = 1;
  161. }
  162. }
  163. return err;
  164. }
  165. #ifdef WOLFCRYPT_ECCSI_CLIENT
  166. /*
  167. * Parameters, as a hex strings in the ECC object, loaded into mp_ints in key.
  168. *
  169. * Parameters loaded: order, A, B, prime.
  170. * Flags that each parameter is available so they aren't loaded multiple times.
  171. *
  172. * @param [in] key ECCSI key.
  173. * @return 0 on success.
  174. * @return MEMORY_E when dynamic memory allocation fails.
  175. */
  176. static int eccsi_load_ecc_params(EccsiKey* key)
  177. {
  178. int err = 0;
  179. EccsiKeyParams* params = &key->params;
  180. err = eccsi_load_order(key);
  181. if ((err == 0) && (!params->haveA)) {
  182. err = mp_read_radix(&params->a, key->ecc.dp->Af, MP_RADIX_HEX);
  183. if (err == 0) {
  184. params->haveA = 1;
  185. }
  186. }
  187. if ((err == 0) && (!params->haveB)) {
  188. err = mp_read_radix(&params->b, key->ecc.dp->Bf, MP_RADIX_HEX);
  189. if (err == 0) {
  190. params->haveB = 1;
  191. }
  192. }
  193. if ((err == 0) && (!params->havePrime)) {
  194. err = mp_read_radix(&params->prime, key->ecc.dp->prime, MP_RADIX_HEX);
  195. if (err == 0) {
  196. params->havePrime = 1;
  197. }
  198. }
  199. return err;
  200. }
  201. #endif /* WOLFCRYPT_ECCSI_CLIENT */
  202. /*
  203. * Get the base point, hex encoded in the ECC object, as an ecc_point.
  204. *
  205. * Flags that base is available so it isn't loaded multiple times.
  206. * @param [in] key ECCSI key.
  207. * @param [out] base Base point of curve.
  208. * @return 0 on success.
  209. * @return MEMORY_E when dynamic memory allocation fails.
  210. */
  211. static int eccsi_load_base(EccsiKey* key)
  212. {
  213. int err = 0;
  214. EccsiKeyParams* params = &key->params;
  215. if (!params->haveBase) {
  216. if (params->base == NULL) {
  217. params->base = wc_ecc_new_point_h(key->heap);
  218. if (params->base == NULL) {
  219. err = MEMORY_E;
  220. }
  221. }
  222. if (err == 0) {
  223. err = mp_read_radix(params->base->x, key->ecc.dp->Gx, MP_RADIX_HEX);
  224. }
  225. if (err == 0) {
  226. err = mp_read_radix(params->base->y, key->ecc.dp->Gy, MP_RADIX_HEX);
  227. }
  228. if (err == 0) {
  229. err = mp_set(params->base->z, 1);
  230. }
  231. if (err == 0) {
  232. params->haveBase = 1;
  233. }
  234. }
  235. return err;
  236. }
  237. /*
  238. * Encode the base point of the curve.
  239. *
  240. * Base point is hex encoded in the ECC object or cached as an ECC point from
  241. * previous load calls.
  242. *
  243. * @param [in] key ECCSI key.
  244. * @param [out] data Buffer to encode base point into.
  245. * @param [out] dataSz Length of base point in bytes.
  246. * @return 0 on success.
  247. * @return MEMORY_E when dynamic memory allocation fails.
  248. * @return Other -ve value when an internal operation fails.
  249. */
  250. static int eccsi_encode_base(EccsiKey* key, byte* data, word32* dataSz)
  251. {
  252. int err;
  253. int idx = wc_ecc_get_curve_idx(key->ecc.dp->id);
  254. err = eccsi_load_base(key);
  255. if (err == 0) {
  256. err = wc_ecc_export_point_der(idx, key->params.base, data, dataSz);
  257. }
  258. return err;
  259. }
  260. #ifndef WOLFSSL_HAVE_SP_ECC
  261. /*
  262. * Convert the KPAK to montgomery form.
  263. *
  264. * The KPAK is needed in Montgomery form for verification.
  265. *
  266. * @param [in] key ECCSI key.
  267. * @return 0 on success.
  268. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  269. * @return Other -ve value when an internal operation fails.
  270. */
  271. static int eccsi_kpak_to_mont(EccsiKey* key)
  272. {
  273. int err = 0;
  274. ecc_point* kpak = &key->ecc.pubkey;
  275. mp_int* mu = &key->tmp;
  276. mp_int* prime = &key->params.prime;
  277. if (!key->kpakMont) {
  278. err = mp_montgomery_calc_normalization(mu, prime);
  279. if (err == 0) {
  280. err = mp_mulmod(kpak->x, mu, prime, kpak->x);
  281. }
  282. if (err == 0) {
  283. err = mp_mulmod(kpak->y, mu, prime, kpak->y);
  284. }
  285. if (err == 0) {
  286. err = mp_mulmod(kpak->z, mu, prime, kpak->z);
  287. }
  288. if (err == 0) {
  289. key->kpakMont = 1;
  290. }
  291. }
  292. return err;
  293. }
  294. #endif
  295. /*
  296. * Convert the KPAK from montgomery form.
  297. *
  298. * The KPAK is needed in Montgomery form for verification.
  299. *
  300. * @param [in] key ECCSI key.
  301. * @return 0 on success.
  302. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  303. * @return Other -ve value when an internal operation fails.
  304. */
  305. static int eccsi_kpak_from_mont(EccsiKey* key)
  306. {
  307. int err = 0;
  308. ecc_point* kpak = &key->ecc.pubkey;
  309. mp_digit mp;
  310. mp_int* prime = &key->params.prime;
  311. if (key->kpakMont) {
  312. err = mp_montgomery_setup(prime, &mp);
  313. if (err == 0) {
  314. err = mp_montgomery_reduce(kpak->x, prime, mp);
  315. }
  316. if (err == 0) {
  317. err = mp_montgomery_reduce(kpak->y, prime, mp);
  318. }
  319. if (err == 0) {
  320. err = mp_montgomery_reduce(kpak->z, prime, mp);
  321. }
  322. if (err == 0) {
  323. key->kpakMont = 0;
  324. }
  325. }
  326. return err;
  327. }
  328. /*
  329. * Compute HS = hash( G | KPAK | ID | PVT )
  330. *
  331. * Use when making a (SSK,PVT) pair, signing and verifying.
  332. *
  333. * @param [in] key ECCSI key.
  334. * @param [in] hashType Type of hash algorithm. e.g. WC_SHA256
  335. * @param [in] id Identity to create hash from.
  336. * @param [in] idSz Length of identity in bytes.
  337. * @param [in] pvt Public Validation Token (PVT) as an ECC point.
  338. * @param [out] hash Buffer to hold hash data.
  339. * @param [out] hashSz Length of hash data in bytes.
  340. * @return 0 on success.
  341. * @return MEMORY_E when dynamic memory allocation fails.
  342. * @return Other -ve value when an internal operation fails.
  343. */
  344. static int eccsi_compute_hs(EccsiKey* key, enum wc_HashType hashType,
  345. const byte* id, word32 idSz, ecc_point* pvt, byte* hash, byte* hashSz)
  346. {
  347. int err;
  348. word32 dataSz = 0;
  349. int idx = wc_ecc_get_curve_idx(key->ecc.dp->id);
  350. ecc_point* kpak = &key->ecc.pubkey;
  351. int hash_inited = 0;
  352. /* HS = hash( G | KPAK | ID | PVT ) */
  353. err = wc_HashInit_ex(&key->hash, hashType, key->heap, INVALID_DEVID);
  354. if (err == 0) {
  355. hash_inited = 1;
  356. /* Base Point - G */
  357. dataSz = sizeof(key->data);
  358. err = eccsi_encode_base(key, key->data, &dataSz);
  359. }
  360. if (err == 0) {
  361. err = wc_HashUpdate(&key->hash, hashType, key->data, dataSz);
  362. }
  363. if (err == 0) {
  364. err = eccsi_kpak_from_mont(key);
  365. }
  366. if (err == 0) {
  367. dataSz = sizeof(key->data);
  368. /* KPAK - public key */
  369. err = wc_ecc_export_point_der(idx, kpak, key->data, &dataSz);
  370. }
  371. if (err == 0) {
  372. err = wc_HashUpdate(&key->hash, hashType, key->data, dataSz);
  373. }
  374. if (err == 0) {
  375. /* Id - Signer's ID */
  376. err = wc_HashUpdate(&key->hash, hashType, id, idSz);
  377. }
  378. if (err == 0) {
  379. dataSz = sizeof(key->data);
  380. /* PVT - Public Validation Token */
  381. err = wc_ecc_export_point_der(idx, pvt, key->data, &dataSz);
  382. }
  383. if (err == 0) {
  384. /* PVT - Public Validation Token */
  385. err = wc_HashUpdate(&key->hash, hashType, key->data, dataSz);
  386. }
  387. if (err == 0) {
  388. err = wc_HashFinal(&key->hash, hashType, hash);
  389. }
  390. if (err == 0) {
  391. *hashSz = (byte)wc_HashGetDigestSize(hashType);
  392. }
  393. if (hash_inited) {
  394. (void)wc_HashFree(&key->hash, hashType);
  395. }
  396. return err;
  397. }
  398. #ifdef WOLFCRYPT_ECCSI_KMS
  399. /**
  400. * Generate KMS Secret Auth Key (KSAK) and KMS Public Auth Key (KPAK).
  401. *
  402. * RFC 6507, Section 4.2
  403. *
  404. * Called when establishing a new KMS.\n
  405. * KSAK must be kept secret while KPAK is required by clients for signing
  406. * and verifying.\n
  407. * Export key using wc_ExportEccsiKey(), once generated, to reuse the key.\n
  408. * Export KPAK using wc_ExportEccsiPublicKey(), once generate to send to
  409. * clients.
  410. *
  411. * Creates a random private key and multiplies it by the base point to calculate
  412. * the public key.
  413. *
  414. * @param [in] key ECCSI key.
  415. * @param [in] rng Random number generator.
  416. * @return 0 on success.
  417. * @return BAD_FUNC_ARG when key or rng is NULL.
  418. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  419. * @return Other -ve value when an internal operation fails.
  420. */
  421. int wc_MakeEccsiKey(EccsiKey* key, WC_RNG* rng)
  422. {
  423. int err = 0;
  424. if ((key == NULL) || (rng == NULL)) {
  425. err = BAD_FUNC_ARG;
  426. }
  427. if (err == 0) {
  428. err = wc_ecc_make_key_ex(rng, key->ecc.dp->size, &key->ecc,
  429. key->ecc.dp->id);
  430. }
  431. return err;
  432. }
  433. /*
  434. * Encode a point into a buffer.
  435. *
  436. * X and y ordinate of point concatenated. Each number is zero padded tosize.
  437. * Descriptor byte (0x04) is prepended when not raw.
  438. *
  439. * @param [in] point ECC point to encode.
  440. * @param [in] size Size of prime in bytes - maximum ordinate length.
  441. * @param [out] data Buffer to hold encoded data.
  442. * NULL when needing length of encoded data.
  443. * @param [in,out] sz In, the size of the buffer in bytes.
  444. * Out, the size of the encoded data in bytes.
  445. * @param [in] raw On 0, prepend descriptor byte.
  446. * On 1, only include ordinates.
  447. * @return 0 on success.
  448. * @return BAD_FUNC_ARG when key or sz is NULL.
  449. * @return LENGTH_ONLY_E when data is NULL - sz will hold the size in bytes of
  450. * the encoded data.
  451. * @return BUFFER_E when size of buffer is too small.
  452. */
  453. static int eccsi_encode_point(ecc_point* point, word32 size, byte* data,
  454. word32* sz, int raw)
  455. {
  456. int err = 0;
  457. if (data == NULL) {
  458. *sz = size * 2 + !raw;
  459. err = LENGTH_ONLY_E;
  460. }
  461. if ((err == 0) && (*sz < size * 2 + !raw)) {
  462. err = BUFFER_E;
  463. }
  464. if (err == 0) {
  465. if (!raw) {
  466. data[0] = 0x04;
  467. data++;
  468. }
  469. /* Write out the point's x ordinate into key size bytes. */
  470. err = mp_to_unsigned_bin_len(point->x, data, size);
  471. }
  472. if (err == 0) {
  473. data += size;
  474. /* Write out the point's y ordinate into key size bytes. */
  475. err = mp_to_unsigned_bin_len(point->y, data, size);
  476. }
  477. if (err == 0) {
  478. *sz = size * 2 + !raw;
  479. }
  480. return err;
  481. }
  482. /*
  483. * Decode the data into an ECC point.
  484. *
  485. * X and y ordinate of point concatenated. Each number is zero padded to
  486. * key size. Supports prepended descriptor byte (0x04).
  487. *
  488. * @param [out] point ECC point to encode.
  489. * @param [in] size Size of prime in bytes - maximum ordinate length.
  490. * @param [in] data Encoded public key.
  491. * @param [in] sz Size of the encoded public key in bytes.
  492. * @return 0 on success.
  493. * @return BAD_FUNC_ARG when key or z is NULL.
  494. * @return BUFFER_E when size of data is not equal to the expected size.
  495. * @return ASN_PARSE_E when format byte is invalid.
  496. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  497. */
  498. static int eccsi_decode_point(ecc_point* point, word32 size, const byte* data,
  499. word32 sz)
  500. {
  501. int err = 0;
  502. if ((sz != size * 2) && (sz != size * 2 + 1)) {
  503. err = BUFFER_E;
  504. }
  505. if ((err == 0) && (sz & 1)) {
  506. if (data[0] != 0x04) {
  507. err = ASN_PARSE_E;
  508. }
  509. data++;
  510. }
  511. if (err == 0) {
  512. /* Read the public key point's x value from key size bytes. */
  513. err = mp_read_unsigned_bin(point->x, data, size);
  514. }
  515. if (err == 0) {
  516. data += size;
  517. /* Read the public key point's y value from key size bytes. */
  518. err = mp_read_unsigned_bin(point->y, data, size);
  519. }
  520. if (err == 0) {
  521. err = mp_set(point->z, 1);
  522. }
  523. return err;
  524. }
  525. /*
  526. * Encode the ECCSI key.
  527. *
  528. * Encodes the private key as big-endian bytes of fixed length.
  529. * Encodes the public key x and y ordinates as big-endian bytes of fixed length.
  530. *
  531. * @param [in] key ECCSI key.
  532. * @param [out] data Buffer to hold encoded ECCSI key.
  533. * @return 0 on success.
  534. * @return MEMORY_E when dynamic memory allocation fails (WOLFSSL_SMALL_STACK).
  535. */
  536. static int eccsi_encode_key(EccsiKey* key, byte* data)
  537. {
  538. int err;
  539. word32 sz = (word32)key->ecc.dp->size * 2;
  540. /* Write out the secret value into key size bytes. */
  541. err = mp_to_unsigned_bin_len(&key->ecc.k, data, key->ecc.dp->size);
  542. if (err == 0) {
  543. data += key->ecc.dp->size;
  544. /* Write the public key. */
  545. err = eccsi_encode_point(&key->ecc.pubkey, (word32)key->ecc.dp->size,
  546. data, &sz, 1);
  547. }
  548. return err;
  549. }
  550. /**
  551. * Export the ECCSI key as encoded public/private ECC key.
  552. *
  553. * Use when saving the KMS key pair.
  554. *
  555. * Private key, x ordinate of public key and y ordinate of public key
  556. * concatenated. Each number is zero padded to key size.
  557. *
  558. * @param [in] key ECCSI key.
  559. * @param [out] data Buffer to hold encoded ECCSI key.
  560. * NULL when requesting required length.
  561. * @param [in,out] sz On in, size of buffer in bytes.
  562. * On out, size of encoded ECCSI key in bytes.
  563. * @return 0 on success.
  564. * @return BAD_FUNC_ARG when key or sz is NULL
  565. * @return BAD_STATE_E when no key to export.
  566. * @return LENGTH_ONLY_E when data is NULL - sz is set.
  567. * @return BUFFER_E when the buffer passed in is too small.
  568. * @return MEMORY_E when dynamic memory allocation fails (WOLFSSL_SMALL_STACK).
  569. */
  570. int wc_ExportEccsiKey(EccsiKey* key, byte* data, word32* sz)
  571. {
  572. int err = 0;
  573. if ((key == NULL) || (sz == NULL)) {
  574. err = BAD_FUNC_ARG;
  575. }
  576. if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY)) {
  577. err = BAD_STATE_E;
  578. }
  579. if (err == 0) {
  580. if (data == NULL) {
  581. *sz = key->ecc.dp->size * 3;
  582. err = LENGTH_ONLY_E;
  583. }
  584. else if (*sz < (word32)key->ecc.dp->size * 3) {
  585. err = BUFFER_E;
  586. }
  587. else {
  588. *sz = key->ecc.dp->size * 3;
  589. }
  590. }
  591. if (err == 0) {
  592. err = eccsi_kpak_from_mont(key);
  593. }
  594. if (err == 0) {
  595. /* Encode key */
  596. err = eccsi_encode_key(key, data);
  597. }
  598. return err;
  599. }
  600. /*
  601. * Import the ECCSI key as encoded public/private ECC key.
  602. *
  603. * Decodes the private key as big-endian bytes of fixed length.
  604. * Decodes the public key x and y ordinates as big-endian bytes of fixed length.
  605. *
  606. * @param [in] key ECCSI key.
  607. * @param [in] data Buffer holding encoded ECCSI key.
  608. * @return 0 on success.
  609. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  610. */
  611. static int eccsi_decode_key(EccsiKey* key, const byte* data)
  612. {
  613. int err;
  614. /* Read the secret value from key size bytes. */
  615. err = mp_read_unsigned_bin(&key->ecc.k, data, key->ecc.dp->size);
  616. if (err == 0) {
  617. data += key->ecc.dp->size;
  618. /* Read public key. */
  619. err = eccsi_decode_point(&key->ecc.pubkey, (word32)key->ecc.dp->size,
  620. data, key->ecc.dp->size * 2);
  621. }
  622. return err;
  623. }
  624. /**
  625. * Import the ECCSI key as encoded public/private ECC key.
  626. *
  627. * Use when restoring the KMS key pair.
  628. *
  629. * Private key, x ordinate of public key and y ordinate of public key
  630. * concatenated. Each number is zero padded to key size.
  631. *
  632. * @param [in] key ECCSI key.
  633. * @param [in] data Buffer holding encoded ECCSI key.
  634. * @param [in] sz Size of encoded ECCSI key in bytes.
  635. * @return 0 on success.
  636. * @return BAD_FUNC_ARG when key or data is NULL.
  637. * @return BUFFER_E when size of data is not equal to the expected size.
  638. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  639. */
  640. int wc_ImportEccsiKey(EccsiKey* key, const byte* data, word32 sz)
  641. {
  642. int err = 0;
  643. if ((key == NULL) || (data == NULL)) {
  644. err = BAD_FUNC_ARG;
  645. }
  646. if ((err == 0) && (sz != (word32)key->ecc.dp->size * 3)) {
  647. err = BUFFER_E;
  648. }
  649. if (err == 0) {
  650. key->kpakMont = 0;
  651. /* Decode key */
  652. err = eccsi_decode_key(key, data);
  653. }
  654. if (err == 0) {
  655. key->ecc.type = ECC_PRIVATEKEY;
  656. }
  657. return err;
  658. }
  659. /**
  660. * Export the ECCSI private key.
  661. *
  662. * Use when saving the KMS key.
  663. *
  664. * Private key is zero padded to key size.
  665. *
  666. * @param [in] key ECCSI key.
  667. * @param [out] data Buffer to hold encoded ECCSI private key.
  668. * NULL when requesting required length.
  669. * @param [in,out] sz On in, size of buffer in bytes.
  670. * On out, size of encoded ECCSI private key in bytes.
  671. * @return 0 on success.
  672. * @return BAD_FUNC_ARG when key or sz is NULL
  673. * @return BAD_STATE_E when no key to export.
  674. * @return LENGTH_ONLY_E when data is NULL - sz is set.
  675. * @return BUFFER_E when the buffer passed in is too small.
  676. * @return MEMORY_E when dynamic memory allocation fails (WOLFSSL_SMALL_STACK).
  677. */
  678. int wc_ExportEccsiPrivateKey(EccsiKey* key, byte* data, word32* sz)
  679. {
  680. int err = 0;
  681. if ((key == NULL) || (sz == NULL)) {
  682. err = BAD_FUNC_ARG;
  683. }
  684. if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY)) {
  685. err = BAD_STATE_E;
  686. }
  687. if (err == 0) {
  688. if (data == NULL) {
  689. *sz = key->ecc.dp->size;
  690. err = LENGTH_ONLY_E;
  691. }
  692. else if (*sz < (word32)key->ecc.dp->size) {
  693. err = BUFFER_E;
  694. }
  695. else {
  696. *sz = key->ecc.dp->size;
  697. }
  698. }
  699. if (err == 0) {
  700. err = mp_to_unsigned_bin_len(&key->ecc.k, data, key->ecc.dp->size);
  701. }
  702. return err;
  703. }
  704. /**
  705. * Import the ECCSI private key.
  706. *
  707. * Use when restoring the KMS key pair.
  708. *
  709. * Private key is zero padded to key size.
  710. *
  711. * @param [in] key ECCSI key.
  712. * @param [in] data Buffer holding encoded ECCSI private key.
  713. * @param [in] sz Size of encoded ECCSI private key in bytes.
  714. * @return 0 on success.
  715. * @return BAD_FUNC_ARG when key or data is NULL.
  716. * @return BUFFER_E when size of data is not equal to the expected size.
  717. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  718. */
  719. int wc_ImportEccsiPrivateKey(EccsiKey* key, const byte* data, word32 sz)
  720. {
  721. int err = 0;
  722. if ((key == NULL) || (data == NULL)) {
  723. err = BAD_FUNC_ARG;
  724. }
  725. if ((err == 0) && (sz != (word32)key->ecc.dp->size)) {
  726. err = BUFFER_E;
  727. }
  728. if (err == 0) {
  729. err = mp_read_unsigned_bin(&key->ecc.k, data, key->ecc.dp->size);
  730. }
  731. return err;
  732. }
  733. /**
  734. * Export the KMS Public Auth Key (KPAK) from the ECCSI object.
  735. *
  736. * KPAK is required by all clients in order to perform cryptographic operations.
  737. *
  738. * X and y ordinate of public key concatenated. Each number is zero padded to
  739. * key size.
  740. * Descriptor byte (0x04) is prepended when not raw.
  741. *
  742. * @param [in] key ECCSI key.
  743. * @param [out] data Buffer to hold the encoded public key.
  744. * @param [in,out] sz On in, size of buffer in bytes.
  745. * On out, length of encoded public key in bytes.
  746. * @param [in] raw On 0, prepend descriptor byte.
  747. * On 1, only include ordinates.
  748. * @return 0 on success.
  749. * @return BAD_FUNC_ARG when key or sz is NULL.
  750. * @return LENGTH_ONLY_E when data is NULL - sz is set.
  751. * @return BUFFER_E when the buffer passed in is too small.
  752. */
  753. int wc_ExportEccsiPublicKey(EccsiKey* key, byte* data, word32* sz, int raw)
  754. {
  755. int err = 0;
  756. if ((key == NULL) || (sz == NULL)) {
  757. err = BAD_FUNC_ARG;
  758. }
  759. if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY) &&
  760. (key->ecc.type != ECC_PUBLICKEY)) {
  761. err = BAD_STATE_E;
  762. }
  763. if ((err == 0) && (data != NULL)) {
  764. err = eccsi_kpak_from_mont(key);
  765. }
  766. if (err == 0) {
  767. /* Write out public key. */
  768. err = eccsi_encode_point(&key->ecc.pubkey, (word32)key->ecc.dp->size,
  769. data, sz, raw);
  770. }
  771. return err;
  772. }
  773. /*
  774. * Generates an (SSK, PVT) Pair - signing key pair.
  775. *
  776. * RFC 6507, Section 5.1.1
  777. *
  778. * @param [in] key ECCSI key.
  779. * @param [in] rng Random number generator.
  780. * @param [in] hashType Type of hash algorithm. e.g. WC_SHA256
  781. * @param [in] id Identity to create hash from.
  782. * @param [in] idSz Length of identity in bytes.
  783. * @param [out] ssk Secret Signing Key as an MP integer.
  784. * @param [out] pvt Public Validation Token (PVT) as an ECC point.
  785. * @return 0 on success.
  786. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  787. * @return Other -ve value when an internal operation fails.
  788. */
  789. static int eccsi_make_pair(EccsiKey* key, WC_RNG* rng,
  790. enum wc_HashType hashType, const byte* id, word32 idSz, mp_int* ssk,
  791. ecc_point* pvt)
  792. {
  793. int err = 0;
  794. byte hashSz = 0;
  795. int genTryCnt = 0;
  796. do {
  797. /* Don't infinitely make pairs when random number generator fails. */
  798. if ((++genTryCnt) > ECCSI_MAX_GEN_COUNT) {
  799. err = RNG_FAILURE_E;
  800. }
  801. if (err == 0) {
  802. wc_ecc_free(&key->pubkey);
  803. /* Step 1 and 2: Generate ephemeral key - v, PVT = [v]G */
  804. err = wc_ecc_make_key_ex(rng, key->ecc.dp->size, &key->pubkey,
  805. key->ecc.dp->id);
  806. }
  807. if (err == 0) {
  808. err = wc_ecc_copy_point(&key->pubkey.pubkey, pvt);
  809. }
  810. /* Step 3: Compute HS */
  811. if (err == 0) {
  812. hashSz = (byte)sizeof(key->data);
  813. err = eccsi_compute_hs(key, hashType, id, idSz, pvt, key->data,
  814. &hashSz);
  815. }
  816. /* Step 4: Compute SSK = ( KSAK + HS * v ) modulo q */
  817. if (err == 0) {
  818. err = mp_read_unsigned_bin(ssk, key->data, hashSz);
  819. }
  820. if (err == 0) {
  821. err = mp_mulmod(ssk, &key->pubkey.k, &key->params.order, ssk);
  822. }
  823. if (err == 0) {
  824. err = mp_addmod(ssk, &key->ecc.k, &key->params.order, ssk);
  825. }
  826. }
  827. while ((err == 0) && (mp_iszero(ssk) ||
  828. (mp_cmp(ssk, &key->ecc.k) == MP_EQ)));
  829. /* Step 5: ensure SSK and HS are non-zero (code lines above) */
  830. /* Step 6: Copy out SSK (done during calc) and PVT. Erase v */
  831. mp_forcezero(&key->pubkey.k);
  832. return err;
  833. }
  834. /**
  835. * Generates an (SSK, PVT) Pair - signing key pair.
  836. *
  837. * RFC 6507, Section 5.1.1
  838. *
  839. * ID should include information to indicate a revocation date.\n
  840. * SSK must be zeroized after sending to client.\n
  841. * SSK is sent to signing client only.\n
  842. * PVT is sent to all client types.
  843. *
  844. * @param [in] key ECCSI key.
  845. * @param [in] rng Random number generator.
  846. * @param [in] hashType Type of hash algorithm. e.g. WC_SHA256
  847. * @param [in] id Identity to create hash from.
  848. * @param [in] idSz Length of identity in bytes.
  849. * @param [out] ssk Secret Signing Key as an MP integer.
  850. * @param [out] pvt Public Validation Token (PVT) as an ECC point.
  851. * @return 0 on success.
  852. * @return BAD_FUNC_ARG when key, rng, id, ssk or pvt is NULL.
  853. * @return BAD_STATE_E when curve not set (key not set).
  854. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  855. * @return Other -ve value when an internal operation fails.
  856. */
  857. int wc_MakeEccsiPair(EccsiKey* key, WC_RNG* rng, enum wc_HashType hashType,
  858. const byte* id, word32 idSz, mp_int* ssk, ecc_point* pvt)
  859. {
  860. int err = 0;
  861. if ((key == NULL) || (rng == NULL) || (id == NULL) || (ssk == NULL) ||
  862. (pvt == NULL)) {
  863. err = BAD_FUNC_ARG;
  864. }
  865. if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY)) {
  866. err = BAD_STATE_E;
  867. }
  868. if (err == 0) {
  869. err = eccsi_load_order(key);
  870. }
  871. if (err == 0) {
  872. err = eccsi_make_pair(key, rng, hashType, id, idSz, ssk, pvt);
  873. }
  874. return err;
  875. }
  876. /**
  877. * Encode the SSK and PVT into a buffer.
  878. *
  879. * SSK and PVT required by client signing messages.
  880. *
  881. * @param [in] key ECCSI key.
  882. * @param [in] ssk Secret Signing Key as an MP integer.
  883. * @param [in] pvt Public Validation Token (PVT) as an ECC point.
  884. * @param [out] data Buffer to encode key pair into.
  885. * @param [in,out] sz In, size of buffer in bytes.
  886. * Out, size of encoded pair data in bytes.
  887. * @return 0 on success.
  888. * @return BAD_FUNC_ARG when key, ssk, pvt or sz is NULL.
  889. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  890. * @return LENGTH_ONLY_E when data is NULL - sz is set.
  891. */
  892. int wc_EncodeEccsiPair(const EccsiKey* key, mp_int* ssk, ecc_point* pvt,
  893. byte* data, word32* sz)
  894. {
  895. int err = 0;
  896. if ((key == NULL) || (ssk == NULL) || (pvt == NULL) || (sz == NULL)) {
  897. err = BAD_FUNC_ARG;
  898. }
  899. if ((err == 0) && (data == NULL)) {
  900. *sz = key->ecc.dp->size * 3;
  901. err = LENGTH_ONLY_E;
  902. }
  903. if ((err == 0) && (*sz < (word32)(key->ecc.dp->size * 3))) {
  904. err = BUFFER_E;
  905. }
  906. if (err == 0) {
  907. err = mp_to_unsigned_bin_len(ssk, data, key->ecc.dp->size);
  908. }
  909. if (err == 0) {
  910. data += key->ecc.dp->size;
  911. /* Write out the PVT's x ordinate into key size bytes. */
  912. err = mp_to_unsigned_bin_len(pvt->x, data, key->ecc.dp->size);
  913. }
  914. if (err == 0) {
  915. data += key->ecc.dp->size;
  916. /* Write out the PVT's y ordinate into key size bytes. */
  917. err = mp_to_unsigned_bin_len(pvt->y, data, key->ecc.dp->size);
  918. }
  919. if (err == 0) {
  920. *sz = key->ecc.dp->size * 3;
  921. }
  922. return err;
  923. }
  924. /**
  925. * Encode the Secret Signing Key (SSK).
  926. *
  927. * Use when saving the key pair.
  928. *
  929. * SSK is zero padded to key size.
  930. *
  931. * @param [in] key ECCSI key.
  932. * @param [in] ssk Secret Signing Key as an MP integer.
  933. * @param [out] data Buffer to hold encoded SSK.
  934. * NULL when requesting required length.
  935. * @param [in,out] sz On in, size of buffer in bytes.
  936. * On out, size of encoded ECCSI key in bytes.
  937. * @return 0 on success.
  938. * @return BAD_FUNC_ARG when key, ssk or sz is NULL
  939. * @return BAD_STATE_E when no key to export.
  940. * @return LENGTH_ONLY_E when data is NULL - sz is set.
  941. * @return BUFFER_E when the buffer passed in is too small.
  942. * @return MEMORY_E when dynamic memory allocation fails (WOLFSSL_SMALL_STACK).
  943. */
  944. int wc_EncodeEccsiSsk(const EccsiKey* key, mp_int* ssk, byte* data, word32* sz)
  945. {
  946. int err = 0;
  947. if ((key == NULL) || (ssk == NULL) || (sz == NULL)) {
  948. err = BAD_FUNC_ARG;
  949. }
  950. if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY)) {
  951. err = BAD_STATE_E;
  952. }
  953. if (err == 0) {
  954. if (data == NULL) {
  955. *sz = key->ecc.dp->size;
  956. err = LENGTH_ONLY_E;
  957. }
  958. else if (*sz < (word32)key->ecc.dp->size) {
  959. err = BUFFER_E;
  960. }
  961. else {
  962. *sz = key->ecc.dp->size;
  963. }
  964. }
  965. if (err == 0) {
  966. err = mp_to_unsigned_bin_len(ssk, data, key->ecc.dp->size);
  967. }
  968. return err;
  969. }
  970. /**
  971. * Decode the Secret Signing Key (SSK).
  972. *
  973. * Use when restoring the key pair.
  974. *
  975. * SSK is zero padded to key size.
  976. *
  977. * @param [in] key ECCSI key.
  978. * @param [in] data Buffer holding encoded ECCSI key.
  979. * @param [in] sz Size of encoded ECCSI key in bytes.
  980. * @param [out] ssk Secret Signing Key as an MP integer.
  981. * @return 0 on success.
  982. * @return BAD_FUNC_ARG when key, data or ssk is NULL.
  983. * @return BUFFER_E when size of data is not equal to the expected size.
  984. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  985. */
  986. int wc_DecodeEccsiSsk(const EccsiKey* key, const byte* data, word32 sz,
  987. mp_int* ssk)
  988. {
  989. int err = 0;
  990. if ((key == NULL) || (data == NULL) || (ssk == NULL)) {
  991. err = BAD_FUNC_ARG;
  992. }
  993. if ((err == 0) && (sz != (word32)key->ecc.dp->size)) {
  994. err = BUFFER_E;
  995. }
  996. if (err == 0) {
  997. err = mp_read_unsigned_bin(ssk, data, key->ecc.dp->size);
  998. }
  999. return err;
  1000. }
  1001. /**
  1002. * Encode the PVT into a buffer.
  1003. *
  1004. * PVT required by client verifying messages.
  1005. *
  1006. * X and y ordinate of public key concatenated. Each number is zero padded to
  1007. * key size.
  1008. * Descriptor byte (0x04) is prepended when not raw.
  1009. *
  1010. * @param [in] key ECCSI key.
  1011. * @param [in] pvt Public Validation Token (PVT) as an ECC point.
  1012. * @param [out] data Buffer to encode key pair into.
  1013. * @param [in,out] sz In, size of buffer in bytes.
  1014. * Out, size of encoded pair data in bytes.
  1015. * @param [in] raw On 0, prepend descriptor byte.
  1016. * On 1, only include ordinates.
  1017. * @return 0 on success.
  1018. * @return BAD_FUNC_ARG when key, pvt or sz is NULL.
  1019. * @return BAD_STATE_E when PVT has not been set.
  1020. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  1021. * @return LENGTH_ONLY_E when data is NULL - sz is set.
  1022. */
  1023. int wc_EncodeEccsiPvt(const EccsiKey* key, ecc_point* pvt, byte* data,
  1024. word32* sz, int raw)
  1025. {
  1026. int err = 0;
  1027. if ((key == NULL) || (pvt == NULL) || (sz == NULL)) {
  1028. err = BAD_FUNC_ARG;
  1029. }
  1030. if (err == 0) {
  1031. err = eccsi_encode_point(pvt, (word32)key->ecc.dp->size, data, sz, raw);
  1032. }
  1033. return err;
  1034. }
  1035. #endif /* WOLFCRYPT_ECCSI_KMS */
  1036. #ifdef WOLFCRYPT_ECCSI_CLIENT
  1037. /**
  1038. * Decode the SSK and PVT data into separate variables.
  1039. *
  1040. * A signing client decodes the data so that it can validate the pair and sign.
  1041. *
  1042. * @param [in] key ECCSI key.
  1043. * @param [in] data Buffer holding key pair data.
  1044. * @param [in] sz Size of data in bytes.
  1045. * @param [out] ssk Secret Signing Key as an MP integer.
  1046. * @param [out] pvt Public Validation Token (PVT) as an ECC point.
  1047. * @return 0 on success.
  1048. * @return BAD_FUNC_ARG when key, data, ssk or pvt is NULL.
  1049. * @return LENGTH_ONLY_E when data is NULL - sz is set.
  1050. * @return BUFFER_E when size of data is not equal to the expected size.
  1051. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  1052. */
  1053. int wc_DecodeEccsiPair(const EccsiKey* key, const byte* data, word32 sz,
  1054. mp_int* ssk, ecc_point* pvt)
  1055. {
  1056. int err = 0;
  1057. if ((key == NULL) || (data == NULL) || (ssk == NULL) || (pvt == NULL)) {
  1058. err = BAD_FUNC_ARG;
  1059. }
  1060. if ((err == 0) && (sz != (word32)(key->ecc.dp->size * 3))) {
  1061. err = BUFFER_E;
  1062. }
  1063. if (err == 0) {
  1064. /* Read the SSK value from key size bytes. */
  1065. err = mp_read_unsigned_bin(ssk, data, key->ecc.dp->size);
  1066. }
  1067. if (err == 0) {
  1068. data += key->ecc.dp->size;
  1069. /* Read the PVT's x value from key size bytes. */
  1070. err = mp_read_unsigned_bin(pvt->x, data, key->ecc.dp->size);
  1071. }
  1072. if (err == 0) {
  1073. data += key->ecc.dp->size;
  1074. /* Read the PVT's y value from key size bytes. */
  1075. err = mp_read_unsigned_bin(pvt->y, data, key->ecc.dp->size);
  1076. }
  1077. if (err == 0) {
  1078. err = mp_set(pvt->z, 1);
  1079. }
  1080. return err;
  1081. }
  1082. /**
  1083. * Decode the PVT data into an ECC point.
  1084. *
  1085. * A verifying client decodes the data so that it can verify a message.
  1086. *
  1087. * X and y ordinate of public key concatenated. Each number is zero padded to
  1088. * key size.
  1089. * Descriptor byte (0x04) is prepended when not raw.
  1090. *
  1091. * @param [in] key ECCSI key.
  1092. * @param [in] data Buffer holding PVT data.
  1093. * @param [in] sz Size of data in bytes.
  1094. * @param [out] pvt Public Validation Token (PVT) as an ECC point.
  1095. * @return 0 on success.
  1096. * @return BAD_FUNC_ARG when key, data, ssk or pvt is NULL.
  1097. * @return BUFFER_E when size of data is not equal to the expected size.
  1098. * @return ASN_PARSE_E when format byte is invalid.
  1099. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  1100. */
  1101. int wc_DecodeEccsiPvt(const EccsiKey* key, const byte* data, word32 sz,
  1102. ecc_point* pvt)
  1103. {
  1104. int err = 0;
  1105. if ((key == NULL) || (data == NULL) || (pvt == NULL)) {
  1106. err = BAD_FUNC_ARG;
  1107. }
  1108. if (err == 0) {
  1109. err = eccsi_decode_point(pvt, (word32)key->ecc.dp->size, data, sz);
  1110. }
  1111. return err;
  1112. }
  1113. /**
  1114. * Decode the PVT data, from a signature, into an ECC point.
  1115. *
  1116. * A verifying client decodes the data so that it can calculate the identity
  1117. * hash.
  1118. *
  1119. * X and y ordinate of public key concatenated. Each number is zero padded to
  1120. * key size.
  1121. * Descriptor byte (0x04) is prepended when not raw.
  1122. *
  1123. * @param [in] key ECCSI key.
  1124. * @param [in] sig Buffer holding signature data.
  1125. * @param [in] sz Size of data in bytes.
  1126. * @param [out] pvt Public Validation Token (PVT) as an ECC point.
  1127. * @return 0 on success.
  1128. * @return BAD_FUNC_ARG when key, data, ssk or pvt is NULL.
  1129. * @return BUFFER_E when size of data is not equal to the expected size.
  1130. * @return ASN_PARSE_E when format byte is invalid.
  1131. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  1132. */
  1133. int wc_DecodeEccsiPvtFromSig(const EccsiKey* key, const byte* sig, word32 sz,
  1134. ecc_point* pvt)
  1135. {
  1136. int err = 0;
  1137. if ((key == NULL) || (sig == NULL) || (pvt == NULL)) {
  1138. err = BAD_FUNC_ARG;
  1139. }
  1140. if (err == 0) {
  1141. word32 rSz = key->ecc.dp->size * 2;
  1142. err = eccsi_decode_point(pvt, (word32)key->ecc.dp->size, sig + rSz,
  1143. sz - rSz);
  1144. }
  1145. return err;
  1146. }
  1147. /**
  1148. * Import the KMS Public Auth Key (KPAK) into the ECCSI object.
  1149. *
  1150. * Clients import the KPAK to perform cryptographic operations.
  1151. *
  1152. * X and y ordinate of public key concatenated. Each number is zero padded to
  1153. * key size.
  1154. * Descriptor byte (0x04) is prepended when not raw.
  1155. *
  1156. * @param [in] key ECCSI key.
  1157. * @param [in] data Encoded public key as an array of bytes.
  1158. * @param [in] sz Length of encoded KPAK in bytes.
  1159. * @param [in] trusted 1 when public key is trusted.
  1160. * 0 when validation is required to be performed.
  1161. * @return 0 on success.
  1162. * @return BAD_FUNC_ARG when key or data is NULL.
  1163. * @return BUFFER_E when size of data is not equal to the expected size.
  1164. * @return ASN_PARSE_E when format byte is invalid.
  1165. * @return ECC_OUT_OF_RANGE_E when point is invalid.
  1166. * @return ECC_INF_E when point is at infinity and invalid.
  1167. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  1168. */
  1169. int wc_ImportEccsiPublicKey(EccsiKey* key, const byte* data, word32 sz,
  1170. int trusted)
  1171. {
  1172. int err = 0;
  1173. if ((key == NULL) || (data == NULL)) {
  1174. err = BAD_FUNC_ARG;
  1175. }
  1176. if (err == 0) {
  1177. key->kpakMont = 0;
  1178. /* Read the public key. */
  1179. err = eccsi_decode_point(&key->ecc.pubkey, (word32)key->ecc.dp->size,
  1180. data, sz);
  1181. }
  1182. if (err == 0) {
  1183. key->ecc.type = ECC_PUBLICKEY;
  1184. }
  1185. if ((err == 0) && (!trusted)) {
  1186. err = wc_ecc_check_key(&key->ecc);
  1187. }
  1188. return err;
  1189. }
  1190. /*
  1191. * Scalar multiply the base point of the curve and add a point.
  1192. *
  1193. * @param [in] key ECCSI key.
  1194. * @param [in] n MP integer representing scalar to multiply by.
  1195. * @param [in] a ECC point to add.
  1196. * @param [out] res ECC point representation of the resulting point.
  1197. * @param [in] mp Montgomery reduction multiplier.
  1198. * @param [in] map 0 indicates to leave in projective representation.
  1199. * 1 indicates map projective point to affine.
  1200. * @return 0 on success.
  1201. * @return MEMORY_E when dynamic memory allocation fails.
  1202. * @return Other -ve value when an internal operation fails.
  1203. */
  1204. static int eccsi_mulmod_base_add(EccsiKey* key, const mp_int* n,
  1205. ecc_point* a, ecc_point* res, mp_digit mp, int map)
  1206. {
  1207. int err = 0;
  1208. #ifdef WOLFSSL_HAVE_SP_ECC
  1209. #ifndef WOLFSSL_SP_NO_256
  1210. if ((key->ecc.idx != ECC_CUSTOM_IDX) &&
  1211. (ecc_sets[key->ecc.idx].id == ECC_SECP256R1)) {
  1212. err = sp_ecc_mulmod_base_add_256(n, a, 1, res, map, key->heap);
  1213. }
  1214. else
  1215. #endif
  1216. #endif
  1217. #ifndef WOLFSSL_SP_MATH
  1218. {
  1219. EccsiKeyParams* params = &key->params;
  1220. err = wc_ecc_mulmod(n, params->base, params->base, &params->a,
  1221. &params->prime, 0);
  1222. key->params.haveBase = 0;
  1223. if (err == 0) {
  1224. err = ecc_projective_add_point(params->base, a, res, &params->a,
  1225. &params->prime, mp);
  1226. }
  1227. if ((err == 0) && map) {
  1228. err = ecc_map(res, &params->prime, mp);
  1229. }
  1230. }
  1231. #else
  1232. {
  1233. err = NOT_COMPILED_IN;
  1234. }
  1235. (void)mp;
  1236. #endif
  1237. return err;
  1238. }
  1239. /*
  1240. * Scalar multiply a point on the curve.
  1241. *
  1242. * @param [in] key ECCSI key.
  1243. * @param [in] n MP integer representing scalar to multiply by.
  1244. * @param [in] point ECC point representation of a point on the curve.
  1245. * @param [out] res ECC point representation of the resulting point.
  1246. * @param [in] map 0 indicates to leave in projective representation.
  1247. * 1 indicates map projective point to affine.
  1248. * @return 0 on success.
  1249. * @return MEMORY_E when dynamic memory allocation fails.
  1250. * @return Other -ve value when an internal operation fails.
  1251. */
  1252. static int eccsi_mulmod_point(EccsiKey* key, const mp_int* n, ecc_point* point,
  1253. ecc_point* res, int map)
  1254. {
  1255. int err;
  1256. #ifdef WOLFSSL_HAVE_SP_ECC
  1257. #ifndef WOLFSSL_SP_NO_256
  1258. if ((key->ecc.idx != ECC_CUSTOM_IDX) &&
  1259. (ecc_sets[key->ecc.idx].id == ECC_SECP256R1)) {
  1260. err = sp_ecc_mulmod_256(n, point, res, map, key->heap);
  1261. }
  1262. else
  1263. #endif
  1264. #endif
  1265. {
  1266. EccsiKeyParams* params = &key->params;
  1267. err = wc_ecc_mulmod(n, point, res, &params->a, &params->prime, map);
  1268. }
  1269. return err;
  1270. }
  1271. /*
  1272. * Scalar multiply a point on the curve and add a.
  1273. *
  1274. * @param [in] key ECCSI key.
  1275. * @param [in] n MP integer representing scalar to multiply by.
  1276. * @param [in] point ECC point representation of a point on the curve.
  1277. * @param [in] a ECC point to add.
  1278. * @param [out] res ECC point representation of the resulting point.
  1279. * @param [in] mp Montgomery reduction multiplier.
  1280. * @param [in] map 0 indicates to leave in projective representation.
  1281. * 1 indicates map projective point to affine.
  1282. * @return 0 on success.
  1283. * @return MEMORY_E when dynamic memory allocation fails.
  1284. * @return Other -ve value when an internal operation fails.
  1285. */
  1286. static int eccsi_mulmod_point_add(EccsiKey* key, const mp_int* n,
  1287. ecc_point* point, ecc_point* a, ecc_point* res, mp_digit mp, int map)
  1288. {
  1289. #ifdef WOLFSSL_HAVE_SP_ECC
  1290. #ifndef WOLFSSL_SP_NO_256
  1291. int err = NOT_COMPILED_IN;
  1292. if ((key->ecc.idx != ECC_CUSTOM_IDX) &&
  1293. (ecc_sets[key->ecc.idx].id == ECC_SECP256R1)) {
  1294. err = sp_ecc_mulmod_add_256(n, point, a, 0, res, map, key->heap);
  1295. }
  1296. (void)mp;
  1297. return err;
  1298. #endif
  1299. #else
  1300. int err;
  1301. EccsiKeyParams* params = &key->params;
  1302. err = wc_ecc_mulmod(n, point, res, &params->a, &params->prime, 0);
  1303. if (err == 0) {
  1304. err = ecc_projective_add_point(res, a, res, &key->params.a,
  1305. &params->prime, mp);
  1306. }
  1307. if ((err == 0) && map) {
  1308. err = ecc_map(res, &params->prime, mp);
  1309. }
  1310. return err;
  1311. #endif
  1312. }
  1313. /**
  1314. * Validate an (SSV, PVT) Pair.
  1315. *
  1316. * RFC 6507, Section 5.1.2
  1317. *
  1318. * A signing client should validate the key pair before first use.
  1319. *
  1320. * @param [in] key ECCSI key.
  1321. * @param [in] hashType Type of hash algorithm. e.g. WC_SHA256
  1322. * @param [in] id Identity to create hash from.
  1323. * @param [in] idSz Length of identity in bytes.
  1324. * @param [in] ssk Secret Signing Key as an MP integer.
  1325. * @param [in] pvt Public Validation Token (PVT) as an ECC point.
  1326. * @param [out] valid 1 when pair is valid and 0 otherwise.
  1327. * @return 0 on success.
  1328. * @return BAD_FUNC_ARG when key, id, ssk, pvt or valid is NULL.
  1329. * @return BAD_STATE_E when curve not set (key not set).
  1330. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  1331. * @return IS_POINT_E when point is not on the curve.
  1332. * @return Other -ve value when an internal operation fails.
  1333. */
  1334. int wc_ValidateEccsiPair(EccsiKey* key, enum wc_HashType hashType,
  1335. const byte* id, word32 idSz, const mp_int* ssk, ecc_point* pvt,
  1336. int* valid)
  1337. {
  1338. int err = 0;
  1339. ecc_point* res = NULL;
  1340. mp_int* hs = NULL;
  1341. mp_digit mp = 0;
  1342. byte hashSz = 0;
  1343. EccsiKeyParams* params = NULL;
  1344. if ((key == NULL) || (id == NULL) || (ssk == NULL) || (pvt == NULL) ||
  1345. (valid == NULL)) {
  1346. err = BAD_FUNC_ARG;
  1347. }
  1348. if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY) &&
  1349. (key->ecc.type != ECC_PUBLICKEY)) {
  1350. err = BAD_STATE_E;
  1351. }
  1352. if (err != 0)
  1353. return err;
  1354. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  1355. params = &key->params;
  1356. hs = &key->tmp;
  1357. res = &key->pubkey.pubkey;
  1358. err = eccsi_load_base(key);
  1359. if (err == 0) {
  1360. err = eccsi_load_ecc_params(key);
  1361. }
  1362. if (err == 0) {
  1363. err = mp_montgomery_setup(&params->prime, &mp);
  1364. }
  1365. /* Step 1: Validate PVT is on curve */
  1366. if (err == 0) {
  1367. err = wc_ecc_is_point(pvt, &params->a, &params->b, &params->prime);
  1368. if (err == -1) {
  1369. err = IS_POINT_E;
  1370. }
  1371. }
  1372. /* Step 2: Compute HS = hash( G | KPAK | ID | PVT ) */
  1373. if (err == 0) {
  1374. hashSz = (byte)sizeof(key->data);
  1375. /* Converts KPAK from mont. */
  1376. err = eccsi_compute_hs(key, hashType, id, idSz, pvt, key->data,
  1377. &hashSz);
  1378. }
  1379. /* Step 3: Validate that KPAK = [SSK]G - [HS]PVT */
  1380. if (err == 0) {
  1381. err = mp_read_unsigned_bin(hs, key->data, hashSz);
  1382. }
  1383. /* [HS]PVT */
  1384. if (err == 0) {
  1385. err = eccsi_mulmod_point(key, hs, pvt, res, 0);
  1386. }
  1387. /* -[HS]PVT */
  1388. if (err == 0) {
  1389. err = mp_sub(&params->prime, res->y, res->y);
  1390. }
  1391. /* [SSK]G + -[HS]PVT */
  1392. if (err == 0) {
  1393. err = eccsi_mulmod_base_add(key, ssk, res, res, mp, 1);
  1394. }
  1395. if (valid != NULL) {
  1396. *valid = (err == 0);
  1397. if (err == 0) {
  1398. ecc_point* kpak = &key->ecc.pubkey;
  1399. /* Compare KPAK and [SSK]G + -[HS]PVT */
  1400. *valid = (wc_ecc_cmp_point(res, kpak) == MP_EQ);
  1401. }
  1402. }
  1403. RESTORE_VECTOR_REGISTERS();
  1404. return err;
  1405. }
  1406. /**
  1407. * Validate Public Validation Token (PVT) is on the curve.
  1408. *
  1409. * RFC 6507, Section 5.1.2, Step 1
  1410. *
  1411. * A verifying client should validate the PVT before first use.
  1412. *
  1413. * @param [in] key ECCSI key.
  1414. * @param [in] pvt Public Validation Token (PVT) as an ECC point.
  1415. * @param [out] valid 1 when PVT is valid and 0 otherwise.
  1416. * @return 0 on success.
  1417. * @return BAD_FUNC_ARG when key, pvt or valid is NULL.
  1418. * @return BAD_STATE_E when curve not set (key not set).
  1419. * @return MP_MEM or MEMORY_E when dynamic memory allocation fails.
  1420. * @return Other -ve value when an internal operation fails.
  1421. */
  1422. int wc_ValidateEccsiPvt(EccsiKey* key, const ecc_point* pvt, int* valid)
  1423. {
  1424. int err = 0;
  1425. if ((key == NULL)| (pvt == NULL) || (valid == NULL)) {
  1426. err = BAD_FUNC_ARG;
  1427. }
  1428. if (err == 0) {
  1429. err = wc_ecc_set_curve(&key->pubkey, key->ecc.dp->size,
  1430. key->ecc.dp->id);
  1431. }
  1432. if (err == 0) {
  1433. err = wc_ecc_copy_point(pvt, &key->pubkey.pubkey);
  1434. }
  1435. if (err == 0) {
  1436. *valid = (wc_ecc_check_key(&key->pubkey) == 0);
  1437. }
  1438. return err;
  1439. }
  1440. /**
  1441. * Creates the Hash of the ID and PVT with the ECCSI key.
  1442. *
  1443. * The hash ID is required as input to the sign and verify operations.\n
  1444. * Signing clients may cache this value.
  1445. *
  1446. * RFC 6507, Section 5.2.1, Step 3
  1447. *
  1448. * Set the calculated hash internally for use.
  1449. *
  1450. * @param [in] key ECCSI key.
  1451. * @param [in] hashType Type of hash algorithm. e.g. WC_SHA256
  1452. * @param [in] id Identity to create hash from.
  1453. * @param [in] idSz Length of identity in bytes.
  1454. * @param [in] pvt Public Validation Token (PVT) as an ECC point.
  1455. * @param [out] hash Buffer to hold hash result.
  1456. * @param [out] hashSz Length of hash data in bytes.
  1457. * @return 0 on success.
  1458. * @return BAD_FUNC_ARG when key, id, pvt, hash or hashSz is NULL.
  1459. * @return BAD_STATE_E when public key not set.
  1460. * @return MEMORY_E when dynamic memory allocation fails.
  1461. * @return Other -ve value when an internal operation fails.
  1462. */
  1463. int wc_HashEccsiId(EccsiKey* key, enum wc_HashType hashType, const byte* id,
  1464. word32 idSz, ecc_point* pvt, byte* hash, byte* hashSz)
  1465. {
  1466. int err = 0;
  1467. if ((key == NULL) || (id == NULL) || (pvt == NULL) || (hash == NULL) ||
  1468. (hashSz == NULL)) {
  1469. err = BAD_FUNC_ARG;
  1470. }
  1471. if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY) &&
  1472. (key->ecc.type != ECC_PUBLICKEY)) {
  1473. err = BAD_STATE_E;
  1474. }
  1475. /* Load the curve parameters for operations */
  1476. if (err == 0) {
  1477. err = eccsi_load_ecc_params(key);
  1478. }
  1479. if (err == 0) {
  1480. err = eccsi_compute_hs(key, hashType, id, idSz, pvt, hash, hashSz);
  1481. }
  1482. if (err == 0) {
  1483. XMEMCPY(key->idHash, hash, *hashSz);
  1484. key->idHashSz = *hashSz;
  1485. }
  1486. return err;
  1487. }
  1488. /**
  1489. * Set the identity hash for use with signing/verification.
  1490. *
  1491. * @param [in] key ECCSI key.
  1492. * @param [in] hash Buffer with hash of identity.
  1493. * @param [in] hashSz Length of hash data in bytes.
  1494. * @return 0 on success.
  1495. * @return BAD_FUNC_ARG when key or hash is NULL, or hashSz is greater than
  1496. * WC_MAX_DIGEST_SIZE.
  1497. */
  1498. int wc_SetEccsiHash(EccsiKey* key, const byte* hash, byte hashSz)
  1499. {
  1500. int err = 0;
  1501. if ((key == NULL) || (hash == NULL) || (hashSz > WC_MAX_DIGEST_SIZE)) {
  1502. err = BAD_FUNC_ARG;
  1503. }
  1504. if (err == 0) {
  1505. XMEMCPY(key->idHash, hash, hashSz);
  1506. key->idHashSz = hashSz;
  1507. }
  1508. return err;
  1509. }
  1510. /**
  1511. * Set an (SSV, PVT) Pair for signing.
  1512. *
  1513. * @param [in] key ECCSI key.
  1514. * @param [in] ssk Secret Signing Key as an MP integer.
  1515. * @param [in] pvt Public Validation Token (PVT) as an ECC point.
  1516. * @return 0 on success.
  1517. * @return BAD_FUNC_ARG when key, ssk or pvt is NULL.
  1518. * @return MP math errors when copy fails
  1519. */
  1520. int wc_SetEccsiPair(EccsiKey* key, const mp_int* ssk, const ecc_point* pvt)
  1521. {
  1522. int err = 0;
  1523. if ((key == NULL) || (ssk == NULL) || (pvt == NULL)) {
  1524. err = BAD_FUNC_ARG;
  1525. }
  1526. if (err == 0) {
  1527. err = mp_copy(ssk, &key->ssk);
  1528. }
  1529. if (err == 0) {
  1530. err = wc_ecc_copy_point(pvt, key->pvt);
  1531. }
  1532. return err;
  1533. }
  1534. #ifdef ECCSI_ORDER_MORE_BITS_THAN_PRIME
  1535. /*
  1536. * Fit the number to the maximum number of bytes.
  1537. *
  1538. * If the number is too big then subtract from order.
  1539. * RFC 6507, Section 5.2.1, Note at end.
  1540. * This should only happen when order is larger than prime in bits.
  1541. *
  1542. * @param [in] a MP integer to fix.
  1543. * @param [in] order MP integer representing order of curve.
  1544. * @param [in] m Maximum number of bytes to encode into.
  1545. * @param [out] r MP integer that is the result after fixing.
  1546. * @return 0 on success.
  1547. * @return MEMORY_E when dynamic memory allocation fails.
  1548. */
  1549. static int eccsi_fit_to_octets(const mp_int* a, mp_int* order, int m,
  1550. mp_int* r)
  1551. {
  1552. int err;
  1553. if (mp_count_bits(a) > m * 8) {
  1554. err = mp_sub(order, (mp_int*)a, r);
  1555. }
  1556. else
  1557. {
  1558. err = mp_copy(a, r);
  1559. }
  1560. return err;
  1561. }
  1562. #else
  1563. /*
  1564. * Fit the number to the maximum number of bytes.
  1565. *
  1566. * If the number is too big then subtract from order.
  1567. * RFC 6507, Section 5.2.1, Note at end.
  1568. * This should only happen when order is larger than prime in bits.
  1569. *
  1570. * @param [in] a MP integer to fix.
  1571. * @param [in] order MP integer representing order of curve.
  1572. * @param [in] m Maximum number of bytes to encode into.
  1573. * @param [out] r MP integer that is the result after fixing.
  1574. * @return 0 on success.
  1575. * @return MEMORY_E when dynamic memory allocation fails.
  1576. */
  1577. static int eccsi_fit_to_octets(const mp_int* a, const mp_int* order, int m,
  1578. mp_int* r)
  1579. {
  1580. (void)order;
  1581. (void)m;
  1582. /* Duplicate line to stop static analyzer complaining. */
  1583. return mp_copy(a, r);
  1584. }
  1585. #endif
  1586. /*
  1587. * Compute the HE = hash( HS | r | M ), hash value of signature.
  1588. *
  1589. * Partial result required for signing and verification.
  1590. *
  1591. * @param [in] key ECCSI key.
  1592. * @param [in] hashType Type of hash algorithm. e.g. WC_SHA256
  1593. * @param [in] r MP integer that is the first signature element.
  1594. * @param [in] msg Message of signature.
  1595. * @param [in] msgSz Length of message in bytes.
  1596. * @param [out] he Signature hash.
  1597. * @param [out] heSz Length of signature hash in bytes
  1598. * @return 0 on success.
  1599. * @return MEMORY_E when dynamic memory allocation fails.
  1600. * @return Other -ve value when an internal operation fails.
  1601. */
  1602. static int eccsi_compute_he(EccsiKey* key, enum wc_HashType hashType,
  1603. mp_int* r, const byte* msg, word32 msgSz, byte* he, word32* heSz)
  1604. {
  1605. int err = 0;
  1606. word32 dataSz = key->ecc.dp->size;
  1607. int hash_inited = 0;
  1608. /* HE = hash( HS | r | M ) */
  1609. err = wc_HashInit_ex(&key->hash, hashType, key->heap, INVALID_DEVID);
  1610. if (err == 0) {
  1611. hash_inited = 1;
  1612. /* HS */
  1613. err = wc_HashUpdate(&key->hash, hashType, key->idHash, key->idHashSz);
  1614. }
  1615. if (err == 0) {
  1616. err = mp_to_unsigned_bin_len(r, key->data, dataSz);
  1617. }
  1618. if (err == 0) {
  1619. /* r */
  1620. err = wc_HashUpdate(&key->hash, hashType, key->data, dataSz);
  1621. }
  1622. if (err == 0) {
  1623. /* M */
  1624. err = wc_HashUpdate(&key->hash, hashType, msg, msgSz);
  1625. }
  1626. if (err == 0) {
  1627. err = wc_HashFinal(&key->hash, hashType, he);
  1628. }
  1629. if (err == 0) {
  1630. *heSz = wc_HashGetDigestSize(hashType);
  1631. }
  1632. if (hash_inited) {
  1633. (void)wc_HashFree(&key->hash, hashType);
  1634. }
  1635. return err;
  1636. }
  1637. /*
  1638. * Encode the signature = ( r | s | PVT )
  1639. *
  1640. * @param [in] key ECCSI key.
  1641. * @param [in] r MP integer that is the first signature element.
  1642. * @param [in] s MP integer that is the second signature element.
  1643. * @param [in] pvt ECC point representing Public Validation Token.
  1644. * @param [out] sig Signature of message.
  1645. * @param [out] sigSz Length of signature in bytes.
  1646. */
  1647. static int eccsi_encode_sig(const EccsiKey* key, mp_int* r, mp_int* s,
  1648. byte* sig, word32* sigSz)
  1649. {
  1650. int err;
  1651. word32 sz = key->ecc.dp->size;
  1652. err = mp_to_unsigned_bin_len(r, sig, sz);
  1653. if (err == 0) {
  1654. err = mp_to_unsigned_bin_len(s, sig + sz, sz);
  1655. }
  1656. if (err == 0) {
  1657. *sigSz = key->ecc.dp->size * 2 + 1;
  1658. err = wc_ecc_export_point_der(wc_ecc_get_curve_idx(key->ecc.dp->id),
  1659. key->pvt, sig + sz * 2, sigSz);
  1660. }
  1661. if (err == 0) {
  1662. *sigSz = sz * 4 + 1;
  1663. }
  1664. return err;
  1665. }
  1666. /*
  1667. * Sign the ECCSI hash (of ID with the key) to two mp_int objects: r and s.
  1668. *
  1669. * RFC 6507, Section 5.2.1, Steps 1 to 4
  1670. *
  1671. * @param [in] key ECCSI key.
  1672. * @param [in] rng Random number generator.
  1673. * @param [in] hashType Type of hash algorithm. e.g. WC_SHA256
  1674. * @param [in] msg Message to sign.
  1675. * @param [in] msgSz Length of message in bytes.
  1676. * @param [out] r First big number integer part of signature.
  1677. * @param [out] s Second big number integer part of signature.
  1678. * @return 0 on success.
  1679. * @return MEMORY_E when dynamic memory allocation fails.
  1680. * @return Other -ve value when an internal operation fails.
  1681. */
  1682. static int eccsi_gen_sig(EccsiKey* key, WC_RNG* rng, enum wc_HashType hashType,
  1683. const byte* msg, word32 msgSz, mp_int* r, mp_int* s)
  1684. {
  1685. int err = 0;
  1686. word32 sz = key->ecc.dp->size;
  1687. word32 heSz = 0;
  1688. const mp_int* jx = NULL;
  1689. mp_int* he = &key->tmp;
  1690. int genTryCnt = 0;
  1691. do {
  1692. /* Don't infinitely gen sigs when random number generator fails. */
  1693. if ((++genTryCnt) > ECCSI_MAX_GEN_COUNT) {
  1694. err = RNG_FAILURE_E;
  1695. }
  1696. if (err == 0) {
  1697. wc_ecc_free(&key->pubkey);
  1698. /* Step 1 and 2: Generate ephemeral key - j, J = [j]G, r = Jx */
  1699. err = wc_ecc_make_key_ex(rng, sz, &key->pubkey, key->ecc.dp->id);
  1700. }
  1701. if (err == 0) {
  1702. jx = key->pubkey.pubkey.x;
  1703. err = eccsi_fit_to_octets(jx, &key->params.order, sz, r);
  1704. }
  1705. /* Step 3: Compute HE = hash( HS | r | M ) */
  1706. if (err == 0) {
  1707. err = eccsi_compute_he(key, hashType, r, msg, msgSz, key->data,
  1708. &heSz);
  1709. }
  1710. /* Step 4: Verify that HE + r * SSK is non-zero modulo q */
  1711. if (err == 0) {
  1712. err = mp_read_unsigned_bin(he, key->data, heSz);
  1713. }
  1714. /* s' = r * SSK */
  1715. if (err == 0) {
  1716. err = mp_mulmod(r, &key->ssk, &key->params.order, s);
  1717. }
  1718. /* s' = HE + r * SSK */
  1719. if (err == 0) {
  1720. err = mp_addmod(he, s, &key->params.order, s);
  1721. }
  1722. }
  1723. while ((err == 0) && (mp_iszero(s) || (mp_cmp(s, he) == MP_EQ)));
  1724. return err;
  1725. }
  1726. /**
  1727. * Sign the ECCSI hash (of ID with the key).
  1728. *
  1729. * RFC 6507, Section 5.2.1
  1730. *
  1731. * Must have imported KPAK using wc_ImportEccsiPublicKey() before calling.\n
  1732. * Use wc_HashEccsiId() to calculate the hash and wc_SetEccsiHash() to set
  1733. * the identity hash to use.
  1734. *
  1735. * @param [in] key ECCSI key.
  1736. * @param [in] rng Random number generator.
  1737. * @param [in] hashType Type of hash algorithm. e.g. WC_SHA256
  1738. * @param [in] msg Message to sign.
  1739. * @param [in] msgSz Length of message in bytes.
  1740. * @param [out] sig Signature of message.
  1741. * @param [out] sigSz Length of signature in bytes.
  1742. * @return 0 on success.
  1743. * @return BAD_FUNC_ARG when key, rng, msg or sigSz is NULL.
  1744. * @return BAD_STATE_E when the curve or id hash has not been set (no key set).
  1745. * @return LENGTH_ONLY_E when sig is NULL - sigSz is set.
  1746. * @return MEMORY_E when dynamic memory allocation fails.
  1747. * @return Other -ve value when an internal operation fails.
  1748. */
  1749. int wc_SignEccsiHash(EccsiKey* key, WC_RNG* rng, enum wc_HashType hashType,
  1750. const byte* msg, word32 msgSz, byte* sig, word32* sigSz)
  1751. {
  1752. int err = 0;
  1753. mp_int* r = NULL;
  1754. mp_int* s = NULL;
  1755. mp_int* j = NULL;
  1756. word32 sz = 0;
  1757. if ((key == NULL) || (rng == NULL) || (msg == NULL) || (sigSz == NULL)) {
  1758. err = BAD_FUNC_ARG;
  1759. }
  1760. if ((err == 0) && (key->ecc.type != ECC_PUBLICKEY) &&
  1761. (key->ecc.type != ECC_PRIVATEKEY)) {
  1762. err = BAD_STATE_E;
  1763. }
  1764. if ((err == 0) && (sig != NULL) && (key->idHashSz == 0)) {
  1765. err = BAD_STATE_E;
  1766. }
  1767. if (err == 0) {
  1768. sz = key->ecc.dp->size;
  1769. if (sig == NULL) {
  1770. *sigSz = sz * 4 + 1;
  1771. err = LENGTH_ONLY_E;
  1772. }
  1773. }
  1774. if ((err == 0) && (*sigSz < sz * 4 + 1)) {
  1775. err = BAD_FUNC_ARG;
  1776. }
  1777. if (err == 0) {
  1778. r = key->pubkey.pubkey.y;
  1779. s = key->pubkey.pubkey.z;
  1780. err = eccsi_load_order(key);
  1781. }
  1782. if (err == 0) {
  1783. /* Steps 1 to 4. */
  1784. err = eccsi_gen_sig(key, rng, hashType, msg, msgSz, r, s);
  1785. }
  1786. /* Step 5: s' = ( (( HE + r * SSK )^-1) * j ) modulo q, erase j */
  1787. if (err == 0) {
  1788. err = mp_invmod(s, &key->params.order, s);
  1789. }
  1790. if (err == 0) {
  1791. j = &key->pubkey.k;
  1792. err = mp_mulmod(s, j, &key->params.order, s);
  1793. }
  1794. if (err == 0) {
  1795. mp_forcezero(j);
  1796. /* Step 6: s = s' fitted */
  1797. err = eccsi_fit_to_octets(s, &key->params.order, sz, s);
  1798. }
  1799. /* Step 7: Output Signature = ( r | s | PVT ) */
  1800. if (err == 0) {
  1801. err = eccsi_encode_sig(key, r, s, sig, sigSz);
  1802. }
  1803. return err;
  1804. }
  1805. /*
  1806. * Decode the s part of the signature = ( r | s | PVT )
  1807. *
  1808. * @param [in] key ECCSI key.
  1809. * @param [in] sig Signature of message.
  1810. * @param [in] sigSz Length of signature in bytes.
  1811. * @param [out] s MP integer that is the second signature element.
  1812. * @return 0 on success.
  1813. * @return MEMORY_E when dynamic memory allocation fails.
  1814. * @return Other -ve value when an internal operation fails.
  1815. */
  1816. static int eccsi_decode_sig_s(const EccsiKey* key, const byte* sig,
  1817. word32 sigSz, mp_int* s)
  1818. {
  1819. int err = 0;
  1820. word32 sz = key->ecc.dp->size;
  1821. if (sigSz != sz * 4 + 1) {
  1822. err = BAD_FUNC_ARG;
  1823. }
  1824. if (err == 0) {
  1825. err = mp_read_unsigned_bin(s, sig + sz, sz);
  1826. }
  1827. return err;
  1828. }
  1829. /*
  1830. * Decode the r and pvt part of the signature = ( r | s | PVT )
  1831. *
  1832. * @param [in] key ECCSI key.
  1833. * @param [in] sig Signature of message.
  1834. * @param [in] sigSz Length of signature in bytes.
  1835. * @param [out] r MP integer that is the first signature element.
  1836. * @param [out] pvt ECC point representing Public Validation Token.
  1837. * @return 0 on success.
  1838. * @return MEMORY_E when dynamic memory allocation fails.
  1839. * @return Other -ve value when an internal operation fails.
  1840. */
  1841. static int eccsi_decode_sig_r_pvt(const EccsiKey* key, const byte* sig,
  1842. word32 sigSz, mp_int* r, ecc_point* pvt)
  1843. {
  1844. int err = 0;
  1845. word32 sz = key->ecc.dp->size;
  1846. if (sigSz != sz * 4 + 1) {
  1847. err = BAD_FUNC_ARG;
  1848. }
  1849. if (err == 0) {
  1850. err = mp_read_unsigned_bin(r, sig, sz);
  1851. }
  1852. if (err == 0) {
  1853. /* must free previous public point otherwise wc_ecc_import_point_der
  1854. * could leak memory */
  1855. mp_clear(pvt->x);
  1856. mp_clear(pvt->y);
  1857. mp_clear(pvt->z);
  1858. err = wc_ecc_import_point_der(sig + sz * 2, sz * 2 + 1,
  1859. wc_ecc_get_curve_idx(key->ecc.dp->id), pvt);
  1860. }
  1861. return err;
  1862. }
  1863. /*
  1864. * Calculate Y point as part of verification process.
  1865. *
  1866. * Y = [HS]PVT + KPAK
  1867. *
  1868. * @param [in] key ECCSI key.
  1869. * @param [in] pvt ECC point representing Public Validation Token.
  1870. * @param [in] mp Montgomery reduction multiplier.
  1871. * @param [out] y ECC point representing calculated value Y.
  1872. * @return 0 on success.
  1873. * @return MEMORY_E when dynamic memory allocation fails.
  1874. * @return Other value when an an internal operation fails.
  1875. */
  1876. static int eccsi_calc_y(EccsiKey* key, ecc_point* pvt, mp_digit mp,
  1877. ecc_point* y)
  1878. {
  1879. int err;
  1880. mp_int* hs = &key->ssk;
  1881. err = mp_read_unsigned_bin(hs, key->idHash, key->idHashSz);
  1882. #ifndef WOLFSSL_HAVE_SP_ECC
  1883. /* Need KPAK in montgomery form. */
  1884. if (err == 0) {
  1885. err = eccsi_kpak_to_mont(key);
  1886. }
  1887. #endif
  1888. /* [HS]PVT + KPAK */
  1889. if (err == 0) {
  1890. ecc_point* kpak = &key->ecc.pubkey;
  1891. err = eccsi_mulmod_point_add(key, hs, pvt, kpak, y, mp, 1);
  1892. }
  1893. return err;
  1894. }
  1895. /*
  1896. * Calculate J point as part of verification process.
  1897. *
  1898. * J = [s]( [HE]G + [r]Y )
  1899. *
  1900. * @param [in] key ECCSI key.
  1901. * @param [in] hem MP int representation of HE = Hash (hs, r and message).
  1902. * @param [in] sig Signature of message.
  1903. * @param [in] sigSz Length of signature in bytes.
  1904. * @param [in] y ECC point representing [r]Y.
  1905. * @param [in] mp Montgomery reduction multiplier.
  1906. * @param [out] j ECC point representing calculated value J.
  1907. * @return 0 on success.
  1908. * @return MEMORY_E when dynamic memory allocation fails.
  1909. * @return Other value when an an internal operation fails.
  1910. */
  1911. static int eccsi_calc_j(EccsiKey* key, const mp_int* hem, const byte* sig,
  1912. word32 sigSz, ecc_point* y, mp_digit mp, ecc_point* j)
  1913. {
  1914. int err;
  1915. mp_int* s = &key->tmp;
  1916. /* [HE]G + [r]Y */
  1917. err = eccsi_mulmod_base_add(key, hem, y, j, mp, 1);
  1918. if (err == 0) {
  1919. err = eccsi_decode_sig_s(key, sig, sigSz, s);
  1920. }
  1921. /* [s]( [HE]G + [r]Y ) */
  1922. if (err == 0) {
  1923. err = eccsi_mulmod_point(key, s, j, j, 1);
  1924. }
  1925. return err;
  1926. }
  1927. /**
  1928. * Verify the ECCSI hash (of ID with the key).
  1929. *
  1930. * RFC 6507, Section 5.2.2
  1931. *
  1932. * Must have imported KPAK using wc_ImportEccsiPublicKey() before calling.\n
  1933. * Use wc_HashEccsiId() to calculate the hash and wc_SetEccsiHash() to set
  1934. * the identity hash to use.
  1935. *
  1936. * @param [in] key ECCSI key.
  1937. * @param [in] hashType Type of hash algorithm. e.g. WC_SHA256
  1938. * @param [in] msg Message to verify.
  1939. * @param [in] msgSz Length of message in bytes.
  1940. * @param [in] sig Signature of message.
  1941. * @param [in] sigSz Length of signature in bytes.
  1942. * @param [out] verified 1 when the signature was verified and 0 otherwise.
  1943. * @return 0 on success.
  1944. * @return BAD_FUNC_ARG when key, hash, msg, sig or ret is NULL.
  1945. * @return BAD_STATE_E when the curve or id hash has not been set (no key set).
  1946. * @return MEMORY_E when dynamic memory allocation fails.
  1947. * @return Other value when an an internal operation fails.
  1948. */
  1949. int wc_VerifyEccsiHash(EccsiKey* key, enum wc_HashType hashType,
  1950. const byte* msg, word32 msgSz, const byte* sig, word32 sigSz,
  1951. int* verified)
  1952. {
  1953. int err = 0;
  1954. byte* he = NULL;
  1955. word32 heSz = 0;
  1956. mp_int* r = NULL;
  1957. mp_int* jx = NULL;
  1958. mp_int* hem = NULL;
  1959. ecc_point* pvt = NULL;
  1960. ecc_point* y = NULL;
  1961. ecc_point* j = NULL;
  1962. mp_digit mp = 0;
  1963. EccsiKeyParams* params = NULL;
  1964. if ((key == NULL) || (msg == NULL) || (sig == NULL) || (verified == NULL)) {
  1965. err = BAD_FUNC_ARG;
  1966. }
  1967. if ((err == 0) && (key->ecc.type != ECC_PRIVATEKEY) &&
  1968. (key->ecc.type != ECC_PUBLICKEY)) {
  1969. err = BAD_STATE_E;
  1970. }
  1971. if ((err == 0) && (key->idHashSz == 0)) {
  1972. err = BAD_STATE_E;
  1973. }
  1974. if (err != 0)
  1975. return err;
  1976. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  1977. /* Decode the signature into components. */
  1978. r = &key->pubkey.k;
  1979. pvt = &key->pubkey.pubkey;
  1980. err = eccsi_decode_sig_r_pvt(key, sig, sigSz, r, pvt);
  1981. /* Load the curve parameters for operations */
  1982. if (err == 0) {
  1983. err = eccsi_load_base(key);
  1984. }
  1985. if (err == 0) {
  1986. err = eccsi_load_ecc_params(key);
  1987. }
  1988. if (err == 0) {
  1989. params = &key->params;
  1990. err = mp_montgomery_setup(&params->prime, &mp);
  1991. }
  1992. /* Step 1: Validate PVT is on curve */
  1993. if (err == 0) {
  1994. err = wc_ecc_is_point(pvt, &params->a, &params->b, &params->prime);
  1995. }
  1996. /* Step 2: Compute HS = hash( G | KPAK | ID | PVT )
  1997. * HS is key->idHash, key->idHashSz */
  1998. /* Step 3: Compute HE = hash( HS | r | M ) */
  1999. if (err == 0) {
  2000. he = key->data;
  2001. err = eccsi_compute_he(key, hashType, r, msg, msgSz, he, &heSz);
  2002. }
  2003. /* Step 4: Y = [HS]PVT + KPAK */
  2004. if (err == 0) {
  2005. y = pvt;
  2006. err = eccsi_calc_y(key, pvt, mp, y);
  2007. }
  2008. /* Step 5: Compute J = [s]( [HE]G + [r]Y ) */
  2009. /* [r]Y */
  2010. if (err == 0) {
  2011. hem = &key->tmp;
  2012. err = mp_read_unsigned_bin(hem, he, heSz);
  2013. }
  2014. if (err == 0) {
  2015. err = eccsi_mulmod_point(key, r, y, y, 0);
  2016. }
  2017. if (err == 0) {
  2018. j = params->base;
  2019. err = eccsi_calc_j(key, hem, sig, sigSz, y, mp, j);
  2020. key->params.haveBase = 0;
  2021. }
  2022. /* Step 6: Jx fitting, compare with r */
  2023. if (err == 0) {
  2024. jx = &key->tmp;
  2025. err = eccsi_fit_to_octets(j->x, &params->order, key->ecc.dp->size, jx);
  2026. }
  2027. if (verified != NULL) {
  2028. *verified = ((err == 0) && (mp_cmp(jx, r) == MP_EQ));
  2029. }
  2030. RESTORE_VECTOR_REGISTERS();
  2031. return err;
  2032. }
  2033. #endif /* WOLFCRYPT_ECCSI_CLIENT */
  2034. #endif /* WOLFCRYPT_HAVE_ECCSI */