1 /* 2 * Copyright (c) 2007, 2012, 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 *********************************************************************** */ 38 39 #ifndef _ECC_IMPL_H 40 #define _ECC_IMPL_H 41 42 #ifdef __cplusplus 43 extern "C" { 44 #endif 45 46 #include <string.h> 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 #define PORT_ZFree(p, l) \ 106 do { \ 107 memset((p), 0, (l)); \ 108 kmem_free((p), (l)); \ 109 } while (0) 110 #else 111 #define PORT_ArenaAlloc(a, n, f) malloc((n)) 112 #define PORT_ArenaZAlloc(a, n, f) calloc(1, (n)) 113 #define PORT_ArenaGrow(a, b, c, d) NULL 114 #define PORT_ZAlloc(n, f) calloc(1, (n)) 115 #define PORT_Alloc(n, f) malloc((n)) 116 #define PORT_ZFree(p, l) \ 117 do { \ 118 memset((p), 0, (l)); \ 119 free((p)); \ 120 } while (0) 121 #endif 122 123 #define PORT_NewArena(b) (char *)12345 124 #define PORT_ArenaMark(a) NULL 125 #define PORT_ArenaUnmark(a, b) 126 #define PORT_ArenaRelease(a, m) 127 #define PORT_FreeArena(a, b) 128 #define PORT_Strlen(s) strlen((s)) 129 #define PORT_SetError(e) 130 131 #define PRBool boolean_t 132 #define PR_TRUE B_TRUE 133 #define PR_FALSE B_FALSE 134 135 #ifdef _KERNEL 136 #define PORT_Assert ASSERT 137 #define PORT_Memcpy(t, f, l) bcopy((f), (t), (l)) 138 #else 139 #define PORT_Assert assert 140 #define PORT_Memcpy(t, f, l) memcpy((t), (f), (l)) 141 #endif 142 143 #define CHECK_OK(func) if (func == NULL) goto cleanup 144 #define CHECK_SEC_OK(func) if (SECSuccess != (rv = func)) goto cleanup 145 146 typedef enum { 147 siBuffer = 0, 148 siClearDataBuffer = 1, 149 siCipherDataBuffer = 2, 150 siDERCertBuffer = 3, 151 siEncodedCertBuffer = 4, 152 siDERNameBuffer = 5, 153 siEncodedNameBuffer = 6, 154 siAsciiNameString = 7, 155 siAsciiString = 8, 156 siDEROID = 9, 157 siUnsignedInteger = 10, 158 siUTCTime = 11, 159 siGeneralizedTime = 12 160 } SECItemType; 161 162 typedef struct SECItemStr SECItem; 163 164 struct SECItemStr { 165 SECItemType type; 166 unsigned char *data; 167 unsigned int len; 168 }; 169 170 typedef SECItem SECKEYECParams; 171 172 typedef enum { ec_params_explicit, 173 ec_params_named 174 } ECParamsType; 175 176 typedef enum { ec_field_GFp = 1, 177 ec_field_GF2m 178 } ECFieldType; 179 180 struct ECFieldIDStr { 181 int size; /* field size in bits */ 182 ECFieldType type; 183 union { 184 SECItem prime; /* prime p for (GFp) */ 185 SECItem poly; /* irreducible binary polynomial for (GF2m) */ 186 } u; 187 int k1; /* first coefficient of pentanomial or 188 * the only coefficient of trinomial 189 */ 190 int k2; /* two remaining coefficients of pentanomial */ 191 int k3; 192 }; 193 typedef struct ECFieldIDStr ECFieldID; 194 195 struct ECCurveStr { 196 SECItem a; /* contains octet stream encoding of 197 * field element (X9.62 section 4.3.3) 198 */ 199 SECItem b; 200 SECItem seed; 201 }; 202 typedef struct ECCurveStr ECCurve; 203 204 typedef void PRArenaPool; 205 206 struct ECParamsStr { 207 PRArenaPool * arena; 208 ECParamsType type; 209 ECFieldID fieldID; 210 ECCurve curve; 211 SECItem base; 212 SECItem order; 213 int cofactor; 214 SECItem DEREncoding; 215 ECCurveName name; 216 SECItem curveOID; 217 }; 218 typedef struct ECParamsStr ECParams; 219 220 struct ECPublicKeyStr { 221 ECParams ecParams; 222 SECItem publicValue; /* elliptic curve point encoded as 223 * octet stream. 224 */ 225 }; 226 typedef struct ECPublicKeyStr ECPublicKey; 227 228 struct ECPrivateKeyStr { 229 ECParams ecParams; 230 SECItem publicValue; /* encoded ec point */ 231 SECItem privateValue; /* private big integer */ 232 SECItem version; /* As per SEC 1, Appendix C, Section C.4 */ 233 }; 234 typedef struct ECPrivateKeyStr ECPrivateKey; 235 236 typedef enum _SECStatus { 237 SECBufferTooSmall = -3, 238 SECWouldBlock = -2, 239 SECFailure = -1, 240 SECSuccess = 0 241 } SECStatus; 242 243 #ifdef _KERNEL 244 #define RNG_GenerateGlobalRandomBytes(p,l) ecc_knzero_random_generator((p), (l)) 245 #else 246 /* 247 This function is no longer required because the random bytes are now 248 supplied by the caller. Force a failure. 249 */ 250 #define RNG_GenerateGlobalRandomBytes(p,l) SECFailure 251 #endif 252 #define CHECK_MPI_OK(func) if (MP_OKAY > (err = func)) goto cleanup 253 #define MP_TO_SEC_ERROR(err) 254 255 #define SECITEM_TO_MPINT(it, mp) \ 256 CHECK_MPI_OK(mp_read_unsigned_octets((mp), (it).data, (it).len)) 257 258 extern int ecc_knzero_random_generator(uint8_t *, size_t); 259 extern ulong_t soft_nzero_random_generator(uint8_t *, ulong_t); 260 261 extern SECStatus EC_DecodeParams(const SECItem *, ECParams **, int); 262 extern SECItem * SECITEM_AllocItem(PRArenaPool *, SECItem *, unsigned int, int); 263 extern SECStatus SECITEM_CopyItem(PRArenaPool *, SECItem *, const SECItem *, 264 int); 265 extern void SECITEM_FreeItem(SECItem *, boolean_t); 266 /* This function has been modified to accept an array of random bytes */ 267 extern SECStatus EC_NewKey(ECParams *ecParams, ECPrivateKey **privKey, 268 const unsigned char* random, int randomlen, int); 269 /* This function has been modified to accept an array of random bytes */ 270 extern SECStatus ECDSA_SignDigest(ECPrivateKey *, SECItem *, const SECItem *, 271 const unsigned char* random, int randomlen, int); 272 extern SECStatus ECDSA_VerifyDigest(ECPublicKey *, const SECItem *, 273 const SECItem *, int); 274 extern SECStatus ECDH_Derive(SECItem *, ECParams *, SECItem *, boolean_t, 275 SECItem *, int); 276 277 #ifdef __cplusplus 278 } 279 #endif 280 281 #endif /* _ECC_IMPL_H */