2 * crypto.h - public data structures and prototypes for the crypto library
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is the Netscape security libraries.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1994-2000
22 * the Initial Developer. All Rights Reserved.
25 * Dr Vipul Gupta <vipul.gupta@sun.com>, Sun Microsystems Laboratories
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
40 /* $Id: blapi.h,v 1.26 2007/02/28 19:47:37 rrelyea%redhat.com Exp $ */
52 ** RSA encryption/decryption. When encrypting/decrypting the output
53 ** buffer must be at least the size of the public key modulus.
57 ** Generate and return a new RSA public and private key.
58 ** Both keys are encoded in a single RSAPrivateKey structure.
59 ** "cx" is the random number generator context
60 ** "keySizeInBits" is the size of the key to be generated, in bits.
62 ** "publicExponent" when not NULL is a pointer to some data that
63 ** represents the public exponent to use. The data is a byte
64 ** encoded integer, in "big endian" order.
66 extern RSAPrivateKey
*RSA_NewKey(int keySizeInBits
,
67 SECItem
* publicExponent
);
70 ** Perform a raw public-key operation
71 ** Length of input and output buffers are equal to key's modulus len.
73 extern SECStatus
RSA_PublicKeyOp(RSAPublicKey
* key
,
74 unsigned char * output
,
75 const unsigned char * input
);
78 ** Perform a raw private-key operation
79 ** Length of input and output buffers are equal to key's modulus len.
81 extern SECStatus
RSA_PrivateKeyOp(RSAPrivateKey
* key
,
82 unsigned char * output
,
83 const unsigned char * input
);
86 ** Perform a raw private-key operation, and check the parameters used in
87 ** the operation for validity by performing a test operation first.
88 ** Length of input and output buffers are equal to key's modulus len.
90 extern SECStatus
RSA_PrivateKeyOpDoubleChecked(RSAPrivateKey
* key
,
91 unsigned char * output
,
92 const unsigned char * input
);
95 ** Perform a check of private key parameters for consistency.
97 extern SECStatus
RSA_PrivateKeyCheck(RSAPrivateKey
*key
);
100 /********************************************************************
101 ** DSA signing algorithm
105 ** Generate and return a new DSA public and private key pair,
106 ** both of which are encoded into a single DSAPrivateKey struct.
107 ** "params" is a pointer to the PQG parameters for the domain
108 ** Uses a random seed.
110 extern SECStatus
DSA_NewKey(const PQGParams
* params
,
111 DSAPrivateKey
** privKey
);
113 /* signature is caller-supplied buffer of at least 20 bytes.
114 ** On input, signature->len == size of buffer to hold signature.
115 ** digest->len == size of digest.
116 ** On output, signature->len == size of signature in buffer.
117 ** Uses a random seed.
119 extern SECStatus
DSA_SignDigest(DSAPrivateKey
* key
,
121 const SECItem
* digest
);
123 /* signature is caller-supplied buffer of at least 20 bytes.
124 ** On input, signature->len == size of buffer to hold signature.
125 ** digest->len == size of digest.
127 extern SECStatus
DSA_VerifyDigest(DSAPublicKey
* key
,
128 const SECItem
* signature
,
129 const SECItem
* digest
);
131 /* For FIPS compliance testing. Seed must be exactly 20 bytes long */
132 extern SECStatus
DSA_NewKeyFromSeed(const PQGParams
*params
,
133 const unsigned char * seed
,
134 DSAPrivateKey
**privKey
);
136 /* For FIPS compliance testing. Seed must be exactly 20 bytes. */
137 extern SECStatus
DSA_SignDigestWithSeed(DSAPrivateKey
* key
,
139 const SECItem
* digest
,
140 const unsigned char * seed
);
142 /******************************************************
143 ** Diffie Helman key exchange algorithm
146 /* Generates parameters for Diffie-Helman key generation.
147 ** primeLen is the length in bytes of prime P to be generated.
149 extern SECStatus
DH_GenParam(int primeLen
, DHParams
** params
);
151 /* Generates a public and private key, both of which are encoded in a single
152 ** DHPrivateKey struct. Params is input, privKey are output.
153 ** This is Phase 1 of Diffie Hellman.
155 extern SECStatus
DH_NewKey(DHParams
* params
,
156 DHPrivateKey
** privKey
);
159 ** DH_Derive does the Diffie-Hellman phase 2 calculation, using the
160 ** other party's publicValue, and the prime and our privateValue.
161 ** maxOutBytes is the requested length of the generated secret in bytes.
162 ** A zero value means produce a value of any length up to the size of
163 ** the prime. If successful, derivedSecret->data is set
164 ** to the address of the newly allocated buffer containing the derived
165 ** secret, and derivedSecret->len is the size of the secret produced.
166 ** The size of the secret produced will never be larger than the length
167 ** of the prime, and it may be smaller than maxOutBytes.
168 ** It is the caller's responsibility to free the allocated buffer
169 ** containing the derived secret.
171 extern SECStatus
DH_Derive(SECItem
* publicValue
,
173 SECItem
* privateValue
,
174 SECItem
* derivedSecret
,
175 unsigned int maxOutBytes
);
178 ** KEA_CalcKey returns octet string with the private key for a dual
179 ** Diffie-Helman key generation as specified for government key exchange.
181 extern SECStatus
KEA_Derive(SECItem
*prime
,
186 SECItem
*derivedSecret
);
189 * verify that a KEA or DSA public key is a valid key for this prime and
192 extern PRBool
KEA_Verify(SECItem
*Y
, SECItem
*prime
, SECItem
*subPrime
);
194 /******************************************************
195 ** Elliptic Curve algorithms
198 /* Generates a public and private key, both of which are encoded
199 ** in a single ECPrivateKey struct. Params is input, privKey are
202 extern SECStatus
EC_NewKey(ECParams
* params
,
203 ECPrivateKey
** privKey
);
205 extern SECStatus
EC_NewKeyFromSeed(ECParams
* params
,
206 ECPrivateKey
** privKey
,
207 const unsigned char* seed
,
210 /* Validates an EC public key as described in Section 5.2.2 of
211 * X9.62. Such validation prevents against small subgroup attacks
212 * when the ECDH primitive is used with the cofactor.
214 extern SECStatus
EC_ValidatePublicKey(ECParams
* params
,
215 SECItem
* publicValue
);
218 ** ECDH_Derive performs a scalar point multiplication of a point
219 ** representing a (peer's) public key and a large integer representing
220 ** a private key (its own). Both keys must use the same elliptic curve
221 ** parameters. If the withCofactor parameter is true, the
222 ** multiplication also uses the cofactor associated with the curve
223 ** parameters. The output of this scheme is the x-coordinate of the
224 ** resulting point. If successful, derivedSecret->data is set to the
225 ** address of the newly allocated buffer containing the derived
226 ** secret, and derivedSecret->len is the size of the secret
227 ** produced. It is the caller's responsibility to free the allocated
228 ** buffer containing the derived secret.
230 extern SECStatus
ECDH_Derive(SECItem
* publicValue
,
232 SECItem
* privateValue
,
234 SECItem
* derivedSecret
);
236 /* On input, signature->len == size of buffer to hold signature.
237 ** digest->len == size of digest.
238 ** On output, signature->len == size of signature in buffer.
239 ** Uses a random seed.
241 extern SECStatus
ECDSA_SignDigest(ECPrivateKey
*key
,
243 const SECItem
*digest
);
245 /* On input, signature->len == size of buffer to hold signature.
246 ** digest->len == size of digest.
248 extern SECStatus
ECDSA_VerifyDigest(ECPublicKey
*key
,
249 const SECItem
*signature
,
250 const SECItem
*digest
);
252 /* Uses the provided seed. */
253 extern SECStatus
ECDSA_SignDigestWithSeed(ECPrivateKey
*key
,
255 const SECItem
*digest
,
256 const unsigned char *seed
,
259 /******************************************/
261 ** RC4 symmetric stream cypher
265 ** Create a new RC4 context suitable for RC4 encryption/decryption.
266 ** "key" raw key data
267 ** "len" the number of bytes of key data
269 extern RC4Context
*RC4_CreateContext(const unsigned char *key
, int len
);
271 extern RC4Context
*RC4_AllocateContext(void);
272 extern SECStatus
RC4_InitContext(RC4Context
*cx
,
273 const unsigned char *key
,
275 const unsigned char *,
281 ** Destroy an RC4 encryption/decryption context.
283 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
285 extern void RC4_DestroyContext(RC4Context
*cx
, PRBool freeit
);
288 ** Perform RC4 encryption.
290 ** "output" the output buffer to store the encrypted data.
291 ** "outputLen" how much data is stored in "output". Set by the routine
292 ** after some data is stored in output.
293 ** "maxOutputLen" the maximum amount of data that can ever be
294 ** stored in "output"
295 ** "input" the input data
296 ** "inputLen" the amount of input data
298 extern SECStatus
RC4_Encrypt(RC4Context
*cx
, unsigned char *output
,
299 unsigned int *outputLen
, unsigned int maxOutputLen
,
300 const unsigned char *input
, unsigned int inputLen
);
303 ** Perform RC4 decryption.
305 ** "output" the output buffer to store the decrypted data.
306 ** "outputLen" how much data is stored in "output". Set by the routine
307 ** after some data is stored in output.
308 ** "maxOutputLen" the maximum amount of data that can ever be
309 ** stored in "output"
310 ** "input" the input data
311 ** "inputLen" the amount of input data
313 extern SECStatus
RC4_Decrypt(RC4Context
*cx
, unsigned char *output
,
314 unsigned int *outputLen
, unsigned int maxOutputLen
,
315 const unsigned char *input
, unsigned int inputLen
);
317 /******************************************/
319 ** RC2 symmetric block cypher
323 ** Create a new RC2 context suitable for RC2 encryption/decryption.
324 ** "key" raw key data
325 ** "len" the number of bytes of key data
326 ** "iv" is the CBC initialization vector (if mode is NSS_RC2_CBC)
327 ** "mode" one of NSS_RC2 or NSS_RC2_CBC
328 ** "effectiveKeyLen" is the effective key length (as specified in
329 ** RFC 2268) in bytes (not bits).
331 ** When mode is set to NSS_RC2_CBC the RC2 cipher is run in "cipher block
334 extern RC2Context
*RC2_CreateContext(const unsigned char *key
, unsigned int len
,
335 const unsigned char *iv
, int mode
,
336 unsigned effectiveKeyLen
);
337 extern RC2Context
*RC2_AllocateContext(void);
338 extern SECStatus
RC2_InitContext(RC2Context
*cx
,
339 const unsigned char *key
,
341 const unsigned char *iv
,
343 unsigned int effectiveKeyLen
,
347 ** Destroy an RC2 encryption/decryption context.
349 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
351 extern void RC2_DestroyContext(RC2Context
*cx
, PRBool freeit
);
354 ** Perform RC2 encryption.
356 ** "output" the output buffer to store the encrypted data.
357 ** "outputLen" how much data is stored in "output". Set by the routine
358 ** after some data is stored in output.
359 ** "maxOutputLen" the maximum amount of data that can ever be
360 ** stored in "output"
361 ** "input" the input data
362 ** "inputLen" the amount of input data
364 extern SECStatus
RC2_Encrypt(RC2Context
*cx
, unsigned char *output
,
365 unsigned int *outputLen
, unsigned int maxOutputLen
,
366 const unsigned char *input
, unsigned int inputLen
);
369 ** Perform RC2 decryption.
371 ** "output" the output buffer to store the decrypted data.
372 ** "outputLen" how much data is stored in "output". Set by the routine
373 ** after some data is stored in output.
374 ** "maxOutputLen" the maximum amount of data that can ever be
375 ** stored in "output"
376 ** "input" the input data
377 ** "inputLen" the amount of input data
379 extern SECStatus
RC2_Decrypt(RC2Context
*cx
, unsigned char *output
,
380 unsigned int *outputLen
, unsigned int maxOutputLen
,
381 const unsigned char *input
, unsigned int inputLen
);
383 /******************************************/
385 ** RC5 symmetric block cypher -- 64-bit block size
389 ** Create a new RC5 context suitable for RC5 encryption/decryption.
390 ** "key" raw key data
391 ** "len" the number of bytes of key data
392 ** "iv" is the CBC initialization vector (if mode is NSS_RC5_CBC)
393 ** "mode" one of NSS_RC5 or NSS_RC5_CBC
395 ** When mode is set to NSS_RC5_CBC the RC5 cipher is run in "cipher block
398 extern RC5Context
*RC5_CreateContext(const SECItem
*key
, unsigned int rounds
,
399 unsigned int wordSize
, const unsigned char *iv
, int mode
);
400 extern RC5Context
*RC5_AllocateContext(void);
401 extern SECStatus
RC5_InitContext(RC5Context
*cx
,
402 const unsigned char *key
,
404 const unsigned char *iv
,
407 unsigned int wordSize
);
410 ** Destroy an RC5 encryption/decryption context.
412 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
414 extern void RC5_DestroyContext(RC5Context
*cx
, PRBool freeit
);
417 ** Perform RC5 encryption.
419 ** "output" the output buffer to store the encrypted data.
420 ** "outputLen" how much data is stored in "output". Set by the routine
421 ** after some data is stored in output.
422 ** "maxOutputLen" the maximum amount of data that can ever be
423 ** stored in "output"
424 ** "input" the input data
425 ** "inputLen" the amount of input data
427 extern SECStatus
RC5_Encrypt(RC5Context
*cx
, unsigned char *output
,
428 unsigned int *outputLen
, unsigned int maxOutputLen
,
429 const unsigned char *input
, unsigned int inputLen
);
432 ** Perform RC5 decryption.
434 ** "output" the output buffer to store the decrypted data.
435 ** "outputLen" how much data is stored in "output". Set by the routine
436 ** after some data is stored in output.
437 ** "maxOutputLen" the maximum amount of data that can ever be
438 ** stored in "output"
439 ** "input" the input data
440 ** "inputLen" the amount of input data
443 extern SECStatus
RC5_Decrypt(RC5Context
*cx
, unsigned char *output
,
444 unsigned int *outputLen
, unsigned int maxOutputLen
,
445 const unsigned char *input
, unsigned int inputLen
);
449 /******************************************/
451 ** DES symmetric block cypher
455 ** Create a new DES context suitable for DES encryption/decryption.
456 ** "key" raw key data
457 ** "len" the number of bytes of key data
458 ** "iv" is the CBC initialization vector (if mode is NSS_DES_CBC or
459 ** mode is DES_EDE3_CBC)
460 ** "mode" one of NSS_DES, NSS_DES_CBC, NSS_DES_EDE3 or NSS_DES_EDE3_CBC
461 ** "encrypt" is PR_TRUE if the context will be used for encryption
463 ** When mode is set to NSS_DES_CBC or NSS_DES_EDE3_CBC then the DES
464 ** cipher is run in "cipher block chaining" mode.
466 extern DESContext
*DES_CreateContext(const unsigned char *key
,
467 const unsigned char *iv
,
468 int mode
, PRBool encrypt
);
469 extern DESContext
*DES_AllocateContext(void);
470 extern SECStatus
DES_InitContext(DESContext
*cx
,
471 const unsigned char *key
,
473 const unsigned char *iv
,
475 unsigned int encrypt
,
479 ** Destroy an DES encryption/decryption context.
481 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
483 extern void DES_DestroyContext(DESContext
*cx
, PRBool freeit
);
486 ** Perform DES encryption.
488 ** "output" the output buffer to store the encrypted data.
489 ** "outputLen" how much data is stored in "output". Set by the routine
490 ** after some data is stored in output.
491 ** "maxOutputLen" the maximum amount of data that can ever be
492 ** stored in "output"
493 ** "input" the input data
494 ** "inputLen" the amount of input data
496 ** NOTE: the inputLen must be a multiple of DES_KEY_LENGTH
498 extern SECStatus
DES_Encrypt(DESContext
*cx
, unsigned char *output
,
499 unsigned int *outputLen
, unsigned int maxOutputLen
,
500 const unsigned char *input
, unsigned int inputLen
);
503 ** Perform DES decryption.
505 ** "output" the output buffer to store the decrypted data.
506 ** "outputLen" how much data is stored in "output". Set by the routine
507 ** after some data is stored in output.
508 ** "maxOutputLen" the maximum amount of data that can ever be
509 ** stored in "output"
510 ** "input" the input data
511 ** "inputLen" the amount of input data
513 ** NOTE: the inputLen must be a multiple of DES_KEY_LENGTH
515 extern SECStatus
DES_Decrypt(DESContext
*cx
, unsigned char *output
,
516 unsigned int *outputLen
, unsigned int maxOutputLen
,
517 const unsigned char *input
, unsigned int inputLen
);
519 /******************************************/
521 ** AES symmetric block cypher (Rijndael)
525 ** Create a new AES context suitable for AES encryption/decryption.
526 ** "key" raw key data
527 ** "keylen" the number of bytes of key data (16, 24, or 32)
528 ** "blocklen" is the blocksize to use (16, 24, or 32)
529 ** XXX currently only blocksize==16 has been tested!
532 AES_CreateContext(const unsigned char *key
, const unsigned char *iv
,
533 int mode
, int encrypt
,
534 unsigned int keylen
, unsigned int blocklen
);
535 extern AESContext
*AES_AllocateContext(void);
536 extern SECStatus
AES_InitContext(AESContext
*cx
,
537 const unsigned char *key
,
539 const unsigned char *iv
,
541 unsigned int encrypt
,
542 unsigned int blocklen
);
545 ** Destroy a AES encryption/decryption context.
547 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
550 AES_DestroyContext(AESContext
*cx
, PRBool freeit
);
553 ** Perform AES encryption.
555 ** "output" the output buffer to store the encrypted data.
556 ** "outputLen" how much data is stored in "output". Set by the routine
557 ** after some data is stored in output.
558 ** "maxOutputLen" the maximum amount of data that can ever be
559 ** stored in "output"
560 ** "input" the input data
561 ** "inputLen" the amount of input data
564 AES_Encrypt(AESContext
*cx
, unsigned char *output
,
565 unsigned int *outputLen
, unsigned int maxOutputLen
,
566 const unsigned char *input
, unsigned int inputLen
);
569 ** Perform AES decryption.
571 ** "output" the output buffer to store the decrypted data.
572 ** "outputLen" how much data is stored in "output". Set by the routine
573 ** after some data is stored in output.
574 ** "maxOutputLen" the maximum amount of data that can ever be
575 ** stored in "output"
576 ** "input" the input data
577 ** "inputLen" the amount of input data
580 AES_Decrypt(AESContext
*cx
, unsigned char *output
,
581 unsigned int *outputLen
, unsigned int maxOutputLen
,
582 const unsigned char *input
, unsigned int inputLen
);
584 /******************************************/
586 ** AES key wrap algorithm, RFC 3394
590 ** Create a new AES context suitable for AES encryption/decryption.
591 ** "key" raw key data
592 ** "iv" The 8 byte "initial value"
593 ** "encrypt", a boolean, true for key wrapping, false for unwrapping.
594 ** "keylen" the number of bytes of key data (16, 24, or 32)
596 extern AESKeyWrapContext
*
597 AESKeyWrap_CreateContext(const unsigned char *key
, const unsigned char *iv
,
598 int encrypt
, unsigned int keylen
);
599 extern AESKeyWrapContext
* AESKeyWrap_AllocateContext(void);
601 AESKeyWrap_InitContext(AESKeyWrapContext
*cx
,
602 const unsigned char *key
,
604 const unsigned char *iv
,
606 unsigned int encrypt
,
610 ** Destroy a AES KeyWrap context.
612 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
615 AESKeyWrap_DestroyContext(AESKeyWrapContext
*cx
, PRBool freeit
);
618 ** Perform AES key wrap.
620 ** "output" the output buffer to store the encrypted data.
621 ** "outputLen" how much data is stored in "output". Set by the routine
622 ** after some data is stored in output.
623 ** "maxOutputLen" the maximum amount of data that can ever be
624 ** stored in "output"
625 ** "input" the input data
626 ** "inputLen" the amount of input data
629 AESKeyWrap_Encrypt(AESKeyWrapContext
*cx
, unsigned char *output
,
630 unsigned int *outputLen
, unsigned int maxOutputLen
,
631 const unsigned char *input
, unsigned int inputLen
);
634 ** Perform AES key unwrap.
636 ** "output" the output buffer to store the decrypted data.
637 ** "outputLen" how much data is stored in "output". Set by the routine
638 ** after some data is stored in output.
639 ** "maxOutputLen" the maximum amount of data that can ever be
640 ** stored in "output"
641 ** "input" the input data
642 ** "inputLen" the amount of input data
645 AESKeyWrap_Decrypt(AESKeyWrapContext
*cx
, unsigned char *output
,
646 unsigned int *outputLen
, unsigned int maxOutputLen
,
647 const unsigned char *input
, unsigned int inputLen
);
649 /******************************************/
651 ** Camellia symmetric block cypher
655 ** Create a new Camellia context suitable for Camellia encryption/decryption.
656 ** "key" raw key data
657 ** "keylen" the number of bytes of key data (16, 24, or 32)
659 extern CamelliaContext
*
660 Camellia_CreateContext(const unsigned char *key
, const unsigned char *iv
,
661 int mode
, int encrypt
, unsigned int keylen
);
663 extern CamelliaContext
*Camellia_AllocateContext(void);
664 extern SECStatus
Camellia_InitContext(CamelliaContext
*cx
,
665 const unsigned char *key
,
667 const unsigned char *iv
,
669 unsigned int encrypt
,
670 unsigned int unused
);
672 ** Destroy a Camellia encryption/decryption context.
674 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
677 Camellia_DestroyContext(CamelliaContext
*cx
, PRBool freeit
);
680 ** Perform Camellia encryption.
682 ** "output" the output buffer to store the encrypted data.
683 ** "outputLen" how much data is stored in "output". Set by the routine
684 ** after some data is stored in output.
685 ** "maxOutputLen" the maximum amount of data that can ever be
686 ** stored in "output"
687 ** "input" the input data
688 ** "inputLen" the amount of input data
691 Camellia_Encrypt(CamelliaContext
*cx
, unsigned char *output
,
692 unsigned int *outputLen
, unsigned int maxOutputLen
,
693 const unsigned char *input
, unsigned int inputLen
);
696 ** Perform Camellia decryption.
698 ** "output" the output buffer to store the decrypted data.
699 ** "outputLen" how much data is stored in "output". Set by the routine
700 ** after some data is stored in output.
701 ** "maxOutputLen" the maximum amount of data that can ever be
702 ** stored in "output"
703 ** "input" the input data
704 ** "inputLen" the amount of input data
707 Camellia_Decrypt(CamelliaContext
*cx
, unsigned char *output
,
708 unsigned int *outputLen
, unsigned int maxOutputLen
,
709 const unsigned char *input
, unsigned int inputLen
);
712 /******************************************/
714 ** MD5 secure hash function
718 ** Hash a null terminated string "src" into "dest" using MD5
720 extern SECStatus
MD5_Hash(unsigned char *dest
, const char *src
);
723 ** Hash a non-null terminated string "src" into "dest" using MD5
725 extern SECStatus
MD5_HashBuf(unsigned char *dest
, const unsigned char *src
,
729 ** Create a new MD5 context
731 extern MD5Context
*MD5_NewContext(void);
735 ** Destroy an MD5 secure hash context.
737 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
739 extern void MD5_DestroyContext(MD5Context
*cx
, PRBool freeit
);
742 ** Reset an MD5 context, preparing it for a fresh round of hashing
744 extern void MD5_Begin(MD5Context
*cx
);
747 ** Update the MD5 hash function with more data.
749 ** "input" the data to hash
750 ** "inputLen" the amount of data to hash
752 extern void MD5_Update(MD5Context
*cx
,
753 const unsigned char *input
, unsigned int inputLen
);
756 ** Finish the MD5 hash function. Produce the digested results in "digest"
758 ** "digest" where the 16 bytes of digest data are stored
759 ** "digestLen" where the digest length (16) is stored
760 ** "maxDigestLen" the maximum amount of data that can ever be
761 ** stored in "digest"
763 extern void MD5_End(MD5Context
*cx
, unsigned char *digest
,
764 unsigned int *digestLen
, unsigned int maxDigestLen
);
767 * Return the the size of a buffer needed to flatten the MD5 Context into
771 extern unsigned int MD5_FlattenSize(MD5Context
*cx
);
774 * Flatten the MD5 Context into a buffer:
776 * "space" the buffer to flatten to
779 extern SECStatus
MD5_Flatten(MD5Context
*cx
,unsigned char *space
);
782 * Resurrect a flattened context into a MD5 Context
783 * "space" the buffer of the flattend buffer
784 * "arg" ptr to void used by cryptographic resurrect
785 * returns resurected context;
787 extern MD5Context
* MD5_Resurrect(unsigned char *space
, void *arg
);
788 extern void MD5_Clone(MD5Context
*dest
, MD5Context
*src
);
791 ** trace the intermediate state info of the MD5 hash.
793 extern void MD5_TraceState(MD5Context
*cx
);
796 /******************************************/
798 ** MD2 secure hash function
802 ** Hash a null terminated string "src" into "dest" using MD2
804 extern SECStatus
MD2_Hash(unsigned char *dest
, const char *src
);
807 ** Create a new MD2 context
809 extern MD2Context
*MD2_NewContext(void);
813 ** Destroy an MD2 secure hash context.
815 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
817 extern void MD2_DestroyContext(MD2Context
*cx
, PRBool freeit
);
820 ** Reset an MD2 context, preparing it for a fresh round of hashing
822 extern void MD2_Begin(MD2Context
*cx
);
825 ** Update the MD2 hash function with more data.
827 ** "input" the data to hash
828 ** "inputLen" the amount of data to hash
830 extern void MD2_Update(MD2Context
*cx
,
831 const unsigned char *input
, unsigned int inputLen
);
834 ** Finish the MD2 hash function. Produce the digested results in "digest"
836 ** "digest" where the 16 bytes of digest data are stored
837 ** "digestLen" where the digest length (16) is stored
838 ** "maxDigestLen" the maximum amount of data that can ever be
839 ** stored in "digest"
841 extern void MD2_End(MD2Context
*cx
, unsigned char *digest
,
842 unsigned int *digestLen
, unsigned int maxDigestLen
);
845 * Return the the size of a buffer needed to flatten the MD2 Context into
849 extern unsigned int MD2_FlattenSize(MD2Context
*cx
);
852 * Flatten the MD2 Context into a buffer:
854 * "space" the buffer to flatten to
857 extern SECStatus
MD2_Flatten(MD2Context
*cx
,unsigned char *space
);
860 * Resurrect a flattened context into a MD2 Context
861 * "space" the buffer of the flattend buffer
862 * "arg" ptr to void used by cryptographic resurrect
863 * returns resurected context;
865 extern MD2Context
* MD2_Resurrect(unsigned char *space
, void *arg
);
866 extern void MD2_Clone(MD2Context
*dest
, MD2Context
*src
);
868 /******************************************/
870 ** SHA-1 secure hash function
874 ** Hash a null terminated string "src" into "dest" using SHA-1
876 extern SECStatus
SHA1_Hash(unsigned char *dest
, const char *src
);
879 ** Hash a non-null terminated string "src" into "dest" using SHA-1
881 extern SECStatus
SHA1_HashBuf(unsigned char *dest
, const unsigned char *src
,
885 ** Create a new SHA-1 context
887 extern SHA1Context
*SHA1_NewContext(void);
891 ** Destroy a SHA-1 secure hash context.
893 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
895 extern void SHA1_DestroyContext(SHA1Context
*cx
, PRBool freeit
);
898 ** Reset a SHA-1 context, preparing it for a fresh round of hashing
900 extern void SHA1_Begin(SHA1Context
*cx
);
903 ** Update the SHA-1 hash function with more data.
905 ** "input" the data to hash
906 ** "inputLen" the amount of data to hash
908 extern void SHA1_Update(SHA1Context
*cx
, const unsigned char *input
,
909 unsigned int inputLen
);
912 ** Finish the SHA-1 hash function. Produce the digested results in "digest"
914 ** "digest" where the 16 bytes of digest data are stored
915 ** "digestLen" where the digest length (20) is stored
916 ** "maxDigestLen" the maximum amount of data that can ever be
917 ** stored in "digest"
919 extern void SHA1_End(SHA1Context
*cx
, unsigned char *digest
,
920 unsigned int *digestLen
, unsigned int maxDigestLen
);
923 ** trace the intermediate state info of the SHA1 hash.
925 extern void SHA1_TraceState(SHA1Context
*cx
);
928 * Return the the size of a buffer needed to flatten the SHA-1 Context into
932 extern unsigned int SHA1_FlattenSize(SHA1Context
*cx
);
935 * Flatten the SHA-1 Context into a buffer:
937 * "space" the buffer to flatten to
940 extern SECStatus
SHA1_Flatten(SHA1Context
*cx
,unsigned char *space
);
943 * Resurrect a flattened context into a SHA-1 Context
944 * "space" the buffer of the flattend buffer
945 * "arg" ptr to void used by cryptographic resurrect
946 * returns resurected context;
948 extern SHA1Context
* SHA1_Resurrect(unsigned char *space
, void *arg
);
949 extern void SHA1_Clone(SHA1Context
*dest
, SHA1Context
*src
);
951 /******************************************/
953 extern SHA256Context
*SHA256_NewContext(void);
954 extern void SHA256_DestroyContext(SHA256Context
*cx
, PRBool freeit
);
955 extern void SHA256_Begin(SHA256Context
*cx
);
956 extern void SHA256_Update(SHA256Context
*cx
, const unsigned char *input
,
957 unsigned int inputLen
);
958 extern void SHA256_End(SHA256Context
*cx
, unsigned char *digest
,
959 unsigned int *digestLen
, unsigned int maxDigestLen
);
960 extern SECStatus
SHA256_HashBuf(unsigned char *dest
, const unsigned char *src
,
962 extern SECStatus
SHA256_Hash(unsigned char *dest
, const char *src
);
963 extern void SHA256_TraceState(SHA256Context
*cx
);
964 extern unsigned int SHA256_FlattenSize(SHA256Context
*cx
);
965 extern SECStatus
SHA256_Flatten(SHA256Context
*cx
,unsigned char *space
);
966 extern SHA256Context
* SHA256_Resurrect(unsigned char *space
, void *arg
);
967 extern void SHA256_Clone(SHA256Context
*dest
, SHA256Context
*src
);
969 /******************************************/
971 extern SHA512Context
*SHA512_NewContext(void);
972 extern void SHA512_DestroyContext(SHA512Context
*cx
, PRBool freeit
);
973 extern void SHA512_Begin(SHA512Context
*cx
);
974 extern void SHA512_Update(SHA512Context
*cx
, const unsigned char *input
,
975 unsigned int inputLen
);
976 extern void SHA512_End(SHA512Context
*cx
, unsigned char *digest
,
977 unsigned int *digestLen
, unsigned int maxDigestLen
);
978 extern SECStatus
SHA512_HashBuf(unsigned char *dest
, const unsigned char *src
,
980 extern SECStatus
SHA512_Hash(unsigned char *dest
, const char *src
);
981 extern void SHA512_TraceState(SHA512Context
*cx
);
982 extern unsigned int SHA512_FlattenSize(SHA512Context
*cx
);
983 extern SECStatus
SHA512_Flatten(SHA512Context
*cx
,unsigned char *space
);
984 extern SHA512Context
* SHA512_Resurrect(unsigned char *space
, void *arg
);
985 extern void SHA512_Clone(SHA512Context
*dest
, SHA512Context
*src
);
987 /******************************************/
989 extern SHA384Context
*SHA384_NewContext(void);
990 extern void SHA384_DestroyContext(SHA384Context
*cx
, PRBool freeit
);
991 extern void SHA384_Begin(SHA384Context
*cx
);
992 extern void SHA384_Update(SHA384Context
*cx
, const unsigned char *input
,
993 unsigned int inputLen
);
994 extern void SHA384_End(SHA384Context
*cx
, unsigned char *digest
,
995 unsigned int *digestLen
, unsigned int maxDigestLen
);
996 extern SECStatus
SHA384_HashBuf(unsigned char *dest
, const unsigned char *src
,
998 extern SECStatus
SHA384_Hash(unsigned char *dest
, const char *src
);
999 extern void SHA384_TraceState(SHA384Context
*cx
);
1000 extern unsigned int SHA384_FlattenSize(SHA384Context
*cx
);
1001 extern SECStatus
SHA384_Flatten(SHA384Context
*cx
,unsigned char *space
);
1002 extern SHA384Context
* SHA384_Resurrect(unsigned char *space
, void *arg
);
1003 extern void SHA384_Clone(SHA384Context
*dest
, SHA384Context
*src
);
1005 /****************************************
1006 * implement TLS Pseudo Random Function (PRF)
1010 TLS_PRF(const SECItem
*secret
, const char *label
, SECItem
*seed
,
1011 SECItem
*result
, PRBool isFIPS
);
1013 /******************************************/
1015 ** Pseudo Random Number Generation. FIPS compliance desirable.
1019 ** Initialize the global RNG context and give it some seed input taken
1020 ** from the system. This function is thread-safe and will only allow
1021 ** the global context to be initialized once. The seed input is likely
1022 ** small, so it is imperative that RNG_RandomUpdate() be called with
1023 ** additional seed data before the generator is used. A good way to
1024 ** provide the generator with additional entropy is to call
1025 ** RNG_SystemInfoForRNG(). Note that NSS_Init() does exactly that.
1027 extern SECStatus
RNG_RNGInit(void);
1030 ** Update the global random number generator with more seeding
1033 extern SECStatus
RNG_RandomUpdate(const void *data
, size_t bytes
);
1036 ** Generate some random bytes, using the global random number generator
1039 extern SECStatus
RNG_GenerateGlobalRandomBytes(void *dest
, size_t len
);
1041 /* Destroy the global RNG context. After a call to RNG_RNGShutdown()
1042 ** a call to RNG_RNGInit() is required in order to use the generator again,
1043 ** along with seed data (see the comment above RNG_RNGInit()).
1045 extern void RNG_RNGShutdown(void);
1047 extern void RNG_SystemInfoForRNG(void);
1050 * FIPS 186-2 Change Notice 1 RNG Algorithm 1, used both to
1051 * generate the DSA X parameter and as a generic purpose RNG.
1053 * The following two FIPS186Change functions are needed for
1054 * NIST RNG Validation System.
1058 * Given the seed-key and the seed, generate the random output.
1061 * XKEY [input/output]: the state of the RNG (seed-key)
1062 * XSEEDj [input]: optional user input (seed)
1063 * x_j [output]: output of the RNG
1066 * This function usually returns SECSuccess. The only reason
1067 * this function returns SECFailure is that XSEEDj equals
1068 * XKEY, including the intermediate XKEY value between the two
1069 * iterations. (This test is actually a FIPS 140-2 requirement
1070 * and not required for FIPS algorithm testing, but it is too
1071 * hard to separate from this function.) If this function fails,
1072 * XKEY is not updated, but some data may have been written to
1073 * x_j, which should be ignored.
1076 FIPS186Change_GenerateX(unsigned char *XKEY
,
1077 const unsigned char *XSEEDj
,
1078 unsigned char *x_j
);
1081 * When generating the DSA X parameter, we generate 2*GSIZE bytes
1082 * of random output and reduce it mod q.
1084 * Input: w, 2*GSIZE bytes
1085 * q, DSA_SUBPRIME_LEN bytes
1086 * Output: xj, DSA_SUBPRIME_LEN bytes
1089 FIPS186Change_ReduceModQForDSA(const unsigned char *w
,
1090 const unsigned char *q
,
1093 /* Generate PQGParams and PQGVerify structs.
1094 * Length of seed and length of h both equal length of P.
1095 * All lengths are specified by "j", according to the table above.
1098 PQG_ParamGen(unsigned int j
, /* input : determines length of P. */
1099 PQGParams
**pParams
, /* output: P Q and G returned here */
1100 PQGVerify
**pVfy
); /* output: counter and seed. */
1102 /* Generate PQGParams and PQGVerify structs.
1103 * Length of P specified by j. Length of h will match length of P.
1104 * Length of SEED in bytes specified in seedBytes.
1105 * seedBbytes must be in the range [20..255] or an error will result.
1108 PQG_ParamGenSeedLen(
1109 unsigned int j
, /* input : determines length of P. */
1110 unsigned int seedBytes
, /* input : length of seed in bytes.*/
1111 PQGParams
**pParams
, /* output: P Q and G returned here */
1112 PQGVerify
**pVfy
); /* output: counter and seed. */
1115 /* Test PQGParams for validity as DSS PQG values.
1116 * If vfy is non-NULL, test PQGParams to make sure they were generated
1117 * using the specified seed, counter, and h values.
1119 * Return value indicates whether Verification operation ran succesfully
1120 * to completion, but does not indicate if PQGParams are valid or not.
1121 * If return value is SECSuccess, then *pResult has these meanings:
1122 * SECSuccess: PQGParams are valid.
1123 * SECFailure: PQGParams are invalid.
1125 * Verify the following 12 facts about PQG counter SEED g and h
1126 * 1. Q is 160 bits long.
1127 * 2. P is one of the 9 valid lengths.
1132 * Steps 7-12 are done only if the optional PQGVerify is supplied.
1134 * 8. g >= 160 and g < 2048 (g is length of seed in bits)
1135 * 9. Q generated from SEED matches Q in PQGParams.
1136 * 10. P generated from (L, counter, g, SEED, Q) matches P in PQGParams.
1138 * 12. G generated from h matches G in PQGParams.
1141 extern SECStatus
PQG_VerifyParams(const PQGParams
*params
,
1142 const PQGVerify
*vfy
, SECStatus
*result
);
1146 * clean-up any global tables freebl may have allocated after it starts up.
1147 * This function is not thread safe and should be called only after the
1148 * library has been quiessed.
1150 extern void BL_Cleanup(void);
1152 /* unload freebl shared library from memory */
1153 extern void BL_Unload(void);
1155 /**************************************************************************
1156 * Verify a given Shared library signature *
1157 **************************************************************************/
1158 PRBool
BLAPI_SHVerify(const char *name
, PRFuncPtr addr
);
1160 /**************************************************************************
1161 * Verify Are Own Shared library signature *
1162 **************************************************************************/
1163 PRBool
BLAPI_VerifySelf(const char *name
);
1165 /*********************************************************************/
1166 extern const SECHashObject
* HASH_GetRawHashObject(HASH_HashType hashType
);
1170 #endif /* _BLAPI_H_ */