ed25519.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. /*!
  2. \ingroup ED25519
  3. \brief This function generates the Ed25519 public key from the private key.
  4. It stores the public key in the buffer pubKey, and sets the bytes
  5. written to this buffer in pubKeySz.
  6. \return 0 Returned upon successfully making the public key.
  7. \return BAD_FUNC_ARG Returned ifi key or pubKey evaluate to NULL, or if the
  8. specified key size is not 32 bytes (Ed25519 has 32 byte keys).
  9. \return MEMORY_E Returned if there is an error allocating memory
  10. during function execution.
  11. \param [in] key Pointer to the ed25519_key for which to generate a key.
  12. \param [out] out Pointer to the buffer in which to store the public key.
  13. \param [in,out] outLen Pointer to a word32 object with the size available
  14. in out. Set with the number of bytes written to out after successfully
  15. exporting the public key.
  16. _Example_
  17. \code
  18. int ret;
  19. ed25519_key key;
  20. byte priv[] = { initialize with 32 byte private key };
  21. byte pub[32];
  22. word32 pubSz = sizeof(pub);
  23. wc_ed25519_init(&key);
  24. wc_ed25519_import_private_only(priv, sizeof(priv), &key);
  25. ret = wc_ed25519_make_public(&key, pub, &pubSz);
  26. if (ret != 0) {
  27. // error making public key
  28. }
  29. \endcode
  30. \sa wc_ed25519_init
  31. \sa wc_ed25519_import_private_only
  32. \sa wc_ed25519_make_key
  33. */
  34. int wc_ed25519_make_public(ed25519_key* key, unsigned char* pubKey,
  35. word32 pubKeySz);
  36. /*!
  37. \ingroup ED25519
  38. \brief This function generates a new Ed25519 key and stores it in key.
  39. \return 0 Returned upon successfully making an ed25519_key.
  40. \return BAD_FUNC_ARG Returned if rng or key evaluate to NULL, or if the
  41. specified key size is not 32 bytes (Ed25519 has 32 byte keys).
  42. \return MEMORY_E Returned if there is an error allocating memory
  43. during function execution.
  44. \param [in] rng Pointer to an initialized RNG object with which to
  45. generate the key.
  46. \param [in] keysize Length of key to generate. Should always be 32 for
  47. Ed25519.
  48. \param [in,out] key Pointer to the ed25519_key for which to generate a key.
  49. _Example_
  50. \code
  51. int ret;
  52. WC_RNG rng;
  53. ed25519_key key;
  54. wc_InitRng(&rng);
  55. wc_ed25519_init(&key);
  56. wc_ed25519_make_key(&rng, 32, &key);
  57. if (ret != 0) {
  58. // error making key
  59. }
  60. \endcode
  61. \sa wc_ed25519_init
  62. */
  63. int wc_ed25519_make_key(WC_RNG* rng, int keysize, ed25519_key* key);
  64. /*!
  65. \ingroup ED25519
  66. \brief This function signs a message using an ed25519_key object
  67. to guarantee authenticity.
  68. \return 0 Returned upon successfully generating a signature for the
  69. message.
  70. \return BAD_FUNC_ARG Returned if any of the input parameters evaluate to
  71. NULL, or if the output buffer is too small to store the generated signature.
  72. \return MEMORY_E Returned if there is an error allocating memory during
  73. function execution.
  74. \param [in] in Pointer to the buffer containing the message to sign.
  75. \param [in] inlen Length of the message to sign.
  76. \param [out] out Buffer in which to store the generated signature.
  77. \param [in,out] outlen Maximum length of the output buffer. Will store the
  78. bytes written to out upon successfully generating a message signature.
  79. \param [in] key Pointer to a private ed25519_key with which to generate the
  80. signature.
  81. _Example_
  82. \code
  83. ed25519_key key;
  84. WC_RNG rng;
  85. int ret, sigSz;
  86. byte sig[64]; // will hold generated signature
  87. sigSz = sizeof(sig);
  88. byte message[] = { initialize with message };
  89. wc_InitRng(&rng); // initialize rng
  90. wc_ed25519_init(&key); // initialize key
  91. wc_ed25519_make_key(&rng, 32, &key); // make public/private key pair
  92. ret = wc_ed25519_sign_msg(message, sizeof(message), sig, &sigSz, &key);
  93. if (ret != 0) {
  94. // error generating message signature
  95. }
  96. \endcode
  97. \sa wc_ed25519ctx_sign_msg
  98. \sa wc_ed25519ph_sign_hash
  99. \sa wc_ed25519ph_sign_msg
  100. \sa wc_ed25519_verify_msg
  101. */
  102. int wc_ed25519_sign_msg(const byte* in, word32 inlen, byte* out,
  103. word32 *outlen, ed25519_key* key);
  104. /*!
  105. \ingroup ED25519
  106. \brief This function signs a message using an ed25519_key object
  107. to guarantee authenticity. The context is part of the data signed.
  108. \return 0 Returned upon successfully generating a signature for the
  109. message.
  110. \return BAD_FUNC_ARG Returned any of the input parameters evaluate to
  111. NULL, or if the output buffer is too small to store the generated signature.
  112. \return MEMORY_E Returned if there is an error allocating memory during
  113. function execution.
  114. \param [in] in Pointer to the buffer containing the message to sign.
  115. \param [in] inlen Length of the message to sign.
  116. \param [out] out Buffer in which to store the generated signature.
  117. \param [in,out] outlen Maximum length of the output buffer. Will store the
  118. bytes written to out upon successfully generating a message signature.
  119. \param [in] key Pointer to a private ed25519_key with which to generate the
  120. signature.
  121. \param [in] context Pointer to the buffer containing the context for which
  122. message is being signed.
  123. \param [in] contextLen Length of the context buffer.
  124. _Example_
  125. \code
  126. ed25519_key key;
  127. WC_RNG rng;
  128. int ret, sigSz;
  129. byte sig[64]; // will hold generated signature
  130. sigSz = sizeof(sig);
  131. byte message[] = { initialize with message };
  132. byte context[] = { initialize with context of signing };
  133. wc_InitRng(&rng); // initialize rng
  134. wc_ed25519_init(&key); // initialize key
  135. wc_ed25519_make_key(&rng, 32, &key); // make public/private key pair
  136. ret = wc_ed25519ctx_sign_msg(message, sizeof(message), sig, &sigSz, &key,
  137. context, sizeof(context));
  138. if (ret != 0) {
  139. // error generating message signature
  140. }
  141. \endcode
  142. \sa wc_ed25519_sign_msg
  143. \sa wc_ed25519ph_sign_hash
  144. \sa wc_ed25519ph_sign_msg
  145. \sa wc_ed25519_verify_msg
  146. */
  147. int wc_ed25519ctx_sign_msg(const byte* in, word32 inlen, byte* out,
  148. word32 *outlen, ed25519_key* key,
  149. const byte* context, byte contextLen);
  150. /*!
  151. \ingroup ED25519
  152. \brief This function signs a message digest using an ed25519_key object
  153. to guarantee authenticity. The context is included as part of the data
  154. signed. The message is pre-hashed before signature calculation. The hash
  155. algorithm used to create message digest must be SHAKE-256.
  156. \return 0 Returned upon successfully generating a signature for the
  157. message digest.
  158. \return BAD_FUNC_ARG Returned any of the input parameters evaluate to
  159. NULL, or if the output buffer is too small to store the generated signature.
  160. \return MEMORY_E Returned if there is an error allocating memory during
  161. function execution.
  162. \param [in] hash Pointer to the buffer containing the hash of the message
  163. to sign.
  164. \param [in] hashLen Length of the hash of the message to sign.
  165. \param [out] out Buffer in which to store the generated signature.
  166. \param [in,out] outlen Maximum length of the output buffer. Will store the
  167. bytes written to out upon successfully generating a message signature.
  168. \param [in] key Pointer to a private ed25519_key with which to generate the
  169. signature.
  170. \param [in] context Pointer to the buffer containing the context for which
  171. message is being signed.
  172. \param [in] contextLen Length of the context buffer.
  173. _Example_
  174. \code
  175. ed25519_key key;
  176. WC_RNG rng;
  177. int ret, sigSz;
  178. byte sig[64]; // will hold generated signature
  179. sigSz = sizeof(sig);
  180. byte hash[] = { initialize with SHA-512 hash of message };
  181. byte context[] = { initialize with context of signing };
  182. wc_InitRng(&rng); // initialize rng
  183. wc_ed25519_init(&key); // initialize key
  184. wc_ed25519_make_key(&rng, 32, &key); // make public/private key pair
  185. ret = wc_ed25519ph_sign_hash(hash, sizeof(hash), sig, &sigSz, &key,
  186. context, sizeof(context));
  187. if (ret != 0) {
  188. // error generating message signature
  189. }
  190. \endcode
  191. \sa wc_ed25519_sign_msg
  192. \sa wc_ed25519ctx_sign_msg
  193. \sa wc_ed25519ph_sign_msg
  194. \sa wc_ed25519_verify_msg
  195. */
  196. int wc_ed25519ph_sign_hash(const byte* hash, word32 hashLen, byte* out,
  197. word32 *outLen, ed25519_key* key,
  198. const byte* context, byte contextLen);
  199. /*!
  200. \ingroup ED25519
  201. \brief This function signs a message using an ed25519_key object
  202. to guarantee authenticity. The context is included as part of the data
  203. signed. The message is pre-hashed before signature calculation.
  204. \return 0 Returned upon successfully generating a signature for the
  205. message.
  206. \return BAD_FUNC_ARG Returned any of the input parameters evaluate to
  207. NULL, or if the output buffer is too small to store the generated signature.
  208. \return MEMORY_E Returned if there is an error allocating memory during
  209. function execution.
  210. \param [in] in Pointer to the buffer containing the message to sign.
  211. \param [in] inlen Length of the message to sign.
  212. \param [out] out Buffer in which to store the generated signature.
  213. \param [in,out] outlen Maximum length of the output buffer. Will store the
  214. bytes written to out upon successfully generating a message signature.
  215. \param [in] key Pointer to a private ed25519_key with which to generate the
  216. signature.
  217. \param [in] context Pointer to the buffer containing the context for which
  218. message is being signed.
  219. \param [in] contextLen Length of the context buffer.
  220. _Example_
  221. \code
  222. ed25519_key key;
  223. WC_RNG rng;
  224. int ret, sigSz;
  225. byte sig[64]; // will hold generated signature
  226. sigSz = sizeof(sig);
  227. byte message[] = { initialize with message };
  228. byte context[] = { initialize with context of signing };
  229. wc_InitRng(&rng); // initialize rng
  230. wc_ed25519_init(&key); // initialize key
  231. wc_ed25519_make_key(&rng, 32, &key); // make public/private key pair
  232. ret = wc_ed25519ph_sign_msg(message, sizeof(message), sig, &sigSz, &key,
  233. context, sizeof(context));
  234. if (ret != 0) {
  235. // error generating message signature
  236. }
  237. \endcode
  238. \sa wc_ed25519_sign_msg
  239. \sa wc_ed25519ctx_sign_msg
  240. \sa wc_ed25519ph_sign_hash
  241. \sa wc_ed25519_verify_msg
  242. */
  243. int wc_ed25519ph_sign_msg(const byte* in, word32 inlen, byte* out,
  244. word32 *outlen, ed25519_key* key,
  245. const byte* context, byte contextLen);
  246. /*!
  247. \ingroup ED25519
  248. \brief This function verifies the Ed25519 signature of a message to ensure
  249. authenticity. It returns the answer through res, with 1 corresponding to
  250. a valid signature, and 0 corresponding to an invalid signature.
  251. \return 0 Returned upon successfully performing the signature
  252. verification and authentication.
  253. \return BAD_FUNC_ARG Returned if any of the input parameters evaluate to
  254. NULL, or if the siglen does not match the actual length of a signature.
  255. \return SIG_VERIFY_E Returned if verification completes, but the signature
  256. generated does not match the signature provided.
  257. \param [in] sig Pointer to the buffer containing the signature to verify.
  258. \param [in] siglen Length of the signature to verify.
  259. \param [in] msg Pointer to the buffer containing the message to verify.
  260. \param [in] msgLen Length of the message to verify.
  261. \param [out] res Pointer to the result of the verification. 1 indicates the
  262. message was successfully verified.
  263. \param [in] key Pointer to a public Ed25519 key with which to verify the
  264. signature.
  265. _Example_
  266. \code
  267. ed25519_key key;
  268. int ret, verified = 0;
  269. byte sig[] { initialize with received signature };
  270. byte msg[] = { initialize with message };
  271. // initialize key with received public key
  272. ret = wc_ed25519_verify_msg(sig, sizeof(sig), msg, sizeof(msg), &verified,
  273. &key);
  274. if (ret < 0) {
  275. // error performing verification
  276. } else if (verified == 0)
  277. // the signature is invalid
  278. }
  279. \endcode
  280. \sa wc_ed25519ctx_verify_msg
  281. \sa wc_ed25519ph_verify_hash
  282. \sa wc_ed25519ph_verify_msg
  283. \sa wc_ed25519_sign_msg
  284. */
  285. int wc_ed25519_verify_msg(const byte* sig, word32 siglen, const byte* msg,
  286. word32 msgLen, int* ret, ed25519_key* key);
  287. /*!
  288. \ingroup ED25519
  289. \brief This function verifies the Ed25519 signature of a message to ensure
  290. authenticity. The context is included as part of the data
  291. verified. It returns the answer through res, with 1 corresponding to
  292. a valid signature, and 0 corresponding to an invalid signature.
  293. \return 0 Returned upon successfully performing the signature
  294. verification and authentication.
  295. \return BAD_FUNC_ARG Returned if any of the input parameters evaluate to
  296. NULL, or if the siglen does not match the actual length of a signature.
  297. \return SIG_VERIFY_E Returned if verification completes, but the signature
  298. generated does not match the signature provided.
  299. \param [in] sig Pointer to the buffer containing the signature to verify.
  300. \param [in] siglen Length of the signature to verify.
  301. \param [in] msg Pointer to the buffer containing the message to verify.
  302. \param [in] msgLen Length of the message to verify.
  303. \param [out] res Pointer to the result of the verification. 1 indicates the
  304. message was successfully verified.
  305. \param [in] key Pointer to a public Ed25519 key with which to verify the
  306. signature.
  307. \param [in] context Pointer to the buffer containing the context for which
  308. the message was signed.
  309. \param [in] contextLen Length of the context buffer.
  310. _Example_
  311. \code
  312. ed25519_key key;
  313. int ret, verified = 0;
  314. byte sig[] { initialize with received signature };
  315. byte msg[] = { initialize with message };
  316. byte context[] = { initialize with context of signature };
  317. // initialize key with received public key
  318. ret = wc_ed25519ctx_verify_msg(sig, sizeof(sig), msg, sizeof(msg),
  319. &verified, &key, );
  320. if (ret < 0) {
  321. // error performing verification
  322. } else if (verified == 0)
  323. // the signature is invalid
  324. }
  325. \endcode
  326. \sa wc_ed25519_verify_msg
  327. \sa wc_ed25519ph_verify_hash
  328. \sa wc_ed25519ph_verify_msg
  329. \sa wc_ed25519_sign_msg
  330. */
  331. int wc_ed25519ctx_verify_msg(const byte* sig, word32 siglen, const byte* msg,
  332. word32 msgLen, int* ret, ed25519_key* key,
  333. const byte* context, byte contextLen);
  334. /*!
  335. \ingroup ED25519
  336. \brief This function verifies the Ed25519 signature of the digest of a
  337. message to ensure authenticity. The context is included as part of the data
  338. verified. The hash is the pre-hashed message before signature calculation.
  339. The hash algorithm used to create message digest must be SHA-512.
  340. The answer is returned through res, with 1 corresponding to a valid
  341. signature, and 0 corresponding to an invalid signature.
  342. \return 0 Returned upon successfully performing the signature
  343. verification and authentication.
  344. \return BAD_FUNC_ARG Returned if any of the input parameters evaluate to
  345. NULL, or if the siglen does not match the actual length of a signature.
  346. \return SIG_VERIFY_E Returned if verification completes, but the signature
  347. generated does not match the signature provided.
  348. \param [in] sig Pointer to the buffer containing the signature to verify.
  349. \param [in] siglen Length of the signature to verify.
  350. \param [in] hash Pointer to the buffer containing the hash of the message
  351. to verify.
  352. \param [in] hashLen Length of the hash to verify.
  353. \param [out] res Pointer to the result of the verification. 1 indicates the
  354. message was successfully verified.
  355. \param [in] key Pointer to a public Ed25519 key with which to verify the
  356. signature.
  357. \param [in] context Pointer to the buffer containing the context for which
  358. the message was signed.
  359. \param [in] contextLen Length of the context buffer.
  360. _Example_
  361. \code
  362. ed25519_key key;
  363. int ret, verified = 0;
  364. byte sig[] { initialize with received signature };
  365. byte hash[] = { initialize with SHA-512 hash of message };
  366. byte context[] = { initialize with context of signature };
  367. // initialize key with received public key
  368. ret = wc_ed25519ph_verify_hash(sig, sizeof(sig), msg, sizeof(msg),
  369. &verified, &key, );
  370. if (ret < 0) {
  371. // error performing verification
  372. } else if (verified == 0)
  373. // the signature is invalid
  374. }
  375. \endcode
  376. \sa wc_ed25519_verify_msg
  377. \sa wc_ed25519ctx_verify_msg
  378. \sa wc_ed25519ph_verify_msg
  379. \sa wc_ed25519_sign_msg
  380. */
  381. int wc_ed25519ph_verify_hash(const byte* sig, word32 siglen, const byte* hash,
  382. word32 hashLen, int* ret, ed25519_key* key,
  383. const byte* context, byte contextLen);
  384. /*!
  385. \ingroup ED25519
  386. \brief This function verifies the Ed25519 signature of a message to ensure
  387. authenticity. The context is included as part of the data
  388. verified. The message is pre-hashed before verification. It returns the
  389. answer through res, with 1 corresponding to a valid signature, and 0
  390. corresponding to an invalid signature.
  391. \return 0 Returned upon successfully performing the signature
  392. verification and authentication.
  393. \return BAD_FUNC_ARG Returned if any of the input parameters evaluate to
  394. NULL, or if the siglen does not match the actual length of a signature.
  395. \return SIG_VERIFY_E Returned if verification completes, but the signature
  396. generated does not match the signature provided.
  397. \param [in] sig Pointer to the buffer containing the signature to verify.
  398. \param [in] siglen Length of the signature to verify.
  399. \param [in] msg Pointer to the buffer containing the message to verify.
  400. \param [in] msgLen Length of the message to verify.
  401. \param [out] res Pointer to the result of the verification. 1 indicates the
  402. message was successfully verified.
  403. \param [in] key Pointer to a public Ed25519 key with which to verify the
  404. signature.
  405. \param [in] context Pointer to the buffer containing the context for which
  406. the message was signed.
  407. \param [in] contextLen Length of the context buffer.
  408. _Example_
  409. \code
  410. ed25519_key key;
  411. int ret, verified = 0;
  412. byte sig[] { initialize with received signature };
  413. byte msg[] = { initialize with message };
  414. byte context[] = { initialize with context of signature };
  415. // initialize key with received public key
  416. ret = wc_ed25519ctx_verify_msg(sig, sizeof(sig), msg, sizeof(msg),
  417. &verified, &key, );
  418. if (ret < 0) {
  419. // error performing verification
  420. } else if (verified == 0)
  421. // the signature is invalid
  422. }
  423. \endcode
  424. \sa wc_ed25519_verify_msg
  425. \sa wc_ed25519ph_verify_hash
  426. \sa wc_ed25519ph_verify_msg
  427. \sa wc_ed25519_sign_msg
  428. */
  429. int wc_ed25519ph_verify_msg(const byte* sig, word32 siglen, const byte* msg,
  430. word32 msgLen, int* ret, ed25519_key* key,
  431. const byte* context, byte contextLen);
  432. /*!
  433. \ingroup ED25519
  434. \brief This function initializes an ed25519_key object for future use
  435. with message verification.
  436. \return 0 Returned upon successfully initializing the ed25519_key object.
  437. \return BAD_FUNC_ARG Returned if key is NULL.
  438. \param [in,out] key Pointer to the ed25519_key object to initialize.
  439. _Example_
  440. \code
  441. ed25519_key key;
  442. wc_ed25519_init(&key);
  443. \endcode
  444. \sa wc_ed25519_make_key
  445. \sa wc_ed25519_free
  446. */
  447. int wc_ed25519_init(ed25519_key* key);
  448. /*!
  449. \ingroup ED25519
  450. \brief This function frees an Ed25519 object after it has been used.
  451. \param [in,out] key Pointer to the ed25519_key object to free
  452. _Example_
  453. \code
  454. ed25519_key key;
  455. // initialize key and perform secure exchanges
  456. ...
  457. wc_ed25519_free(&key);
  458. \endcode
  459. \sa wc_ed25519_init
  460. */
  461. void wc_ed25519_free(ed25519_key* key);
  462. /*!
  463. \ingroup ED25519
  464. \brief This function imports a public ed25519_key pair from a buffer
  465. containing the public key. This function will handle both compressed and
  466. uncompressed keys. The public key is checked that it matches the private
  467. key when one is present.
  468. \return 0 Returned on successfully importing the ed25519_key.
  469. \return BAD_FUNC_ARG Returned if in or key evaluate to NULL, or inLen is
  470. less than the size of an Ed25519 key.
  471. \param [in] in Pointer to the buffer containing the public key.
  472. \param [in] inLen Length of the buffer containing the public key.
  473. \param [in,out] key Pointer to the ed25519_key object in which to store the
  474. public key.
  475. _Example_
  476. \code
  477. int ret;
  478. byte pub[] = { initialize Ed25519 public key };
  479. ed_25519 key;
  480. wc_ed25519_init_key(&key);
  481. ret = wc_ed25519_import_public(pub, sizeof(pub), &key);
  482. if (ret != 0) {
  483. // error importing key
  484. }
  485. \endcode
  486. \sa wc_ed25519_import_public_ex
  487. \sa wc_ed25519_import_private_key
  488. \sa wc_ed25519_import_private_key_ex
  489. \sa wc_ed25519_export_public
  490. */
  491. int wc_ed25519_import_public(const byte* in, word32 inLen, ed25519_key* key);
  492. /*!
  493. \ingroup ED25519
  494. \brief This function imports a public ed25519_key pair from a buffer
  495. containing the public key. This function will handle both compressed and
  496. uncompressed keys. Check public key matches private key, when present,
  497. when not trusted.
  498. \return 0 Returned on successfully importing the ed25519_key.
  499. \return BAD_FUNC_ARG Returned if in or key evaluate to NULL, or inLen is
  500. less than the size of an Ed25519 key.
  501. \param [in] in Pointer to the buffer containing the public key.
  502. \param [in] inLen Length of the buffer containing the public key.
  503. \param [in,out] key Pointer to the ed25519_key object in which to store the
  504. public key.
  505. \param [in] trusted Public key data is trusted or not.
  506. _Example_
  507. \code
  508. int ret;
  509. byte pub[] = { initialize Ed25519 public key };
  510. ed_25519 key;
  511. wc_ed25519_init_key(&key);
  512. ret = wc_ed25519_import_public_ex(pub, sizeof(pub), &key, 1);
  513. if (ret != 0) {
  514. // error importing key
  515. }
  516. \endcode
  517. \sa wc_ed25519_import_public
  518. \sa wc_ed25519_import_private_key
  519. \sa wc_ed25519_import_private_key_ex
  520. \sa wc_ed25519_export_public
  521. */
  522. int wc_ed25519_import_public_ex(const byte* in, word32 inLen, ed25519_key* key,
  523. int trusted);
  524. /*!
  525. \ingroup ED25519
  526. \brief This function imports an Ed25519 private key only from a
  527. buffer.
  528. \return 0 Returned on successfully importing the Ed25519 key.
  529. \return BAD_FUNC_ARG Returned if in or key evaluate to NULL, or if
  530. privSz is less than ED25519_KEY_SIZE.
  531. \param [in] priv Pointer to the buffer containing the private key.
  532. \param [in] privSz Length of the private key.
  533. \param [in] pub Pointer to the buffer containing the public key.
  534. \param [in] pubSz Length of the public key.
  535. \param [in,out] key Pointer to the ed25519_key object in which to store the
  536. imported private key.
  537. _Example_
  538. \code
  539. int ret;
  540. byte priv[] = { initialize with 32 byte private key };
  541. ed25519_key key;
  542. wc_ed25519_init_key(&key);
  543. ret = wc_ed25519_import_private_only(priv, sizeof(priv), &key);
  544. if (ret != 0) {
  545. // error importing private key
  546. }
  547. \endcode
  548. \sa wc_ed25519_import_public
  549. \sa wc_ed25519_import_public_ex
  550. \sa wc_ed25519_import_private_key
  551. \sa wc_ed25519_import_private_key_ex
  552. \sa wc_ed25519_export_private_only
  553. */
  554. int wc_ed25519_import_private_only(const byte* priv, word32 privSz,
  555. ed25519_key* key);
  556. /*!
  557. \ingroup ED25519
  558. \brief This function imports a public/private Ed25519 key pair from a
  559. pair of buffers. This function will handle both compressed and
  560. uncompressed keys. The public key is assumed to be untrusted and is
  561. checked against the private key.
  562. \return 0 Returned on successfully importing the ed25519_key.
  563. \return BAD_FUNC_ARG Returned if in or key evaluate to NULL, or if
  564. either privSz is less than ED25519_KEY_SIZE or pubSz is less than
  565. ED25519_PUB_KEY_SIZE.
  566. \param [in] priv Pointer to the buffer containing the private key.
  567. \param [in] privSz Length of the private key.
  568. \param [in] pub Pointer to the buffer containing the public key.
  569. \param [in] pubSz Length of the public key.
  570. \param [in,out] key Pointer to the ed25519_key object in which to store the
  571. imported private/public key pair.
  572. _Example_
  573. \code
  574. int ret;
  575. byte priv[] = { initialize with 32 byte private key };
  576. byte pub[] = { initialize with the corresponding public key };
  577. ed25519_key key;
  578. wc_ed25519_init_key(&key);
  579. ret = wc_ed25519_import_private_key(priv, sizeof(priv), pub, sizeof(pub),
  580. &key);
  581. if (ret != 0) {
  582. // error importing key
  583. }
  584. \endcode
  585. \sa wc_ed25519_import_public
  586. \sa wc_ed25519_import_public_ex
  587. \sa wc_ed25519_import_private_only
  588. \sa wc_ed25519_import_private_key_ex
  589. \sa wc_ed25519_export_private
  590. */
  591. int wc_ed25519_import_private_key(const byte* priv, word32 privSz,
  592. const byte* pub, word32 pubSz, ed25519_key* key);
  593. /*!
  594. \ingroup ED25519
  595. \brief This function imports a public/private Ed25519 key pair from a
  596. pair of buffers. This function will handle both compressed and
  597. uncompressed keys. The public is checked against private key if not trusted.
  598. \return 0 Returned on successfully importing the ed25519_key.
  599. \return BAD_FUNC_ARG Returned if in or key evaluate to NULL, or if
  600. either privSz is less than ED25519_KEY_SIZE or pubSz is less than
  601. ED25519_PUB_KEY_SIZE.
  602. \param [in] priv Pointer to the buffer containing the private key.
  603. \param [in] privSz Length of the private key.
  604. \param [in] pub Pointer to the buffer containing the public key.
  605. \param [in] pubSz Length of the public key.
  606. \param [in,out] key Pointer to the ed25519_key object in which to store the
  607. imported private/public key pair.
  608. \param [in] trusted Public key data is trusted or not.
  609. _Example_
  610. \code
  611. int ret;
  612. byte priv[] = { initialize with 32 byte private key };
  613. byte pub[] = { initialize with the corresponding public key };
  614. ed25519_key key;
  615. wc_ed25519_init_key(&key);
  616. ret = wc_ed25519_import_private_key(priv, sizeof(priv), pub, sizeof(pub),
  617. &key, 1);
  618. if (ret != 0) {
  619. // error importing key
  620. }
  621. \endcode
  622. \sa wc_ed25519_import_public
  623. \sa wc_ed25519_import_public_ex
  624. \sa wc_ed25519_import_private_only
  625. \sa wc_ed25519_import_private_key
  626. \sa wc_ed25519_export_private
  627. */
  628. int wc_ed25519_import_private_key_ex(const byte* priv, word32 privSz,
  629. const byte* pub, word32 pubSz, ed25519_key* key, int trusted);
  630. /*!
  631. \ingroup ED25519
  632. \brief This function exports the private key from an ed25519_key
  633. structure. It stores the public key in the buffer out, and sets the bytes
  634. written to this buffer in outLen.
  635. \return 0 Returned upon successfully exporting the public key.
  636. \return BAD_FUNC_ARG Returned if any of the input values evaluate to NULL.
  637. \return BUFFER_E Returned if the buffer provided is not large enough to
  638. store the private key. Upon returning this error, the function sets the
  639. size required in outLen.
  640. \param [in] key Pointer to an ed25519_key structure from which to export the
  641. public key.
  642. \param [out] out Pointer to the buffer in which to store the public key.
  643. \param [in,out] outLen Pointer to a word32 object with the size available
  644. in out. Set with the number of bytes written to out after successfully
  645. exporting the public key.
  646. _Example_
  647. \code
  648. int ret;
  649. ed25519_key key;
  650. // initialize key, make key
  651. char pub[32];
  652. word32 pubSz = sizeof(pub);
  653. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  654. if (ret != 0) {
  655. // error exporting public key
  656. }
  657. \endcode
  658. \sa wc_ed25519_import_public
  659. \sa wc_ed25519_import_public_ex
  660. \sa wc_ed25519_export_private_only
  661. */
  662. int wc_ed25519_export_public(ed25519_key* key, byte* out, word32* outLen);
  663. /*!
  664. \ingroup ED25519
  665. \brief This function exports only the private key from an ed25519_key
  666. structure. It stores the private key in the buffer out, and sets
  667. the bytes written to this buffer in outLen.
  668. \return 0 Returned upon successfully exporting the private key.
  669. \return ECC_BAD_ARG_E Returned if any of the input values evaluate to NULL.
  670. \return BUFFER_E Returned if the buffer provided is not large enough
  671. to store the private key.
  672. \param [in] key Pointer to an ed25519_key structure from which to export
  673. the private key.
  674. \param [out] out Pointer to the buffer in which to store the private key.
  675. \param [in,out] outLen Pointer to a word32 object with the size available in
  676. out. Set with the number of bytes written to out after successfully
  677. exporting the private key.
  678. _Example_
  679. \code
  680. int ret;
  681. ed25519_key key;
  682. // initialize key, make key
  683. char priv[32]; // 32 bytes because only private key
  684. word32 privSz = sizeof(priv);
  685. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  686. if (ret != 0) {
  687. // error exporting private key
  688. }
  689. \endcode
  690. \sa wc_ed25519_export_public
  691. \sa wc_ed25519_import_private_key
  692. \sa wc_ed25519_import_private_key_ex
  693. */
  694. int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen);
  695. /*!
  696. \ingroup ED25519
  697. \brief This function exports the key pair from an ed25519_key
  698. structure. It stores the key pair in the buffer out, and sets
  699. the bytes written to this buffer in outLen.
  700. \return 0 Returned upon successfully exporting the key pair.
  701. \return ECC_BAD_ARG_E Returned if any of the input values evaluate to NULL.
  702. \return BUFFER_E Returned if the buffer provided is not large enough
  703. to store the key pair.
  704. \param [in] key Pointer to an ed25519_key structure from which to export
  705. the key pair.
  706. \param [out] out Pointer to the buffer in which to store the key pair.
  707. \param [in,out] outLen Pointer to a word32 object with the size available in
  708. out. Set with the number of bytes written to out after successfully
  709. exporting the key pair.
  710. _Example_
  711. \code
  712. ed25519_key key;
  713. wc_ed25519_init(&key);
  714. WC_RNG rng;
  715. wc_InitRng(&rng);
  716. wc_ed25519_make_key(&rng, 32, &key); // initialize 32 byte Ed25519 key
  717. byte out[64]; // out needs to be a sufficient buffer size
  718. word32 outLen = sizeof(out);
  719. int key_size = wc_ed25519_export_private(&key, out, &outLen);
  720. if (key_size == BUFFER_E) {
  721. // Check size of out compared to outLen to see if function reset outLen
  722. }
  723. \endcode
  724. \sa wc_ed25519_import_private_key
  725. \sa wc_ed25519_import_private_key_ex
  726. \sa wc_ed25519_export_private_only
  727. */
  728. int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen);
  729. /*!
  730. \ingroup ED25519
  731. \brief This function exports the private and public key separately from an
  732. ed25519_key structure. It stores the private key in the buffer priv, and
  733. sets the bytes written to this buffer in privSz. It stores the public key
  734. in the buffer pub, and sets the bytes written to this buffer in pubSz.
  735. \return 0 Returned upon successfully exporting the key pair.
  736. \return ECC_BAD_ARG_E Returned if any of the input values evaluate to NULL.
  737. \return BUFFER_E Returned if the buffer provided is not large enough
  738. to store the key pair.
  739. \param [in] key Pointer to an ed25519_key structure from which to export
  740. the key pair.
  741. \param [out] priv Pointer to the buffer in which to store the private key.
  742. \param [in,out] privSz Pointer to a word32 object with the size available in
  743. out. Set with the number of bytes written to out after successfully
  744. exporting the private key.
  745. \param [out] pub Pointer to the buffer in which to store the public key.
  746. \param [in,out] pubSz Pointer to a word32 object with the size available in
  747. out. Set with the number of bytes written to out after successfully
  748. exporting the public key.
  749. _Example_
  750. \code
  751. int ret;
  752. ed25519_key key;
  753. // initialize key, make key
  754. char pub[32];
  755. word32 pubSz = sizeof(pub);
  756. char priv[32];
  757. word32 privSz = sizeof(priv);
  758. ret = wc_ed25519_export_key(&key, priv, &pubSz, pub, &pubSz);
  759. if (ret != 0) {
  760. // error exporting public key
  761. }
  762. \endcode
  763. \sa wc_ed25519_export_private
  764. \sa wc_ed25519_export_public
  765. */
  766. int wc_ed25519_export_key(ed25519_key* key,
  767. byte* priv, word32 *privSz,
  768. byte* pub, word32 *pubSz);
  769. /*!
  770. \ingroup ED25519
  771. \brief This function checks the public key in ed25519_key structure matches
  772. the private key.
  773. \return 0 Returned if the private and public key matched.
  774. \return BAD_FUNC_ARGS Returned if the given key is NULL.
  775. \param [in] key Pointer to an ed25519_key structure holding a private and
  776. public key.
  777. _Example_
  778. \code
  779. int ret;
  780. byte priv[] = { initialize with 57 byte private key };
  781. byte pub[] = { initialize with the corresponding public key };
  782. ed25519_key key;
  783. wc_ed25519_init_key(&key);
  784. wc_ed25519_import_private_key_ex(priv, sizeof(priv), pub, sizeof(pub), &key,
  785. 1);
  786. ret = wc_ed25519_check_key(&key);
  787. if (ret != 0) {
  788. // error checking key
  789. }
  790. \endcode
  791. \sa wc_ed25519_import_private_key
  792. \sa wc_ed25519_import_private_key_ex
  793. */
  794. int wc_ed25519_check_key(ed25519_key* key);
  795. /*!
  796. \ingroup ED25519
  797. \brief This function returns the size of an Ed25519 - 32 bytes.
  798. \return ED25519_KEY_SIZE The size of a valid private key (32 bytes).
  799. \return BAD_FUNC_ARGS Returned if the given key is NULL.
  800. \param [in] key Pointer to an ed25519_key structure for which to get the
  801. key size.
  802. _Example_
  803. \code
  804. int keySz;
  805. ed25519_key key;
  806. // initialize key, make key
  807. keySz = wc_ed25519_size(&key);
  808. if (keySz == 0) {
  809. // error determining key size
  810. }
  811. \endcode
  812. \sa wc_ed25519_make_key
  813. */
  814. int wc_ed25519_size(ed25519_key* key);
  815. /*!
  816. \ingroup ED25519
  817. \brief This function returns the private key size (secret + public) in
  818. bytes.
  819. \return ED25519_PRV_KEY_SIZE The size of the private key (64 bytes).
  820. \return BAD_FUNC_ARG Returned if key argument is NULL.
  821. \param [in] key Pointer to an ed25519_key structure for which to get the
  822. key size.
  823. _Example_
  824. \code
  825. ed25519_key key;
  826. wc_ed25519_init(&key);
  827. WC_RNG rng;
  828. wc_InitRng(&rng);
  829. wc_ed25519_make_key(&rng, 32, &key); // initialize 32 byte Ed25519 key
  830. int key_size = wc_ed25519_priv_size(&key);
  831. \endcode
  832. \sa wc_ed25519_pub_size
  833. */
  834. int wc_ed25519_priv_size(ed25519_key* key);
  835. /*!
  836. \ingroup ED25519
  837. \brief This function returns the compressed key size in bytes (public key).
  838. \return ED25519_PUB_KEY_SIZE The size of the compressed public key
  839. (32 bytes).
  840. \return BAD_FUNC_ARG Returns if key argument is NULL.
  841. \param [in] key Pointer to an ed25519_key structure for which to get the
  842. key size.
  843. _Example_
  844. \code
  845. ed25519_key key;
  846. wc_ed25519_init(&key);
  847. WC_RNG rng;
  848. wc_InitRng(&rng);
  849. wc_ed25519_make_key(&rng, 32, &key); // initialize 32 byte Ed25519 key
  850. int key_size = wc_ed25519_pub_size(&key);
  851. \endcode
  852. \sa wc_ed25519_priv_size
  853. */
  854. int wc_ed25519_pub_size(ed25519_key* key);
  855. /*!
  856. \ingroup ED25519
  857. \brief This function returns the size of an Ed25519 signature (64 in bytes).
  858. \return ED25519_SIG_SIZE The size of an Ed25519 signature (64 bytes).
  859. \return BAD_FUNC_ARG Returns if key argument is NULL.
  860. \param [in] key Pointer to an ed25519_key structure for which to get the
  861. signature size.
  862. _Example_
  863. \code
  864. int sigSz;
  865. ed25519_key key;
  866. // initialize key, make key
  867. sigSz = wc_ed25519_sig_size(&key);
  868. if (sigSz == 0) {
  869. // error determining sig size
  870. }
  871. \endcode
  872. \sa wc_ed25519_sign_msg
  873. */
  874. int wc_ed25519_sig_size(ed25519_key* key);