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 <sys/types.h> 47 #include "ecl-exp.h" 48 49 /* 50 * Multi-platform definitions 51 */ 52 #ifdef __linux__ 53 #define B_FALSE FALSE 54 #define B_TRUE TRUE 55 typedef unsigned char uint8_t; 56 typedef unsigned long ulong_t; 57 typedef enum { B_FALSE, B_TRUE } boolean_t; 58 #endif /* __linux__ */ 59 60 #ifdef _ALLBSD_SOURCE 61 #include <stdint.h> 62 #define B_FALSE FALSE 63 #define B_TRUE TRUE 64 typedef unsigned long ulong_t; 65 typedef enum boolean { B_FALSE, B_TRUE } boolean_t; 66 #endif /* _ALLBSD_SOURCE */ 67 68 #ifdef AIX 69 #define B_FALSE FALSE 70 #define B_TRUE TRUE 71 typedef unsigned char uint8_t; 72 typedef unsigned long ulong_t; 73 #endif /* AIX */ 74 75 #ifdef _WIN32 76 typedef unsigned char uint8_t; 77 typedef unsigned long ulong_t; 78 typedef enum boolean { B_FALSE, B_TRUE } boolean_t; 79 #define strdup _strdup /* Replace POSIX name with ISO C++ name */ 80 #endif /* _WIN32 */ 81 82 #ifndef _KERNEL 83 #include <stdlib.h> 84 #endif /* _KERNEL */ 85 86 #define EC_MAX_DIGEST_LEN 1024 /* max digest that can be signed */ 87 #define EC_MAX_POINT_LEN 145 /* max len of DER encoded Q */ 88 #define EC_MAX_VALUE_LEN 72 /* max len of ANSI X9.62 private value d */ 89 #define EC_MAX_SIG_LEN 144 /* max signature len for supported curves */ 90 #define EC_MIN_KEY_LEN 112 /* min key length in bits */ 91 #define EC_MAX_KEY_LEN 571 /* max key length in bits */ 92 #define EC_MAX_OID_LEN 10 /* max length of OID buffer */ 93 94 /* 95 * Various structures and definitions from NSS are here. 96 */ 97 98 #ifdef _KERNEL 99 #define PORT_ArenaAlloc(a, n, f) kmem_alloc((n), (f)) 100 #define PORT_ArenaZAlloc(a, n, f) kmem_zalloc((n), (f)) 101 #define PORT_ArenaGrow(a, b, c, d) NULL 102 #define PORT_ZAlloc(n, f) kmem_zalloc((n), (f)) 103 #define PORT_Alloc(n, f) kmem_alloc((n), (f)) 104 #else 105 #define PORT_ArenaAlloc(a, n, f) malloc((n)) 106 #define PORT_ArenaZAlloc(a, n, f) calloc(1, (n)) 107 #define PORT_ArenaGrow(a, b, c, d) NULL 108 #define PORT_ZAlloc(n, f) calloc(1, (n)) 109 #define PORT_Alloc(n, f) malloc((n)) 110 #endif 111 112 #define PORT_NewArena(b) (char *)12345 113 #define PORT_ArenaMark(a) NULL 114 #define PORT_ArenaUnmark(a, b) 115 #define PORT_ArenaRelease(a, m) 116 #define PORT_FreeArena(a, b) 117 #define PORT_Strlen(s) strlen((s)) 118 #define PORT_SetError(e) 119 120 #define PRBool boolean_t 121 #define PR_TRUE B_TRUE 122 #define PR_FALSE B_FALSE 123 124 #ifdef _KERNEL 125 #define PORT_Assert ASSERT 126 #define PORT_Memcpy(t, f, l) bcopy((f), (t), (l)) 127 #else 128 #define PORT_Assert assert 129 #define PORT_Memcpy(t, f, l) memcpy((t), (f), (l)) 130 #endif 131 132 #define CHECK_OK(func) if (func == NULL) goto cleanup 133 #define CHECK_SEC_OK(func) if (SECSuccess != (rv = func)) goto cleanup 134 135 typedef enum { 136 siBuffer = 0, 137 siClearDataBuffer = 1, 138 siCipherDataBuffer = 2, 139 siDERCertBuffer = 3, 140 siEncodedCertBuffer = 4, 141 siDERNameBuffer = 5, 142 siEncodedNameBuffer = 6, 143 siAsciiNameString = 7, 144 siAsciiString = 8, 145 siDEROID = 9, 146 siUnsignedInteger = 10, 147 siUTCTime = 11, 148 siGeneralizedTime = 12 149 } SECItemType; 150 151 typedef struct SECItemStr SECItem; 152 153 struct SECItemStr { 154 SECItemType type; 155 unsigned char *data; 156 unsigned int len; 157 }; 158 159 typedef SECItem SECKEYECParams; 160 161 typedef enum { ec_params_explicit, 162 ec_params_named 163 } ECParamsType; 164 165 typedef enum { ec_field_GFp = 1, 166 ec_field_GF2m 167 } ECFieldType; 168 169 struct ECFieldIDStr { 170 int size; /* field size in bits */ 171 ECFieldType type; 172 union { 173 SECItem prime; /* prime p for (GFp) */ 174 SECItem poly; /* irreducible binary polynomial for (GF2m) */ 175 } u; 176 int k1; /* first coefficient of pentanomial or 177 * the only coefficient of trinomial 178 */ 179 int k2; /* two remaining coefficients of pentanomial */ 180 int k3; 181 }; 182 typedef struct ECFieldIDStr ECFieldID; 183 184 struct ECCurveStr { 185 SECItem a; /* contains octet stream encoding of 186 * field element (X9.62 section 4.3.3) 187 */ 188 SECItem b; 189 SECItem seed; 190 }; 191 typedef struct ECCurveStr ECCurve; 192 193 typedef void PRArenaPool; 194 195 struct ECParamsStr { 196 PRArenaPool * arena; 197 ECParamsType type; 198 ECFieldID fieldID; 199 ECCurve curve; 200 SECItem base; 201 SECItem order; 202 int cofactor; 203 SECItem DEREncoding; 204 ECCurveName name; 205 SECItem curveOID; 206 }; 207 typedef struct ECParamsStr ECParams; 208 209 struct ECPublicKeyStr { 210 ECParams ecParams; 211 SECItem publicValue; /* elliptic curve point encoded as 212 * octet stream. 213 */ 214 }; 215 typedef struct ECPublicKeyStr ECPublicKey; 216 217 struct ECPrivateKeyStr { 218 ECParams ecParams; 219 SECItem publicValue; /* encoded ec point */ 220 SECItem privateValue; /* private big integer */ 221 SECItem version; /* As per SEC 1, Appendix C, Section C.4 */ 222 }; 223 typedef struct ECPrivateKeyStr ECPrivateKey; 224 225 typedef enum _SECStatus { 226 SECBufferTooSmall = -3, 227 SECWouldBlock = -2, 228 SECFailure = -1, 229 SECSuccess = 0 230 } SECStatus; 231 232 #ifdef _KERNEL 233 #define RNG_GenerateGlobalRandomBytes(p,l) ecc_knzero_random_generator((p), (l)) 234 #else 235 /* 236 This function is no longer required because the random bytes are now 237 supplied by the caller. Force a failure. 238 */ 239 #define RNG_GenerateGlobalRandomBytes(p,l) SECFailure 240 #endif 241 #define CHECK_MPI_OK(func) if (MP_OKAY > (err = func)) goto cleanup 242 #define MP_TO_SEC_ERROR(err) 243 244 #define SECITEM_TO_MPINT(it, mp) \ 245 CHECK_MPI_OK(mp_read_unsigned_octets((mp), (it).data, (it).len)) 246 247 extern int ecc_knzero_random_generator(uint8_t *, size_t); 248 extern ulong_t soft_nzero_random_generator(uint8_t *, ulong_t); 249 250 extern SECStatus EC_DecodeParams(const SECItem *, ECParams **, int); 251 extern SECItem * SECITEM_AllocItem(PRArenaPool *, SECItem *, unsigned int, int); 252 extern SECStatus SECITEM_CopyItem(PRArenaPool *, SECItem *, const SECItem *, 253 int); 254 extern void SECITEM_FreeItem(SECItem *, boolean_t); 255 /* This function has been modified to accept an array of random bytes */ 256 extern SECStatus EC_NewKey(ECParams *ecParams, ECPrivateKey **privKey, 257 const unsigned char* random, int randomlen, int); 258 /* This function has been modified to accept an array of random bytes */ 259 extern SECStatus ECDSA_SignDigest(ECPrivateKey *, SECItem *, const SECItem *, 260 const unsigned char* random, int randomlen, int); 261 extern SECStatus ECDSA_VerifyDigest(ECPublicKey *, const SECItem *, 262 const SECItem *, int); 263 extern SECStatus ECDH_Derive(SECItem *, ECParams *, SECItem *, boolean_t, 264 SECItem *, int); 265 266 #ifdef __cplusplus 267 } 268 #endif 269 270 #endif /* _ECC_IMPL_H */