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