ed25519.h (8101B)
1 /* ed25519.h 2 * 3 * Copyright (C) 2006-2025 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 3 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 /*! 23 \file wolfssl/wolfcrypt/ed25519.h 24 */ 25 26 27 #ifndef WOLF_CRYPT_ED25519_H 28 #define WOLF_CRYPT_ED25519_H 29 30 #include <wolfssl/wolfcrypt/types.h> 31 32 #ifdef HAVE_ED25519 33 34 #include <wolfssl/wolfcrypt/random.h> 35 #ifndef WOLFSSL_SHA512 36 #error ED25519 requires SHA512 37 #endif 38 #include <wolfssl/wolfcrypt/sha512.h> 39 40 #ifdef WOLFSSL_ASYNC_CRYPT 41 #include <wolfssl/wolfcrypt/async.h> 42 #endif 43 44 #ifdef __cplusplus 45 extern "C" { 46 #endif 47 48 #if FIPS_VERSION3_GE(6,0,0) 49 extern const unsigned int wolfCrypt_FIPS_ed25519_ro_sanity[2]; 50 WOLFSSL_LOCAL int wolfCrypt_FIPS_ED25519_sanity(void); 51 #endif 52 53 /* info about EdDSA curve specifically ed25519, defined as an elliptic curve 54 over GF(p) */ 55 /* 56 32, key size 57 "ED25519", curve name 58 "2^255-19", prime number 59 "SHA512", hash function 60 "-121665/121666", value of d 61 */ 62 63 #define ED25519_KEY_SIZE 32 /* private key only */ 64 #define ED25519_SIG_SIZE 64 65 66 #define ED25519_PUB_KEY_SIZE 32 /* compressed */ 67 /* both private and public key */ 68 #define ED25519_PRV_KEY_SIZE (ED25519_PUB_KEY_SIZE+ED25519_KEY_SIZE) 69 70 71 enum { 72 Ed25519 = -1, 73 Ed25519ctx = 0, 74 Ed25519ph = 1 75 }; 76 77 /* ED25519 Flags */ 78 enum { 79 WC_ED25519_FLAG_NONE = 0x00, 80 WC_ED25519_FLAG_DEC_SIGN = 0x01 81 }; 82 83 /* An ED25519 Key */ 84 struct ed25519_key { 85 ALIGN16 byte p[ED25519_PUB_KEY_SIZE]; /* compressed public key */ 86 ALIGN16 byte k[ED25519_PRV_KEY_SIZE]; /* private key: 32 secret, 32 pub */ 87 #ifdef FREESCALE_LTC_ECC 88 /* uncompressed point coordinates */ 89 ALIGN16 byte pointX[ED25519_KEY_SIZE]; /* recovered X coordinate */ 90 ALIGN16 byte pointY[ED25519_KEY_SIZE]; /* Y coordinate is the public key with The most significant bit of the final octet always zero. */ 91 #endif 92 #ifdef WOLFSSL_SE050 93 word32 keyId; 94 word32 flags; 95 byte keyIdSet; 96 #endif 97 WC_BITFIELD privKeySet:1; 98 WC_BITFIELD pubKeySet:1; 99 WC_BITFIELD sha_clean_flag:1; /* only used if WOLFSSL_ED25519_PERSISTENT_SHA */ 100 #ifdef WOLFSSL_ASYNC_CRYPT 101 WC_ASYNC_DEV asyncDev; 102 #endif 103 #if defined(WOLF_CRYPTO_CB) 104 void* devCtx; 105 int devId; 106 #endif 107 void *heap; 108 #ifdef WOLFSSL_ED25519_PERSISTENT_SHA 109 wc_Sha512 sha; 110 #endif 111 }; 112 113 #ifndef WC_ED25519KEY_TYPE_DEFINED 114 typedef struct ed25519_key ed25519_key; 115 #define WC_ED25519KEY_TYPE_DEFINED 116 #endif 117 118 119 WOLFSSL_API 120 int wc_ed25519_make_public(ed25519_key* key, unsigned char* pubKey, 121 word32 pubKeySz); 122 WOLFSSL_API 123 int wc_ed25519_make_key(WC_RNG* rng, int keysize, ed25519_key* key); 124 #ifdef HAVE_ED25519_SIGN 125 WOLFSSL_API 126 int wc_ed25519_sign_msg(const byte* in, word32 inLen, byte* out, 127 word32 *outLen, ed25519_key* key); 128 WOLFSSL_API 129 int wc_ed25519ctx_sign_msg(const byte* in, word32 inLen, byte* out, 130 word32 *outLen, ed25519_key* key, 131 const byte* context, byte contextLen); 132 WOLFSSL_API 133 int wc_ed25519ph_sign_hash(const byte* hash, word32 hashLen, byte* out, 134 word32 *outLen, ed25519_key* key, 135 const byte* context, byte contextLen); 136 WOLFSSL_API 137 int wc_ed25519ph_sign_msg(const byte* in, word32 inLen, byte* out, 138 word32 *outLen, ed25519_key* key, const byte* context, 139 byte contextLen); 140 WOLFSSL_API 141 int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out, 142 word32 *outLen, ed25519_key* key, byte type, 143 const byte* context, byte contextLen); 144 #endif /* HAVE_ED25519_SIGN */ 145 #ifdef HAVE_ED25519_VERIFY 146 WOLFSSL_API 147 int wc_ed25519_verify_msg(const byte* sig, word32 sigLen, const byte* msg, 148 word32 msgLen, int* res, ed25519_key* key); 149 WOLFSSL_API 150 int wc_ed25519ctx_verify_msg(const byte* sig, word32 sigLen, const byte* msg, 151 word32 msgLen, int* res, ed25519_key* key, 152 const byte* context, byte contextLen); 153 WOLFSSL_API 154 int wc_ed25519ph_verify_hash(const byte* sig, word32 sigLen, const byte* hash, 155 word32 hashLen, int* res, ed25519_key* key, 156 const byte* context, byte contextLen); 157 WOLFSSL_API 158 int wc_ed25519ph_verify_msg(const byte* sig, word32 sigLen, const byte* msg, 159 word32 msgLen, int* res, ed25519_key* key, 160 const byte* context, byte contextLen); 161 WOLFSSL_API 162 int wc_ed25519_verify_msg_ex(const byte* sig, word32 sigLen, const byte* msg, 163 word32 msgLen, int* res, ed25519_key* key, 164 byte type, const byte* context, byte contextLen); 165 #ifdef WOLFSSL_ED25519_STREAMING_VERIFY 166 WOLFSSL_API 167 int wc_ed25519_verify_msg_init(const byte* sig, word32 sigLen, ed25519_key* key, 168 byte type, const byte* context, byte contextLen); 169 WOLFSSL_API 170 int wc_ed25519_verify_msg_update(const byte* msgSegment, word32 msgSegmentLen, 171 ed25519_key* key); 172 WOLFSSL_API 173 int wc_ed25519_verify_msg_final(const byte* sig, word32 sigLen, int* res, 174 ed25519_key* key); 175 #endif /* WOLFSSL_ED25519_STREAMING_VERIFY */ 176 #endif /* HAVE_ED25519_VERIFY */ 177 178 WOLFSSL_API 179 int wc_ed25519_init(ed25519_key* key); 180 WOLFSSL_API 181 int wc_ed25519_init_ex(ed25519_key* key, void* heap, int devId); 182 WOLFSSL_API 183 void wc_ed25519_free(ed25519_key* key); 184 #ifndef WC_NO_CONSTRUCTORS 185 WOLFSSL_API 186 ed25519_key* wc_ed25519_new(void* heap, int devId, int *result_code); 187 WOLFSSL_API 188 int wc_ed25519_delete(ed25519_key* key, ed25519_key** key_p); 189 #endif 190 191 #ifdef HAVE_ED25519_KEY_IMPORT 192 WOLFSSL_API 193 int wc_ed25519_import_public(const byte* in, word32 inLen, ed25519_key* key); 194 WOLFSSL_API 195 int wc_ed25519_import_public_ex(const byte* in, word32 inLen, ed25519_key* key, 196 int trusted); 197 WOLFSSL_API 198 int wc_ed25519_import_private_only(const byte* priv, word32 privSz, 199 ed25519_key* key); 200 WOLFSSL_API 201 int wc_ed25519_import_private_key(const byte* priv, word32 privSz, 202 const byte* pub, word32 pubSz, ed25519_key* key); 203 WOLFSSL_API 204 int wc_ed25519_import_private_key_ex(const byte* priv, word32 privSz, 205 const byte* pub, word32 pubSz, ed25519_key* key, int trusted); 206 #endif /* HAVE_ED25519_KEY_IMPORT */ 207 208 #ifdef HAVE_ED25519_KEY_EXPORT 209 WOLFSSL_API 210 int wc_ed25519_export_public(ed25519_key* key, byte* out, word32* outLen); 211 WOLFSSL_API 212 int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen); 213 WOLFSSL_API 214 int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen); 215 WOLFSSL_API 216 int wc_ed25519_export_key(ed25519_key* key, 217 byte* priv, word32 *privSz, 218 byte* pub, word32 *pubSz); 219 #endif /* HAVE_ED25519_KEY_EXPORT */ 220 221 WOLFSSL_API 222 int wc_ed25519_check_key(ed25519_key* key); 223 224 /* size helper */ 225 WOLFSSL_API 226 int wc_ed25519_size(ed25519_key* key); 227 WOLFSSL_API 228 int wc_ed25519_priv_size(ed25519_key* key); 229 WOLFSSL_API 230 int wc_ed25519_pub_size(ed25519_key* key); 231 WOLFSSL_API 232 int wc_ed25519_sig_size(ed25519_key* key); 233 234 #ifdef __cplusplus 235 } /* extern "C" */ 236 #endif 237 238 #endif /* HAVE_ED25519 */ 239 #endif /* WOLF_CRYPT_ED25519_H */