Back to index

lightning-sunbird  0.9+nobinonly
blapit.h
Go to the documentation of this file.
00001 /*
00002  * blapit.h - public data structures for the crypto library
00003  *
00004  * ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is the Netscape security libraries.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Dr Vipul Gupta <vipul.gupta@sun.com> and
00026  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 /* $Id: blapit.h,v 1.18 2005/11/07 18:44:21 wtchang%redhat.com Exp $ */
00042 
00043 #ifndef _BLAPIT_H_
00044 #define _BLAPIT_H_
00045 
00046 #include "seccomon.h"
00047 #include "prlink.h"
00048 #include "plarena.h"
00049 #include "ecl-exp.h"
00050 
00051 
00052 /* RC2 operation modes */
00053 #define NSS_RC2                    0
00054 #define NSS_RC2_CBC         1
00055 
00056 /* RC5 operation modes */
00057 #define NSS_RC5                 0
00058 #define NSS_RC5_CBC             1
00059 
00060 /* DES operation modes */
00061 #define NSS_DES                    0
00062 #define NSS_DES_CBC         1
00063 #define NSS_DES_EDE3        2
00064 #define NSS_DES_EDE3_CBC    3
00065 
00066 #define DES_KEY_LENGTH             8      /* Bytes */
00067 
00068 /* AES operation modes */
00069 #define NSS_AES                 0
00070 #define NSS_AES_CBC             1
00071 
00072 #define DSA_SIGNATURE_LEN   40     /* Bytes */
00073 #define DSA_SUBPRIME_LEN    20     /* Bytes */
00074 
00075 /* XXX We shouldn't have to hard code this limit. For
00076  * now, this is the quickest way to support ECDSA signature
00077  * processing (ECDSA signature lengths depend on curve
00078  * size). This limit is sufficient for curves upto
00079  * 576 bits.
00080  */
00081 #define MAX_ECKEY_LEN               72    /* Bytes */
00082 
00083 /*
00084  * Number of bytes each hash algorithm produces
00085  */
00086 #define MD2_LENGTH          16     /* Bytes */
00087 #define MD5_LENGTH          16     /* Bytes */
00088 #define SHA1_LENGTH         20     /* Bytes */
00089 #define SHA256_LENGTH              32     /* bytes */
00090 #define SHA384_LENGTH              48     /* bytes */
00091 #define SHA512_LENGTH              64     /* bytes */
00092 #define HASH_LENGTH_MAX         SHA512_LENGTH
00093 
00094 /*
00095  * Input block size for each hash algorithm.
00096  */
00097 
00098 #define MD2_BLOCK_LENGTH     64    /* bytes */
00099 #define MD5_BLOCK_LENGTH     64    /* bytes */
00100 #define SHA1_BLOCK_LENGTH    64    /* bytes */
00101 #define SHA256_BLOCK_LENGTH         64    /* bytes */
00102 #define SHA384_BLOCK_LENGTH        128    /* bytes */
00103 #define SHA512_BLOCK_LENGTH        128    /* bytes */
00104 #define HASH_BLOCK_LENGTH_MAX      SHA512_BLOCK_LENGTH
00105 
00106 #define AES_KEY_WRAP_IV_BYTES    8
00107 #define AES_KEY_WRAP_BLOCK_SIZE  8  /* bytes */
00108 #define AES_BLOCK_SIZE          16  /* bytes */
00109 
00110 #define NSS_FREEBL_DEFAULT_CHUNKSIZE 2048
00111 
00112 /*
00113  * these values come from the intial key size limits from the PKCS #11
00114  * module. They may be aribitarily adjusted to any value freebl supports
00115  * RSA_MAX_MODULUS_BITS is not defined since there is only memory constraints
00116  * on the largest RSA Modulus that PKCS #11 or freebl may support.
00117  */
00118 #define RSA_MIN_MODULUS_BITS   128
00119 #define DH_MIN_P_BITS              128
00120 #define DH_MAX_P_BITS         2236
00121 
00122 /*
00123  * The FIPS 186 algorithm for generating primes P and Q allows only 9
00124  * distinct values for the length of P, and only one value for the
00125  * length of Q.
00126  * The algorithm uses a variable j to indicate which of the 9 lengths
00127  * of P is to be used.
00128  * The following table relates j to the lengths of P and Q in bits.
00129  *
00130  *     j      bits in P     bits in Q
00131  *     _      _________     _________
00132  *     0       512          160
00133  *     1       576          160
00134  *     2       640          160
00135  *     3       704          160
00136  *     4       768          160
00137  *     5       832          160
00138  *     6       896          160
00139  *     7       960          160
00140  *     8      1024          160
00141  *
00142  * The FIPS-186 compliant PQG generator takes j as an input parameter.
00143  */
00144 
00145 #define DSA_Q_BITS       160
00146 #define DSA_MAX_P_BITS      1024
00147 #define DSA_MIN_P_BITS       512
00148 
00149 /*
00150  * function takes desired number of bits in P,
00151  * returns index (0..8) or -1 if number of bits is invalid.
00152  */
00153 #define PQG_PBITS_TO_INDEX(bits) \
00154     (((bits) < 512 || (bits) > 1024 || (bits) % 64) ? \
00155     -1 : (int)((bits)-512)/64)
00156 
00157 /*
00158  * function takes index (0-8)
00159  * returns number of bits in P for that index, or -1 if index is invalid.
00160  */
00161 #define PQG_INDEX_TO_PBITS(j) (((unsigned)(j) > 8) ? -1 : (512 + 64 * (j)))
00162 
00163 
00164 /***************************************************************************
00165 ** Opaque objects 
00166 */
00167 
00168 struct DESContextStr        ;
00169 struct RC2ContextStr        ;
00170 struct RC4ContextStr        ;
00171 struct RC5ContextStr        ;
00172 struct AESContextStr        ;
00173 struct MD2ContextStr        ;
00174 struct MD5ContextStr        ;
00175 struct SHA1ContextStr       ;
00176 struct SHA256ContextStr     ;
00177 struct SHA512ContextStr     ;
00178 struct AESKeyWrapContextStr ;
00179 
00180 typedef struct DESContextStr        DESContext;
00181 typedef struct RC2ContextStr        RC2Context;
00182 typedef struct RC4ContextStr        RC4Context;
00183 typedef struct RC5ContextStr        RC5Context;
00184 typedef struct AESContextStr        AESContext;
00185 typedef struct MD2ContextStr        MD2Context;
00186 typedef struct MD5ContextStr        MD5Context;
00187 typedef struct SHA1ContextStr       SHA1Context;
00188 typedef struct SHA256ContextStr     SHA256Context;
00189 typedef struct SHA512ContextStr     SHA512Context;
00190 /* SHA384Context is really a SHA512ContextStr.  This is not a mistake. */
00191 typedef struct SHA512ContextStr     SHA384Context;
00192 typedef struct AESKeyWrapContextStr AESKeyWrapContext;
00193 
00194 /***************************************************************************
00195 ** RSA Public and Private Key structures
00196 */
00197 
00198 /* member names from PKCS#1, section 7.1 */
00199 struct RSAPublicKeyStr {
00200     PRArenaPool * arena;
00201     SECItem modulus;
00202     SECItem publicExponent;
00203 };
00204 typedef struct RSAPublicKeyStr RSAPublicKey;
00205 
00206 /* member names from PKCS#1, section 7.2 */
00207 struct RSAPrivateKeyStr {
00208     PRArenaPool * arena;
00209     SECItem version;
00210     SECItem modulus;
00211     SECItem publicExponent;
00212     SECItem privateExponent;
00213     SECItem prime1;
00214     SECItem prime2;
00215     SECItem exponent1;
00216     SECItem exponent2;
00217     SECItem coefficient;
00218 };
00219 typedef struct RSAPrivateKeyStr RSAPrivateKey;
00220 
00221 
00222 /***************************************************************************
00223 ** DSA Public and Private Key and related structures
00224 */
00225 
00226 struct PQGParamsStr {
00227     PRArenaPool *arena;
00228     SECItem prime;    /* p */
00229     SECItem subPrime; /* q */
00230     SECItem base;     /* g */
00231     /* XXX chrisk: this needs to be expanded to hold j and validationParms (RFC2459 7.3.2) */
00232 };
00233 typedef struct PQGParamsStr PQGParams;
00234 
00235 struct PQGVerifyStr {
00236     PRArenaPool * arena;    /* includes this struct, seed, & h. */
00237     unsigned int  counter;
00238     SECItem       seed;
00239     SECItem       h;
00240 };
00241 typedef struct PQGVerifyStr PQGVerify;
00242 
00243 struct DSAPublicKeyStr {
00244     PQGParams params;
00245     SECItem publicValue;
00246 };
00247 typedef struct DSAPublicKeyStr DSAPublicKey;
00248 
00249 struct DSAPrivateKeyStr {
00250     PQGParams params;
00251     SECItem publicValue;
00252     SECItem privateValue;
00253 };
00254 typedef struct DSAPrivateKeyStr DSAPrivateKey;
00255 
00256 /***************************************************************************
00257 ** Diffie-Hellman Public and Private Key and related structures
00258 ** Structure member names suggested by PKCS#3.
00259 */
00260 
00261 struct DHParamsStr {
00262     PRArenaPool * arena;
00263     SECItem prime; /* p */
00264     SECItem base; /* g */
00265 };
00266 typedef struct DHParamsStr DHParams;
00267 
00268 struct DHPublicKeyStr {
00269     PRArenaPool * arena;
00270     SECItem prime;
00271     SECItem base;
00272     SECItem publicValue;
00273 };
00274 typedef struct DHPublicKeyStr DHPublicKey;
00275 
00276 struct DHPrivateKeyStr {
00277     PRArenaPool * arena;
00278     SECItem prime;
00279     SECItem base;
00280     SECItem publicValue;
00281     SECItem privateValue;
00282 };
00283 typedef struct DHPrivateKeyStr DHPrivateKey;
00284 
00285 /***************************************************************************
00286 ** Data structures used for elliptic curve parameters and
00287 ** public and private keys.
00288 */
00289 
00290 /*
00291 ** The ECParams data structures can encode elliptic curve 
00292 ** parameters for both GFp and GF2m curves.
00293 */
00294 
00295 typedef enum { ec_params_explicit,
00296               ec_params_named
00297 } ECParamsType;
00298 
00299 typedef enum { ec_field_GFp = 1,
00300                ec_field_GF2m
00301 } ECFieldType;
00302 
00303 struct ECFieldIDStr {
00304     int         size;   /* field size in bits */
00305     ECFieldType type;
00306     union {
00307         SECItem  prime; /* prime p for (GFp) */
00308         SECItem  poly;  /* irreducible binary polynomial for (GF2m) */
00309     } u;
00310     int         k1;     /* first coefficient of pentanomial or
00311                          * the only coefficient of trinomial 
00312                          */
00313     int         k2;     /* two remaining coefficients of pentanomial */
00314     int         k3;
00315 };
00316 typedef struct ECFieldIDStr ECFieldID;
00317 
00318 struct ECCurveStr {
00319     SECItem a;          /* contains octet stream encoding of
00320                          * field element (X9.62 section 4.3.3) 
00321                       */
00322     SECItem b;
00323     SECItem seed;
00324 };
00325 typedef struct ECCurveStr ECCurve;
00326 
00327 struct ECParamsStr {
00328     PRArenaPool * arena;
00329     ECParamsType  type;
00330     ECFieldID     fieldID;
00331     ECCurve       curve; 
00332     SECItem       base;
00333     SECItem       order; 
00334     int           cofactor;
00335     SECItem       DEREncoding;
00336     ECCurveName   name;
00337     SECItem       curveOID;
00338 };
00339 typedef struct ECParamsStr ECParams;
00340 
00341 struct ECPublicKeyStr {
00342     ECParams ecParams;   
00343     SECItem publicValue;   /* elliptic curve point encoded as 
00344                          * octet stream.
00345                          */
00346 };
00347 typedef struct ECPublicKeyStr ECPublicKey;
00348 
00349 struct ECPrivateKeyStr {
00350     ECParams ecParams;   
00351     SECItem publicValue;   /* encoded ec point */
00352     SECItem privateValue;  /* private big integer */
00353     SECItem version;       /* As per SEC 1, Appendix C, Section C.4 */
00354 };
00355 typedef struct ECPrivateKeyStr ECPrivateKey;
00356 
00357 typedef void * (*BLapiAllocateFunc)(void);
00358 typedef void (*BLapiDestroyContextFunc)(void *cx, PRBool freeit);
00359 typedef SECStatus (*BLapiInitContextFunc)(void *cx, 
00360                                const unsigned char *key, 
00361                                unsigned int keylen,
00362                                const unsigned char *, 
00363                                int, 
00364                                unsigned int ,
00365                                unsigned int );
00366 typedef SECStatus (*BLapiEncrypt)(void *cx, unsigned char *output,
00367                             unsigned int *outputLen, 
00368                             unsigned int maxOutputLen,
00369                             const unsigned char *input, 
00370                             unsigned int inputLen);
00371 
00372 #endif /* _BLAPIT_H_ */