yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* ed25519.h |
| 2 | * |
| 3 | * Copyright (C) 2006-2021 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 | /*! |
| 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/fe_operations.h> |
| 35 | #include <wolfssl/wolfcrypt/ge_operations.h> |
| 36 | #include <wolfssl/wolfcrypt/random.h> |
| 37 | #include <wolfssl/wolfcrypt/sha512.h> |
| 38 | |
| 39 | #ifdef WOLFSSL_ASYNC_CRYPT |
| 40 | #include <wolfssl/wolfcrypt/async.h> |
| 41 | #endif |
| 42 | |
| 43 | #ifdef __cplusplus |
| 44 | extern "C" { |
| 45 | #endif |
| 46 | |
| 47 | |
| 48 | /* info about EdDSA curve specifically ed25519, defined as an elliptic curve |
| 49 | over GF(p) */ |
| 50 | /* |
| 51 | 32, key size |
| 52 | "ED25519", curve name |
| 53 | "2^255-19", prime number |
| 54 | "SHA512", hash function |
| 55 | "-121665/121666", value of d |
| 56 | */ |
| 57 | |
| 58 | #define ED25519_KEY_SIZE 32 /* private key only */ |
| 59 | #define ED25519_SIG_SIZE 64 |
| 60 | |
| 61 | #define ED25519_PUB_KEY_SIZE 32 /* compressed */ |
| 62 | /* both private and public key */ |
| 63 | #define ED25519_PRV_KEY_SIZE (ED25519_PUB_KEY_SIZE+ED25519_KEY_SIZE) |
| 64 | |
| 65 | |
| 66 | enum { |
| 67 | Ed25519 = -1, |
| 68 | Ed25519ctx = 0, |
| 69 | Ed25519ph = 1, |
| 70 | }; |
| 71 | |
| 72 | #ifndef WC_ED25519KEY_TYPE_DEFINED |
| 73 | typedef struct ed25519_key ed25519_key; |
| 74 | #define WC_ED25519KEY_TYPE_DEFINED |
| 75 | #endif |
| 76 | |
| 77 | /* An ED25519 Key */ |
| 78 | struct ed25519_key { |
| 79 | byte p[ED25519_PUB_KEY_SIZE]; /* compressed public key */ |
| 80 | byte k[ED25519_PRV_KEY_SIZE]; /* private key : 32 secret -- 32 public */ |
| 81 | #ifdef FREESCALE_LTC_ECC |
| 82 | /* uncompressed point coordinates */ |
| 83 | byte pointX[ED25519_KEY_SIZE]; /* recovered X coordinate */ |
| 84 | byte pointY[ED25519_KEY_SIZE]; /* Y coordinate is the public key with The most significant bit of the final octet always zero. */ |
| 85 | #endif |
| 86 | word16 pubKeySet:1; |
| 87 | #ifdef WOLFSSL_ASYNC_CRYPT |
| 88 | WC_ASYNC_DEV asyncDev; |
| 89 | #endif |
| 90 | #if defined(WOLF_CRYPTO_CB) |
| 91 | int devId; |
| 92 | #endif |
| 93 | }; |
| 94 | |
| 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 | WOLFSSL_API |
| 102 | int wc_ed25519_sign_msg(const byte* in, word32 inLen, byte* out, |
| 103 | word32 *outLen, ed25519_key* key); |
| 104 | WOLFSSL_API |
| 105 | int wc_ed25519ctx_sign_msg(const byte* in, word32 inLen, byte* out, |
| 106 | word32 *outLen, ed25519_key* key, |
| 107 | const byte* context, byte contextLen); |
| 108 | WOLFSSL_API |
| 109 | int wc_ed25519ph_sign_hash(const byte* hash, word32 hashLen, byte* out, |
| 110 | word32 *outLen, ed25519_key* key, |
| 111 | const byte* context, byte contextLen); |
| 112 | WOLFSSL_API |
| 113 | int wc_ed25519ph_sign_msg(const byte* in, word32 inLen, byte* out, |
| 114 | word32 *outLen, ed25519_key* key, const byte* context, |
| 115 | byte contextLen); |
| 116 | WOLFSSL_API |
| 117 | int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out, |
| 118 | word32 *outLen, ed25519_key* key, byte type, |
| 119 | const byte* context, byte contextLen); |
| 120 | WOLFSSL_API |
| 121 | int wc_ed25519_verify_msg(const byte* sig, word32 sigLen, const byte* msg, |
| 122 | word32 msgLen, int* stat, ed25519_key* key); |
| 123 | WOLFSSL_API |
| 124 | int wc_ed25519ctx_verify_msg(const byte* sig, word32 sigLen, const byte* msg, |
| 125 | word32 msgLen, int* stat, ed25519_key* key, |
| 126 | const byte* context, byte contextLen); |
| 127 | WOLFSSL_API |
| 128 | int wc_ed25519ph_verify_hash(const byte* sig, word32 sigLen, const byte* hash, |
| 129 | word32 hashLen, int* stat, ed25519_key* key, |
| 130 | const byte* context, byte contextLen); |
| 131 | WOLFSSL_API |
| 132 | int wc_ed25519ph_verify_msg(const byte* sig, word32 sigLen, const byte* msg, |
| 133 | word32 msgLen, int* stat, ed25519_key* key, |
| 134 | const byte* context, byte contextLen); |
| 135 | WOLFSSL_API |
| 136 | int wc_ed25519_verify_msg_ex(const byte* sig, word32 sigLen, const byte* msg, |
| 137 | word32 msgLen, int* res, ed25519_key* key, |
| 138 | byte type, const byte* context, byte contextLen); |
| 139 | |
| 140 | WOLFSSL_API |
| 141 | int wc_ed25519_init(ed25519_key* key); |
| 142 | WOLFSSL_API |
| 143 | int wc_ed25519_init_ex(ed25519_key* key, void* heap, int devId); |
| 144 | WOLFSSL_API |
| 145 | void wc_ed25519_free(ed25519_key* key); |
| 146 | WOLFSSL_API |
| 147 | int wc_ed25519_import_public(const byte* in, word32 inLen, ed25519_key* key); |
| 148 | WOLFSSL_API |
| 149 | int wc_ed25519_import_private_only(const byte* priv, word32 privSz, |
| 150 | ed25519_key* key); |
| 151 | WOLFSSL_API |
| 152 | int wc_ed25519_import_private_key(const byte* priv, word32 privSz, |
| 153 | const byte* pub, word32 pubSz, ed25519_key* key); |
| 154 | WOLFSSL_API |
| 155 | int wc_ed25519_export_public(ed25519_key*, byte* out, word32* outLen); |
| 156 | WOLFSSL_API |
| 157 | int wc_ed25519_export_private_only(ed25519_key* key, byte* out, word32* outLen); |
| 158 | WOLFSSL_API |
| 159 | int wc_ed25519_export_private(ed25519_key* key, byte* out, word32* outLen); |
| 160 | WOLFSSL_API |
| 161 | int wc_ed25519_export_key(ed25519_key* key, |
| 162 | byte* priv, word32 *privSz, |
| 163 | byte* pub, word32 *pubSz); |
| 164 | |
| 165 | WOLFSSL_API |
| 166 | int wc_ed25519_check_key(ed25519_key* key); |
| 167 | |
| 168 | /* size helper */ |
| 169 | WOLFSSL_API |
| 170 | int wc_ed25519_size(ed25519_key* key); |
| 171 | WOLFSSL_API |
| 172 | int wc_ed25519_priv_size(ed25519_key* key); |
| 173 | WOLFSSL_API |
| 174 | int wc_ed25519_pub_size(ed25519_key* key); |
| 175 | WOLFSSL_API |
| 176 | int wc_ed25519_sig_size(ed25519_key* key); |
| 177 | |
| 178 | #ifdef __cplusplus |
| 179 | } /* extern "C" */ |
| 180 | #endif |
| 181 | |
| 182 | #endif /* HAVE_ED25519 */ |
| 183 | #endif /* WOLF_CRYPT_ED25519_H */ |