ed25519.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* ed25519.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*!
  22. \file wolfssl/wolfcrypt/ed25519.h
  23. */
  24. #ifndef WOLF_CRYPT_ED25519_H
  25. #define WOLF_CRYPT_ED25519_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #ifdef HAVE_ED25519
  28. #include <wolfssl/wolfcrypt/fe_operations.h>
  29. #include <wolfssl/wolfcrypt/ge_operations.h>
  30. #include <wolfssl/wolfcrypt/random.h>
  31. #ifndef WOLFSSL_SHA512
  32. #error ED25519 requires SHA512
  33. #endif
  34. #include <wolfssl/wolfcrypt/sha512.h>
  35. #ifdef WOLFSSL_ASYNC_CRYPT
  36. #include <wolfssl/wolfcrypt/async.h>
  37. #endif
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /* info about EdDSA curve specifically ed25519, defined as an elliptic curve
  42. over GF(p) */
  43. /*
  44. 32, key size
  45. "ED25519", curve name
  46. "2^255-19", prime number
  47. "SHA512", hash function
  48. "-121665/121666", value of d
  49. */
  50. #define ED25519_KEY_SIZE 32 /* private key only */
  51. #define ED25519_SIG_SIZE 64
  52. #define ED25519_PUB_KEY_SIZE 32 /* compressed */
  53. /* both private and public key */
  54. #define ED25519_PRV_KEY_SIZE (ED25519_PUB_KEY_SIZE+ED25519_KEY_SIZE)
  55. enum {
  56. Ed25519 = -1,
  57. Ed25519ctx = 0,
  58. Ed25519ph = 1
  59. };
  60. #ifndef WC_ED25519KEY_TYPE_DEFINED
  61. typedef struct ed25519_key ed25519_key;
  62. #define WC_ED25519KEY_TYPE_DEFINED
  63. #endif
  64. /* ED25519 Flags */
  65. enum {
  66. WC_ED25519_FLAG_NONE = 0x00,
  67. WC_ED25519_FLAG_DEC_SIGN = 0x01
  68. };
  69. /* An ED25519 Key */
  70. struct ed25519_key {
  71. ALIGN16 byte p[ED25519_PUB_KEY_SIZE]; /* compressed public key */
  72. ALIGN16 byte k[ED25519_PRV_KEY_SIZE]; /* private key: 32 secret, 32 pub */
  73. #ifdef FREESCALE_LTC_ECC
  74. /* uncompressed point coordinates */
  75. ALIGN16 byte pointX[ED25519_KEY_SIZE]; /* recovered X coordinate */
  76. ALIGN16 byte pointY[ED25519_KEY_SIZE]; /* Y coordinate is the public key with The most significant bit of the final octet always zero. */
  77. #endif
  78. #ifdef WOLFSSL_SE050
  79. word32 keyId;
  80. word32 flags;
  81. byte keyIdSet;
  82. #endif
  83. word16 privKeySet:1;
  84. word16 pubKeySet:1;
  85. #ifdef WOLFSSL_ASYNC_CRYPT
  86. WC_ASYNC_DEV asyncDev;
  87. #endif
  88. #if defined(WOLF_CRYPTO_CB)
  89. void* devCtx;
  90. int devId;
  91. #endif
  92. void *heap;
  93. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  94. wc_Sha512 sha;
  95. int sha_clean_flag;
  96. #endif
  97. };
  98. WOLFSSL_API
  99. int wc_ed25519_make_public(ed25519_key* key, unsigned char* pubKey,
  100. word32 pubKeySz);
  101. WOLFSSL_API
  102. int wc_ed25519_make_key(WC_RNG* rng, int keysize, ed25519_key* key);
  103. #ifdef HAVE_ED25519_SIGN
  104. WOLFSSL_API
  105. int wc_ed25519_sign_msg(const byte* in, word32 inLen, byte* out,
  106. word32 *outLen, ed25519_key* key);
  107. WOLFSSL_API
  108. int wc_ed25519ctx_sign_msg(const byte* in, word32 inLen, byte* out,
  109. word32 *outLen, ed25519_key* key,
  110. const byte* context, byte contextLen);
  111. WOLFSSL_API
  112. int wc_ed25519ph_sign_hash(const byte* hash, word32 hashLen, byte* out,
  113. word32 *outLen, ed25519_key* key,
  114. const byte* context, byte contextLen);
  115. WOLFSSL_API
  116. int wc_ed25519ph_sign_msg(const byte* in, word32 inLen, byte* out,
  117. word32 *outLen, ed25519_key* key, const byte* context,
  118. byte contextLen);
  119. WOLFSSL_API
  120. int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out,
  121. word32 *outLen, ed25519_key* key, byte type,
  122. const byte* context, byte contextLen);
  123. #endif /* HAVE_ED25519_SIGN */
  124. #ifdef HAVE_ED25519_VERIFY
  125. WOLFSSL_API
  126. int wc_ed25519_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  127. word32 msgLen, int* res, ed25519_key* key);
  128. WOLFSSL_API
  129. int wc_ed25519ctx_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  130. word32 msgLen, int* res, ed25519_key* key,
  131. const byte* context, byte contextLen);
  132. WOLFSSL_API
  133. int wc_ed25519ph_verify_hash(const byte* sig, word32 sigLen, const byte* hash,
  134. word32 hashLen, int* res, ed25519_key* key,
  135. const byte* context, byte contextLen);
  136. WOLFSSL_API
  137. int wc_ed25519ph_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  138. word32 msgLen, int* res, ed25519_key* key,
  139. const byte* context, byte contextLen);
  140. WOLFSSL_API
  141. int wc_ed25519_verify_msg_ex(const byte* sig, word32 sigLen, const byte* msg,
  142. word32 msgLen, int* res, ed25519_key* key,
  143. byte type, const byte* context, byte contextLen);
  144. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  145. WOLFSSL_API
  146. int wc_ed25519_verify_msg_init(const byte* sig, word32 sigLen, ed25519_key* key,
  147. byte type, const byte* context, byte contextLen);
  148. WOLFSSL_API
  149. int wc_ed25519_verify_msg_update(const byte* msgSegment, word32 msgSegmentLen,
  150. ed25519_key* key);
  151. WOLFSSL_API
  152. int wc_ed25519_verify_msg_final(const byte* sig, word32 sigLen, int* res,
  153. ed25519_key* key);
  154. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  155. #endif /* HAVE_ED25519_VERIFY */
  156. WOLFSSL_API
  157. int wc_ed25519_init(ed25519_key* key);
  158. WOLFSSL_API
  159. int wc_ed25519_init_ex(ed25519_key* key, void* heap, int devId);
  160. WOLFSSL_API
  161. void wc_ed25519_free(ed25519_key* key);
  162. #ifdef HAVE_ED25519_KEY_IMPORT
  163. WOLFSSL_API
  164. int wc_ed25519_import_public(const byte* in, word32 inLen, ed25519_key* key);
  165. WOLFSSL_API
  166. int wc_ed25519_import_public_ex(const byte* in, word32 inLen, ed25519_key* key,
  167. int trusted);
  168. WOLFSSL_API
  169. int wc_ed25519_import_private_only(const byte* priv, word32 privSz,
  170. ed25519_key* key);
  171. WOLFSSL_API
  172. int wc_ed25519_import_private_key(const byte* priv, word32 privSz,
  173. const byte* pub, word32 pubSz, ed25519_key* key);
  174. WOLFSSL_API
  175. int wc_ed25519_import_private_key_ex(const byte* priv, word32 privSz,
  176. const byte* pub, word32 pubSz, ed25519_key* key, int trusted);
  177. #endif /* HAVE_ED25519_KEY_IMPORT */
  178. #ifdef HAVE_ED25519_KEY_EXPORT
  179. WOLFSSL_API
  180. int wc_ed25519_export_public(ed25519_key* key, byte* out, word32* outLen);
  181. WOLFSSL_API
  182. int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen);
  183. WOLFSSL_API
  184. int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen);
  185. WOLFSSL_API
  186. int wc_ed25519_export_key(ed25519_key* key,
  187. byte* priv, word32 *privSz,
  188. byte* pub, word32 *pubSz);
  189. #endif /* HAVE_ED25519_KEY_EXPORT */
  190. WOLFSSL_API
  191. int wc_ed25519_check_key(ed25519_key* key);
  192. /* size helper */
  193. WOLFSSL_API
  194. int wc_ed25519_size(ed25519_key* key);
  195. WOLFSSL_API
  196. int wc_ed25519_priv_size(ed25519_key* key);
  197. WOLFSSL_API
  198. int wc_ed25519_pub_size(ed25519_key* key);
  199. WOLFSSL_API
  200. int wc_ed25519_sig_size(ed25519_key* key);
  201. #ifdef __cplusplus
  202. } /* extern "C" */
  203. #endif
  204. #endif /* HAVE_ED25519 */
  205. #endif /* WOLF_CRYPT_ED25519_H */