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