ed25519.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* ed25519.h
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*!
  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. byte p[ED25519_PUB_KEY_SIZE]; /* compressed public key */
  72. byte k[ED25519_PRV_KEY_SIZE]; /* private key : 32 secret -- 32 public */
  73. #ifdef FREESCALE_LTC_ECC
  74. /* uncompressed point coordinates */
  75. byte pointX[ED25519_KEY_SIZE]; /* recovered X coordinate */
  76. 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. int keyId;
  80. word32 flags;
  81. #endif
  82. word16 privKeySet:1;
  83. word16 pubKeySet:1;
  84. #ifdef WOLFSSL_ASYNC_CRYPT
  85. WC_ASYNC_DEV asyncDev;
  86. #endif
  87. #if defined(WOLF_CRYPTO_CB)
  88. int devId;
  89. #endif
  90. void *heap;
  91. #ifdef WOLFSSL_ED25519_PERSISTENT_SHA
  92. wc_Sha512 sha;
  93. int sha_clean_flag;
  94. #endif
  95. };
  96. WOLFSSL_API
  97. int wc_ed25519_make_public(ed25519_key* key, unsigned char* pubKey,
  98. word32 pubKeySz);
  99. WOLFSSL_API
  100. int wc_ed25519_make_key(WC_RNG* rng, int keysize, ed25519_key* key);
  101. #ifdef HAVE_ED25519_SIGN
  102. WOLFSSL_API
  103. int wc_ed25519_sign_msg(const byte* in, word32 inLen, byte* out,
  104. word32 *outLen, ed25519_key* key);
  105. WOLFSSL_API
  106. int wc_ed25519ctx_sign_msg(const byte* in, word32 inLen, byte* out,
  107. word32 *outLen, ed25519_key* key,
  108. const byte* context, byte contextLen);
  109. WOLFSSL_API
  110. int wc_ed25519ph_sign_hash(const byte* hash, word32 hashLen, byte* out,
  111. word32 *outLen, ed25519_key* key,
  112. const byte* context, byte contextLen);
  113. WOLFSSL_API
  114. int wc_ed25519ph_sign_msg(const byte* in, word32 inLen, byte* out,
  115. word32 *outLen, ed25519_key* key, const byte* context,
  116. byte contextLen);
  117. WOLFSSL_API
  118. int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out,
  119. word32 *outLen, ed25519_key* key, byte type,
  120. const byte* context, byte contextLen);
  121. #endif /* HAVE_ED25519_SIGN */
  122. #ifdef HAVE_ED25519_VERIFY
  123. WOLFSSL_API
  124. int wc_ed25519_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  125. word32 msgLen, int* res, ed25519_key* key);
  126. WOLFSSL_API
  127. int wc_ed25519ctx_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  128. word32 msgLen, int* res, ed25519_key* key,
  129. const byte* context, byte contextLen);
  130. WOLFSSL_API
  131. int wc_ed25519ph_verify_hash(const byte* sig, word32 sigLen, const byte* hash,
  132. word32 hashLen, int* res, ed25519_key* key,
  133. const byte* context, byte contextLen);
  134. WOLFSSL_API
  135. int wc_ed25519ph_verify_msg(const byte* sig, word32 sigLen, const byte* msg,
  136. word32 msgLen, int* res, ed25519_key* key,
  137. const byte* context, byte contextLen);
  138. WOLFSSL_API
  139. int wc_ed25519_verify_msg_ex(const byte* sig, word32 sigLen, const byte* msg,
  140. word32 msgLen, int* res, ed25519_key* key,
  141. byte type, const byte* context, byte contextLen);
  142. #ifdef WOLFSSL_ED25519_STREAMING_VERIFY
  143. WOLFSSL_API
  144. int wc_ed25519_verify_msg_init(const byte* sig, word32 sigLen, ed25519_key* key,
  145. byte type, const byte* context, byte contextLen);
  146. WOLFSSL_API
  147. int wc_ed25519_verify_msg_update(const byte* msgSegment, word32 msgSegmentLen,
  148. ed25519_key* key);
  149. WOLFSSL_API
  150. int wc_ed25519_verify_msg_final(const byte* sig, word32 sigLen, int* res,
  151. ed25519_key* key);
  152. #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */
  153. #endif /* HAVE_ED25519_VERIFY */
  154. WOLFSSL_API
  155. int wc_ed25519_init(ed25519_key* key);
  156. WOLFSSL_API
  157. int wc_ed25519_init_ex(ed25519_key* key, void* heap, int devId);
  158. WOLFSSL_API
  159. void wc_ed25519_free(ed25519_key* key);
  160. #ifdef HAVE_ED25519_KEY_IMPORT
  161. WOLFSSL_API
  162. int wc_ed25519_import_public(const byte* in, word32 inLen, ed25519_key* key);
  163. WOLFSSL_API
  164. int wc_ed25519_import_public_ex(const byte* in, word32 inLen, ed25519_key* key,
  165. int trusted);
  166. WOLFSSL_API
  167. int wc_ed25519_import_private_only(const byte* priv, word32 privSz,
  168. ed25519_key* key);
  169. WOLFSSL_API
  170. int wc_ed25519_import_private_key(const byte* priv, word32 privSz,
  171. const byte* pub, word32 pubSz, ed25519_key* key);
  172. WOLFSSL_API
  173. int wc_ed25519_import_private_key_ex(const byte* priv, word32 privSz,
  174. const byte* pub, word32 pubSz, ed25519_key* key, int trusted);
  175. #endif /* HAVE_ED25519_KEY_IMPORT */
  176. #ifdef HAVE_ED25519_KEY_EXPORT
  177. WOLFSSL_API
  178. int wc_ed25519_export_public(ed25519_key* key, byte* out, word32* outLen);
  179. WOLFSSL_API
  180. int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen);
  181. WOLFSSL_API
  182. int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen);
  183. WOLFSSL_API
  184. int wc_ed25519_export_key(ed25519_key* key,
  185. byte* priv, word32 *privSz,
  186. byte* pub, word32 *pubSz);
  187. #endif /* HAVE_ED25519_KEY_EXPORT */
  188. WOLFSSL_API
  189. int wc_ed25519_check_key(ed25519_key* key);
  190. /* size helper */
  191. WOLFSSL_API
  192. int wc_ed25519_size(ed25519_key* key);
  193. WOLFSSL_API
  194. int wc_ed25519_priv_size(ed25519_key* key);
  195. WOLFSSL_API
  196. int wc_ed25519_pub_size(ed25519_key* key);
  197. WOLFSSL_API
  198. int wc_ed25519_sig_size(ed25519_key* key);
  199. #ifdef __cplusplus
  200. } /* extern "C" */
  201. #endif
  202. #endif /* HAVE_ED25519 */
  203. #endif /* WOLF_CRYPT_ED25519_H */