2
0

ecc.h 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  1. /*!
  2. \ingroup ECC
  3. \brief This function generates a new ecc_key and stores it in key.
  4. \return 0 Returned on success.
  5. \return ECC_BAD_ARG_E Returned if rng or key evaluate to NULL
  6. \return BAD_FUNC_ARG Returned if the specified key size is not in the
  7. correct range of supported keys
  8. \return MEMORY_E Returned if there is an error allocating memory while
  9. computing the ecc key
  10. \return MP_INIT_E may be returned if there is an error while computing
  11. the ecc key
  12. \return MP_READ_E may be returned if there is an error while computing
  13. the ecc key
  14. \return MP_CMP_E may be returned if there is an error while computing the
  15. ecc key
  16. \return MP_INVMOD_E may be returned if there is an error while computing
  17. the ecc key
  18. \return MP_EXPTMOD_E may be returned if there is an error while computing
  19. the ecc key
  20. \return MP_MOD_E may be returned if there is an error while computing the
  21. ecc key
  22. \return MP_MUL_E may be returned if there is an error while computing the
  23. ecc key
  24. \return MP_ADD_E may be returned if there is an error while computing the
  25. ecc key
  26. \return MP_MULMOD_E may be returned if there is an error while computing
  27. the ecc key
  28. \return MP_TO_E may be returned if there is an error while computing the
  29. ecc key
  30. \return MP_MEM may be returned if there is an error while computing the
  31. ecc key
  32. \param rng pointer to an initialized RNG object with which to generate
  33. the key
  34. \param keysize desired length for the ecc_key
  35. \param key pointer to the ecc_key for which to generate a key
  36. _Example_
  37. \code
  38. ecc_key key;
  39. wc_ecc_init(&key);
  40. WC_WC_RNG rng;
  41. wc_InitRng(&rng);
  42. wc_ecc_make_key(&rng, 32, &key); // initialize 32 byte ecc key
  43. \endcode
  44. \sa wc_ecc_init
  45. \sa wc_ecc_shared_secret
  46. */
  47. WOLFSSL_API
  48. int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key);
  49. /*!
  50. \ingroup ECC
  51. \brief This function generates a new ecc_key and stores it in key.
  52. \return 0 Returned on success.
  53. \return ECC_BAD_ARG_E Returned if rng or key evaluate to NULL
  54. \return BAD_FUNC_ARG Returned if the specified key size is not in the
  55. correct range of supported keys
  56. \return MEMORY_E Returned if there is an error allocating memory while
  57. computing the ecc key
  58. \return MP_INIT_E may be returned if there is an error while computing
  59. the ecc key
  60. \return MP_READ_E may be returned if there is an error while computing
  61. the ecc key
  62. \return MP_CMP_E may be returned if there is an error while computing the
  63. ecc key
  64. \return MP_INVMOD_E may be returned if there is an error while computing
  65. the ecc key
  66. \return MP_EXPTMOD_E may be returned if there is an error while computing
  67. the ecc key
  68. \return MP_MOD_E may be returned if there is an error while computing the
  69. ecc key
  70. \return MP_MUL_E may be returned if there is an error while computing the
  71. ecc key
  72. \return MP_ADD_E may be returned if there is an error while computing the
  73. ecc key
  74. \return MP_MULMOD_E may be returned if there is an error while computing
  75. the ecc key
  76. \return MP_TO_E may be returned if there is an error while computing the
  77. ecc key
  78. \return MP_MEM may be returned if there is an error while computing the
  79. ecc key
  80. \param key Pointer to store the created key.
  81. \param keysize size of key to be created in bytes, set based on curveId
  82. \param rng Rng to be used in key creation
  83. \param curve_id Curve to use for key
  84. _Example_
  85. \code
  86. ecc_key key;
  87. int ret;
  88. WC_WC_RNG rng;
  89. wc_ecc_init(&key);
  90. wc_InitRng(&rng);
  91. int curveId = ECC_SECP521R1;
  92. int keySize = wc_ecc_get_curve_size_from_id(curveId);
  93. ret = wc_ecc_make_key_ex(&rng, keySize, &key, curveId);
  94. if (ret != MP_OKAY) {
  95. // error handling
  96. }
  97. \endcode
  98. \sa wc_ecc_make_key
  99. \sa wc_ecc_get_curve_size_from_id
  100. */
  101. WOLFSSL_API
  102. int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id);
  103. /*!
  104. \ingroup ECC
  105. \brief Perform sanity checks on ecc key validity.
  106. \return MP_OKAY Success, key is OK.
  107. \return BAD_FUNC_ARG Returns if key is NULL.
  108. \return ECC_INF_E Returns if wc_ecc_point_is_at_infinity returns 1.
  109. \param key Pointer to key to check.
  110. _Example_
  111. \code
  112. ecc_key key;
  113. WC_WC_RNG rng;
  114. int check_result;
  115. wc_ecc_init(&key);
  116. wc_InitRng(&rng);
  117. wc_ecc_make_key(&rng, 32, &key);
  118. check_result = wc_ecc_check_key(&key);
  119. if (check_result == MP_OKAY)
  120. {
  121. // key check succeeded
  122. }
  123. else
  124. {
  125. // key check failed
  126. }
  127. \endcode
  128. \sa wc_ecc_point_is_at_infinity
  129. */
  130. WOLFSSL_API
  131. int wc_ecc_check_key(ecc_key* key);
  132. /*!
  133. \ingroup ECC
  134. \brief This function generates a new secret key using a local private key
  135. and a received public key. It stores this shared secret key in the buffer
  136. out and updates outlen to hold the number of bytes written to the output
  137. buffer.
  138. \return 0 Returned upon successfully generating a shared secret key
  139. \return BAD_FUNC_ARG Returned if any of the input parameters evaluate to
  140. NULL
  141. \return ECC_BAD_ARG_E Returned if the type of the private key given as
  142. argument, private_key, is not ECC_PRIVATEKEY, or if the public and private
  143. key types (given by ecc->dp) are not equivalent
  144. \return MEMORY_E Returned if there is an error generating a new ecc point
  145. \return BUFFER_E Returned if the generated shared secret key is too long
  146. to store in the provided buffer
  147. \return MP_INIT_E may be returned if there is an error while computing the
  148. shared key
  149. \return MP_READ_E may be returned if there is an error while computing the
  150. shared key
  151. \return MP_CMP_E may be returned if there is an error while computing the
  152. shared key
  153. \return MP_INVMOD_E may be returned if there is an error while computing
  154. the shared key
  155. \return MP_EXPTMOD_E may be returned if there is an error while computing
  156. the shared key
  157. \return MP_MOD_E may be returned if there is an error while computing the
  158. shared key
  159. \return MP_MUL_E may be returned if there is an error while computing the
  160. shared key
  161. \return MP_ADD_E may be returned if there is an error while computing the
  162. shared key
  163. \return MP_MULMOD_E may be returned if there is an error while computing
  164. the shared key
  165. \return MP_TO_E may be returned if there is an error while computing the
  166. shared key
  167. \return MP_MEM may be returned if there is an error while computing the
  168. shared key
  169. \param private_key pointer to the ecc_key structure containing the local
  170. private key
  171. \param public_key pointer to the ecc_key structure containing the received
  172. public key
  173. \param out pointer to an output buffer in which to store the generated
  174. shared secret key
  175. \param outlen pointer to the word32 object containing the length of the
  176. output buffer. Will be overwritten with the length written to the output
  177. buffer upon successfully generating a shared secret key
  178. _Example_
  179. \code
  180. ecc_key priv, pub;
  181. WC_WC_RNG rng;
  182. byte secret[1024]; // can hold 1024 byte shared secret key
  183. word32 secretSz = sizeof(secret);
  184. int ret;
  185. wc_InitRng(&rng); // initialize rng
  186. wc_ecc_init(&priv); // initialize key
  187. wc_ecc_make_key(&rng, 32, &priv); // make public/private key pair
  188. // receive public key, and initialise into pub
  189. ret = wc_ecc_shared_secret(&priv, &pub, secret, &secretSz);
  190. // generate secret key
  191. if ( ret != 0 ) {
  192. // error generating shared secret key
  193. }
  194. \endcode
  195. \sa wc_ecc_init
  196. \sa wc_ecc_make_key
  197. */
  198. WOLFSSL_API
  199. int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
  200. word32* outlen);
  201. /*!
  202. \ingroup ECC
  203. \brief Create an ECC shared secret between private key and public point.
  204. \return MP_OKAY Indicates success.
  205. \return BAD_FUNC_ARG Error returned when any arguments are null.
  206. \return ECC_BAD_ARG_E Error returned if private_key->type is not
  207. ECC_PRIVATEKEY or private_key->idx fails to validate.
  208. \return BUFFER_E Error when outlen is too small.
  209. \return MEMORY_E Error to create a new point.
  210. \return MP_VAL possible when an initialization failure occurs.
  211. \return MP_MEM possible when an initialization failure occurs.
  212. \param private_key The private ECC key.
  213. \param point The point to use (public key).
  214. \param out Output destination of the shared secret. Conforms to
  215. EC-DH from ANSI X9.63.
  216. \param outlen Input the max size and output the resulting size of
  217. the shared secret.
  218. _Example_
  219. \code
  220. ecc_key key;
  221. ecc_point* point;
  222. byte shared_secret[];
  223. int secret_size;
  224. int result;
  225. point = wc_ecc_new_point();
  226. result = wc_ecc_shared_secret_ex(&key, point,
  227. &shared_secret, &secret_size);
  228. if (result != MP_OKAY)
  229. {
  230. // Handle error
  231. }
  232. \endcode
  233. \sa wc_ecc_verify_hash_ex
  234. */
  235. WOLFSSL_API
  236. int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
  237. byte* out, word32 *outlen);
  238. /*!
  239. \ingroup ECC
  240. \brief This function signs a message digest using an ecc_key object to
  241. guarantee authenticity.
  242. \return 0 Returned upon successfully generating a signature for the
  243. message digest
  244. \return BAD_FUNC_ARG Returned if any of the input parameters evaluate to
  245. NULL, or if the output buffer is too small to store the generated signature
  246. \return ECC_BAD_ARG_E Returned if the input key is not a private key, or
  247. if the ECC OID is invalid
  248. \return RNG_FAILURE_E Returned if the rng cannot successfully generate a
  249. satisfactory key
  250. \return MP_INIT_E may be returned if there is an error while computing
  251. the message signature
  252. \return MP_READ_E may be returned if there is an error while computing
  253. the message signature
  254. \return MP_CMP_E may be returned if there is an error while computing the
  255. message signature
  256. \return MP_INVMOD_E may be returned if there is an error while computing
  257. the message signature
  258. \return MP_EXPTMOD_E may be returned if there is an error while computing
  259. the message signature
  260. \return MP_MOD_E may be returned if there is an error while computing the
  261. message signature
  262. \return MP_MUL_E may be returned if there is an error while computing the
  263. message signature
  264. \return MP_ADD_E may be returned if there is an error while computing the
  265. message signature
  266. \return MP_MULMOD_E may be returned if there is an error while computing
  267. the message signature
  268. \return MP_TO_E may be returned if there is an error while computing the
  269. message signature
  270. \return MP_MEM may be returned if there is an error while computing the
  271. message signature
  272. \param in pointer to the buffer containing the message hash to sign
  273. \param inlen length of the message hash to sign
  274. \param out buffer in which to store the generated signature
  275. \param outlen max length of the output buffer. Will store the bytes
  276. written to out upon successfully generating a message signature
  277. \param key pointer to a private ECC key with which to generate the
  278. signature
  279. _Example_
  280. \code
  281. ecc_key key;
  282. WC_WC_RNG rng;
  283. int ret, sigSz;
  284. byte sig[512]; // will hold generated signature
  285. sigSz = sizeof(sig);
  286. byte digest[] = { // initialize with message hash };
  287. wc_InitRng(&rng); // initialize rng
  288. wc_ecc_init(&key); // initialize key
  289. wc_ecc_make_key(&rng, 32, &key); // make public/private key pair
  290. ret = wc_ecc_sign_hash(digest, sizeof(digest), sig, &sigSz, &key);
  291. if ( ret != 0 ) {
  292. // error generating message signature
  293. }
  294. \endcode
  295. \sa wc_ecc_verify_hash
  296. */
  297. WOLFSSL_API
  298. int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
  299. WC_RNG* rng, ecc_key* key);
  300. /*!
  301. \ingroup ECC
  302. \brief Sign a message digest.
  303. \return MP_OKAY Returned upon successfully generating a signature for the
  304. message digest
  305. \return ECC_BAD_ARG_E Returned if the input key is not a private key, or
  306. if the ECC IDX is invalid, or if any of the input parameters evaluate to
  307. NULL, or if the output buffer is too small to store the generated signature
  308. \return RNG_FAILURE_E Returned if the rng cannot successfully generate a
  309. satisfactory key
  310. \return MP_INIT_E may be returned if there is an error while computing the
  311. message signature
  312. \return MP_READ_E may be returned if there is an error while computing the
  313. message signature
  314. \return MP_CMP_E may be returned if there is an error while computing the
  315. message signature
  316. \return MP_INVMOD_E may be returned if there is an error while computing
  317. the message signature
  318. \return MP_EXPTMOD_E may be returned if there is an error while computing
  319. the message signature
  320. \return MP_MOD_E may be returned if there is an error while computing the
  321. message signature
  322. \return MP_MUL_E may be returned if there is an error while computing the
  323. message signature
  324. \return MP_ADD_E may be returned if there is an error while computing the
  325. message signature
  326. \return MP_MULMOD_E may be returned if there is an error while computing
  327. the message signature
  328. \return MP_TO_E may be returned if there is an error while computing the
  329. message signature
  330. \return MP_MEM may be returned if there is an error while computing the
  331. message signature
  332. \param in The message digest to sign.
  333. \param inlen The length of the digest.
  334. \param rng Pointer to WC_RNG struct.
  335. \param key A private ECC key.
  336. \param r The destination for r component of the signature.
  337. \param s The destination for s component of the signature.
  338. _Example_
  339. \code
  340. ecc_key key;
  341. WC_WC_WC_RNG rng;
  342. int ret, sigSz;
  343. mp_int r; // destination for r component of signature.
  344. mp_int s; // destination for s component of signature.
  345. byte sig[512]; // will hold generated signature
  346. sigSz = sizeof(sig);
  347. byte digest[] = { initialize with message hash };
  348. wc_InitRng(&rng); // initialize rng
  349. wc_ecc_init(&key); // initialize key
  350. wc_ecc_make_key(&rng, 32, &key); // make public/private key pair
  351. ret = wc_ecc_sign_hash_ex(digest, sizeof(digest), &rng, &key, &r, &s);
  352. if ( ret != MP_OKAY ) {
  353. // error generating message signature
  354. }
  355. \endcode
  356. \sa wc_ecc_verify_hash_ex
  357. */
  358. WOLFSSL_API
  359. int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
  360. ecc_key* key, mp_int *r, mp_int *s);
  361. /*!
  362. \ingroup ECC
  363. \brief This function verifies the ECC signature of a hash to ensure
  364. authenticity. It returns the answer through stat, with 1 corresponding
  365. to a valid signature, and 0 corresponding to an invalid signature.
  366. \return 0 Returned upon successfully performing the signature
  367. verification. Note: This does not mean that the signature is verified.
  368. The authenticity information is stored instead in stat
  369. \return BAD_FUNC_ARG Returned any of the input parameters evaluate to NULL
  370. \return MEMORY_E Returned if there is an error allocating memory
  371. \return MP_INIT_E may be returned if there is an error while computing
  372. the message signature
  373. \return MP_READ_E may be returned if there is an error while computing
  374. the message signature
  375. \return MP_CMP_E may be returned if there is an error while computing
  376. the message signature
  377. \return MP_INVMOD_E may be returned if there is an error while computing
  378. the message signature
  379. \return MP_EXPTMOD_E may be returned if there is an error while
  380. computing the message signature
  381. \return MP_MOD_E may be returned if there is an error while computing
  382. the message signature
  383. \return MP_MUL_E may be returned if there is an error while computing
  384. the message signature
  385. \return MP_ADD_E may be returned if there is an error while computing
  386. the message signature
  387. \return MP_MULMOD_E may be returned if there is an error while computing
  388. the message signature
  389. \return MP_TO_E may be returned if there is an error while computing the
  390. message signature
  391. \return MP_MEM may be returned if there is an error while computing the
  392. message signature
  393. \param sig pointer to the buffer containing the signature to verify
  394. \param siglen length of the signature to verify
  395. \param hash pointer to the buffer containing the hash of the message
  396. verified
  397. \param hashlen length of the hash of the message verified
  398. \param stat pointer to the result of the verification. 1 indicates the
  399. message was successfully verified
  400. \param key pointer to a public ECC key with which to verify the signature
  401. _Example_
  402. \code
  403. ecc_key key;
  404. int ret, verified = 0;
  405. byte sig[1024] { initialize with received signature };
  406. byte digest[] = { initialize with message hash };
  407. // initialize key with received public key
  408. ret = wc_ecc_verify_hash(sig, sizeof(sig), digest,sizeof(digest),
  409. &verified, &key);
  410. if ( ret != 0 ) {
  411. // error performing verification
  412. } else if ( verified == 0 ) {
  413. // the signature is invalid
  414. }
  415. \endcode
  416. \sa wc_ecc_sign_hash
  417. \sa wc_ecc_verify_hash_ex
  418. */
  419. WOLFSSL_API
  420. int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
  421. word32 hashlen, int* stat, ecc_key* key);
  422. /*!
  423. \ingroup ECC
  424. \brief Verify an ECC signature. Result is written to stat.
  425. 1 is valid, 0 is invalid.
  426. Note: Do not use the return value to test for valid. Only use stat.
  427. \return MP_OKAY If successful (even if the signature is not valid)
  428. \return ECC_BAD_ARG_E Returns if arguments are null or if
  429. key-idx is invalid.
  430. \return MEMORY_E Error allocating ints or points.
  431. \param r The signature R component to verify
  432. \param s The signature S component to verify
  433. \param hash The hash (message digest) that was signed
  434. \param hashlen The length of the hash (octets)
  435. \param stat Result of signature, 1==valid, 0==invalid
  436. \param key The corresponding public ECC key
  437. _Example_
  438. \code
  439. mp_int r;
  440. mp_int s;
  441. int stat;
  442. byte hash[] = { Some hash }
  443. ecc_key key;
  444. if(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &stat, &key) == MP_OKAY)
  445. {
  446. // Check stat
  447. }
  448. \endcode
  449. \sa wc_ecc_verify_hash
  450. */
  451. WOLFSSL_API
  452. int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
  453. word32 hashlen, int* stat, ecc_key* key);
  454. /*!
  455. \ingroup ECC
  456. \brief This function initializes an ecc_key object for future
  457. use with message verification or key negotiation.
  458. \return 0 Returned upon successfully initializing the ecc_key object
  459. \return MEMORY_E Returned if there is an error allocating memory
  460. \param key pointer to the ecc_key object to initialize
  461. _Example_
  462. \code
  463. ecc_key key;
  464. wc_ecc_init(&key);
  465. \endcode
  466. \sa wc_ecc_make_key
  467. \sa wc_ecc_free
  468. */
  469. WOLFSSL_API
  470. int wc_ecc_init(ecc_key* key);
  471. /*!
  472. \ingroup ECC
  473. \brief This function frees an ecc_key object after it has been used.
  474. \return int integer returned indicating wolfSSL error or success status.
  475. \param key pointer to the ecc_key object to free
  476. _Example_
  477. \code
  478. // initialize key and perform secure exchanges
  479. ...
  480. wc_ecc_free(&key);
  481. \endcode
  482. \sa wc_ecc_init
  483. */
  484. WOLFSSL_API
  485. int wc_ecc_free(ecc_key* key);
  486. /*!
  487. \ingroup ECC
  488. \brief This function frees the fixed-point cache, which can be used
  489. with ecc to speed up computation times. To use this functionality,
  490. FP_ECC (fixed-point ecc), should be defined.
  491. \return none No returns.
  492. \param none No parameters.
  493. _Example_
  494. \code
  495. ecc_key key;
  496. // initialize key and perform secure exchanges
  497. ...
  498. wc_ecc_fp_free();
  499. \endcode
  500. \sa wc_ecc_free
  501. */
  502. WOLFSSL_API
  503. void wc_ecc_fp_free(void);
  504. /*!
  505. \ingroup ECC
  506. \brief Checks if an ECC idx is valid.
  507. \return 1 Return if valid.
  508. \return 0 Return if not valid.
  509. \param n The idx number to check.
  510. _Example_
  511. \code
  512. ecc_key key;
  513. WC_WC_RNG rng;
  514. int is_valid;
  515. wc_ecc_init(&key);
  516. wc_InitRng(&rng);
  517. wc_ecc_make_key(&rng, 32, &key);
  518. is_valid = wc_ecc_is_valid_idx(key.idx);
  519. if (is_valid == 1)
  520. {
  521. // idx is valid
  522. }
  523. else if (is_valid == 0)
  524. {
  525. // idx is not valid
  526. }
  527. \endcode
  528. \sa none
  529. */
  530. WOLFSSL_API
  531. int wc_ecc_is_valid_idx(int n);
  532. /*!
  533. \ingroup ECC
  534. \brief Allocate a new ECC point.
  535. \return p A newly allocated point.
  536. \return NULL Returns NULL on error.
  537. \param none No parameters.
  538. _Example_
  539. \code
  540. ecc_point* point;
  541. point = wc_ecc_new_point();
  542. if (point == NULL)
  543. {
  544. // Handle point creation error
  545. }
  546. // Do stuff with point
  547. \endcode
  548. \sa wc_ecc_del_point
  549. \sa wc_ecc_cmp_point
  550. \sa wc_ecc_copy_point
  551. */
  552. WOLFSSL_API
  553. ecc_point* wc_ecc_new_point(void);
  554. /*!
  555. \ingroup ECC
  556. \brief Free an ECC point from memory.
  557. \return none No returns.
  558. \param p The point to free.
  559. _Example_
  560. \code
  561. ecc_point* point;
  562. point = wc_ecc_new_point();
  563. if (point == NULL)
  564. {
  565. // Handle point creation error
  566. }
  567. // Do stuff with point
  568. wc_ecc_del_point(point);
  569. \endcode
  570. \sa wc_ecc_new_point
  571. \sa wc_ecc_cmp_point
  572. \sa wc_ecc_copy_point
  573. */
  574. WOLFSSL_API
  575. void wc_ecc_del_point(ecc_point* p);
  576. /*!
  577. \ingroup ECC
  578. \brief Copy the value of one point to another one.
  579. \return ECC_BAD_ARG_E Error thrown when p or r is null.
  580. \return MP_OKAY Point copied successfully
  581. \return ret Error from internal functions. Can be...
  582. \param p The point to copy.
  583. \param r The created point.
  584. _Example_
  585. \code
  586. ecc_point* point;
  587. ecc_point* copied_point;
  588. int copy_return;
  589. point = wc_ecc_new_point();
  590. copy_return = wc_ecc_copy_point(point, copied_point);
  591. if (copy_return != MP_OKAY)
  592. {
  593. // Handle error
  594. }
  595. \endcode
  596. \sa wc_ecc_new_point
  597. \sa wc_ecc_cmp_point
  598. \sa wc_ecc_del_point
  599. */
  600. WOLFSSL_API
  601. int wc_ecc_copy_point(ecc_point* p, ecc_point *r);
  602. /*!
  603. \ingroup ECC
  604. \brief Compare the value of a point with another one.
  605. \return BAD_FUNC_ARG One or both arguments are NULL.
  606. \return MP_EQ The points are equal.
  607. \return ret Either MP_LT or MP_GT and signifies that the
  608. points are not equal.
  609. \param a First point to compare.
  610. \param b Second point to compare.
  611. _Example_
  612. \code
  613. ecc_point* point;
  614. ecc_point* point_to_compare;
  615. int cmp_result;
  616. point = wc_ecc_new_point();
  617. point_to_compare = wc_ecc_new_point();
  618. cmp_result = wc_ecc_cmp_point(point, point_to_compare);
  619. if (cmp_result == BAD_FUNC_ARG)
  620. {
  621. // arguments are invalid
  622. }
  623. else if (cmp_result == MP_EQ)
  624. {
  625. // Points are equal
  626. }
  627. else
  628. {
  629. // Points are not equal
  630. }
  631. \endcode
  632. \sa wc_ecc_new_point
  633. \sa wc_ecc_del_point
  634. \sa wc_ecc_copy_point
  635. */
  636. WOLFSSL_API
  637. int wc_ecc_cmp_point(ecc_point* a, ecc_point *b);
  638. /*!
  639. \ingroup ECC
  640. \brief Checks if a point is at infinity. Returns 1 if point is
  641. at infinity, 0 if not, < 0 on error
  642. \return 1 p is at infinity.
  643. \return 0 p is not at infinity.
  644. \return <0 Error.
  645. \param p The point to check.
  646. _Example_
  647. \code
  648. ecc_point* point;
  649. int is_infinity;
  650. point = wc_ecc_new_point();
  651. is_infinity = wc_ecc_point_is_at_infinity(point);
  652. if (is_infinity < 0)
  653. {
  654. // Handle error
  655. }
  656. else if (is_infinity == 0)
  657. {
  658. // Point is not at infinity
  659. }
  660. else if (is_infinity == 1)
  661. {
  662. // Point is at infinity
  663. }
  664. \endcode
  665. \sa wc_ecc_new_point
  666. \sa wc_ecc_del_point
  667. \sa wc_ecc_cmp_point
  668. \sa wc_ecc_copy_point
  669. */
  670. WOLFSSL_API
  671. int wc_ecc_point_is_at_infinity(ecc_point *p);
  672. /*!
  673. \ingroup ECC
  674. \brief Perform ECC Fixed Point multiplication.
  675. \return MP_OKAY Returns on successful operation.
  676. \return MP_INIT_E Returned if there is an error initializing an integer
  677. for use with the multiple precision integer (mp_int) library.
  678. \param k The multiplicand.
  679. \param G Base point to multiply.
  680. \param R Destination of product.
  681. \param modulus The modulus for the curve.
  682. \param map If non-zero maps the point back to affine coordinates,
  683. otherwise it's left in jacobian-montgomery form.
  684. _Example_
  685. \code
  686. ecc_point* base;
  687. ecc_point* destination;
  688. // Initialize points
  689. base = wc_ecc_new_point();
  690. destination = wc_ecc_new_point();
  691. // Setup other arguments
  692. mp_int multiplicand;
  693. mp_int modulus;
  694. int map;
  695. \endcode
  696. \sa none
  697. */
  698. WOLFSSL_API
  699. int wc_ecc_mulmod(mp_int* k, ecc_point *G, ecc_point *R,
  700. mp_int* a, mp_int* modulus, int map);
  701. /*!
  702. \ingroup ECC
  703. \brief This function exports the ECC key from the ecc_key structure,
  704. storing the result in out. The key will be stored in ANSI X9.63 format.
  705. It stores the bytes written to the output buffer in outLen.
  706. \return 0 Returned on successfully exporting the ecc_key
  707. \return LENGTH_ONLY_E Returned if the output buffer evaluates to NULL,
  708. but the other two input parameters are valid. Indicates that the function
  709. is only returning the length required to store the key
  710. \return ECC_BAD_ARG_E Returned if any of the input parameters are NULL,
  711. or the key is unsupported (has an invalid index)
  712. \return BUFFER_E Returned if the output buffer is too small to store the
  713. ecc key. If the output buffer is too small, the size needed will be
  714. returned in outLen
  715. \return MEMORY_E Returned if there is an error allocating memory with
  716. XMALLOC
  717. \return MP_INIT_E may be returned if there is an error processing the
  718. ecc_key
  719. \return MP_READ_E may be returned if there is an error processing the
  720. ecc_key
  721. \return MP_CMP_E may be returned if there is an error processing the
  722. ecc_key
  723. \return MP_INVMOD_E may be returned if there is an error processing the
  724. ecc_key
  725. \return MP_EXPTMOD_E may be returned if there is an error processing the
  726. ecc_key
  727. \return MP_MOD_E may be returned if there is an error processing the
  728. ecc_key
  729. \return MP_MUL_E may be returned if there is an error processing the
  730. ecc_key
  731. \return MP_ADD_E may be returned if there is an error processing the
  732. ecc_key
  733. \return MP_MULMOD_E may be returned if there is an error processing the
  734. ecc_key
  735. \return MP_TO_E may be returned if there is an error processing the ecc_key
  736. \return MP_MEM may be returned if there is an error processing the ecc_key
  737. \param key pointer to the ecc_key object to export
  738. \param out pointer to the buffer in which to store the ANSI X9.63
  739. formatted key
  740. \param outLen size of the output buffer. On successfully storing the
  741. key, will hold the bytes written to the output buffer
  742. _Example_
  743. \code
  744. int ret;
  745. byte buff[1024];
  746. word32 buffSz = sizeof(buff);
  747. ecc_key key;
  748. // initialize key, make key
  749. ret = wc_ecc_export_x963(&key, buff, &buffSz);
  750. if ( ret != 0) {
  751. // error exporting key
  752. }
  753. \endcode
  754. \sa wc_ecc_export_x963_ex
  755. \sa wc_ecc_import_x963
  756. */
  757. WOLFSSL_API
  758. int wc_ecc_export_x963(ecc_key*, byte* out, word32* outLen);
  759. /*!
  760. \ingroup ECC
  761. \brief This function exports the ECC key from the ecc_key structure,
  762. storing the result in out. The key will be stored in ANSI X9.63 format.
  763. It stores the bytes written to the output buffer in outLen. This function
  764. allows the additional option of compressing the certificate through the
  765. compressed parameter. When this parameter is true, the key will be stored
  766. in ANSI X9.63 compressed format.
  767. \return 0 Returned on successfully exporting the ecc_key
  768. \return NOT_COMPILED_IN Returned if the HAVE_COMP_KEY was not enabled at
  769. compile time, but the key was requested in compressed format
  770. \return LENGTH_ONLY_E Returned if the output buffer evaluates to NULL, but
  771. the other two input parameters are valid. Indicates that the function is
  772. only returning the length required to store the key
  773. \return ECC_BAD_ARG_E Returned if any of the input parameters are NULL, or
  774. the key is unsupported (has an invalid index)
  775. \return BUFFER_E Returned if the output buffer is too small to store the
  776. ecc key. If the output buffer is too small, the size needed will be
  777. returned in outLen
  778. \return MEMORY_E Returned if there is an error allocating memory with
  779. XMALLOC
  780. \return MP_INIT_E may be returned if there is an error processing the
  781. ecc_key
  782. \return MP_READ_E may be returned if there is an error processing the
  783. ecc_key
  784. \return MP_CMP_E may be returned if there is an error processing the
  785. ecc_key
  786. \return MP_INVMOD_E may be returned if there is an error processing the
  787. ecc_key
  788. \return MP_EXPTMOD_E may be returned if there is an error processing
  789. the ecc_key
  790. \return MP_MOD_E may be returned if there is an error processing the
  791. ecc_key
  792. \return MP_MUL_E may be returned if there is an error processing the
  793. ecc_key
  794. \return MP_ADD_E may be returned if there is an error processing the
  795. ecc_key
  796. \return MP_MULMOD_E may be returned if there is an error processing
  797. the ecc_key
  798. \return MP_TO_E may be returned if there is an error processing the ecc_key
  799. \return MP_MEM may be returned if there is an error processing the ecc_key
  800. \param key pointer to the ecc_key object to export
  801. \param out pointer to the buffer in which to store the ANSI X9.63
  802. formatted key
  803. \param outLen size of the output buffer. On successfully storing the
  804. key, will hold the bytes written to the output buffer
  805. \param compressed indicator of whether to store the key in compressed
  806. format. 1==compressed, 0==uncompressed
  807. _Example_
  808. \code
  809. int ret;
  810. byte buff[1024];
  811. word32 buffSz = sizeof(buff);
  812. ecc_key key;
  813. // initialize key, make key
  814. ret = wc_ecc_export_x963_ex(&key, buff, &buffSz, 1);
  815. if ( ret != 0) {
  816. // error exporting key
  817. }
  818. \endcode
  819. \sa wc_ecc_export_x963
  820. \sa wc_ecc_import_x963
  821. */
  822. WOLFSSL_API
  823. int wc_ecc_export_x963_ex(ecc_key*, byte* out, word32* outLen, int compressed);
  824. /*!
  825. \ingroup ECC
  826. \brief This function imports a public ECC key from a buffer containing the
  827. key stored in ANSI X9.63 format. This function will handle both compressed
  828. and uncompressed keys, as long as compressed keys are enabled at compile
  829. time through the HAVE_COMP_KEY option.
  830. \return 0 Returned on successfully importing the ecc_key
  831. \return NOT_COMPILED_IN Returned if the HAVE_COMP_KEY was not enabled at
  832. compile time, but the key is stored in compressed format
  833. \return ECC_BAD_ARG_E Returned if in or key evaluate to NULL, or the
  834. inLen is even (according to the x9.63 standard, the key must be odd)
  835. \return MEMORY_E Returned if there is an error allocating memory
  836. \return ASN_PARSE_E Returned if there is an error parsing the ECC key;
  837. may indicate that the ECC key is not stored in valid ANSI X9.63 format
  838. \return IS_POINT_E Returned if the public key exported is not a point
  839. on the ECC curve
  840. \return MP_INIT_E may be returned if there is an error processing the
  841. ecc_key
  842. \return MP_READ_E may be returned if there is an error processing the
  843. ecc_key
  844. \return MP_CMP_E may be returned if there is an error processing the
  845. ecc_key
  846. \return MP_INVMOD_E may be returned if there is an error processing the
  847. ecc_key
  848. \return MP_EXPTMOD_E may be returned if there is an error processing the
  849. ecc_key
  850. \return MP_MOD_E may be returned if there is an error processing the
  851. ecc_key
  852. \return MP_MUL_E may be returned if there is an error processing the
  853. ecc_key
  854. \return MP_ADD_E may be returned if there is an error processing the
  855. ecc_key
  856. \return MP_MULMOD_E may be returned if there is an error processing the
  857. ecc_key
  858. \return MP_TO_E may be returned if there is an error processing the ecc_key
  859. \return MP_MEM may be returned if there is an error processing the ecc_key
  860. \param in pointer to the buffer containing the ANSI x9.63 formatted ECC key
  861. \param inLen length of the input buffer
  862. \param key pointer to the ecc_key object in which to store the imported key
  863. _Example_
  864. \code
  865. int ret;
  866. byte buff[] = { initialize with ANSI X9.63 formatted key };
  867. ecc_key pubKey;
  868. wc_ecc_init(&pubKey);
  869. ret = wc_ecc_import_x963(buff, sizeof(buff), &pubKey);
  870. if ( ret != 0) {
  871. // error importing key
  872. }
  873. \endcode
  874. \sa wc_ecc_export_x963
  875. \sa wc_ecc_import_private_key
  876. */
  877. WOLFSSL_API
  878. int wc_ecc_import_x963(const byte* in, word32 inLen, ecc_key* key);
  879. /*!
  880. \ingroup ECC
  881. \brief This function imports a public/private ECC key pair from a buffer
  882. containing the raw private key, and a second buffer containing the ANSI
  883. X9.63 formatted public key. This function will handle both compressed and
  884. uncompressed keys, as long as compressed keys are enabled at compile time
  885. through the HAVE_COMP_KEY option.
  886. \return 0 Returned on successfully importing the ecc_key
  887. NOT_COMPILED_IN Returned if the HAVE_COMP_KEY was not enabled at compile
  888. time, but the key is stored in compressed format
  889. \return ECC_BAD_ARG_E Returned if in or key evaluate to NULL, or the
  890. inLen is even (according to the x9.63 standard, the key must be odd)
  891. \return MEMORY_E Returned if there is an error allocating memory
  892. \return ASN_PARSE_E Returned if there is an error parsing the ECC key;
  893. may indicate that the ECC key is not stored in valid ANSI X9.63 format
  894. \return IS_POINT_E Returned if the public key exported is not a point
  895. on the ECC curve
  896. \return MP_INIT_E may be returned if there is an error processing the
  897. ecc_key
  898. \return MP_READ_E may be returned if there is an error processing the
  899. ecc_key
  900. \return MP_CMP_E may be returned if there is an error processing the
  901. ecc_key
  902. \return MP_INVMOD_E may be returned if there is an error processing
  903. the ecc_key
  904. \return MP_EXPTMOD_E may be returned if there is an error processing
  905. the ecc_key
  906. \return MP_MOD_E may be returned if there is an error processing the
  907. ecc_key
  908. \return MP_MUL_E may be returned if there is an error processing the
  909. ecc_key
  910. \return MP_ADD_E may be returned if there is an error processing the
  911. ecc_key
  912. \return MP_MULMOD_E may be returned if there is an error processing
  913. the ecc_key
  914. \return MP_TO_E may be returned if there is an error processing the ecc_key
  915. \return MP_MEM may be returned if there is an error processing the ecc_key
  916. \param priv pointer to the buffer containing the raw private key
  917. \param privSz size of the private key buffer
  918. \param pub pointer to the buffer containing the ANSI x9.63 formatted ECC
  919. public key
  920. \param pubSz length of the public key input buffer
  921. \param key pointer to the ecc_key object in which to store the imported
  922. private/public key pair
  923. _Example_
  924. \code
  925. int ret;
  926. byte pub[] = { initialize with ANSI X9.63 formatted key };
  927. byte priv[] = { initialize with the raw private key };
  928. ecc_key key;
  929. wc_ecc_init(&key);
  930. ret = wc_ecc_import_private_key(priv, sizeof(priv), pub, sizeof(pub),
  931. &key);
  932. if ( ret != 0) {
  933. // error importing key
  934. }
  935. \endcode
  936. \sa wc_ecc_export_x963
  937. \sa wc_ecc_import_private_key
  938. */
  939. WOLFSSL_API
  940. int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
  941. word32 pubSz, ecc_key* key);
  942. /*!
  943. \ingroup ECC
  944. \brief This function converts the R and S portions of an ECC signature
  945. into a DER-encoded ECDSA signature. This function also stores the length
  946. written to the output buffer, out, in outlen.
  947. \return 0 Returned on successfully converting the signature
  948. \return ECC_BAD_ARG_E Returned if any of the input parameters evaluate
  949. to NULL, or if the input buffer is not large enough to hold the
  950. DER-encoded ECDSA signature
  951. \return MP_INIT_E may be returned if there is an error processing
  952. the ecc_key
  953. \return MP_READ_E may be returned if there is an error processing
  954. the ecc_key
  955. \return MP_CMP_E may be returned if there is an error processing
  956. the ecc_key
  957. \return MP_INVMOD_E may be returned if there is an error processing
  958. the ecc_key
  959. \return MP_EXPTMOD_E may be returned if there is an error processing
  960. the ecc_key
  961. \return MP_MOD_E may be returned if there is an error processing the
  962. ecc_key
  963. \return MP_MUL_E may be returned if there is an error processing the
  964. ecc_key
  965. \return MP_ADD_E may be returned if there is an error processing the
  966. ecc_key
  967. \return MP_MULMOD_E may be returned if there is an error processing
  968. the ecc_key
  969. \return MP_TO_E may be returned if there is an error processing the ecc_key
  970. \return MP_MEM may be returned if there is an error processing the ecc_key
  971. \param r pointer to the buffer containing the R portion of the signature
  972. as a string
  973. \param s pointer to the buffer containing the S portion of the signature
  974. as a string
  975. \param out pointer to the buffer in which to store the DER-encoded ECDSA
  976. signature
  977. \param outlen length of the output buffer available. Will store the bytes
  978. written to the buffer after successfully converting the signature to
  979. ECDSA format
  980. _Example_
  981. \code
  982. int ret;
  983. ecc_key key;
  984. // initialize key, generate R and S
  985. char r[] = { initialize with R };
  986. char s[] = { initialize with S };
  987. byte sig[wc_ecc_sig_size(key)];
  988. // signature size will be 2 * ECC key size + ~10 bytes for ASN.1 overhead
  989. word32 sigSz = sizeof(sig);
  990. ret = wc_ecc_rs_to_sig(r, s, sig, &sigSz);
  991. if ( ret != 0) {
  992. // error converting parameters to signature
  993. }
  994. \endcode
  995. \sa wc_ecc_sign_hash
  996. \sa wc_ecc_sig_size
  997. */
  998. WOLFSSL_API
  999. int wc_ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen);
  1000. /*!
  1001. \ingroup ECC
  1002. \brief This function fills an ecc_key structure with the raw components
  1003. of an ECC signature.
  1004. \return 0 Returned upon successfully importing into the ecc_key structure
  1005. \return ECC_BAD_ARG_E Returned if any of the input values evaluate to NULL
  1006. \return MEMORY_E Returned if there is an error initializing space to
  1007. store the parameters of the ecc_key
  1008. \return ASN_PARSE_E Returned if the input curveName is not defined
  1009. in ecc_sets
  1010. \return MP_INIT_E may be returned if there is an error processing the
  1011. input parameters
  1012. \return MP_READ_E may be returned if there is an error processing the
  1013. input parameters
  1014. \return MP_CMP_E may be returned if there is an error processing the
  1015. input parameters
  1016. \return MP_INVMOD_E may be returned if there is an error processing the
  1017. input parameters
  1018. \return MP_EXPTMOD_E may be returned if there is an error processing the
  1019. input parameters
  1020. \return MP_MOD_E may be returned if there is an error processing the
  1021. input parameters
  1022. \return MP_MUL_E may be returned if there is an error processing the
  1023. input parameters
  1024. \return MP_ADD_E may be returned if there is an error processing the
  1025. input parameters
  1026. \return MP_MULMOD_E may be returned if there is an error processing
  1027. the input parameters
  1028. \return MP_TO_E may be returned if there is an error processing the
  1029. input parameters
  1030. \return MP_MEM may be returned if there is an error processing the
  1031. input parameters
  1032. \param key pointer to an ecc_key structure to fill
  1033. \param qx pointer to a buffer containing the x component of the base
  1034. point as an ASCII hex string
  1035. \param qy pointer to a buffer containing the y component of the base
  1036. point as an ASCII hex string
  1037. \param d pointer to a buffer containing the private key as an ASCII
  1038. hex string
  1039. \param curveName pointer to a string containing the ECC curve name,
  1040. as found in ecc_sets
  1041. _Example_
  1042. \code
  1043. int ret;
  1044. ecc_key key;
  1045. wc_ecc_init(&key);
  1046. char qx[] = { initialize with x component of base point };
  1047. char qy[] = { initialize with y component of base point };
  1048. char d[] = { initialize with private key };
  1049. ret = wc_ecc_import_raw(&key,qx, qy, d, "ECC-256");
  1050. if ( ret != 0) {
  1051. // error initializing key with given inputs
  1052. }
  1053. \endcode
  1054. \sa wc_ecc_import_private_key
  1055. */
  1056. WOLFSSL_API
  1057. int wc_ecc_import_raw(ecc_key* key, const char* qx, const char* qy,
  1058. const char* d, const char* curveName);
  1059. /*!
  1060. \ingroup ECC
  1061. \brief This function exports only the private key from an ecc_key
  1062. structure. It stores the private key in the buffer out, and sets
  1063. the bytes written to this buffer in outLen.
  1064. \return 0 Returned upon successfully exporting the private key
  1065. \return ECC_BAD_ARG_E Returned if any of the input values evaluate to NULL
  1066. \return MEMORY_E Returned if there is an error initializing space
  1067. to store the parameters of the ecc_key
  1068. \return ASN_PARSE_E Returned if the input curveName is not defined
  1069. in ecc_sets
  1070. \return MP_INIT_E may be returned if there is an error processing
  1071. the input parameters
  1072. \return MP_READ_E may be returned if there is an error processing the
  1073. input parameters
  1074. \return MP_CMP_E may be returned if there is an error processing the
  1075. input parameters
  1076. \return MP_INVMOD_E may be returned if there is an error processing
  1077. the input parameters
  1078. \return MP_EXPTMOD_E may be returned if there is an error processing
  1079. the input parameters
  1080. \return MP_MOD_E may be returned if there is an error processing the
  1081. input parameters
  1082. \return MP_MUL_E may be returned if there is an error processing the
  1083. input parameters
  1084. \return MP_ADD_E may be returned if there is an error processing the
  1085. input parameters
  1086. \return MP_MULMOD_E may be returned if there is an error processing
  1087. the input parameters
  1088. \return MP_TO_E may be returned if there is an error processing the
  1089. input parameters
  1090. \return MP_MEM may be returned if there is an error processing the
  1091. input parameters
  1092. \param key pointer to an ecc_key structure from which to export the
  1093. private key
  1094. \param out pointer to the buffer in which to store the private key
  1095. \param outLen pointer to a word32 object with the size available in
  1096. out. Set with the number of bytes written to out after successfully
  1097. exporting the private key
  1098. _Example_
  1099. \code
  1100. int ret;
  1101. ecc_key key;
  1102. // initialize key, make key
  1103. char priv[ECC_KEY_SIZE];
  1104. word32 privSz = sizeof(priv);
  1105. ret = wc_ecc_export_private_only(&key, priv, &privSz);
  1106. if ( ret != 0) {
  1107. // error exporting private key
  1108. }
  1109. \endcode
  1110. \sa wc_ecc_import_private_key
  1111. */
  1112. WOLFSSL_API
  1113. int wc_ecc_export_private_only(ecc_key* key, byte* out, word32* outLen);
  1114. /*!
  1115. \ingroup ECC
  1116. \brief Export point to der.
  1117. \return 0 Returned on success.
  1118. \return ECC_BAD_ARG_E Returns if curve_idx is less than 0 or
  1119. invalid. Also returns when
  1120. \return LENGTH_ONLY_E outLen is set but nothing else.
  1121. \return BUFFER_E Returns if outLen is less than 1 + 2 * the curve size.
  1122. \return MEMORY_E Returns if there is a problem allocating memory.
  1123. \param curve_idx Index of the curve used from ecc_sets.
  1124. \param point Point to export to der.
  1125. \param out Destination for the output.
  1126. \param outLen Maxsize allowed for output, destination for
  1127. final size of output
  1128. _Example_
  1129. \code
  1130. int curve_idx;
  1131. ecc_point* point;
  1132. byte out[];
  1133. word32 outLen;
  1134. wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  1135. \endcode
  1136. \sa wc_ecc_import_point_der
  1137. */
  1138. WOLFSSL_API
  1139. int wc_ecc_export_point_der(const int curve_idx, ecc_point* point,
  1140. byte* out, word32* outLen);
  1141. /*!
  1142. \ingroup ECC
  1143. \brief Import point from der format.
  1144. \return ECC_BAD_ARG_E Returns if any arguments are null or if
  1145. inLen is even.
  1146. \return MEMORY_E Returns if there is an error initializing
  1147. \return NOT_COMPILED_IN Returned if HAVE_COMP_KEY is not true
  1148. and in is a compressed cert
  1149. \return MP_OKAY Successful operation.
  1150. \param in der buffer to import point from.
  1151. \param inLen Length of der buffer.
  1152. \param curve_idx Index of curve.
  1153. \param point Destination for point.
  1154. _Example_
  1155. \code
  1156. byte in[];
  1157. word32 inLen;
  1158. int curve_idx;
  1159. ecc_point* point;
  1160. wc_ecc_import_point_der(in, inLen, curve_idx, point);
  1161. \endcode
  1162. \sa wc_ecc_export_point_der
  1163. */
  1164. WOLFSSL_API
  1165. int wc_ecc_import_point_der(byte* in, word32 inLen, const int curve_idx,
  1166. ecc_point* point);
  1167. /*!
  1168. \ingroup ECC
  1169. \brief This function returns the key size of an ecc_key
  1170. structure in octets.
  1171. \return Given a valid key, returns the key size in octets
  1172. \return 0 Returned if the given key is NULL
  1173. \param key pointer to an ecc_key structure for which to get the key size
  1174. _Example_
  1175. \code
  1176. int keySz;
  1177. ecc_key key;
  1178. // initialize key, make key
  1179. keySz = wc_ecc_size(&key);
  1180. if ( keySz == 0) {
  1181. // error determining key size
  1182. }
  1183. \endcode
  1184. \sa wc_ecc_make_key
  1185. */
  1186. WOLFSSL_API
  1187. int wc_ecc_size(ecc_key* key);
  1188. /*!
  1189. \ingroup ECC
  1190. \brief This function returns the worst case size for an ECC signature,
  1191. given by: (keySz * 2) + SIG_HEADER_SZ + ECC_MAX_PAD_SZ.
  1192. The actual signature size can be computed with wc_ecc_sign_hash.
  1193. \return returns the maximum signature
  1194. size, in octets
  1195. \param key size
  1196. _Example_
  1197. \code
  1198. int sigSz = wc_ecc_sig_size_calc(32);
  1199. if ( sigSz == 0) {
  1200. // error determining sig size
  1201. }
  1202. \endcode
  1203. \sa wc_ecc_sign_hash
  1204. \sa wc_ecc_sig_size
  1205. */
  1206. WOLFSSL_API
  1207. int wc_ecc_sig_size_calc(int sz);
  1208. /*!
  1209. \ingroup ECC
  1210. \brief This function returns the worst case size for an ECC signature,
  1211. given by: (keySz * 2) + SIG_HEADER_SZ + ECC_MAX_PAD_SZ.
  1212. The actual signature size can be computed with wc_ecc_sign_hash.
  1213. \return Success Given a valid key, returns the maximum signature
  1214. size, in octets
  1215. \return 0 Returned if the given key is NULL
  1216. \param key pointer to an ecc_key structure for which to get the
  1217. signature size
  1218. _Example_
  1219. \code
  1220. int sigSz;
  1221. ecc_key key;
  1222. // initialize key, make key
  1223. sigSz = wc_ecc_sig_size(&key);
  1224. if ( sigSz == 0) {
  1225. // error determining sig size
  1226. }
  1227. \endcode
  1228. \sa wc_ecc_sign_hash
  1229. \sa wc_ecc_sig_size_calc
  1230. */
  1231. WOLFSSL_API
  1232. int wc_ecc_sig_size(ecc_key* key);
  1233. /*!
  1234. \ingroup ECC
  1235. \brief This function allocates and initializes space for a new ECC
  1236. context object to allow secure message exchange with ECC.
  1237. \return Success On successfully generating a new ecEncCtx object,
  1238. returns a pointer to that object
  1239. \return NULL Returned if the function fails to generate a new
  1240. ecEncCtx object
  1241. \param flags indicate whether this is a server or client context
  1242. Options are: REQ_RESP_CLIENT, and REQ_RESP_SERVER
  1243. \param rng pointer to a RNG object with which to generate a salt
  1244. _Example_
  1245. \code
  1246. ecEncCtx* ctx;
  1247. WC_WC_RNG rng;
  1248. wc_InitRng(&rng);
  1249. ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  1250. if(ctx == NULL) {
  1251. // error generating new ecEncCtx object
  1252. }
  1253. \endcode
  1254. \sa wc_ecc_encrypt
  1255. \sa wc_ecc_decrypt
  1256. */
  1257. WOLFSSL_API
  1258. ecEncCtx* wc_ecc_ctx_new(int flags, WC_RNG* rng);
  1259. /*!
  1260. \ingroup ECC
  1261. \brief This function frees the ecEncCtx object used for encrypting
  1262. and decrypting messages.
  1263. \return none Returns.
  1264. \param ctx pointer to the ecEncCtx object to free
  1265. _Example_
  1266. \code
  1267. ecEncCtx* ctx;
  1268. WC_WC_RNG rng;
  1269. wc_InitRng(&rng);
  1270. ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  1271. // do secure communication
  1272. ...
  1273. wc_ecc_ctx_free(&ctx);
  1274. \endcode
  1275. \sa wc_ecc_ctx_new
  1276. */
  1277. WOLFSSL_API
  1278. void wc_ecc_ctx_free(ecEncCtx*);
  1279. /*!
  1280. \ingroup ECC
  1281. \brief This function resets an ecEncCtx structure to avoid having
  1282. to free and allocate a new context object.
  1283. \return 0 Returned if the ecEncCtx structure is successfully reset
  1284. \return BAD_FUNC_ARG Returned if either rng or ctx is NULL
  1285. \return RNG_FAILURE_E Returned if there is an error generating a
  1286. new salt for the ECC object
  1287. \param ctx pointer to the ecEncCtx object to reset
  1288. \param rng pointer to an RNG object with which to generate a new salt
  1289. _Example_
  1290. \code
  1291. ecEncCtx* ctx;
  1292. WC_WC_RNG rng;
  1293. wc_InitRng(&rng);
  1294. ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  1295. // do secure communication
  1296. ...
  1297. wc_ecc_ctx_reset(&ctx, &rng);
  1298. // do more secure communication
  1299. \endcode
  1300. \sa wc_ecc_ctx_new
  1301. */
  1302. WOLFSSL_API
  1303. int wc_ecc_ctx_reset(ecEncCtx*, WC_RNG*); /* reset for use again w/o alloc/free */
  1304. /*!
  1305. \ingroup ECC
  1306. \brief This function returns the salt of an ecEncCtx object. This
  1307. function should only be called when the ecEncCtx's state is
  1308. ecSRV_INIT or ecCLI_INIT.
  1309. \return Success On success, returns the ecEncCtx salt
  1310. \return NULL Returned if the ecEncCtx object is NULL, or the ecEncCtx's
  1311. state is not ecSRV_INIT or ecCLI_INIT. In the latter two cases, this
  1312. function also sets the ecEncCtx's state to ecSRV_BAD_STATE or
  1313. ecCLI_BAD_STATE, respectively
  1314. \param ctx pointer to the ecEncCtx object from which to get the salt
  1315. _Example_
  1316. \code
  1317. ecEncCtx* ctx;
  1318. WC_WC_RNG rng;
  1319. const byte* salt;
  1320. wc_InitRng(&rng);
  1321. ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  1322. salt = wc_ecc_ctx_get_own_salt(&ctx);
  1323. if(salt == NULL) {
  1324. // error getting salt
  1325. }
  1326. \endcode
  1327. \sa wc_ecc_ctx_new
  1328. \sa wc_ecc_ctx_set_peer_salt
  1329. */
  1330. WOLFSSL_API
  1331. const byte* wc_ecc_ctx_get_own_salt(ecEncCtx*);
  1332. /*!
  1333. \ingroup ECC
  1334. \brief This function sets the peer salt of an ecEncCtx object.
  1335. \return 0 Returned upon successfully setting the peer salt for the
  1336. ecEncCtx object.
  1337. \return BAD_FUNC_ARG Returned if the given ecEncCtx object is NULL
  1338. or has an invalid protocol, or if the given salt is NULL
  1339. \return BAD_ENC_STATE_E Returned if the ecEncCtx's state is
  1340. ecSRV_SALT_GET or ecCLI_SALT_GET. In the latter two cases, this
  1341. function also sets the ecEncCtx's state to ecSRV_BAD_STATE or
  1342. ecCLI_BAD_STATE, respectively
  1343. \param ctx pointer to the ecEncCtx for which to set the salt
  1344. \param salt pointer to the peer's salt
  1345. _Example_
  1346. \code
  1347. ecEncCtx* cliCtx, srvCtx;
  1348. WC_WC_RNG rng;
  1349. const byte* cliSalt, srvSalt;
  1350. int ret;
  1351. wc_InitRng(&rng);
  1352. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  1353. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  1354. cliSalt = wc_ecc_ctx_get_own_salt(&cliCtx);
  1355. srvSalt = wc_ecc_ctx_get_own_salt(&srvCtx);
  1356. ret = wc_ecc_ctx_set_peer_salt(&cliCtx, srvSalt);
  1357. \endcode
  1358. \sa wc_ecc_ctx_get_own_salt
  1359. */
  1360. WOLFSSL_API
  1361. int wc_ecc_ctx_set_peer_salt(ecEncCtx*, const byte* salt);
  1362. /*!
  1363. \ingroup ECC
  1364. \brief This function can optionally be called before or after
  1365. wc_ecc_ctx_set_peer_salt. It sets optional information for
  1366. an ecEncCtx object.
  1367. \return 0 Returned upon successfully setting the information
  1368. for the ecEncCtx object.
  1369. \return BAD_FUNC_ARG Returned if the given ecEncCtx object is
  1370. NULL, the input info is NULL or it's size is invalid
  1371. \param ctx pointer to the ecEncCtx for which to set the info
  1372. \param info pointer to a buffer containing the info to set
  1373. \param sz size of the info buffer
  1374. _Example_
  1375. \code
  1376. ecEncCtx* ctx;
  1377. byte info[] = { initialize with information };
  1378. // initialize ctx, get salt,
  1379. if(wc_ecc_ctx_set_info(&ctx, info, sizeof(info))) {
  1380. // error setting info
  1381. }
  1382. \endcode
  1383. \sa wc_ecc_ctx_new
  1384. */
  1385. WOLFSSL_API
  1386. int wc_ecc_ctx_set_info(ecEncCtx*, const byte* info, int sz);
  1387. /*!
  1388. \ingroup ECC
  1389. \brief This function encrypts the given input message from msg
  1390. to out. This function takes an optional ctx object as parameter.
  1391. When supplied, encryption proceeds based on the ecEncCtx's
  1392. encAlgo, kdfAlgo, and macAlgo. If ctx is not supplied, processing
  1393. completes with the default algorithms, ecAES_128_CBC,
  1394. ecHKDF_SHA256 and ecHMAC_SHA256. This function requires that
  1395. the messages are padded according to the encryption type specified by ctx.
  1396. \return 0 Returned upon successfully encrypting the input message
  1397. \return BAD_FUNC_ARG Returned if privKey, pubKey, msg, msgSz, out,
  1398. or outSz are NULL, or the ctx object specifies an unsupported
  1399. encryption type
  1400. \return BAD_ENC_STATE_E Returned if the ctx object given is in a
  1401. state that is not appropriate for encryption
  1402. \return BUFFER_E Returned if the supplied output buffer is too
  1403. small to store the encrypted ciphertext
  1404. \return MEMORY_E Returned if there is an error allocating memory
  1405. for the shared secret key
  1406. \param privKey pointer to the ecc_key object containing the
  1407. private key to use for encryption
  1408. \param pubKey pointer to the ecc_key object containing the public
  1409. key of the peer with whom one wishes to communicate
  1410. \param msg pointer to the buffer holding the message to encrypt
  1411. \param msgSz size of the buffer to encrypt
  1412. \param out pointer to the buffer in which to store the encrypted
  1413. ciphertext
  1414. \param outSz pointer to a word32 object containing the available
  1415. size in the out buffer. Upon successfully encrypting the message,
  1416. holds the number of bytes written to the output buffer
  1417. \param ctx Optional: pointer to an ecEncCtx object specifying different
  1418. encryption algorithms to use
  1419. _Example_
  1420. \code
  1421. byte msg[] = { initialize with msg to encrypt. Ensure padded to block size };
  1422. byte out[sizeof(msg)];
  1423. word32 outSz = sizeof(out);
  1424. int ret;
  1425. ecc_key cli, serv;
  1426. // initialize cli with private key
  1427. // initialize serv with received public key
  1428. ecEncCtx* cliCtx, servCtx;
  1429. // initialize cliCtx and servCtx
  1430. // exchange salts
  1431. ret = wc_ecc_encrypt(&cli, &serv, msg, sizeof(msg), out, &outSz, cliCtx);
  1432. if(ret != 0) {
  1433. // error encrypting message
  1434. }
  1435. \endcode
  1436. \sa wc_ecc_decrypt
  1437. */
  1438. WOLFSSL_API
  1439. int wc_ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
  1440. word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
  1441. /*!
  1442. \ingroup ECC
  1443. \brief This function decrypts the ciphertext from msg to out. This
  1444. function takes an optional ctx object as parameter. When supplied,
  1445. encryption proceeds based on the ecEncCtx's encAlgo, kdfAlgo, and
  1446. macAlgo. If ctx is not supplied, processing completes with the
  1447. default algorithms, ecAES_128_CBC, ecHKDF_SHA256 and ecHMAC_SHA256.
  1448. This function requires that the messages are padded according to
  1449. the encryption type specified by ctx.
  1450. \return 0 Returned upon successfully decrypting the input message
  1451. \return BAD_FUNC_ARG Returned if privKey, pubKey, msg, msgSz, out,
  1452. or outSz are NULL, or the ctx object specifies an unsupported
  1453. encryption type
  1454. \return BAD_ENC_STATE_E Returned if the ctx object given is in
  1455. a state that is not appropriate for decryption
  1456. \return BUFFER_E Returned if the supplied output buffer is too
  1457. small to store the decrypted plaintext
  1458. \return MEMORY_E Returned if there is an error allocating memory
  1459. for the shared secret key
  1460. \param privKey pointer to the ecc_key object containing the private
  1461. key to use for decryption
  1462. \param pubKey pointer to the ecc_key object containing the public
  1463. key of the peer with whom one wishes to communicate
  1464. \param msg pointer to the buffer holding the ciphertext to decrypt
  1465. \param msgSz size of the buffer to decrypt
  1466. \param out pointer to the buffer in which to store the decrypted plaintext
  1467. \param outSz pointer to a word32 object containing the available
  1468. size in the out buffer. Upon successfully decrypting the
  1469. ciphertext, holds the number of bytes written to the output buffer
  1470. \param ctx Optional: pointer to an ecEncCtx object specifying
  1471. different decryption algorithms to use
  1472. _Example_
  1473. \code
  1474. byte cipher[] = { initialize with
  1475. ciphertext to decrypt. Ensure padded to block size };
  1476. byte plain[sizeof(cipher)];
  1477. word32 plainSz = sizeof(plain);
  1478. int ret;
  1479. ecc_key cli, serv;
  1480. // initialize cli with private key
  1481. // initialize serv with received public key
  1482. ecEncCtx* cliCtx, servCtx;
  1483. // initialize cliCtx and servCtx
  1484. // exchange salts
  1485. ret = wc_ecc_decrypt(&cli, &serv, cipher, sizeof(cipher),
  1486. plain, &plainSz, cliCtx);
  1487. if(ret != 0) {
  1488. // error decrypting message
  1489. }
  1490. \endcode
  1491. \sa Wc_ecc_encrypt
  1492. */
  1493. WOLFSSL_API
  1494. int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
  1495. word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);