1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is the Netscape security libraries.
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.
22 * Dr Vipul Gupta <vipul.gupta@sun.com> and
23 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
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.
37 * ***** END LICENSE BLOCK ***** */
39 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
40 * Use is subject to license terms.
42 * Sun elects to use this software under the MPL license.
52 #include <sys/types.h>
55 #include <security/cryptoki.h>
56 #include <security/pkcs11t.h>
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 */
68 * Various structures and definitions from NSS are here.
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))
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))
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)
93 #define PRBool boolean_t
94 #define PR_TRUE B_TRUE
95 #define PR_FALSE B_FALSE
98 #define PORT_Assert ASSERT
99 #define PORT_Memcpy(t, f, l) bcopy((f), (t), (l))
101 #define PORT_Assert assert
102 #define PORT_Memcpy(t, f, l) memcpy((t), (f), (l))
105 #define CHECK_OK(func) if (func == NULL) goto cleanup
106 #define CHECK_SEC_OK(func) if (SECSuccess != (rv = func)) goto cleanup
110 siClearDataBuffer
= 1,
111 siCipherDataBuffer
= 2,
113 siEncodedCertBuffer
= 4,
115 siEncodedNameBuffer
= 6,
116 siAsciiNameString
= 7,
119 siUnsignedInteger
= 10,
121 siGeneralizedTime
= 12
124 typedef struct SECItemStr SECItem
;
132 typedef SECItem SECKEYECParams
;
134 typedef enum { ec_params_explicit
,
138 typedef enum { ec_field_GFp
= 1,
142 struct ECFieldIDStr
{
143 int size
; /* field size in bits */
146 SECItem prime
; /* prime p for (GFp) */
147 SECItem poly
; /* irreducible binary polynomial for (GF2m) */
149 int k1
; /* first coefficient of pentanomial or
150 * the only coefficient of trinomial
152 int k2
; /* two remaining coefficients of pentanomial */
155 typedef struct ECFieldIDStr ECFieldID
;
158 SECItem a
; /* contains octet stream encoding of
159 * field element (X9.62 section 4.3.3)
164 typedef struct ECCurveStr ECCurve
;
166 typedef void PRArenaPool
;
180 typedef struct ECParamsStr ECParams
;
182 struct ECPublicKeyStr
{
184 SECItem publicValue
; /* elliptic curve point encoded as
188 typedef struct ECPublicKeyStr ECPublicKey
;
190 struct ECPrivateKeyStr
{
192 SECItem publicValue
; /* encoded ec point */
193 SECItem privateValue
; /* private big integer */
194 SECItem version
; /* As per SEC 1, Appendix C, Section C.4 */
196 typedef struct ECPrivateKeyStr ECPrivateKey
;
198 typedef enum _SECStatus
{
199 SECBufferTooSmall
= -3,
206 #define RNG_GenerateGlobalRandomBytes(p,l) ecc_knzero_random_generator((p), (l))
208 #define RNG_GenerateGlobalRandomBytes(p,l) \
209 (pkcs11_get_nzero_urandom((p), (l)) < 0 ? CKR_DEVICE_ERROR : CKR_OK)
211 #define CHECK_MPI_OK(func) if (MP_OKAY > (err = func)) goto cleanup
212 #define MP_TO_SEC_ERROR(err)
214 #define SECITEM_TO_MPINT(it, mp) \
215 CHECK_MPI_OK(mp_read_unsigned_octets((mp), (it).data, (it).len))
217 extern int ecc_knzero_random_generator(uint8_t *, size_t);
218 extern int pkcs11_get_nzero_urandom(void *, size_t);
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
*,
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
*,
228 extern SECStatus
ECDSA_VerifyDigest(ECPublicKey
*, const SECItem
*,
229 const SECItem
*, int);
230 extern SECStatus
ECDH_Derive(SECItem
*, ECParams
*, SECItem
*, boolean_t
,
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);
243 #endif /* _ECC_IMPL_H */