|
1 /* ********************************************************************* |
|
2 * |
|
3 * Sun elects to have this file available under and governed by the |
|
4 * Mozilla Public License Version 1.1 ("MPL") (see |
|
5 * http://www.mozilla.org/MPL/ for full license text). For the avoidance |
|
6 * of doubt and subject to the following, Sun also elects to allow |
|
7 * licensees to use this file under the MPL, the GNU General Public |
|
8 * License version 2 only or the Lesser General Public License version |
|
9 * 2.1 only. Any references to the "GNU General Public License version 2 |
|
10 * or later" or "GPL" in the following shall be construed to mean the |
|
11 * GNU General Public License version 2 only. Any references to the "GNU |
|
12 * Lesser General Public License version 2.1 or later" or "LGPL" in the |
|
13 * following shall be construed to mean the GNU Lesser General Public |
|
14 * License version 2.1 only. However, the following notice accompanied |
|
15 * the original version of this file: |
|
16 * |
|
17 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
|
18 * |
|
19 * The contents of this file are subject to the Mozilla Public License Version |
|
20 * 1.1 (the "License"); you may not use this file except in compliance with |
|
21 * the License. You may obtain a copy of the License at |
|
22 * http://www.mozilla.org/MPL/ |
|
23 * |
|
24 * Software distributed under the License is distributed on an "AS IS" basis, |
|
25 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
|
26 * for the specific language governing rights and limitations under the |
|
27 * License. |
|
28 * |
|
29 * The Original Code is the Netscape security libraries. |
|
30 * |
|
31 * The Initial Developer of the Original Code is |
|
32 * Netscape Communications Corporation. |
|
33 * Portions created by the Initial Developer are Copyright (C) 1994-2000 |
|
34 * the Initial Developer. All Rights Reserved. |
|
35 * |
|
36 * Contributor(s): |
|
37 * Dr Vipul Gupta <vipul.gupta@sun.com> and |
|
38 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories |
|
39 * |
|
40 * Alternatively, the contents of this file may be used under the terms of |
|
41 * either the GNU General Public License Version 2 or later (the "GPL"), or |
|
42 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
|
43 * in which case the provisions of the GPL or the LGPL are applicable instead |
|
44 * of those above. If you wish to allow use of your version of this file only |
|
45 * under the terms of either the GPL or the LGPL, and not to allow others to |
|
46 * use your version of this file under the terms of the MPL, indicate your |
|
47 * decision by deleting the provisions above and replace them with the notice |
|
48 * and other provisions required by the GPL or the LGPL. If you do not delete |
|
49 * the provisions above, a recipient may use your version of this file under |
|
50 * the terms of any one of the MPL, the GPL or the LGPL. |
|
51 * |
|
52 *********************************************************************** */ |
|
53 /* |
|
54 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
|
55 * Use is subject to license terms. |
|
56 */ |
|
57 |
|
58 #ifndef _ECC_IMPL_H |
|
59 #define _ECC_IMPL_H |
|
60 |
|
61 #pragma ident "%Z%%M% %I% %E% SMI" |
|
62 |
|
63 #ifdef __cplusplus |
|
64 extern "C" { |
|
65 #endif |
|
66 |
|
67 #include <sys/types.h> |
|
68 #include "ecl-exp.h" |
|
69 |
|
70 /* |
|
71 * Multi-platform definitions |
|
72 */ |
|
73 #ifdef __linux__ |
|
74 #define B_FALSE FALSE |
|
75 #define B_TRUE TRUE |
|
76 typedef unsigned char uint8_t; |
|
77 typedef unsigned long ulong_t; |
|
78 typedef enum { B_FALSE, B_TRUE } boolean_t; |
|
79 #endif /* __linux__ */ |
|
80 |
|
81 #ifdef _WIN32 |
|
82 typedef unsigned char uint8_t; |
|
83 typedef unsigned long ulong_t; |
|
84 typedef enum boolean { B_FALSE, B_TRUE } boolean_t; |
|
85 #endif /* _WIN32 */ |
|
86 |
|
87 #ifndef _KERNEL |
|
88 #include <stdlib.h> |
|
89 #endif /* _KERNEL */ |
|
90 |
|
91 #define EC_MAX_DIGEST_LEN 1024 /* max digest that can be signed */ |
|
92 #define EC_MAX_POINT_LEN 145 /* max len of DER encoded Q */ |
|
93 #define EC_MAX_VALUE_LEN 72 /* max len of ANSI X9.62 private value d */ |
|
94 #define EC_MAX_SIG_LEN 144 /* max signature len for supported curves */ |
|
95 #define EC_MIN_KEY_LEN 112 /* min key length in bits */ |
|
96 #define EC_MAX_KEY_LEN 571 /* max key length in bits */ |
|
97 #define EC_MAX_OID_LEN 10 /* max length of OID buffer */ |
|
98 |
|
99 /* |
|
100 * Various structures and definitions from NSS are here. |
|
101 */ |
|
102 |
|
103 #ifdef _KERNEL |
|
104 #define PORT_ArenaAlloc(a, n, f) kmem_alloc((n), (f)) |
|
105 #define PORT_ArenaZAlloc(a, n, f) kmem_zalloc((n), (f)) |
|
106 #define PORT_ArenaGrow(a, b, c, d) NULL |
|
107 #define PORT_ZAlloc(n, f) kmem_zalloc((n), (f)) |
|
108 #define PORT_Alloc(n, f) kmem_alloc((n), (f)) |
|
109 #else |
|
110 #define PORT_ArenaAlloc(a, n, f) malloc((n)) |
|
111 #define PORT_ArenaZAlloc(a, n, f) calloc(1, (n)) |
|
112 #define PORT_ArenaGrow(a, b, c, d) NULL |
|
113 #define PORT_ZAlloc(n, f) calloc(1, (n)) |
|
114 #define PORT_Alloc(n, f) malloc((n)) |
|
115 #endif |
|
116 |
|
117 #define PORT_NewArena(b) (char *)12345 |
|
118 #define PORT_ArenaMark(a) NULL |
|
119 #define PORT_ArenaUnmark(a, b) |
|
120 #define PORT_ArenaRelease(a, m) |
|
121 #define PORT_FreeArena(a, b) |
|
122 #define PORT_Strlen(s) strlen((s)) |
|
123 #define PORT_SetError(e) |
|
124 |
|
125 #define PRBool boolean_t |
|
126 #define PR_TRUE B_TRUE |
|
127 #define PR_FALSE B_FALSE |
|
128 |
|
129 #ifdef _KERNEL |
|
130 #define PORT_Assert ASSERT |
|
131 #define PORT_Memcpy(t, f, l) bcopy((f), (t), (l)) |
|
132 #else |
|
133 #define PORT_Assert assert |
|
134 #define PORT_Memcpy(t, f, l) memcpy((t), (f), (l)) |
|
135 #endif |
|
136 |
|
137 #define CHECK_OK(func) if (func == NULL) goto cleanup |
|
138 #define CHECK_SEC_OK(func) if (SECSuccess != (rv = func)) goto cleanup |
|
139 |
|
140 typedef enum { |
|
141 siBuffer = 0, |
|
142 siClearDataBuffer = 1, |
|
143 siCipherDataBuffer = 2, |
|
144 siDERCertBuffer = 3, |
|
145 siEncodedCertBuffer = 4, |
|
146 siDERNameBuffer = 5, |
|
147 siEncodedNameBuffer = 6, |
|
148 siAsciiNameString = 7, |
|
149 siAsciiString = 8, |
|
150 siDEROID = 9, |
|
151 siUnsignedInteger = 10, |
|
152 siUTCTime = 11, |
|
153 siGeneralizedTime = 12 |
|
154 } SECItemType; |
|
155 |
|
156 typedef struct SECItemStr SECItem; |
|
157 |
|
158 struct SECItemStr { |
|
159 SECItemType type; |
|
160 unsigned char *data; |
|
161 unsigned int len; |
|
162 }; |
|
163 |
|
164 typedef SECItem SECKEYECParams; |
|
165 |
|
166 typedef enum { ec_params_explicit, |
|
167 ec_params_named |
|
168 } ECParamsType; |
|
169 |
|
170 typedef enum { ec_field_GFp = 1, |
|
171 ec_field_GF2m |
|
172 } ECFieldType; |
|
173 |
|
174 struct ECFieldIDStr { |
|
175 int size; /* field size in bits */ |
|
176 ECFieldType type; |
|
177 union { |
|
178 SECItem prime; /* prime p for (GFp) */ |
|
179 SECItem poly; /* irreducible binary polynomial for (GF2m) */ |
|
180 } u; |
|
181 int k1; /* first coefficient of pentanomial or |
|
182 * the only coefficient of trinomial |
|
183 */ |
|
184 int k2; /* two remaining coefficients of pentanomial */ |
|
185 int k3; |
|
186 }; |
|
187 typedef struct ECFieldIDStr ECFieldID; |
|
188 |
|
189 struct ECCurveStr { |
|
190 SECItem a; /* contains octet stream encoding of |
|
191 * field element (X9.62 section 4.3.3) |
|
192 */ |
|
193 SECItem b; |
|
194 SECItem seed; |
|
195 }; |
|
196 typedef struct ECCurveStr ECCurve; |
|
197 |
|
198 typedef void PRArenaPool; |
|
199 |
|
200 struct ECParamsStr { |
|
201 PRArenaPool * arena; |
|
202 ECParamsType type; |
|
203 ECFieldID fieldID; |
|
204 ECCurve curve; |
|
205 SECItem base; |
|
206 SECItem order; |
|
207 int cofactor; |
|
208 SECItem DEREncoding; |
|
209 ECCurveName name; |
|
210 SECItem curveOID; |
|
211 }; |
|
212 typedef struct ECParamsStr ECParams; |
|
213 |
|
214 struct ECPublicKeyStr { |
|
215 ECParams ecParams; |
|
216 SECItem publicValue; /* elliptic curve point encoded as |
|
217 * octet stream. |
|
218 */ |
|
219 }; |
|
220 typedef struct ECPublicKeyStr ECPublicKey; |
|
221 |
|
222 struct ECPrivateKeyStr { |
|
223 ECParams ecParams; |
|
224 SECItem publicValue; /* encoded ec point */ |
|
225 SECItem privateValue; /* private big integer */ |
|
226 SECItem version; /* As per SEC 1, Appendix C, Section C.4 */ |
|
227 }; |
|
228 typedef struct ECPrivateKeyStr ECPrivateKey; |
|
229 |
|
230 typedef enum _SECStatus { |
|
231 SECBufferTooSmall = -3, |
|
232 SECWouldBlock = -2, |
|
233 SECFailure = -1, |
|
234 SECSuccess = 0 |
|
235 } SECStatus; |
|
236 |
|
237 #ifdef _KERNEL |
|
238 #define RNG_GenerateGlobalRandomBytes(p,l) ecc_knzero_random_generator((p), (l)) |
|
239 #else |
|
240 /* |
|
241 This function is no longer required because the random bytes are now |
|
242 supplied by the caller. Force a failure. |
|
243 VR |
|
244 #define RNG_GenerateGlobalRandomBytes(p,l) SECFailure |
|
245 */ |
|
246 #define RNG_GenerateGlobalRandomBytes(p,l) SECSuccess |
|
247 #endif |
|
248 #define CHECK_MPI_OK(func) if (MP_OKAY > (err = func)) goto cleanup |
|
249 #define MP_TO_SEC_ERROR(err) |
|
250 |
|
251 #define SECITEM_TO_MPINT(it, mp) \ |
|
252 CHECK_MPI_OK(mp_read_unsigned_octets((mp), (it).data, (it).len)) |
|
253 |
|
254 extern int ecc_knzero_random_generator(uint8_t *, size_t); |
|
255 extern ulong_t soft_nzero_random_generator(uint8_t *, ulong_t); |
|
256 |
|
257 extern SECStatus EC_DecodeParams(const SECItem *, ECParams **, int); |
|
258 extern SECItem * SECITEM_AllocItem(PRArenaPool *, SECItem *, unsigned int, int); |
|
259 extern SECStatus SECITEM_CopyItem(PRArenaPool *, SECItem *, const SECItem *, |
|
260 int); |
|
261 extern void SECITEM_FreeItem(SECItem *, boolean_t); |
|
262 extern SECStatus EC_NewKey(ECParams *ecParams, ECPrivateKey **privKey, const unsigned char* random, int randomlen, int); |
|
263 extern SECStatus EC_NewKeyFromSeed(ECParams *ecParams, ECPrivateKey **privKey, |
|
264 const unsigned char *seed, int seedlen, int kmflag); |
|
265 extern SECStatus ECDSA_SignDigest(ECPrivateKey *, SECItem *, const SECItem *, |
|
266 const unsigned char* randon, int randomlen, int); |
|
267 extern SECStatus ECDSA_SignDigestWithSeed(ECPrivateKey *, SECItem *, |
|
268 const SECItem *, const unsigned char *seed, int seedlen, int kmflag); |
|
269 extern SECStatus ECDSA_VerifyDigest(ECPublicKey *, const SECItem *, |
|
270 const SECItem *, int); |
|
271 extern SECStatus ECDH_Derive(SECItem *, ECParams *, SECItem *, boolean_t, |
|
272 SECItem *, int); |
|
273 |
|
274 #ifdef __cplusplus |
|
275 } |
|
276 #endif |
|
277 |
|
278 #endif /* _ECC_IMPL_H */ |