[352] | 1 | /* ecc.h
|
---|
| 2 | *
|
---|
| 3 | * Copyright (C) 2006-2017 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 | #ifndef WOLF_CRYPT_ECC_H
|
---|
| 24 | #define WOLF_CRYPT_ECC_H
|
---|
| 25 |
|
---|
| 26 | #include <wolfssl/wolfcrypt/types.h>
|
---|
| 27 |
|
---|
| 28 | #ifdef HAVE_ECC
|
---|
| 29 |
|
---|
| 30 | #include <wolfssl/wolfcrypt/integer.h>
|
---|
| 31 | #include <wolfssl/wolfcrypt/random.h>
|
---|
| 32 |
|
---|
| 33 | #ifdef HAVE_X963_KDF
|
---|
| 34 | #include <wolfssl/wolfcrypt/hash.h>
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
| 37 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
| 38 | #include <wolfssl/wolfcrypt/async.h>
|
---|
| 39 | #ifdef WOLFSSL_CERT_GEN
|
---|
| 40 | #include <wolfssl/wolfcrypt/asn.h>
|
---|
| 41 | #endif
|
---|
| 42 | #endif
|
---|
| 43 |
|
---|
| 44 | #ifdef WOLFSSL_ATECC508A
|
---|
| 45 | #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
|
---|
| 46 | #endif /* WOLFSSL_ATECC508A */
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | #ifdef __cplusplus
|
---|
| 50 | extern "C" {
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
| 53 |
|
---|
| 54 | /* Enable curve B parameter if needed */
|
---|
| 55 | #if defined(HAVE_COMP_KEY) || defined(ECC_CACHE_CURVE)
|
---|
| 56 | #ifndef USE_ECC_B_PARAM /* Allow someone to force enable */
|
---|
| 57 | #define USE_ECC_B_PARAM
|
---|
| 58 | #endif
|
---|
| 59 | #endif
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | /* Use this as the key->idx if a custom ecc_set is used for key->dp */
|
---|
| 63 | #define ECC_CUSTOM_IDX (-1)
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 | /* Determine max ECC bits based on enabled curves */
|
---|
| 67 | #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
|
---|
| 68 | #define MAX_ECC_BITS 521
|
---|
| 69 | #elif defined(HAVE_ECC512)
|
---|
| 70 | #define MAX_ECC_BITS 512
|
---|
| 71 | #elif defined(HAVE_ECC384)
|
---|
| 72 | #define MAX_ECC_BITS 384
|
---|
| 73 | #elif defined(HAVE_ECC320)
|
---|
| 74 | #define MAX_ECC_BITS 320
|
---|
| 75 | #elif defined(HAVE_ECC239)
|
---|
| 76 | #define MAX_ECC_BITS 239
|
---|
| 77 | #elif defined(HAVE_ECC224)
|
---|
| 78 | #define MAX_ECC_BITS 224
|
---|
| 79 | #elif !defined(NO_ECC256)
|
---|
| 80 | #define MAX_ECC_BITS 256
|
---|
| 81 | #elif defined(HAVE_ECC192)
|
---|
| 82 | #define MAX_ECC_BITS 192
|
---|
| 83 | #elif defined(HAVE_ECC160)
|
---|
| 84 | #define MAX_ECC_BITS 160
|
---|
| 85 | #elif defined(HAVE_ECC128)
|
---|
| 86 | #define MAX_ECC_BITS 128
|
---|
| 87 | #elif defined(HAVE_ECC112)
|
---|
| 88 | #define MAX_ECC_BITS 112
|
---|
| 89 | #endif
|
---|
| 90 |
|
---|
| 91 | /* calculate max ECC bytes */
|
---|
| 92 | #if ((MAX_ECC_BITS * 2) % 8) == 0
|
---|
| 93 | #define MAX_ECC_BYTES (MAX_ECC_BITS / 8)
|
---|
| 94 | #else
|
---|
| 95 | /* add byte if not aligned */
|
---|
| 96 | #define MAX_ECC_BYTES ((MAX_ECC_BITS / 8) + 1)
|
---|
| 97 | #endif
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 | enum {
|
---|
| 101 | ECC_PUBLICKEY = 1,
|
---|
| 102 | ECC_PRIVATEKEY = 2,
|
---|
| 103 | ECC_PRIVATEKEY_ONLY = 3,
|
---|
| 104 | ECC_MAXNAME = 16, /* MAX CURVE NAME LENGTH */
|
---|
| 105 | SIG_HEADER_SZ = 6, /* ECC signature header size */
|
---|
| 106 | ECC_BUFSIZE = 256, /* for exported keys temp buffer */
|
---|
| 107 | ECC_MINSIZE = 20, /* MIN Private Key size */
|
---|
| 108 | ECC_MAXSIZE = 66, /* MAX Private Key size */
|
---|
| 109 | ECC_MAXSIZE_GEN = 74, /* MAX Buffer size required when generating ECC keys*/
|
---|
| 110 | ECC_MAX_PAD_SZ = 4, /* ECC maximum padding size */
|
---|
| 111 | ECC_MAX_OID_LEN = 16,
|
---|
| 112 | ECC_MAX_SIG_SIZE= ((MAX_ECC_BYTES * 2) + ECC_MAX_PAD_SZ + SIG_HEADER_SZ)
|
---|
| 113 | };
|
---|
| 114 |
|
---|
| 115 | /* Curve Types */
|
---|
| 116 | typedef enum ecc_curve_id {
|
---|
| 117 | ECC_CURVE_INVALID = -1,
|
---|
| 118 | ECC_CURVE_DEF = 0, /* NIST or SECP */
|
---|
| 119 |
|
---|
| 120 | /* NIST Prime Curves */
|
---|
| 121 | ECC_SECP192R1,
|
---|
| 122 | ECC_PRIME192V2,
|
---|
| 123 | ECC_PRIME192V3,
|
---|
| 124 | ECC_PRIME239V1,
|
---|
| 125 | ECC_PRIME239V2,
|
---|
| 126 | ECC_PRIME239V3,
|
---|
| 127 | ECC_SECP256R1,
|
---|
| 128 |
|
---|
| 129 | /* SECP Curves */
|
---|
| 130 | ECC_SECP112R1,
|
---|
| 131 | ECC_SECP112R2,
|
---|
| 132 | ECC_SECP128R1,
|
---|
| 133 | ECC_SECP128R2,
|
---|
| 134 | ECC_SECP160R1,
|
---|
| 135 | ECC_SECP160R2,
|
---|
| 136 | ECC_SECP224R1,
|
---|
| 137 | ECC_SECP384R1,
|
---|
| 138 | ECC_SECP521R1,
|
---|
| 139 |
|
---|
| 140 | /* Koblitz */
|
---|
| 141 | ECC_SECP160K1,
|
---|
| 142 | ECC_SECP192K1,
|
---|
| 143 | ECC_SECP224K1,
|
---|
| 144 | ECC_SECP256K1,
|
---|
| 145 |
|
---|
| 146 | /* Brainpool Curves */
|
---|
| 147 | ECC_BRAINPOOLP160R1,
|
---|
| 148 | ECC_BRAINPOOLP192R1,
|
---|
| 149 | ECC_BRAINPOOLP224R1,
|
---|
| 150 | ECC_BRAINPOOLP256R1,
|
---|
| 151 | ECC_BRAINPOOLP320R1,
|
---|
| 152 | ECC_BRAINPOOLP384R1,
|
---|
| 153 | ECC_BRAINPOOLP512R1,
|
---|
| 154 |
|
---|
| 155 | /* Twisted Edwards Curves */
|
---|
| 156 | #ifdef HAVE_CURVE25519
|
---|
| 157 | ECC_X25519,
|
---|
| 158 | #endif
|
---|
| 159 | #ifdef HAVE_X448
|
---|
| 160 | ECC_X448,
|
---|
| 161 | #endif
|
---|
| 162 |
|
---|
| 163 | #ifdef WOLFSSL_CUSTOM_CURVES
|
---|
| 164 | ECC_CURVE_CUSTOM,
|
---|
| 165 | #endif
|
---|
| 166 | } ecc_curve_id;
|
---|
| 167 |
|
---|
| 168 | #ifdef HAVE_OID_ENCODING
|
---|
| 169 | typedef word16 ecc_oid_t;
|
---|
| 170 | #else
|
---|
| 171 | typedef byte ecc_oid_t;
|
---|
| 172 | /* OID encoded with ASN scheme:
|
---|
| 173 | first element = (oid[0] * 40) + oid[1]
|
---|
| 174 | if any element > 127 then MSB 0x80 indicates additional byte */
|
---|
| 175 | #endif
|
---|
| 176 |
|
---|
| 177 | /* ECC set type defined a GF(p) curve */
|
---|
| 178 | typedef struct ecc_set_type {
|
---|
| 179 | int size; /* The size of the curve in octets */
|
---|
| 180 | int id; /* id of this curve */
|
---|
| 181 | const char* name; /* name of this curve */
|
---|
| 182 | const char* prime; /* prime that defines the field, curve is in (hex) */
|
---|
| 183 | const char* Af; /* fields A param (hex) */
|
---|
| 184 | const char* Bf; /* fields B param (hex) */
|
---|
| 185 | const char* order; /* order of the curve (hex) */
|
---|
| 186 | const char* Gx; /* x coordinate of the base point on curve (hex) */
|
---|
| 187 | const char* Gy; /* y coordinate of the base point on curve (hex) */
|
---|
| 188 | const ecc_oid_t* oid;
|
---|
| 189 | word32 oidSz;
|
---|
| 190 | word32 oidSum; /* sum of encoded OID bytes */
|
---|
| 191 | int cofactor;
|
---|
| 192 | } ecc_set_type;
|
---|
| 193 |
|
---|
| 194 |
|
---|
| 195 | #ifdef ALT_ECC_SIZE
|
---|
| 196 |
|
---|
| 197 | /* Note on ALT_ECC_SIZE:
|
---|
| 198 | * The fast math code uses an array of a fixed size to store the big integers.
|
---|
| 199 | * By default, the array is big enough for RSA keys. There is a size,
|
---|
| 200 | * FP_MAX_BITS which can be used to make the array smaller when one wants ECC
|
---|
| 201 | * but not RSA. Some people want fast math sized for both RSA and ECC, where
|
---|
| 202 | * ECC won't use as much as RSA. The flag ALT_ECC_SIZE switches in an alternate
|
---|
| 203 | * ecc_point structure that uses an alternate fp_int that has a shorter array
|
---|
| 204 | * of fp_digits.
|
---|
| 205 | *
|
---|
| 206 | * Now, without ALT_ECC_SIZE, the ecc_point has three single item arrays of
|
---|
| 207 | * mp_ints for the components of the point. With ALT_ECC_SIZE, the components
|
---|
| 208 | * of the point are pointers that are set to each of a three item array of
|
---|
| 209 | * alt_fp_ints. While an mp_int will have 4096 bits of digit inside the
|
---|
| 210 | * structure, the alt_fp_int will only have 528 bits. A size value was added
|
---|
| 211 | * in the ALT case, as well, and is set by mp_init() and alt_fp_init(). The
|
---|
| 212 | * functions fp_zero() and fp_copy() use the size parameter. An int needs to
|
---|
| 213 | * be initialized before using it instead of just fp_zeroing it, the init will
|
---|
| 214 | * call zero. FP_MAX_BITS_ECC defaults to 528, but can be set to change the
|
---|
| 215 | * number of bits used in the alternate FP_INT.
|
---|
| 216 | *
|
---|
| 217 | * Do not enable ALT_ECC_SIZE and disable fast math in the configuration.
|
---|
| 218 | */
|
---|
| 219 |
|
---|
| 220 | #ifndef USE_FAST_MATH
|
---|
| 221 | #error USE_FAST_MATH must be defined to use ALT_ECC_SIZE
|
---|
| 222 | #endif
|
---|
| 223 |
|
---|
| 224 | /* determine max bits required for ECC math */
|
---|
| 225 | #ifndef FP_MAX_BITS_ECC
|
---|
| 226 | /* check alignment */
|
---|
| 227 | #if ((MAX_ECC_BITS * 2) % DIGIT_BIT) == 0
|
---|
| 228 | /* max bits is double */
|
---|
| 229 | #define FP_MAX_BITS_ECC (MAX_ECC_BITS * 2)
|
---|
| 230 | #else
|
---|
| 231 | /* max bits is doubled, plus one digit of fudge */
|
---|
| 232 | #define FP_MAX_BITS_ECC ((MAX_ECC_BITS * 2) + DIGIT_BIT)
|
---|
| 233 | #endif
|
---|
| 234 | #else
|
---|
| 235 | /* verify alignment */
|
---|
| 236 | #if FP_MAX_BITS_ECC % CHAR_BIT
|
---|
| 237 | #error FP_MAX_BITS_ECC must be a multiple of CHAR_BIT
|
---|
| 238 | #endif
|
---|
| 239 | #endif
|
---|
| 240 |
|
---|
| 241 | /* determine buffer size */
|
---|
| 242 | #define FP_SIZE_ECC (FP_MAX_BITS_ECC/DIGIT_BIT)
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 | /* This needs to match the size of the fp_int struct, except the
|
---|
| 246 | * fp_digit array will be shorter. */
|
---|
| 247 | typedef struct alt_fp_int {
|
---|
| 248 | int used, sign, size;
|
---|
| 249 | fp_digit dp[FP_SIZE_ECC];
|
---|
| 250 | } alt_fp_int;
|
---|
| 251 | #endif /* ALT_ECC_SIZE */
|
---|
| 252 |
|
---|
| 253 |
|
---|
| 254 | /* A point on an ECC curve, stored in Jacbobian format such that (x,y,z) =>
|
---|
| 255 | (x/z^2, y/z^3, 1) when interpreted as affine */
|
---|
| 256 | typedef struct {
|
---|
| 257 | #ifndef ALT_ECC_SIZE
|
---|
| 258 | mp_int x[1]; /* The x coordinate */
|
---|
| 259 | mp_int y[1]; /* The y coordinate */
|
---|
| 260 | mp_int z[1]; /* The z coordinate */
|
---|
| 261 | #else
|
---|
| 262 | mp_int* x; /* The x coordinate */
|
---|
| 263 | mp_int* y; /* The y coordinate */
|
---|
| 264 | mp_int* z; /* The z coordinate */
|
---|
| 265 | alt_fp_int xyz[3];
|
---|
| 266 | #endif
|
---|
| 267 | } ecc_point;
|
---|
| 268 |
|
---|
| 269 | /* ECC Flags */
|
---|
| 270 | enum {
|
---|
| 271 | WC_ECC_FLAG_NONE = 0x00,
|
---|
| 272 | #ifdef HAVE_ECC_CDH
|
---|
| 273 | WC_ECC_FLAG_COFACTOR = 0x01,
|
---|
| 274 | #endif
|
---|
| 275 | };
|
---|
| 276 |
|
---|
| 277 | /* An ECC Key */
|
---|
| 278 | struct ecc_key {
|
---|
| 279 | int type; /* Public or Private */
|
---|
| 280 | int idx; /* Index into the ecc_sets[] for the parameters of
|
---|
| 281 | this curve if -1, this key is using user supplied
|
---|
| 282 | curve in dp */
|
---|
| 283 | int state;
|
---|
| 284 | word32 flags;
|
---|
| 285 | const ecc_set_type* dp; /* domain parameters, either points to NIST
|
---|
| 286 | curves (idx >= 0) or user supplied */
|
---|
| 287 | void* heap; /* heap hint */
|
---|
| 288 | #ifdef WOLFSSL_ATECC508A
|
---|
| 289 | int slot; /* Key Slot Number (-1 unknown) */
|
---|
| 290 | byte pubkey[PUB_KEY_SIZE];
|
---|
| 291 | #else
|
---|
| 292 | ecc_point pubkey; /* public key */
|
---|
| 293 | mp_int k; /* private key */
|
---|
| 294 | #endif
|
---|
| 295 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
| 296 | mp_int* r; /* sign/verify temps */
|
---|
| 297 | mp_int* s;
|
---|
| 298 | WC_ASYNC_DEV asyncDev;
|
---|
| 299 | #ifdef WOLFSSL_CERT_GEN
|
---|
| 300 | CertSignCtx certSignCtx; /* context info for cert sign (MakeSignature) */
|
---|
| 301 | #endif
|
---|
| 302 | #endif /* WOLFSSL_ASYNC_CRYPT */
|
---|
| 303 | };
|
---|
| 304 |
|
---|
| 305 | #ifndef WC_ECCKEY_TYPE_DEFINED
|
---|
| 306 | typedef struct ecc_key ecc_key;
|
---|
| 307 | #define WC_ECCKEY_TYPE_DEFINED
|
---|
| 308 | #endif
|
---|
| 309 |
|
---|
| 310 |
|
---|
| 311 | /* ECC predefined curve sets */
|
---|
| 312 | extern const ecc_set_type ecc_sets[];
|
---|
| 313 |
|
---|
| 314 | WOLFSSL_API
|
---|
| 315 | const char* wc_ecc_get_name(int curve_id);
|
---|
| 316 |
|
---|
| 317 | #ifndef WOLFSSL_ATECC508A
|
---|
| 318 |
|
---|
| 319 | #ifdef WOLFSSL_PUBLIC_ECC_ADD_DBL
|
---|
| 320 | #define ECC_API WOLFSSL_API
|
---|
| 321 | #else
|
---|
| 322 | #define ECC_API WOLFSSL_LOCAL
|
---|
| 323 | #endif
|
---|
| 324 |
|
---|
| 325 | ECC_API int ecc_map(ecc_point*, mp_int*, mp_digit);
|
---|
| 326 | ECC_API int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R,
|
---|
| 327 | mp_int* a, mp_int* modulus, mp_digit mp);
|
---|
| 328 | ECC_API int ecc_projective_dbl_point(ecc_point* P, ecc_point* R, mp_int* a,
|
---|
| 329 | mp_int* modulus, mp_digit mp);
|
---|
| 330 |
|
---|
| 331 | #endif
|
---|
| 332 |
|
---|
| 333 | WOLFSSL_API
|
---|
| 334 | int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key);
|
---|
| 335 | WOLFSSL_API
|
---|
| 336 | int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key,
|
---|
| 337 | int curve_id);
|
---|
| 338 | WOLFSSL_API
|
---|
| 339 | int wc_ecc_make_pub(ecc_key* key, ecc_point* pubOut);
|
---|
| 340 | WOLFSSL_API
|
---|
| 341 | int wc_ecc_check_key(ecc_key* key);
|
---|
| 342 | WOLFSSL_API
|
---|
| 343 | int wc_ecc_is_point(ecc_point* ecp, mp_int* a, mp_int* b, mp_int* prime);
|
---|
| 344 |
|
---|
| 345 | #ifdef HAVE_ECC_DHE
|
---|
| 346 | WOLFSSL_API
|
---|
| 347 | int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
|
---|
| 348 | word32* outlen);
|
---|
| 349 | WOLFSSL_LOCAL
|
---|
| 350 | int wc_ecc_shared_secret_gen(ecc_key* private_key, ecc_point* point,
|
---|
| 351 | byte* out, word32 *outlen);
|
---|
| 352 | WOLFSSL_API
|
---|
| 353 | int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
|
---|
| 354 | byte* out, word32 *outlen);
|
---|
| 355 | #define wc_ecc_shared_secret_ssh wc_ecc_shared_secret_ex /* For backwards compat */
|
---|
| 356 | #endif /* HAVE_ECC_DHE */
|
---|
| 357 |
|
---|
| 358 | #ifdef HAVE_ECC_SIGN
|
---|
| 359 | WOLFSSL_API
|
---|
| 360 | int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
|
---|
| 361 | WC_RNG* rng, ecc_key* key);
|
---|
| 362 | WOLFSSL_API
|
---|
| 363 | int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
|
---|
| 364 | ecc_key* key, mp_int *r, mp_int *s);
|
---|
| 365 | #endif /* HAVE_ECC_SIGN */
|
---|
| 366 |
|
---|
| 367 | #ifdef HAVE_ECC_VERIFY
|
---|
| 368 | WOLFSSL_API
|
---|
| 369 | int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
|
---|
| 370 | word32 hashlen, int* stat, ecc_key* key);
|
---|
| 371 | WOLFSSL_API
|
---|
| 372 | int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
|
---|
| 373 | word32 hashlen, int* stat, ecc_key* key);
|
---|
| 374 | #endif /* HAVE_ECC_VERIFY */
|
---|
| 375 |
|
---|
| 376 | WOLFSSL_API
|
---|
| 377 | int wc_ecc_init(ecc_key* key);
|
---|
| 378 | WOLFSSL_API
|
---|
| 379 | int wc_ecc_init_ex(ecc_key* key, void* heap, int devId);
|
---|
| 380 | WOLFSSL_API
|
---|
| 381 | void wc_ecc_free(ecc_key* key);
|
---|
| 382 | WOLFSSL_API
|
---|
| 383 | int wc_ecc_set_flags(ecc_key* key, word32 flags);
|
---|
| 384 | WOLFSSL_API
|
---|
| 385 | void wc_ecc_fp_free(void);
|
---|
| 386 |
|
---|
| 387 | WOLFSSL_API
|
---|
| 388 | int wc_ecc_set_curve(ecc_key* key, int keysize, int curve_id);
|
---|
| 389 |
|
---|
| 390 | WOLFSSL_API
|
---|
| 391 | int wc_ecc_is_valid_idx(int n);
|
---|
| 392 | WOLFSSL_API
|
---|
| 393 | int wc_ecc_get_curve_idx(int curve_id);
|
---|
| 394 | WOLFSSL_API
|
---|
| 395 | int wc_ecc_get_curve_id(int curve_idx);
|
---|
| 396 | #define wc_ecc_get_curve_name_from_id wc_ecc_get_name
|
---|
| 397 | WOLFSSL_API
|
---|
| 398 | int wc_ecc_get_curve_size_from_id(int curve_id);
|
---|
| 399 |
|
---|
| 400 | WOLFSSL_API
|
---|
| 401 | int wc_ecc_get_curve_idx_from_name(const char* curveName);
|
---|
| 402 | WOLFSSL_API
|
---|
| 403 | int wc_ecc_get_curve_size_from_name(const char* curveName);
|
---|
| 404 | WOLFSSL_API
|
---|
| 405 | int wc_ecc_get_curve_id_from_name(const char* curveName);
|
---|
| 406 | WOLFSSL_API
|
---|
| 407 | int wc_ecc_get_curve_id_from_params(int fieldSize,
|
---|
| 408 | const byte* prime, word32 primeSz, const byte* Af, word32 AfSz,
|
---|
| 409 | const byte* Bf, word32 BfSz, const byte* order, word32 orderSz,
|
---|
| 410 | const byte* Gx, word32 GxSz, const byte* Gy, word32 GySz, int cofactor);
|
---|
| 411 |
|
---|
| 412 | #ifndef WOLFSSL_ATECC508A
|
---|
| 413 |
|
---|
| 414 | WOLFSSL_API
|
---|
| 415 | ecc_point* wc_ecc_new_point(void);
|
---|
| 416 | WOLFSSL_API
|
---|
| 417 | ecc_point* wc_ecc_new_point_h(void* h);
|
---|
| 418 | WOLFSSL_API
|
---|
| 419 | void wc_ecc_del_point(ecc_point* p);
|
---|
| 420 | WOLFSSL_API
|
---|
| 421 | void wc_ecc_del_point_h(ecc_point* p, void* h);
|
---|
| 422 | WOLFSSL_API
|
---|
| 423 | int wc_ecc_copy_point(ecc_point* p, ecc_point *r);
|
---|
| 424 | WOLFSSL_API
|
---|
| 425 | int wc_ecc_cmp_point(ecc_point* a, ecc_point *b);
|
---|
| 426 | WOLFSSL_API
|
---|
| 427 | int wc_ecc_point_is_at_infinity(ecc_point *p);
|
---|
| 428 | WOLFSSL_API
|
---|
| 429 | int wc_ecc_mulmod(mp_int* k, ecc_point *G, ecc_point *R,
|
---|
| 430 | mp_int* a, mp_int* modulus, int map);
|
---|
| 431 | WOLFSSL_LOCAL
|
---|
| 432 | int wc_ecc_mulmod_ex(mp_int* k, ecc_point *G, ecc_point *R,
|
---|
| 433 | mp_int* a, mp_int* modulus, int map, void* heap);
|
---|
| 434 | #endif /* !WOLFSSL_ATECC508A */
|
---|
| 435 |
|
---|
| 436 |
|
---|
| 437 | #ifdef HAVE_ECC_KEY_EXPORT
|
---|
| 438 | /* ASN key helpers */
|
---|
| 439 | WOLFSSL_API
|
---|
| 440 | int wc_ecc_export_x963(ecc_key*, byte* out, word32* outLen);
|
---|
| 441 | WOLFSSL_API
|
---|
| 442 | int wc_ecc_export_x963_ex(ecc_key*, byte* out, word32* outLen, int compressed);
|
---|
| 443 | /* extended functionality with compressed option */
|
---|
| 444 | #endif /* HAVE_ECC_KEY_EXPORT */
|
---|
| 445 |
|
---|
| 446 | #ifdef HAVE_ECC_KEY_IMPORT
|
---|
| 447 | WOLFSSL_API
|
---|
| 448 | int wc_ecc_import_x963(const byte* in, word32 inLen, ecc_key* key);
|
---|
| 449 | WOLFSSL_API
|
---|
| 450 | int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
|
---|
| 451 | int curve_id);
|
---|
| 452 | WOLFSSL_API
|
---|
| 453 | int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
|
---|
| 454 | word32 pubSz, ecc_key* key);
|
---|
| 455 | WOLFSSL_API
|
---|
| 456 | int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz,
|
---|
| 457 | const byte* pub, word32 pubSz, ecc_key* key, int curve_id);
|
---|
| 458 | WOLFSSL_API
|
---|
| 459 | int wc_ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen);
|
---|
| 460 | WOLFSSL_API
|
---|
| 461 | int wc_ecc_sig_to_rs(const byte* sig, word32 sigLen, byte* r, word32* rLen,
|
---|
| 462 | byte* s, word32* sLen);
|
---|
| 463 | WOLFSSL_API
|
---|
| 464 | int wc_ecc_import_raw(ecc_key* key, const char* qx, const char* qy,
|
---|
| 465 | const char* d, const char* curveName);
|
---|
| 466 | WOLFSSL_API
|
---|
| 467 | int wc_ecc_import_raw_ex(ecc_key* key, const char* qx, const char* qy,
|
---|
| 468 | const char* d, int curve_id);
|
---|
| 469 | #endif /* HAVE_ECC_KEY_IMPORT */
|
---|
| 470 |
|
---|
| 471 | #ifdef HAVE_ECC_KEY_EXPORT
|
---|
| 472 | WOLFSSL_API
|
---|
| 473 | int wc_ecc_export_private_only(ecc_key* key, byte* out, word32* outLen);
|
---|
| 474 | WOLFSSL_API
|
---|
| 475 | int wc_ecc_export_public_raw(ecc_key* key, byte* qx, word32* qxLen,
|
---|
| 476 | byte* qy, word32* qyLen);
|
---|
| 477 | WOLFSSL_API
|
---|
| 478 | int wc_ecc_export_private_raw(ecc_key* key, byte* qx, word32* qxLen,
|
---|
| 479 | byte* qy, word32* qyLen, byte* d, word32* dLen);
|
---|
| 480 | #endif /* HAVE_ECC_KEY_EXPORT */
|
---|
| 481 |
|
---|
| 482 | #ifdef HAVE_ECC_KEY_EXPORT
|
---|
| 483 |
|
---|
| 484 | WOLFSSL_API
|
---|
| 485 | int wc_ecc_export_point_der(const int curve_idx, ecc_point* point,
|
---|
| 486 | byte* out, word32* outLen);
|
---|
| 487 | #endif /* HAVE_ECC_KEY_EXPORT */
|
---|
| 488 |
|
---|
| 489 |
|
---|
| 490 | #ifdef HAVE_ECC_KEY_IMPORT
|
---|
| 491 | WOLFSSL_API
|
---|
| 492 | int wc_ecc_import_point_der(byte* in, word32 inLen, const int curve_idx,
|
---|
| 493 | ecc_point* point);
|
---|
| 494 | #endif /* HAVE_ECC_KEY_IMPORT */
|
---|
| 495 |
|
---|
| 496 | /* size helper */
|
---|
| 497 | WOLFSSL_API
|
---|
| 498 | int wc_ecc_size(ecc_key* key);
|
---|
| 499 | WOLFSSL_API
|
---|
| 500 | int wc_ecc_sig_size(ecc_key* key);
|
---|
| 501 |
|
---|
| 502 | WOLFSSL_API
|
---|
| 503 | int wc_ecc_get_oid(word32 oidSum, const byte** oid, word32* oidSz);
|
---|
| 504 |
|
---|
| 505 | #ifdef WOLFSSL_CUSTOM_CURVES
|
---|
| 506 | WOLFSSL_API
|
---|
| 507 | int wc_ecc_set_custom_curve(ecc_key* key, const ecc_set_type* dp);
|
---|
| 508 | #endif
|
---|
| 509 |
|
---|
| 510 | #ifdef HAVE_ECC_ENCRYPT
|
---|
| 511 | /* ecc encrypt */
|
---|
| 512 |
|
---|
| 513 | enum ecEncAlgo {
|
---|
| 514 | ecAES_128_CBC = 1, /* default */
|
---|
| 515 | ecAES_256_CBC = 2
|
---|
| 516 | };
|
---|
| 517 |
|
---|
| 518 | enum ecKdfAlgo {
|
---|
| 519 | ecHKDF_SHA256 = 1, /* default */
|
---|
| 520 | ecHKDF_SHA1 = 2
|
---|
| 521 | };
|
---|
| 522 |
|
---|
| 523 | enum ecMacAlgo {
|
---|
| 524 | ecHMAC_SHA256 = 1, /* default */
|
---|
| 525 | ecHMAC_SHA1 = 2
|
---|
| 526 | };
|
---|
| 527 |
|
---|
| 528 | enum {
|
---|
| 529 | KEY_SIZE_128 = 16,
|
---|
| 530 | KEY_SIZE_256 = 32,
|
---|
| 531 | IV_SIZE_64 = 8,
|
---|
| 532 | IV_SIZE_128 = 16,
|
---|
| 533 | EXCHANGE_SALT_SZ = 16,
|
---|
| 534 | EXCHANGE_INFO_SZ = 23
|
---|
| 535 | };
|
---|
| 536 |
|
---|
| 537 | enum ecFlags {
|
---|
| 538 | REQ_RESP_CLIENT = 1,
|
---|
| 539 | REQ_RESP_SERVER = 2
|
---|
| 540 | };
|
---|
| 541 |
|
---|
| 542 |
|
---|
| 543 | typedef struct ecEncCtx ecEncCtx;
|
---|
| 544 |
|
---|
| 545 | WOLFSSL_API
|
---|
| 546 | ecEncCtx* wc_ecc_ctx_new(int flags, WC_RNG* rng);
|
---|
| 547 | WOLFSSL_API
|
---|
| 548 | ecEncCtx* wc_ecc_ctx_new_ex(int flags, WC_RNG* rng, void* heap);
|
---|
| 549 | WOLFSSL_API
|
---|
| 550 | void wc_ecc_ctx_free(ecEncCtx*);
|
---|
| 551 | WOLFSSL_API
|
---|
| 552 | int wc_ecc_ctx_reset(ecEncCtx*, WC_RNG*); /* reset for use again w/o alloc/free */
|
---|
| 553 |
|
---|
| 554 | WOLFSSL_API
|
---|
| 555 | const byte* wc_ecc_ctx_get_own_salt(ecEncCtx*);
|
---|
| 556 | WOLFSSL_API
|
---|
| 557 | int wc_ecc_ctx_set_peer_salt(ecEncCtx*, const byte* salt);
|
---|
| 558 | WOLFSSL_API
|
---|
| 559 | int wc_ecc_ctx_set_info(ecEncCtx*, const byte* info, int sz);
|
---|
| 560 |
|
---|
| 561 | WOLFSSL_API
|
---|
| 562 | int wc_ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
|
---|
| 563 | word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
|
---|
| 564 | WOLFSSL_API
|
---|
| 565 | int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
|
---|
| 566 | word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
|
---|
| 567 |
|
---|
| 568 | #endif /* HAVE_ECC_ENCRYPT */
|
---|
| 569 |
|
---|
| 570 | #ifdef HAVE_X963_KDF
|
---|
| 571 | WOLFSSL_API int wc_X963_KDF(enum wc_HashType type, const byte* secret,
|
---|
| 572 | word32 secretSz, const byte* sinfo, word32 sinfoSz,
|
---|
| 573 | byte* out, word32 outSz);
|
---|
| 574 | #endif
|
---|
| 575 |
|
---|
| 576 | #ifdef ECC_CACHE_CURVE
|
---|
| 577 | WOLFSSL_API int wc_ecc_curve_cache_init(void);
|
---|
| 578 | WOLFSSL_API void wc_ecc_curve_cache_free(void);
|
---|
| 579 | #endif
|
---|
| 580 |
|
---|
| 581 |
|
---|
| 582 | #ifdef __cplusplus
|
---|
| 583 | } /* extern "C" */
|
---|
| 584 | #endif
|
---|
| 585 |
|
---|
| 586 | #endif /* HAVE_ECC */
|
---|
| 587 | #endif /* WOLF_CRYPT_ECC_H */
|
---|