| 1 | /* |
| 2 | * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. |
| 3 | * Use is subject to license terms. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public License |
| 16 | * along with this library; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
| 22 | */ |
| 23 | |
| 24 | /* ********************************************************************* |
| 25 | * |
| 26 | * The Original Code is the Netscape security libraries. |
| 27 | * |
| 28 | * The Initial Developer of the Original Code is |
| 29 | * Netscape Communications Corporation. |
| 30 | * Portions created by the Initial Developer are Copyright (C) 1994-2000 |
| 31 | * the Initial Developer. All Rights Reserved. |
| 32 | * |
| 33 | * Contributor(s): |
| 34 | * Dr Vipul Gupta <vipul.gupta@sun.com> and |
| 35 | * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories |
| 36 | * |
| 37 | * Last Modified Date from the Original Code: May 2017 |
| 38 | *********************************************************************** */ |
| 39 | |
| 40 | #ifndef _ECC_IMPL_H |
| 41 | #define _ECC_IMPL_H |
| 42 | |
| 43 | #ifdef __cplusplus |
| 44 | extern "C" { |
| 45 | #endif |
| 46 | |
| 47 | #include <sys/types.h> |
| 48 | #include "ecl-exp.h" |
| 49 | |
| 50 | /* |
| 51 | * Multi-platform definitions |
| 52 | */ |
| 53 | #ifdef __linux__ |
| 54 | #define B_FALSE FALSE |
| 55 | #define B_TRUE TRUE |
| 56 | typedef unsigned char uint8_t; |
| 57 | typedef unsigned long ulong_t; |
| 58 | typedef enum { B_FALSE, B_TRUE } boolean_t; |
| 59 | #endif /* __linux__ */ |
| 60 | |
| 61 | #ifdef _ALLBSD_SOURCE |
| 62 | #include <stdint.h> |
| 63 | #define B_FALSE FALSE |
| 64 | #define B_TRUE TRUE |
| 65 | typedef unsigned long ulong_t; |
| 66 | typedef enum boolean { B_FALSE, B_TRUE } boolean_t; |
| 67 | #endif /* _ALLBSD_SOURCE */ |
| 68 | |
| 69 | #ifdef AIX |
| 70 | #define B_FALSE FALSE |
| 71 | #define B_TRUE TRUE |
| 72 | typedef unsigned char uint8_t; |
| 73 | typedef unsigned long ulong_t; |
| 74 | #endif /* AIX */ |
| 75 | |
| 76 | #ifdef _WIN32 |
| 77 | typedef unsigned char uint8_t; |
| 78 | typedef unsigned long ulong_t; |
| 79 | typedef enum boolean { B_FALSE, B_TRUE } boolean_t; |
| 80 | #define strdup _strdup /* Replace POSIX name with ISO C++ name */ |
| 81 | #endif /* _WIN32 */ |
| 82 | |
| 83 | #ifndef _KERNEL |
| 84 | #include <stdlib.h> |
| 85 | #endif /* _KERNEL */ |
| 86 | |
| 87 | #define EC_MAX_DIGEST_LEN 1024 /* max digest that can be signed */ |
| 88 | #define EC_MAX_POINT_LEN 145 /* max len of DER encoded Q */ |
| 89 | #define EC_MAX_VALUE_LEN 72 /* max len of ANSI X9.62 private value d */ |
| 90 | #define EC_MAX_SIG_LEN 144 /* max signature len for supported curves */ |
| 91 | #define EC_MIN_KEY_LEN 112 /* min key length in bits */ |
| 92 | #define EC_MAX_KEY_LEN 571 /* max key length in bits */ |
| 93 | #define EC_MAX_OID_LEN 10 /* max length of OID buffer */ |
| 94 | |
| 95 | /* |
| 96 | * Various structures and definitions from NSS are here. |
| 97 | */ |
| 98 | |
| 99 | #ifdef _KERNEL |
| 100 | #define PORT_ArenaAlloc(a, n, f) kmem_alloc((n), (f)) |
| 101 | #define PORT_ArenaZAlloc(a, n, f) kmem_zalloc((n), (f)) |
| 102 | #define PORT_ArenaGrow(a, b, c, d) NULL |
| 103 | #define PORT_ZAlloc(n, f) kmem_zalloc((n), (f)) |
| 104 | #define PORT_Alloc(n, f) kmem_alloc((n), (f)) |
| 105 | #else |
| 106 | #define PORT_ArenaAlloc(a, n, f) malloc((n)) |
| 107 | #define PORT_ArenaZAlloc(a, n, f) calloc(1, (n)) |
| 108 | #define PORT_ArenaGrow(a, b, c, d) NULL |
| 109 | #define PORT_ZAlloc(n, f) calloc(1, (n)) |
| 110 | #define PORT_Alloc(n, f) malloc((n)) |
| 111 | #endif |
| 112 | |
| 113 | #define PORT_NewArena(b) (char *)12345 |
| 114 | #define PORT_ArenaMark(a) NULL |
| 115 | #define PORT_ArenaUnmark(a, b) |
| 116 | #define PORT_ArenaRelease(a, m) |
| 117 | #define PORT_FreeArena(a, b) |
| 118 | #define PORT_Strlen(s) strlen((s)) |
| 119 | #define PORT_SetError(e) |
| 120 | |
| 121 | #define PRBool boolean_t |
| 122 | #define PR_TRUE B_TRUE |
| 123 | #define PR_FALSE B_FALSE |
| 124 | |
| 125 | #ifdef _KERNEL |
| 126 | #define PORT_Assert ASSERT |
| 127 | #define PORT_Memcpy(t, f, l) bcopy((f), (t), (l)) |
| 128 | #else |
| 129 | #define PORT_Assert assert |
| 130 | #define PORT_Memcpy(t, f, l) memcpy((t), (f), (l)) |
| 131 | #endif |
| 132 | |
| 133 | #define CHECK_OK(func) if (func == NULL) goto cleanup |
| 134 | #define CHECK_SEC_OK(func) if (SECSuccess != (rv = func)) goto cleanup |
| 135 | |
| 136 | typedef enum { |
| 137 | siBuffer = 0, |
| 138 | siClearDataBuffer = 1, |
| 139 | siCipherDataBuffer = 2, |
| 140 | siDERCertBuffer = 3, |
| 141 | siEncodedCertBuffer = 4, |
| 142 | siDERNameBuffer = 5, |
| 143 | siEncodedNameBuffer = 6, |
| 144 | siAsciiNameString = 7, |
| 145 | siAsciiString = 8, |
| 146 | siDEROID = 9, |
| 147 | siUnsignedInteger = 10, |
| 148 | siUTCTime = 11, |
| 149 | siGeneralizedTime = 12 |
| 150 | } SECItemType; |
| 151 | |
| 152 | typedef struct SECItemStr SECItem; |
| 153 | |
| 154 | struct SECItemStr { |
| 155 | SECItemType type; |
| 156 | unsigned char *data; |
| 157 | unsigned int len; |
| 158 | }; |
| 159 | |
| 160 | typedef SECItem SECKEYECParams; |
| 161 | |
| 162 | typedef enum { ec_params_explicit, |
| 163 | ec_params_named |
| 164 | } ECParamsType; |
| 165 | |
| 166 | typedef enum { ec_field_GFp = 1, |
| 167 | ec_field_GF2m |
| 168 | } ECFieldType; |
| 169 | |
| 170 | struct ECFieldIDStr { |
| 171 | int size; /* field size in bits */ |
| 172 | ECFieldType type; |
| 173 | union { |
| 174 | SECItem prime; /* prime p for (GFp) */ |
| 175 | SECItem poly; /* irreducible binary polynomial for (GF2m) */ |
| 176 | } u; |
| 177 | int k1; /* first coefficient of pentanomial or |
| 178 | * the only coefficient of trinomial |
| 179 | */ |
| 180 | int k2; /* two remaining coefficients of pentanomial */ |
| 181 | int k3; |
| 182 | }; |
| 183 | typedef struct ECFieldIDStr ECFieldID; |
| 184 | |
| 185 | struct ECCurveStr { |
| 186 | SECItem a; /* contains octet stream encoding of |
| 187 | * field element (X9.62 section 4.3.3) |
| 188 | */ |
| 189 | SECItem b; |
| 190 | SECItem seed; |
| 191 | }; |
| 192 | typedef struct ECCurveStr ECCurve; |
| 193 | |
| 194 | typedef void PRArenaPool; |
| 195 | |
| 196 | struct ECParamsStr { |
| 197 | PRArenaPool * arena; |
| 198 | ECParamsType type; |
| 199 | ECFieldID fieldID; |
| 200 | ECCurve curve; |
| 201 | SECItem base; |
| 202 | SECItem order; |
| 203 | int cofactor; |
| 204 | SECItem DEREncoding; |
| 205 | ECCurveName name; |
| 206 | SECItem curveOID; |
| 207 | }; |
| 208 | typedef struct ECParamsStr ECParams; |
| 209 | |
| 210 | struct ECPublicKeyStr { |
| 211 | ECParams ecParams; |
| 212 | SECItem publicValue; /* elliptic curve point encoded as |
| 213 | * octet stream. |
| 214 | */ |
| 215 | }; |
| 216 | typedef struct ECPublicKeyStr ECPublicKey; |
| 217 | |
| 218 | struct ECPrivateKeyStr { |
| 219 | ECParams ecParams; |
| 220 | SECItem publicValue; /* encoded ec point */ |
| 221 | SECItem privateValue; /* private big integer */ |
| 222 | SECItem version; /* As per SEC 1, Appendix C, Section C.4 */ |
| 223 | }; |
| 224 | typedef struct ECPrivateKeyStr ECPrivateKey; |
| 225 | |
| 226 | typedef enum _SECStatus { |
| 227 | SECBufferTooSmall = -3, |
| 228 | SECWouldBlock = -2, |
| 229 | SECFailure = -1, |
| 230 | SECSuccess = 0 |
| 231 | } SECStatus; |
| 232 | |
| 233 | #ifdef _KERNEL |
| 234 | #define RNG_GenerateGlobalRandomBytes(p,l) ecc_knzero_random_generator((p), (l)) |
| 235 | #else |
| 236 | /* |
| 237 | This function is no longer required because the random bytes are now |
| 238 | supplied by the caller. Force a failure. |
| 239 | */ |
| 240 | #define RNG_GenerateGlobalRandomBytes(p,l) SECFailure |
| 241 | #endif |
| 242 | #define CHECK_MPI_OK(func) if (MP_OKAY > (err = func)) goto cleanup |
| 243 | #define MP_TO_SEC_ERROR(err) |
| 244 | |
| 245 | #define SECITEM_TO_MPINT(it, mp) \ |
| 246 | CHECK_MPI_OK(mp_read_unsigned_octets((mp), (it).data, (it).len)) |
| 247 | |
| 248 | extern int ecc_knzero_random_generator(uint8_t *, size_t); |
| 249 | extern ulong_t soft_nzero_random_generator(uint8_t *, ulong_t); |
| 250 | |
| 251 | extern SECStatus EC_DecodeParams(const SECItem *, ECParams **, int); |
| 252 | extern SECItem * SECITEM_AllocItem(PRArenaPool *, SECItem *, unsigned int, int); |
| 253 | extern SECStatus SECITEM_CopyItem(PRArenaPool *, SECItem *, const SECItem *, |
| 254 | int); |
| 255 | extern void SECITEM_FreeItem(SECItem *, boolean_t); |
| 256 | /* This function has been modified to accept an array of random bytes */ |
| 257 | extern SECStatus EC_NewKey(ECParams *ecParams, ECPrivateKey **privKey, |
| 258 | const unsigned char* random, int randomlen, int); |
| 259 | /* This function has been modified to accept an array of random bytes */ |
| 260 | extern SECStatus ECDSA_SignDigest(ECPrivateKey *, SECItem *, const SECItem *, |
| 261 | const unsigned char* random, int randomlen, int, int timing); |
| 262 | extern SECStatus ECDSA_VerifyDigest(ECPublicKey *, const SECItem *, |
| 263 | const SECItem *, int); |
| 264 | extern SECStatus ECDH_Derive(SECItem *, ECParams *, SECItem *, boolean_t, |
| 265 | SECItem *, int); |
| 266 | |
| 267 | #ifdef __cplusplus |
| 268 | } |
| 269 | #endif |
| 270 | |
| 271 | #endif /* _ECC_IMPL_H */ |
| 272 | |