1N/A/* BEGIN CSTYLED */
1N/A/* ***** BEGIN LICENSE BLOCK *****
1N/A * Version: MPL 1.1/GPL 2.0/LGPL 2.1
1N/A *
1N/A * The contents of this file are subject to the Mozilla Public License Version
1N/A * 1.1 (the "License"); you may not use this file except in compliance with
1N/A * the License. You may obtain a copy of the License at
1N/A * http://www.mozilla.org/MPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS IS" basis,
1N/A * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1N/A * for the specific language governing rights and limitations under the
1N/A * License.
1N/A *
1N/A * The Original Code is the Netscape security libraries.
1N/A *
1N/A * The Initial Developer of the Original Code is
1N/A * Netscape Communications Corporation.
1N/A * Portions created by the Initial Developer are Copyright (C) 1994-2000
1N/A * the Initial Developer. All Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A * Dr Vipul Gupta <vipul.gupta@sun.com> and
1N/A * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
1N/A *
1N/A * Alternatively, the contents of this file may be used under the terms of
1N/A * either the GNU General Public License Version 2 or later (the "GPL"), or
1N/A * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
1N/A * in which case the provisions of the GPL or the LGPL are applicable instead
1N/A * of those above. If you wish to allow use of your version of this file only
1N/A * under the terms of either the GPL or the LGPL, and not to allow others to
1N/A * use your version of this file under the terms of the MPL, indicate your
1N/A * decision by deleting the provisions above and replace them with the notice
1N/A * and other provisions required by the GPL or the LGPL. If you do not delete
1N/A * the provisions above, a recipient may use your version of this file under
1N/A * the terms of any one of the MPL, the GPL or the LGPL.
1N/A *
1N/A * ***** END LICENSE BLOCK ***** */
2N/A
1N/A/*
2N/A * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
1N/A *
1N/A * Sun elects to use this software under the MPL license.
1N/A */
1N/A
1N/A#ifndef _ECC_IMPL_H
1N/A#define _ECC_IMPL_H
1N/A
1N/A#ifdef __cplusplus
1N/Aextern "C" {
1N/A#endif
1N/A
1N/A#include <sys/types.h>
1N/A#include "ecl-exp.h"
1N/A#ifndef _KERNEL
1N/A#include <security/cryptoki.h>
1N/A#include <security/pkcs11t.h>
1N/A#endif /* _KERNEL */
1N/A
1N/A#define EC_MAX_DIGEST_LEN 1024 /* max digest that can be signed */
1N/A#define EC_MAX_POINT_LEN 145 /* max len of DER encoded Q */
1N/A#define EC_MAX_VALUE_LEN 72 /* max len of ANSI X9.62 private value d */
1N/A#define EC_MAX_SIG_LEN 144 /* max signature len for supported curves */
1N/A#define EC_MIN_KEY_LEN 112 /* min key length in bits */
1N/A#define EC_MAX_KEY_LEN 571 /* max key length in bits */
1N/A#define EC_MAX_OID_LEN 10 /* max length of OID buffer */
1N/A
1N/A/*
1N/A * Various structures and definitions from NSS are here.
1N/A */
1N/A
1N/A#ifdef _KERNEL
1N/A#define PORT_ArenaAlloc(a, n, f) kmem_alloc((n), (f))
1N/A#define PORT_ArenaZAlloc(a, n, f) kmem_zalloc((n), (f))
1N/A#define PORT_ArenaGrow(a, b, c, d) NULL
1N/A#define PORT_ZAlloc(n, f) kmem_zalloc((n), (f))
1N/A#define PORT_Alloc(n, f) kmem_alloc((n), (f))
1N/A#define PORT_ZFree(x, n) (bzero((x), (n)), kmem_free((x), (n)))
1N/A#define PORT_Free(x, n) kmem_free((x), (n))
1N/A#else
1N/A#define PORT_ArenaAlloc(a, n, f) malloc((n))
1N/A#define PORT_ArenaZAlloc(a, n, f) calloc(1, (n))
1N/A#define PORT_ArenaGrow(a, b, c, d) NULL
1N/A#define PORT_ZAlloc(n, f) calloc(1, (n))
1N/A#define PORT_Alloc(n, f) malloc((n))
1N/A#define PORT_ZFree(x, n) (memset((x), 0, (n)), free((x)))
1N/A#define PORT_Free(x, n) free((x))
1N/A#endif
1N/A
1N/A#define PORT_NewArena(b) (char *)12345
1N/A#define PORT_ArenaMark(a) NULL
1N/A#define PORT_ArenaUnmark(a, b)
1N/A#define PORT_ArenaRelease(a, m)
1N/A#define PORT_FreeArena(a, b)
1N/A#define PORT_Strlen(s) strlen((s))
1N/A#define PORT_SetError(e)
1N/A
1N/A#define PRBool boolean_t
1N/A#define PR_TRUE B_TRUE
1N/A#define PR_FALSE B_FALSE
1N/A
1N/A#ifdef _KERNEL
1N/A#define PORT_Assert ASSERT
1N/A#define PORT_Memcpy(t, f, l) bcopy((f), (t), (l))
1N/A#else
1N/A#define PORT_Assert assert
1N/A#define PORT_Memcpy(t, f, l) memcpy((t), (f), (l))
1N/A#endif
1N/A
1N/A#define CHECK_OK(func) if (func == NULL) goto cleanup
1N/A#define CHECK_SEC_OK(func) if (SECSuccess != (rv = func)) goto cleanup
1N/A
1N/Atypedef enum {
1N/A siBuffer = 0,
1N/A siClearDataBuffer = 1,
1N/A siCipherDataBuffer = 2,
1N/A siDERCertBuffer = 3,
1N/A siEncodedCertBuffer = 4,
1N/A siDERNameBuffer = 5,
1N/A siEncodedNameBuffer = 6,
1N/A siAsciiNameString = 7,
1N/A siAsciiString = 8,
1N/A siDEROID = 9,
1N/A siUnsignedInteger = 10,
1N/A siUTCTime = 11,
1N/A siGeneralizedTime = 12
1N/A} SECItemType;
1N/A
1N/Atypedef struct SECItemStr SECItem;
1N/A
1N/Astruct SECItemStr {
1N/A SECItemType type;
1N/A unsigned char *data;
1N/A unsigned int len;
1N/A};
1N/A
1N/Atypedef SECItem SECKEYECParams;
1N/A
1N/Atypedef enum { ec_params_explicit,
1N/A ec_params_named
1N/A} ECParamsType;
1N/A
1N/Atypedef enum { ec_field_GFp = 1,
1N/A ec_field_GF2m
1N/A} ECFieldType;
1N/A
1N/Astruct ECFieldIDStr {
1N/A int size; /* field size in bits */
1N/A ECFieldType type;
1N/A union {
1N/A SECItem prime; /* prime p for (GFp) */
1N/A SECItem poly; /* irreducible binary polynomial for (GF2m) */
1N/A } u;
1N/A int k1; /* first coefficient of pentanomial or
1N/A * the only coefficient of trinomial
1N/A */
1N/A int k2; /* two remaining coefficients of pentanomial */
1N/A int k3;
1N/A};
1N/Atypedef struct ECFieldIDStr ECFieldID;
1N/A
1N/Astruct ECCurveStr {
1N/A SECItem a; /* contains octet stream encoding of
1N/A * field element (X9.62 section 4.3.3)
1N/A */
1N/A SECItem b;
1N/A SECItem seed;
1N/A};
1N/Atypedef struct ECCurveStr ECCurve;
1N/A
1N/Atypedef void PRArenaPool;
1N/A
1N/Astruct ECParamsStr {
1N/A PRArenaPool * arena;
1N/A ECParamsType type;
1N/A ECFieldID fieldID;
1N/A ECCurve curve;
1N/A SECItem base;
1N/A SECItem order;
1N/A int cofactor;
1N/A SECItem DEREncoding;
1N/A ECCurveName name;
1N/A SECItem curveOID;
1N/A};
1N/Atypedef struct ECParamsStr ECParams;
1N/A
1N/Astruct ECPublicKeyStr {
1N/A ECParams ecParams;
1N/A SECItem publicValue; /* elliptic curve point encoded as
1N/A * octet stream.
1N/A */
1N/A};
1N/Atypedef struct ECPublicKeyStr ECPublicKey;
1N/A
1N/Astruct ECPrivateKeyStr {
1N/A ECParams ecParams;
1N/A SECItem publicValue; /* encoded ec point */
1N/A SECItem privateValue; /* private big integer */
1N/A SECItem version; /* As per SEC 1, Appendix C, Section C.4 */
1N/A};
1N/Atypedef struct ECPrivateKeyStr ECPrivateKey;
1N/A
1N/Atypedef enum _SECStatus {
1N/A SECInvalidArgs = -4,
1N/A SECBufferTooSmall = -3,
1N/A SECWouldBlock = -2,
1N/A SECFailure = -1,
1N/A SECSuccess = 0
1N/A} SECStatus;
1N/A
1N/A#ifdef _KERNEL
2N/A#include <sys/random.h>
2N/A#define RNG_GenerateGlobalRandomBytes(p,l) \
2N/A random_get_nzero_pseudo_bytes((p), (l))
1N/A#else
2N/A#include <cryptoutil.h>
1N/A#define RNG_GenerateGlobalRandomBytes(p,l) \
1N/A (pkcs11_get_nzero_urandom((p), (l)) < 0 ? CKR_DEVICE_ERROR : CKR_OK)
1N/A#endif
1N/A#define CHECK_MPI_OK(func) if (MP_OKAY > (err = func)) goto cleanup
1N/A#define MP_TO_SEC_ERROR(err)
1N/A
1N/A#define SECITEM_TO_MPINT(it, mp) \
1N/A CHECK_MPI_OK(mp_read_unsigned_octets((mp), (it).data, (it).len))
1N/A
1N/Aextern SECStatus EC_DecodeParams(const SECItem *, ECParams **, int);
1N/Aextern SECItem * SECITEM_AllocItem(PRArenaPool *, SECItem *, unsigned int, int);
1N/Aextern SECStatus SECITEM_CopyItem(PRArenaPool *, SECItem *, const SECItem *,
1N/A int);
1N/Aextern void SECITEM_FreeItem(SECItem *, boolean_t);
1N/Aextern SECStatus EC_NewKey(ECParams *ecParams, ECPrivateKey **privKey, int);
1N/Aextern SECStatus ECDSA_SignDigest(ECPrivateKey *, SECItem *, const SECItem *,
1N/A int);
1N/Aextern SECStatus ECDSA_VerifyDigest(ECPublicKey *, const SECItem *,
1N/A const SECItem *, int);
1N/Aextern SECStatus ECDH_Derive(SECItem *, ECParams *, SECItem *, boolean_t,
1N/A SECItem *, int);
1N/Aextern SECStatus EC_CopyParams(PRArenaPool *, ECParams *, const ECParams *);
1N/Aextern SECStatus EC_ValidatePublicKey(ECParams *, SECItem *, int);
1N/Aextern SECStatus ECDSA_SignDigestWithSeed(ECPrivateKey *, SECItem *,
1N/A const SECItem *, const unsigned char *, const int kblen, int);
1N/Aextern SECStatus ec_NewKey(ECParams *, ECPrivateKey **,
1N/A const unsigned char *, int, int);
1N/A
1N/Aextern void EC_FreeParams(ECParams *, boolean_t);
1N/Aextern SECStatus EC_NewKeyFromSeed(ECParams *ecParams, ECPrivateKey **privKey,
1N/A const unsigned char *seed, int seedlen, int);
1N/Aextern void EC_FreePrivateKey(ECPrivateKey *, boolean_t);
1N/Aextern void EC_FreePublicKey(ECPublicKey *, boolean_t);
1N/Aextern void EC_FreeDerivedKey(SECItem *, boolean_t);
1N/Aextern void SECITEM_ZfreeItem(SECItem *, boolean_t);
1N/A
1N/A#ifdef __cplusplus
1N/A}
1N/A#endif
1N/A
1N/A#endif /* _ECC_IMPL_H */
1N/A/* END CSTYLED */