Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL
ecc.h
00001 /* ecc.h 00002 * 00003 * Copyright (C) 2006-2014 wolfSSL Inc. 00004 * 00005 * This file is part of CyaSSL. 00006 * 00007 * CyaSSL is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * CyaSSL is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 00020 */ 00021 00022 #ifdef HAVE_ECC 00023 00024 #ifndef CTAO_CRYPT_ECC_H 00025 #define CTAO_CRYPT_ECC_H 00026 00027 #include <cyassl/ctaocrypt/types.h> 00028 #include <cyassl/ctaocrypt/integer.h> 00029 #include <cyassl/ctaocrypt/random.h> 00030 00031 #ifdef __cplusplus 00032 extern "C" { 00033 #endif 00034 00035 00036 enum { 00037 ECC_PUBLICKEY = 1, 00038 ECC_PRIVATEKEY = 2, 00039 ECC_MAXNAME = 16, /* MAX CURVE NAME LENGTH */ 00040 SIG_HEADER_SZ = 6, /* ECC signature header size */ 00041 ECC_BUFSIZE = 256, /* for exported keys temp buffer */ 00042 ECC_MINSIZE = 20, /* MIN Private Key size */ 00043 ECC_MAXSIZE = 66 /* MAX Private Key size */ 00044 }; 00045 00046 00047 /* ECC set type defined a NIST GF(p) curve */ 00048 typedef struct { 00049 int size; /* The size of the curve in octets */ 00050 const char* name; /* name of this curve */ 00051 const char* prime; /* prime that defines the field, curve is in (hex) */ 00052 const char* Af; /* fields A param (hex) */ 00053 const char* Bf; /* fields B param (hex) */ 00054 const char* order; /* order of the curve (hex) */ 00055 const char* Gx; /* x coordinate of the base point on curve (hex) */ 00056 const char* Gy; /* y coordinate of the base point on curve (hex) */ 00057 } ecc_set_type; 00058 00059 00060 /* A point on an ECC curve, stored in Jacbobian format such that (x,y,z) => 00061 (x/z^2, y/z^3, 1) when interpreted as affine */ 00062 typedef struct { 00063 mp_int x; /* The x coordinate */ 00064 mp_int y; /* The y coordinate */ 00065 mp_int z; /* The z coordinate */ 00066 } ecc_point; 00067 00068 00069 /* An ECC Key */ 00070 typedef struct { 00071 int type; /* Public or Private */ 00072 int idx; /* Index into the ecc_sets[] for the parameters of 00073 this curve if -1, this key is using user supplied 00074 curve in dp */ 00075 const ecc_set_type* dp; /* domain parameters, either points to NIST 00076 curves (idx >= 0) or user supplied */ 00077 ecc_point pubkey; /* public key */ 00078 mp_int k; /* private key */ 00079 } ecc_key; 00080 00081 00082 /* ECC predefined curve sets */ 00083 extern const ecc_set_type ecc_sets[]; 00084 00085 00086 CYASSL_API 00087 int ecc_make_key(RNG* rng, int keysize, ecc_key* key); 00088 CYASSL_API 00089 int ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out, 00090 word32* outlen); 00091 CYASSL_API 00092 int ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen, 00093 RNG* rng, ecc_key* key); 00094 CYASSL_API 00095 int ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash, 00096 word32 hashlen, int* stat, ecc_key* key); 00097 CYASSL_API 00098 void ecc_init(ecc_key* key); 00099 CYASSL_API 00100 void ecc_free(ecc_key* key); 00101 CYASSL_API 00102 void ecc_fp_free(void); 00103 00104 00105 /* ASN key helpers */ 00106 CYASSL_API 00107 int ecc_export_x963(ecc_key*, byte* out, word32* outLen); 00108 CYASSL_API 00109 int ecc_export_x963_ex(ecc_key*, byte* out, word32* outLen, int compressed); 00110 /* extended functionality with compressed option */ 00111 CYASSL_API 00112 int ecc_import_x963(const byte* in, word32 inLen, ecc_key* key); 00113 CYASSL_API 00114 int ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub, 00115 word32 pubSz, ecc_key* key); 00116 CYASSL_API 00117 int ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen); 00118 CYASSL_API 00119 int ecc_import_raw(ecc_key* key, const char* qx, const char* qy, 00120 const char* d, const char* curveName); 00121 00122 CYASSL_API 00123 int ecc_export_private_only(ecc_key* key, byte* out, word32* outLen); 00124 00125 /* size helper */ 00126 CYASSL_API 00127 int ecc_size(ecc_key* key); 00128 CYASSL_API 00129 int ecc_sig_size(ecc_key* key); 00130 00131 00132 #ifdef HAVE_ECC_ENCRYPT 00133 /* ecc encrypt */ 00134 00135 enum ecEncAlgo { 00136 ecAES_128_CBC = 1, /* default */ 00137 ecAES_256_CBC = 2 00138 }; 00139 00140 enum ecKdfAlgo { 00141 ecHKDF_SHA256 = 1, /* default */ 00142 ecHKDF_SHA1 = 2 00143 }; 00144 00145 enum ecMacAlgo { 00146 ecHMAC_SHA256 = 1, /* default */ 00147 ecHMAC_SHA1 = 2 00148 }; 00149 00150 enum { 00151 KEY_SIZE_128 = 16, 00152 KEY_SIZE_256 = 32, 00153 IV_SIZE_64 = 8, 00154 EXCHANGE_SALT_SZ = 16, 00155 EXCHANGE_INFO_SZ = 23 00156 }; 00157 00158 enum ecFlags { 00159 REQ_RESP_CLIENT = 1, 00160 REQ_RESP_SERVER = 2 00161 }; 00162 00163 00164 typedef struct ecEncCtx ecEncCtx; 00165 00166 CYASSL_API 00167 ecEncCtx* ecc_ctx_new(int flags, RNG* rng); 00168 CYASSL_API 00169 void ecc_ctx_free(ecEncCtx*); 00170 CYASSL_API 00171 int ecc_ctx_reset(ecEncCtx*, RNG*); /* reset for use again w/o alloc/free */ 00172 00173 CYASSL_API 00174 const byte* ecc_ctx_get_own_salt(ecEncCtx*); 00175 CYASSL_API 00176 int ecc_ctx_set_peer_salt(ecEncCtx*, const byte* salt); 00177 CYASSL_API 00178 int ecc_ctx_set_info(ecEncCtx*, const byte* info, int sz); 00179 00180 CYASSL_API 00181 int ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg, 00182 word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx); 00183 CYASSL_API 00184 int ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg, 00185 word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx); 00186 00187 #endif /* HAVE_ECC_ENCRYPT */ 00188 00189 #ifdef __cplusplus 00190 } /* extern "C" */ 00191 #endif 00192 00193 #endif /* CTAO_CRYPT_ECC_H */ 00194 #endif /* HAVE_ECC */
Generated on Wed Jul 13 2022 02:33:56 by
1.7.2