fork of cyassl-lib
Dependents: TLS_cyassl TLS_cyassl
cyassl/ctaocrypt/ecc.h@4:f377303c41be, 2013-09-16 (annotated)
- Committer:
- feb11
- Date:
- Mon Sep 16 09:53:35 2013 +0000
- Revision:
- 4:f377303c41be
- Parent:
- 0:714293de3836
changed settings
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ashleymills | 0:714293de3836 | 1 | /* ecc.h |
ashleymills | 0:714293de3836 | 2 | * |
ashleymills | 0:714293de3836 | 3 | * Copyright (C) 2006-2013 wolfSSL Inc. |
ashleymills | 0:714293de3836 | 4 | * |
ashleymills | 0:714293de3836 | 5 | * This file is part of CyaSSL. |
ashleymills | 0:714293de3836 | 6 | * |
ashleymills | 0:714293de3836 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
ashleymills | 0:714293de3836 | 8 | * it under the terms of the GNU General Public License as published by |
ashleymills | 0:714293de3836 | 9 | * the Free Software Foundation; either version 2 of the License, or |
ashleymills | 0:714293de3836 | 10 | * (at your option) any later version. |
ashleymills | 0:714293de3836 | 11 | * |
ashleymills | 0:714293de3836 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
ashleymills | 0:714293de3836 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
ashleymills | 0:714293de3836 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ashleymills | 0:714293de3836 | 15 | * GNU General Public License for more details. |
ashleymills | 0:714293de3836 | 16 | * |
ashleymills | 0:714293de3836 | 17 | * You should have received a copy of the GNU General Public License |
ashleymills | 0:714293de3836 | 18 | * along with this program; if not, write to the Free Software |
ashleymills | 0:714293de3836 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
ashleymills | 0:714293de3836 | 20 | */ |
ashleymills | 0:714293de3836 | 21 | |
ashleymills | 0:714293de3836 | 22 | #ifdef HAVE_ECC |
ashleymills | 0:714293de3836 | 23 | |
ashleymills | 0:714293de3836 | 24 | #ifndef CTAO_CRYPT_ECC_H |
ashleymills | 0:714293de3836 | 25 | #define CTAO_CRYPT_ECC_H |
ashleymills | 0:714293de3836 | 26 | |
ashleymills | 0:714293de3836 | 27 | #include <cyassl/ctaocrypt/types.h> |
ashleymills | 0:714293de3836 | 28 | #include <cyassl/ctaocrypt/integer.h> |
ashleymills | 0:714293de3836 | 29 | #include <cyassl/ctaocrypt/random.h> |
ashleymills | 0:714293de3836 | 30 | |
ashleymills | 0:714293de3836 | 31 | #ifdef __cplusplus |
ashleymills | 0:714293de3836 | 32 | extern "C" { |
ashleymills | 0:714293de3836 | 33 | #endif |
ashleymills | 0:714293de3836 | 34 | |
ashleymills | 0:714293de3836 | 35 | |
ashleymills | 0:714293de3836 | 36 | enum { |
ashleymills | 0:714293de3836 | 37 | ECC_PUBLICKEY = 1, |
ashleymills | 0:714293de3836 | 38 | ECC_PRIVATEKEY = 2, |
ashleymills | 0:714293de3836 | 39 | ECC_MAXNAME = 16, /* MAX CURVE NAME LENGTH */ |
ashleymills | 0:714293de3836 | 40 | SIG_HEADER_SZ = 6, /* ECC signature header size */ |
ashleymills | 0:714293de3836 | 41 | ECC_BUFSIZE = 256, /* for exported keys temp buffer */ |
ashleymills | 0:714293de3836 | 42 | ECC_MINSIZE = 20, /* MIN Private Key size */ |
ashleymills | 0:714293de3836 | 43 | ECC_MAXSIZE = 66 /* MAX Private Key size */ |
ashleymills | 0:714293de3836 | 44 | }; |
ashleymills | 0:714293de3836 | 45 | |
ashleymills | 0:714293de3836 | 46 | |
ashleymills | 0:714293de3836 | 47 | /* ECC set type defined a NIST GF(p) curve */ |
ashleymills | 0:714293de3836 | 48 | typedef struct { |
ashleymills | 0:714293de3836 | 49 | int size; /* The size of the curve in octets */ |
ashleymills | 0:714293de3836 | 50 | const char* name; /* name of this curve */ |
ashleymills | 0:714293de3836 | 51 | const char* prime; /* prime that defines the field, curve is in (hex) */ |
ashleymills | 0:714293de3836 | 52 | const char* B; /* fields B param (hex) */ |
ashleymills | 0:714293de3836 | 53 | const char* order; /* order of the curve (hex) */ |
ashleymills | 0:714293de3836 | 54 | const char* Gx; /* x coordinate of the base point on curve (hex) */ |
ashleymills | 0:714293de3836 | 55 | const char* Gy; /* y coordinate of the base point on curve (hex) */ |
ashleymills | 0:714293de3836 | 56 | } ecc_set_type; |
ashleymills | 0:714293de3836 | 57 | |
ashleymills | 0:714293de3836 | 58 | |
ashleymills | 0:714293de3836 | 59 | /* A point on an ECC curve, stored in Jacbobian format such that (x,y,z) => |
ashleymills | 0:714293de3836 | 60 | (x/z^2, y/z^3, 1) when interpreted as affine */ |
ashleymills | 0:714293de3836 | 61 | typedef struct { |
ashleymills | 0:714293de3836 | 62 | mp_int x; /* The x coordinate */ |
ashleymills | 0:714293de3836 | 63 | mp_int y; /* The y coordinate */ |
ashleymills | 0:714293de3836 | 64 | mp_int z; /* The z coordinate */ |
ashleymills | 0:714293de3836 | 65 | } ecc_point; |
ashleymills | 0:714293de3836 | 66 | |
ashleymills | 0:714293de3836 | 67 | |
ashleymills | 0:714293de3836 | 68 | /* An ECC Key */ |
ashleymills | 0:714293de3836 | 69 | typedef struct { |
ashleymills | 0:714293de3836 | 70 | int type; /* Public or Private */ |
ashleymills | 0:714293de3836 | 71 | int idx; /* Index into the ecc_sets[] for the parameters of |
ashleymills | 0:714293de3836 | 72 | this curve if -1, this key is using user supplied |
ashleymills | 0:714293de3836 | 73 | curve in dp */ |
ashleymills | 0:714293de3836 | 74 | const ecc_set_type* dp; /* domain parameters, either points to NIST |
ashleymills | 0:714293de3836 | 75 | curves (idx >= 0) or user supplied */ |
ashleymills | 0:714293de3836 | 76 | ecc_point pubkey; /* public key */ |
ashleymills | 0:714293de3836 | 77 | mp_int k; /* private key */ |
ashleymills | 0:714293de3836 | 78 | } ecc_key; |
ashleymills | 0:714293de3836 | 79 | |
ashleymills | 0:714293de3836 | 80 | |
ashleymills | 0:714293de3836 | 81 | /* ECC predefined curve sets */ |
ashleymills | 0:714293de3836 | 82 | extern const ecc_set_type ecc_sets[]; |
ashleymills | 0:714293de3836 | 83 | |
ashleymills | 0:714293de3836 | 84 | |
ashleymills | 0:714293de3836 | 85 | CYASSL_API |
ashleymills | 0:714293de3836 | 86 | int ecc_make_key(RNG* rng, int keysize, ecc_key* key); |
ashleymills | 0:714293de3836 | 87 | CYASSL_API |
ashleymills | 0:714293de3836 | 88 | int ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out, |
ashleymills | 0:714293de3836 | 89 | word32* outlen); |
ashleymills | 0:714293de3836 | 90 | CYASSL_API |
ashleymills | 0:714293de3836 | 91 | int ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen, |
ashleymills | 0:714293de3836 | 92 | RNG* rng, ecc_key* key); |
ashleymills | 0:714293de3836 | 93 | CYASSL_API |
ashleymills | 0:714293de3836 | 94 | int ecc_verify_hash(const byte* sig, word32 siglen, byte* hash, word32 hashlen, |
ashleymills | 0:714293de3836 | 95 | int* stat, ecc_key* key); |
ashleymills | 0:714293de3836 | 96 | CYASSL_API |
ashleymills | 0:714293de3836 | 97 | void ecc_init(ecc_key* key); |
ashleymills | 0:714293de3836 | 98 | CYASSL_API |
ashleymills | 0:714293de3836 | 99 | void ecc_free(ecc_key* key); |
ashleymills | 0:714293de3836 | 100 | |
ashleymills | 0:714293de3836 | 101 | |
ashleymills | 0:714293de3836 | 102 | /* ASN key helpers */ |
ashleymills | 0:714293de3836 | 103 | CYASSL_API |
ashleymills | 0:714293de3836 | 104 | int ecc_export_x963(ecc_key*, byte* out, word32* outLen); |
ashleymills | 0:714293de3836 | 105 | CYASSL_API |
ashleymills | 0:714293de3836 | 106 | int ecc_import_x963(const byte* in, word32 inLen, ecc_key* key); |
ashleymills | 0:714293de3836 | 107 | CYASSL_API |
ashleymills | 0:714293de3836 | 108 | int ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub, |
ashleymills | 0:714293de3836 | 109 | word32 pubSz, ecc_key* key); |
ashleymills | 0:714293de3836 | 110 | |
ashleymills | 0:714293de3836 | 111 | /* size helper */ |
ashleymills | 0:714293de3836 | 112 | CYASSL_API |
ashleymills | 0:714293de3836 | 113 | int ecc_size(ecc_key* key); |
ashleymills | 0:714293de3836 | 114 | CYASSL_API |
ashleymills | 0:714293de3836 | 115 | int ecc_sig_size(ecc_key* key); |
ashleymills | 0:714293de3836 | 116 | |
ashleymills | 0:714293de3836 | 117 | /* TODO: fix mutex types */ |
ashleymills | 0:714293de3836 | 118 | #define MUTEX_GLOBAL(x) int (x); |
ashleymills | 0:714293de3836 | 119 | #define MUTEX_LOCK(x) |
ashleymills | 0:714293de3836 | 120 | #define MUTEX_UNLOCK(x) |
ashleymills | 0:714293de3836 | 121 | |
ashleymills | 0:714293de3836 | 122 | |
ashleymills | 0:714293de3836 | 123 | |
ashleymills | 0:714293de3836 | 124 | #ifdef __cplusplus |
ashleymills | 0:714293de3836 | 125 | } /* extern "C" */ |
ashleymills | 0:714293de3836 | 126 | #endif |
ashleymills | 0:714293de3836 | 127 | |
ashleymills | 0:714293de3836 | 128 | #endif /* CTAO_CRYPT_ECC_H */ |
ashleymills | 0:714293de3836 | 129 | #endif /* HAVE_ECC */ |