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