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