1 /*
   2  * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * Use is subject to license terms.
   4  *
   5  * This library is free software; you can redistribute it and/or
   6  * modify it under the terms of the GNU Lesser General Public
   7  * License as published by the Free Software Foundation; either
   8  * version 2.1 of the License, or (at your option) any later version.
   9  *
  10  * This library is distributed in the hope that it will be useful,
  11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13  * Lesser General Public License for more details.
  14  *
  15  * You should have received a copy of the GNU Lesser General Public License
  16  * along with this library; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* *********************************************************************
  25  *
  26  * The Original Code is the Netscape security libraries.
  27  *
  28  * The Initial Developer of the Original Code is
  29  * Netscape Communications Corporation.
  30  * Portions created by the Initial Developer are Copyright (C) 1994-2000
  31  * the Initial Developer. All Rights Reserved.
  32  *
  33  * Contributor(s):
  34  *   Dr Vipul Gupta <vipul.gupta@sun.com> and
  35  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
  36  *
  37  *********************************************************************** */
  38 
  39 #ifndef _ECC_IMPL_H
  40 #define _ECC_IMPL_H
  41 
  42 #ifdef __cplusplus
  43 extern "C" {
  44 #endif
  45 
  46 #include <sys/types.h>
  47 #include "ecl-exp.h"
  48 
  49 /*
  50  * Multi-platform definitions
  51  */
  52 #ifdef __linux__
  53 #define B_FALSE FALSE
  54 #define B_TRUE TRUE
  55 typedef unsigned char uint8_t;
  56 typedef unsigned long ulong_t;
  57 typedef enum { B_FALSE, B_TRUE } boolean_t;
  58 #endif /* __linux__ */
  59 
  60 #ifdef _ALLBSD_SOURCE
  61 #include <stdint.h>
  62 #define B_FALSE FALSE
  63 #define B_TRUE TRUE
  64 typedef unsigned long ulong_t;
  65 typedef enum boolean { B_FALSE, B_TRUE } boolean_t;
  66 #endif /* _ALLBSD_SOURCE */
  67 
  68 #ifdef _WIN32
  69 typedef unsigned char uint8_t;
  70 typedef unsigned long ulong_t;
  71 typedef enum boolean { B_FALSE, B_TRUE } boolean_t;
  72 #define strdup _strdup          /* Replace POSIX name with ISO C++ name */
  73 #endif /* _WIN32 */
  74 
  75 #ifndef _KERNEL
  76 #include <stdlib.h>
  77 #endif  /* _KERNEL */
  78 
  79 #define EC_MAX_DIGEST_LEN 1024  /* max digest that can be signed */
  80 #define EC_MAX_POINT_LEN 145    /* max len of DER encoded Q */
  81 #define EC_MAX_VALUE_LEN 72     /* max len of ANSI X9.62 private value d */
  82 #define EC_MAX_SIG_LEN 144      /* max signature len for supported curves */
  83 #define EC_MIN_KEY_LEN  112     /* min key length in bits */
  84 #define EC_MAX_KEY_LEN  571     /* max key length in bits */
  85 #define EC_MAX_OID_LEN 10       /* max length of OID buffer */
  86 
  87 /*
  88  * Various structures and definitions from NSS are here.
  89  */
  90 
  91 #ifdef _KERNEL
  92 #define PORT_ArenaAlloc(a, n, f)        kmem_alloc((n), (f))
  93 #define PORT_ArenaZAlloc(a, n, f)       kmem_zalloc((n), (f))
  94 #define PORT_ArenaGrow(a, b, c, d)      NULL
  95 #define PORT_ZAlloc(n, f)               kmem_zalloc((n), (f))
  96 #define PORT_Alloc(n, f)                kmem_alloc((n), (f))
  97 #else
  98 #define PORT_ArenaAlloc(a, n, f)        malloc((n))
  99 #define PORT_ArenaZAlloc(a, n, f)       calloc(1, (n))
 100 #define PORT_ArenaGrow(a, b, c, d)      NULL
 101 #define PORT_ZAlloc(n, f)               calloc(1, (n))
 102 #define PORT_Alloc(n, f)                malloc((n))
 103 #endif
 104 
 105 #define PORT_NewArena(b)                (char *)12345
 106 #define PORT_ArenaMark(a)               NULL
 107 #define PORT_ArenaUnmark(a, b)
 108 #define PORT_ArenaRelease(a, m)
 109 #define PORT_FreeArena(a, b)
 110 #define PORT_Strlen(s)                  strlen((s))
 111 #define PORT_SetError(e)
 112 
 113 #define PRBool                          boolean_t
 114 #define PR_TRUE                         B_TRUE
 115 #define PR_FALSE                        B_FALSE
 116 
 117 #ifdef _KERNEL
 118 #define PORT_Assert                     ASSERT
 119 #define PORT_Memcpy(t, f, l)            bcopy((f), (t), (l))
 120 #else
 121 #define PORT_Assert                     assert
 122 #define PORT_Memcpy(t, f, l)            memcpy((t), (f), (l))
 123 #endif
 124 
 125 #define CHECK_OK(func) if (func == NULL) goto cleanup
 126 #define CHECK_SEC_OK(func) if (SECSuccess != (rv = func)) goto cleanup
 127 
 128 typedef enum {
 129         siBuffer = 0,
 130         siClearDataBuffer = 1,
 131         siCipherDataBuffer = 2,
 132         siDERCertBuffer = 3,
 133         siEncodedCertBuffer = 4,
 134         siDERNameBuffer = 5,
 135         siEncodedNameBuffer = 6,
 136         siAsciiNameString = 7,
 137         siAsciiString = 8,
 138         siDEROID = 9,
 139         siUnsignedInteger = 10,
 140         siUTCTime = 11,
 141         siGeneralizedTime = 12
 142 } SECItemType;
 143 
 144 typedef struct SECItemStr SECItem;
 145 
 146 struct SECItemStr {
 147         SECItemType type;
 148         unsigned char *data;
 149         unsigned int len;
 150 };
 151 
 152 typedef SECItem SECKEYECParams;
 153 
 154 typedef enum { ec_params_explicit,
 155                ec_params_named
 156 } ECParamsType;
 157 
 158 typedef enum { ec_field_GFp = 1,
 159                ec_field_GF2m
 160 } ECFieldType;
 161 
 162 struct ECFieldIDStr {
 163     int         size;   /* field size in bits */
 164     ECFieldType type;
 165     union {
 166         SECItem  prime; /* prime p for (GFp) */
 167         SECItem  poly;  /* irreducible binary polynomial for (GF2m) */
 168     } u;
 169     int         k1;     /* first coefficient of pentanomial or
 170                          * the only coefficient of trinomial
 171                          */
 172     int         k2;     /* two remaining coefficients of pentanomial */
 173     int         k3;
 174 };
 175 typedef struct ECFieldIDStr ECFieldID;
 176 
 177 struct ECCurveStr {
 178         SECItem a;      /* contains octet stream encoding of
 179                          * field element (X9.62 section 4.3.3)
 180                          */
 181         SECItem b;
 182         SECItem seed;
 183 };
 184 typedef struct ECCurveStr ECCurve;
 185 
 186 typedef void PRArenaPool;
 187 
 188 struct ECParamsStr {
 189     PRArenaPool * arena;
 190     ECParamsType  type;
 191     ECFieldID     fieldID;
 192     ECCurve       curve;
 193     SECItem       base;
 194     SECItem       order;
 195     int           cofactor;
 196     SECItem       DEREncoding;
 197     ECCurveName   name;
 198     SECItem       curveOID;
 199 };
 200 typedef struct ECParamsStr ECParams;
 201 
 202 struct ECPublicKeyStr {
 203     ECParams ecParams;
 204     SECItem publicValue;   /* elliptic curve point encoded as
 205                             * octet stream.
 206                             */
 207 };
 208 typedef struct ECPublicKeyStr ECPublicKey;
 209 
 210 struct ECPrivateKeyStr {
 211     ECParams ecParams;
 212     SECItem publicValue;   /* encoded ec point */
 213     SECItem privateValue;  /* private big integer */
 214     SECItem version;       /* As per SEC 1, Appendix C, Section C.4 */
 215 };
 216 typedef struct ECPrivateKeyStr ECPrivateKey;
 217 
 218 typedef enum _SECStatus {
 219         SECBufferTooSmall = -3,
 220         SECWouldBlock = -2,
 221         SECFailure = -1,
 222         SECSuccess = 0
 223 } SECStatus;
 224 
 225 #ifdef _KERNEL
 226 #define RNG_GenerateGlobalRandomBytes(p,l) ecc_knzero_random_generator((p), (l))
 227 #else
 228 /*
 229  This function is no longer required because the random bytes are now
 230  supplied by the caller. Force a failure.
 231 */
 232 #define RNG_GenerateGlobalRandomBytes(p,l) SECFailure
 233 #endif
 234 #define CHECK_MPI_OK(func) if (MP_OKAY > (err = func)) goto cleanup
 235 #define MP_TO_SEC_ERROR(err)
 236 
 237 #define SECITEM_TO_MPINT(it, mp)                                        \
 238         CHECK_MPI_OK(mp_read_unsigned_octets((mp), (it).data, (it).len))
 239 
 240 extern int ecc_knzero_random_generator(uint8_t *, size_t);
 241 extern ulong_t soft_nzero_random_generator(uint8_t *, ulong_t);
 242 
 243 extern SECStatus EC_DecodeParams(const SECItem *, ECParams **, int);
 244 extern SECItem * SECITEM_AllocItem(PRArenaPool *, SECItem *, unsigned int, int);
 245 extern SECStatus SECITEM_CopyItem(PRArenaPool *, SECItem *, const SECItem *,
 246     int);
 247 extern void SECITEM_FreeItem(SECItem *, boolean_t);
 248 /* This function has been modified to accept an array of random bytes */
 249 extern SECStatus EC_NewKey(ECParams *ecParams, ECPrivateKey **privKey,
 250     const unsigned char* random, int randomlen, int);
 251 /* This function has been modified to accept an array of random bytes */
 252 extern SECStatus ECDSA_SignDigest(ECPrivateKey *, SECItem *, const SECItem *,
 253     const unsigned char* random, int randomlen, int);
 254 extern SECStatus ECDSA_VerifyDigest(ECPublicKey *, const SECItem *,
 255     const SECItem *, int);
 256 extern SECStatus ECDH_Derive(SECItem *, ECParams *, SECItem *, boolean_t,
 257     SECItem *, int);
 258 
 259 #ifdef  __cplusplus
 260 }
 261 #endif
 262 
 263 #endif /* _ECC_IMPL_H */