A library for setting up Secure Socket Layer (SSL) connections and verifying remote hosts using certificates. Contains only the source files for mbed platform implementation of the library.

Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL

Committer:
Mike Fiore
Date:
Mon Mar 23 16:51:07 2015 -0500
Revision:
6:cf58d49e1a86
Parent:
0:b86d15c6ba29
fix whitespace in sha512.c

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vanger 0:b86d15c6ba29 1 /* ecc.h
Vanger 0:b86d15c6ba29 2 *
Vanger 0:b86d15c6ba29 3 * Copyright (C) 2006-2014 wolfSSL Inc.
Vanger 0:b86d15c6ba29 4 *
Vanger 0:b86d15c6ba29 5 * This file is part of CyaSSL.
Vanger 0:b86d15c6ba29 6 *
Vanger 0:b86d15c6ba29 7 * CyaSSL is free software; you can redistribute it and/or modify
Vanger 0:b86d15c6ba29 8 * it under the terms of the GNU General Public License as published by
Vanger 0:b86d15c6ba29 9 * the Free Software Foundation; either version 2 of the License, or
Vanger 0:b86d15c6ba29 10 * (at your option) any later version.
Vanger 0:b86d15c6ba29 11 *
Vanger 0:b86d15c6ba29 12 * CyaSSL is distributed in the hope that it will be useful,
Vanger 0:b86d15c6ba29 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Vanger 0:b86d15c6ba29 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Vanger 0:b86d15c6ba29 15 * GNU General Public License for more details.
Vanger 0:b86d15c6ba29 16 *
Vanger 0:b86d15c6ba29 17 * You should have received a copy of the GNU General Public License
Vanger 0:b86d15c6ba29 18 * along with this program; if not, write to the Free Software
Vanger 0:b86d15c6ba29 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Vanger 0:b86d15c6ba29 20 */
Vanger 0:b86d15c6ba29 21
Vanger 0:b86d15c6ba29 22 #ifdef HAVE_ECC
Vanger 0:b86d15c6ba29 23
Vanger 0:b86d15c6ba29 24 #ifndef CTAO_CRYPT_ECC_H
Vanger 0:b86d15c6ba29 25 #define CTAO_CRYPT_ECC_H
Vanger 0:b86d15c6ba29 26
Vanger 0:b86d15c6ba29 27 #include <cyassl/ctaocrypt/types.h>
Vanger 0:b86d15c6ba29 28 #include <cyassl/ctaocrypt/integer.h>
Vanger 0:b86d15c6ba29 29 #include <cyassl/ctaocrypt/random.h>
Vanger 0:b86d15c6ba29 30
Vanger 0:b86d15c6ba29 31 #ifdef __cplusplus
Vanger 0:b86d15c6ba29 32 extern "C" {
Vanger 0:b86d15c6ba29 33 #endif
Vanger 0:b86d15c6ba29 34
Vanger 0:b86d15c6ba29 35
Vanger 0:b86d15c6ba29 36 enum {
Vanger 0:b86d15c6ba29 37 ECC_PUBLICKEY = 1,
Vanger 0:b86d15c6ba29 38 ECC_PRIVATEKEY = 2,
Vanger 0:b86d15c6ba29 39 ECC_MAXNAME = 16, /* MAX CURVE NAME LENGTH */
Vanger 0:b86d15c6ba29 40 SIG_HEADER_SZ = 6, /* ECC signature header size */
Vanger 0:b86d15c6ba29 41 ECC_BUFSIZE = 256, /* for exported keys temp buffer */
Vanger 0:b86d15c6ba29 42 ECC_MINSIZE = 20, /* MIN Private Key size */
Vanger 0:b86d15c6ba29 43 ECC_MAXSIZE = 66 /* MAX Private Key size */
Vanger 0:b86d15c6ba29 44 };
Vanger 0:b86d15c6ba29 45
Vanger 0:b86d15c6ba29 46
Vanger 0:b86d15c6ba29 47 /* ECC set type defined a NIST GF(p) curve */
Vanger 0:b86d15c6ba29 48 typedef struct {
Vanger 0:b86d15c6ba29 49 int size; /* The size of the curve in octets */
Vanger 0:b86d15c6ba29 50 const char* name; /* name of this curve */
Vanger 0:b86d15c6ba29 51 const char* prime; /* prime that defines the field, curve is in (hex) */
Vanger 0:b86d15c6ba29 52 const char* Af; /* fields A param (hex) */
Vanger 0:b86d15c6ba29 53 const char* Bf; /* fields B param (hex) */
Vanger 0:b86d15c6ba29 54 const char* order; /* order of the curve (hex) */
Vanger 0:b86d15c6ba29 55 const char* Gx; /* x coordinate of the base point on curve (hex) */
Vanger 0:b86d15c6ba29 56 const char* Gy; /* y coordinate of the base point on curve (hex) */
Vanger 0:b86d15c6ba29 57 } ecc_set_type;
Vanger 0:b86d15c6ba29 58
Vanger 0:b86d15c6ba29 59
Vanger 0:b86d15c6ba29 60 /* A point on an ECC curve, stored in Jacbobian format such that (x,y,z) =>
Vanger 0:b86d15c6ba29 61 (x/z^2, y/z^3, 1) when interpreted as affine */
Vanger 0:b86d15c6ba29 62 typedef struct {
Vanger 0:b86d15c6ba29 63 mp_int x; /* The x coordinate */
Vanger 0:b86d15c6ba29 64 mp_int y; /* The y coordinate */
Vanger 0:b86d15c6ba29 65 mp_int z; /* The z coordinate */
Vanger 0:b86d15c6ba29 66 } ecc_point;
Vanger 0:b86d15c6ba29 67
Vanger 0:b86d15c6ba29 68
Vanger 0:b86d15c6ba29 69 /* An ECC Key */
Vanger 0:b86d15c6ba29 70 typedef struct {
Vanger 0:b86d15c6ba29 71 int type; /* Public or Private */
Vanger 0:b86d15c6ba29 72 int idx; /* Index into the ecc_sets[] for the parameters of
Vanger 0:b86d15c6ba29 73 this curve if -1, this key is using user supplied
Vanger 0:b86d15c6ba29 74 curve in dp */
Vanger 0:b86d15c6ba29 75 const ecc_set_type* dp; /* domain parameters, either points to NIST
Vanger 0:b86d15c6ba29 76 curves (idx >= 0) or user supplied */
Vanger 0:b86d15c6ba29 77 ecc_point pubkey; /* public key */
Vanger 0:b86d15c6ba29 78 mp_int k; /* private key */
Vanger 0:b86d15c6ba29 79 } ecc_key;
Vanger 0:b86d15c6ba29 80
Vanger 0:b86d15c6ba29 81
Vanger 0:b86d15c6ba29 82 /* ECC predefined curve sets */
Vanger 0:b86d15c6ba29 83 extern const ecc_set_type ecc_sets[];
Vanger 0:b86d15c6ba29 84
Vanger 0:b86d15c6ba29 85
Vanger 0:b86d15c6ba29 86 CYASSL_API
Vanger 0:b86d15c6ba29 87 int ecc_make_key(RNG* rng, int keysize, ecc_key* key);
Vanger 0:b86d15c6ba29 88 CYASSL_API
Vanger 0:b86d15c6ba29 89 int ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
Vanger 0:b86d15c6ba29 90 word32* outlen);
Vanger 0:b86d15c6ba29 91 CYASSL_API
Vanger 0:b86d15c6ba29 92 int ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
Vanger 0:b86d15c6ba29 93 RNG* rng, ecc_key* key);
Vanger 0:b86d15c6ba29 94 CYASSL_API
Vanger 0:b86d15c6ba29 95 int ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
Vanger 0:b86d15c6ba29 96 word32 hashlen, int* stat, ecc_key* key);
Vanger 0:b86d15c6ba29 97 CYASSL_API
Vanger 0:b86d15c6ba29 98 void ecc_init(ecc_key* key);
Vanger 0:b86d15c6ba29 99 CYASSL_API
Vanger 0:b86d15c6ba29 100 void ecc_free(ecc_key* key);
Vanger 0:b86d15c6ba29 101 CYASSL_API
Vanger 0:b86d15c6ba29 102 void ecc_fp_free(void);
Vanger 0:b86d15c6ba29 103
Vanger 0:b86d15c6ba29 104
Vanger 0:b86d15c6ba29 105 /* ASN key helpers */
Vanger 0:b86d15c6ba29 106 CYASSL_API
Vanger 0:b86d15c6ba29 107 int ecc_export_x963(ecc_key*, byte* out, word32* outLen);
Vanger 0:b86d15c6ba29 108 CYASSL_API
Vanger 0:b86d15c6ba29 109 int ecc_export_x963_ex(ecc_key*, byte* out, word32* outLen, int compressed);
Vanger 0:b86d15c6ba29 110 /* extended functionality with compressed option */
Vanger 0:b86d15c6ba29 111 CYASSL_API
Vanger 0:b86d15c6ba29 112 int ecc_import_x963(const byte* in, word32 inLen, ecc_key* key);
Vanger 0:b86d15c6ba29 113 CYASSL_API
Vanger 0:b86d15c6ba29 114 int ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
Vanger 0:b86d15c6ba29 115 word32 pubSz, ecc_key* key);
Vanger 0:b86d15c6ba29 116 CYASSL_API
Vanger 0:b86d15c6ba29 117 int ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen);
Vanger 0:b86d15c6ba29 118 CYASSL_API
Vanger 0:b86d15c6ba29 119 int ecc_import_raw(ecc_key* key, const char* qx, const char* qy,
Vanger 0:b86d15c6ba29 120 const char* d, const char* curveName);
Vanger 0:b86d15c6ba29 121
Vanger 0:b86d15c6ba29 122 CYASSL_API
Vanger 0:b86d15c6ba29 123 int ecc_export_private_only(ecc_key* key, byte* out, word32* outLen);
Vanger 0:b86d15c6ba29 124
Vanger 0:b86d15c6ba29 125 /* size helper */
Vanger 0:b86d15c6ba29 126 CYASSL_API
Vanger 0:b86d15c6ba29 127 int ecc_size(ecc_key* key);
Vanger 0:b86d15c6ba29 128 CYASSL_API
Vanger 0:b86d15c6ba29 129 int ecc_sig_size(ecc_key* key);
Vanger 0:b86d15c6ba29 130
Vanger 0:b86d15c6ba29 131
Vanger 0:b86d15c6ba29 132 #ifdef HAVE_ECC_ENCRYPT
Vanger 0:b86d15c6ba29 133 /* ecc encrypt */
Vanger 0:b86d15c6ba29 134
Vanger 0:b86d15c6ba29 135 enum ecEncAlgo {
Vanger 0:b86d15c6ba29 136 ecAES_128_CBC = 1, /* default */
Vanger 0:b86d15c6ba29 137 ecAES_256_CBC = 2
Vanger 0:b86d15c6ba29 138 };
Vanger 0:b86d15c6ba29 139
Vanger 0:b86d15c6ba29 140 enum ecKdfAlgo {
Vanger 0:b86d15c6ba29 141 ecHKDF_SHA256 = 1, /* default */
Vanger 0:b86d15c6ba29 142 ecHKDF_SHA1 = 2
Vanger 0:b86d15c6ba29 143 };
Vanger 0:b86d15c6ba29 144
Vanger 0:b86d15c6ba29 145 enum ecMacAlgo {
Vanger 0:b86d15c6ba29 146 ecHMAC_SHA256 = 1, /* default */
Vanger 0:b86d15c6ba29 147 ecHMAC_SHA1 = 2
Vanger 0:b86d15c6ba29 148 };
Vanger 0:b86d15c6ba29 149
Vanger 0:b86d15c6ba29 150 enum {
Vanger 0:b86d15c6ba29 151 KEY_SIZE_128 = 16,
Vanger 0:b86d15c6ba29 152 KEY_SIZE_256 = 32,
Vanger 0:b86d15c6ba29 153 IV_SIZE_64 = 8,
Vanger 0:b86d15c6ba29 154 EXCHANGE_SALT_SZ = 16,
Vanger 0:b86d15c6ba29 155 EXCHANGE_INFO_SZ = 23
Vanger 0:b86d15c6ba29 156 };
Vanger 0:b86d15c6ba29 157
Vanger 0:b86d15c6ba29 158 enum ecFlags {
Vanger 0:b86d15c6ba29 159 REQ_RESP_CLIENT = 1,
Vanger 0:b86d15c6ba29 160 REQ_RESP_SERVER = 2
Vanger 0:b86d15c6ba29 161 };
Vanger 0:b86d15c6ba29 162
Vanger 0:b86d15c6ba29 163
Vanger 0:b86d15c6ba29 164 typedef struct ecEncCtx ecEncCtx;
Vanger 0:b86d15c6ba29 165
Vanger 0:b86d15c6ba29 166 CYASSL_API
Vanger 0:b86d15c6ba29 167 ecEncCtx* ecc_ctx_new(int flags, RNG* rng);
Vanger 0:b86d15c6ba29 168 CYASSL_API
Vanger 0:b86d15c6ba29 169 void ecc_ctx_free(ecEncCtx*);
Vanger 0:b86d15c6ba29 170 CYASSL_API
Vanger 0:b86d15c6ba29 171 int ecc_ctx_reset(ecEncCtx*, RNG*); /* reset for use again w/o alloc/free */
Vanger 0:b86d15c6ba29 172
Vanger 0:b86d15c6ba29 173 CYASSL_API
Vanger 0:b86d15c6ba29 174 const byte* ecc_ctx_get_own_salt(ecEncCtx*);
Vanger 0:b86d15c6ba29 175 CYASSL_API
Vanger 0:b86d15c6ba29 176 int ecc_ctx_set_peer_salt(ecEncCtx*, const byte* salt);
Vanger 0:b86d15c6ba29 177 CYASSL_API
Vanger 0:b86d15c6ba29 178 int ecc_ctx_set_info(ecEncCtx*, const byte* info, int sz);
Vanger 0:b86d15c6ba29 179
Vanger 0:b86d15c6ba29 180 CYASSL_API
Vanger 0:b86d15c6ba29 181 int ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
Vanger 0:b86d15c6ba29 182 word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
Vanger 0:b86d15c6ba29 183 CYASSL_API
Vanger 0:b86d15c6ba29 184 int ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
Vanger 0:b86d15c6ba29 185 word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
Vanger 0:b86d15c6ba29 186
Vanger 0:b86d15c6ba29 187 #endif /* HAVE_ECC_ENCRYPT */
Vanger 0:b86d15c6ba29 188
Vanger 0:b86d15c6ba29 189 #ifdef __cplusplus
Vanger 0:b86d15c6ba29 190 } /* extern "C" */
Vanger 0:b86d15c6ba29 191 #endif
Vanger 0:b86d15c6ba29 192
Vanger 0:b86d15c6ba29 193 #endif /* CTAO_CRYPT_ECC_H */
Vanger 0:b86d15c6ba29 194 #endif /* HAVE_ECC */