blapi.h (77383B)
1 /* 2 * blapi.h - public prototypes for the freebl library 3 * 4 * This Source Code Form is subject to the terms of the Mozilla Public 5 * License, v. 2.0. If a copy of the MPL was not distributed with this 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 7 8 #ifndef _BLAPI_H_ 9 #define _BLAPI_H_ 10 11 #include "blapit.h" 12 #include "hasht.h" 13 #include "cmac.h" 14 #include "alghmac.h" 15 #include "kyber.h" 16 17 SEC_BEGIN_PROTOS 18 19 /* 20 ** RSA encryption/decryption. When encrypting/decrypting the output 21 ** buffer must be at least the size of the public key modulus. 22 */ 23 24 extern SECStatus BL_Init(void); 25 26 /* 27 ** Generate and return a new RSA public and private key. 28 ** Both keys are encoded in a single RSAPrivateKey structure. 29 ** "cx" is the random number generator context 30 ** "keySizeInBits" is the size of the key to be generated, in bits. 31 ** 512, 1024, etc. 32 ** "publicExponent" when not NULL is a pointer to some data that 33 ** represents the public exponent to use. The data is a byte 34 ** encoded integer, in "big endian" order. 35 */ 36 extern RSAPrivateKey *RSA_NewKey(int keySizeInBits, 37 SECItem *publicExponent); 38 39 /* 40 ** Perform a raw public-key operation 41 ** Length of input and output buffers are equal to key's modulus len. 42 */ 43 extern SECStatus RSA_PublicKeyOp(RSAPublicKey *key, 44 unsigned char *output, 45 const unsigned char *input); 46 47 /* 48 ** Perform a raw private-key operation 49 ** Length of input and output buffers are equal to key's modulus len. 50 */ 51 extern SECStatus RSA_PrivateKeyOp(RSAPrivateKey *key, 52 unsigned char *output, 53 const unsigned char *input); 54 55 /* 56 ** Perform a raw private-key operation, and check the parameters used in 57 ** the operation for validity by performing a test operation first. 58 ** Length of input and output buffers are equal to key's modulus len. 59 */ 60 extern SECStatus RSA_PrivateKeyOpDoubleChecked(RSAPrivateKey *key, 61 unsigned char *output, 62 const unsigned char *input); 63 64 /* 65 ** Perform a check of private key parameters for consistency. 66 */ 67 extern SECStatus RSA_PrivateKeyCheck(const RSAPrivateKey *key); 68 69 /* 70 ** Given only minimal private key parameters, fill in the rest of the 71 ** parameters. 72 ** 73 ** 74 ** All the entries, including those supplied by the caller, will be 75 ** overwritten with data alocated out of the arena. 76 ** 77 ** If no arena is supplied, one will be created. 78 ** 79 ** The following fields must be supplied in order for this function 80 ** to succeed: 81 ** one of either publicExponent or privateExponent 82 ** two more of the following 5 parameters (not counting the above). 83 ** modulus (n) 84 ** prime1 (p) 85 ** prime2 (q) 86 ** publicExponent (e) 87 ** privateExponent (d) 88 ** 89 ** NOTE: if only the publicExponent, privateExponent, and one prime is given, 90 ** then there may be more than one RSA key that matches that combination. If 91 ** we find 2 possible valid keys that meet this criteria, we return an error. 92 ** If we return the wrong key, and the original modulus is compared to the 93 ** new modulus, both can be factored by calculateing gcd(n_old,n_new) to get 94 ** the common prime. 95 ** 96 ** NOTE: in some cases the publicExponent must be less than 2^23 for this 97 ** function to work correctly. (The case where we have only one of: modulus 98 ** prime1 and prime2). 99 ** 100 ** All parameters will be replaced in the key structure with new parameters 101 ** allocated out of the arena. There is no attempt to free the old structures. 102 ** prime1 will always be greater than prime2 (even if the caller supplies the 103 ** smaller prime as prime1 or the larger prime as prime2). The parameters are 104 ** not overwritten on failure. 105 ** 106 ** While the remaining Chinese remainder theorem parameters (dp,dp, and qinv) 107 ** can also be used in reconstructing the private key, they are currently 108 ** ignored in this implementation. 109 */ 110 extern SECStatus RSA_PopulatePrivateKey(RSAPrivateKey *key); 111 112 /******************************************************************** 113 ** RSA algorithm 114 */ 115 116 /******************************************************************** 117 ** Raw signing/encryption/decryption operations. 118 ** 119 ** No padding or formatting will be applied. 120 ** inputLen MUST be equivalent to the modulus size (in bytes). 121 */ 122 extern SECStatus 123 RSA_SignRaw(RSAPrivateKey *key, 124 unsigned char *output, 125 unsigned int *outputLen, 126 unsigned int maxOutputLen, 127 const unsigned char *input, 128 unsigned int inputLen); 129 130 extern SECStatus 131 RSA_CheckSignRaw(RSAPublicKey *key, 132 const unsigned char *sig, 133 unsigned int sigLen, 134 const unsigned char *hash, 135 unsigned int hashLen); 136 137 extern SECStatus 138 RSA_CheckSignRecoverRaw(RSAPublicKey *key, 139 unsigned char *data, 140 unsigned int *dataLen, 141 unsigned int maxDataLen, 142 const unsigned char *sig, 143 unsigned int sigLen); 144 145 extern SECStatus 146 RSA_EncryptRaw(RSAPublicKey *key, 147 unsigned char *output, 148 unsigned int *outputLen, 149 unsigned int maxOutputLen, 150 const unsigned char *input, 151 unsigned int inputLen); 152 153 extern SECStatus 154 RSA_DecryptRaw(RSAPrivateKey *key, 155 unsigned char *output, 156 unsigned int *outputLen, 157 unsigned int maxOutputLen, 158 const unsigned char *input, 159 unsigned int inputLen); 160 161 /******************************************************************** 162 ** RSAES-OAEP encryption/decryption, as defined in RFC 3447, Section 7.1. 163 ** 164 ** Note: Only MGF1 is supported as the mask generation function. It will be 165 ** used with maskHashAlg as the inner hash function. 166 ** 167 ** Unless performing Known Answer Tests, "seed" should be NULL, indicating that 168 ** freebl should generate a random value. Otherwise, it should be an octet 169 ** string of seedLen bytes, which should be the same size as the output of 170 ** hashAlg. 171 */ 172 extern SECStatus 173 RSA_EncryptOAEP(RSAPublicKey *key, 174 HASH_HashType hashAlg, 175 HASH_HashType maskHashAlg, 176 const unsigned char *label, 177 unsigned int labelLen, 178 const unsigned char *seed, 179 unsigned int seedLen, 180 unsigned char *output, 181 unsigned int *outputLen, 182 unsigned int maxOutputLen, 183 const unsigned char *input, 184 unsigned int inputLen); 185 186 extern SECStatus 187 RSA_DecryptOAEP(RSAPrivateKey *key, 188 HASH_HashType hashAlg, 189 HASH_HashType maskHashAlg, 190 const unsigned char *label, 191 unsigned int labelLen, 192 unsigned char *output, 193 unsigned int *outputLen, 194 unsigned int maxOutputLen, 195 const unsigned char *input, 196 unsigned int inputLen); 197 198 /******************************************************************** 199 ** RSAES-PKCS1-v1_5 encryption/decryption, as defined in RFC 3447, Section 7.2. 200 */ 201 extern SECStatus 202 RSA_EncryptBlock(RSAPublicKey *key, 203 unsigned char *output, 204 unsigned int *outputLen, 205 unsigned int maxOutputLen, 206 const unsigned char *input, 207 unsigned int inputLen); 208 209 extern SECStatus 210 RSA_DecryptBlock(RSAPrivateKey *key, 211 unsigned char *output, 212 unsigned int *outputLen, 213 unsigned int maxOutputLen, 214 const unsigned char *input, 215 unsigned int inputLen); 216 217 /******************************************************************** 218 ** RSASSA-PSS signing/verifying, as defined in RFC 3447, Section 8.1. 219 ** 220 ** Note: Only MGF1 is supported as the mask generation function. It will be 221 ** used with maskHashAlg as the inner hash function. 222 ** 223 ** Unless performing Known Answer Tests, "salt" should be NULL, indicating that 224 ** freebl should generate a random value. 225 */ 226 extern SECStatus 227 RSA_SignPSS(RSAPrivateKey *key, 228 HASH_HashType hashAlg, 229 HASH_HashType maskHashAlg, 230 const unsigned char *salt, 231 unsigned int saltLen, 232 unsigned char *output, 233 unsigned int *outputLen, 234 unsigned int maxOutputLen, 235 const unsigned char *input, 236 unsigned int inputLen); 237 238 extern SECStatus 239 RSA_CheckSignPSS(RSAPublicKey *key, 240 HASH_HashType hashAlg, 241 HASH_HashType maskHashAlg, 242 unsigned int saltLen, 243 const unsigned char *sig, 244 unsigned int sigLen, 245 const unsigned char *hash, 246 unsigned int hashLen); 247 248 /******************************************************************** 249 ** RSASSA-PKCS1-v1_5 signing/verifying, as defined in RFC 3447, Section 8.2. 250 ** 251 ** These functions expect as input to be the raw value to be signed. For most 252 ** cases using PKCS1-v1_5, this should be the value of T, the DER-encoded 253 ** DigestInfo structure defined in Section 9.2, Step 2. 254 ** Note: This can also be used for signatures that use PKCS1-v1_5 padding, such 255 ** as the signatures used in SSL/TLS, which sign a raw hash. 256 */ 257 extern SECStatus 258 RSA_Sign(RSAPrivateKey *key, 259 unsigned char *output, 260 unsigned int *outputLen, 261 unsigned int maxOutputLen, 262 const unsigned char *data, 263 unsigned int dataLen); 264 265 extern SECStatus 266 RSA_CheckSign(RSAPublicKey *key, 267 const unsigned char *sig, 268 unsigned int sigLen, 269 const unsigned char *data, 270 unsigned int dataLen); 271 272 extern SECStatus 273 RSA_CheckSignRecover(RSAPublicKey *key, 274 unsigned char *output, 275 unsigned int *outputLen, 276 unsigned int maxOutputLen, 277 const unsigned char *sig, 278 unsigned int sigLen); 279 280 /******************************************************************** 281 ** DSA signing algorithm 282 */ 283 284 /* Generate a new random value within the interval [2, q-1]. 285 */ 286 extern SECStatus DSA_NewRandom(PLArenaPool *arena, const SECItem *q, 287 SECItem *random); 288 289 /* 290 ** Generate and return a new DSA public and private key pair, 291 ** both of which are encoded into a single DSAPrivateKey struct. 292 ** "params" is a pointer to the PQG parameters for the domain 293 ** Uses a random seed. 294 */ 295 extern SECStatus DSA_NewKey(const PQGParams *params, 296 DSAPrivateKey **privKey); 297 298 /* signature is caller-supplied buffer of at least 20 bytes. 299 ** On input, signature->len == size of buffer to hold signature. 300 ** digest->len == size of digest. 301 ** On output, signature->len == size of signature in buffer. 302 ** Uses a random seed. 303 */ 304 extern SECStatus DSA_SignDigest(DSAPrivateKey *key, 305 SECItem *signature, 306 const SECItem *digest); 307 308 /* signature is caller-supplied buffer of at least 20 bytes. 309 ** On input, signature->len == size of buffer to hold signature. 310 ** digest->len == size of digest. 311 */ 312 extern SECStatus DSA_VerifyDigest(DSAPublicKey *key, 313 const SECItem *signature, 314 const SECItem *digest); 315 316 /* For FIPS compliance testing. Seed must be exactly 20 bytes long */ 317 extern SECStatus DSA_NewKeyFromSeed(const PQGParams *params, 318 const unsigned char *seed, 319 DSAPrivateKey **privKey); 320 321 /* For FIPS compliance testing. Seed must be exactly 20 bytes. */ 322 extern SECStatus DSA_SignDigestWithSeed(DSAPrivateKey *key, 323 SECItem *signature, 324 const SECItem *digest, 325 const unsigned char *seed); 326 327 /****************************************************** 328 ** Diffie Helman key exchange algorithm 329 */ 330 331 /* Generates parameters for Diffie-Helman key generation. 332 ** primeLen is the length in bytes of prime P to be generated. 333 */ 334 extern SECStatus DH_GenParam(int primeLen, DHParams **params); 335 336 /* Generates a public and private key, both of which are encoded in a single 337 ** DHPrivateKey struct. Params is input, privKey are output. 338 ** This is Phase 1 of Diffie Hellman. 339 */ 340 extern SECStatus DH_NewKey(DHParams *params, 341 DHPrivateKey **privKey); 342 343 /* 344 ** DH_Derive does the Diffie-Hellman phase 2 calculation, using the 345 ** other party's publicValue, and the prime and our privateValue. 346 ** maxOutBytes is the requested length of the generated secret in bytes. 347 ** A zero value means produce a value of any length up to the size of 348 ** the prime. If successful, derivedSecret->data is set 349 ** to the address of the newly allocated buffer containing the derived 350 ** secret, and derivedSecret->len is the size of the secret produced. 351 ** The size of the secret produced will depend on the value of outBytes. 352 ** If outBytes is 0, the key length will be all the significant bytes of 353 ** the derived secret (leading zeros are dropped). This length could be less 354 ** than the length of the prime. If outBytes is nonzero, the length of the 355 ** produced key will be outBytes long. If the key is truncated, the most 356 ** significant bytes are truncated. If it is expanded, zero bytes are added 357 ** at the beginning. 358 ** It is the caller's responsibility to free the allocated buffer 359 ** containing the derived secret. 360 */ 361 extern SECStatus DH_Derive(SECItem *publicValue, 362 SECItem *prime, 363 SECItem *privateValue, 364 SECItem *derivedSecret, 365 unsigned int outBytes); 366 367 /* 368 ** KEA_CalcKey returns octet string with the private key for a dual 369 ** Diffie-Helman key generation as specified for government key exchange. 370 */ 371 extern SECStatus KEA_Derive(SECItem *prime, 372 SECItem *public1, 373 SECItem *public2, 374 SECItem *private1, 375 SECItem *private2, 376 SECItem *derivedSecret); 377 378 /* 379 * verify that a KEA or DSA public key is a valid key for this prime and 380 * subprime domain. 381 */ 382 extern PRBool KEA_Verify(SECItem *Y, SECItem *prime, SECItem *subPrime); 383 384 /* verify a value is prime */ 385 PRBool KEA_PrimeCheck(SECItem *prime); 386 387 /**************************************** 388 * J-PAKE key transport 389 */ 390 391 /* Given gx == g^x, create a Schnorr zero-knowledge proof for the value x 392 * using the specified hash algorithm and signer ID. The signature is 393 * returned in the values gv and r. testRandom must be NULL for a PRNG 394 * generated random committment to be used in the sigature. When testRandom 395 * is non-NULL, that value must contain a value in the subgroup q; that 396 * value will be used instead of a PRNG-generated committment in order to 397 * facilitate known-answer tests. 398 * 399 * If gxIn is non-NULL then it must contain a pre-computed value of g^x that 400 * will be used by the function; in this case, the gxOut parameter must be NULL. 401 * If the gxIn parameter is NULL then gxOut must be non-NULL; in this case 402 * gxOut will contain the value g^x on output. 403 * 404 * gx (if not supplied by the caller), gv, and r will be allocated in the arena. 405 * The arena is *not* optional so do not pass NULL for the arena parameter. 406 * The arena should be zeroed when it is freed. 407 */ 408 SECStatus 409 JPAKE_Sign(PLArenaPool *arena, const PQGParams *pqg, HASH_HashType hashType, 410 const SECItem *signerID, const SECItem *x, 411 const SECItem *testRandom, const SECItem *gxIn, SECItem *gxOut, 412 SECItem *gv, SECItem *r); 413 414 /* Given gx == g^x, verify the Schnorr zero-knowledge proof (gv, r) for the 415 * value x using the specified hash algorithm and signer ID. 416 * 417 * The arena is *not* optional so do not pass NULL for the arena parameter. 418 */ 419 SECStatus 420 JPAKE_Verify(PLArenaPool *arena, const PQGParams *pqg, 421 HASH_HashType hashType, const SECItem *signerID, 422 const SECItem *peerID, const SECItem *gx, 423 const SECItem *gv, const SECItem *r); 424 425 /* Call before round 2 with x2, s, and x2s all non-NULL. This will calculate 426 * base = g^(x1+x3+x4) (mod p) and x2s = x2*s (mod q). The values to send in 427 * round 2 (A and the proof of knowledge of x2s) can then be calculated with 428 * JPAKE_Sign using pqg->base = base and x = x2s. 429 * 430 * Call after round 2 with x2, s, and x2s all NULL, and passing (gx1, gx2, gx3) 431 * instead of (gx1, gx3, gx4). This will calculate base = g^(x1+x2+x3). Then call 432 * JPAKE_Verify with pqg->base = base and then JPAKE_Final. 433 * 434 * base and x2s will be allocated in the arena. The arena is *not* optional so 435 * do not pass NULL for the arena parameter. The arena should be zeroed when it 436 * is freed. 437 */ 438 SECStatus 439 JPAKE_Round2(PLArenaPool *arena, const SECItem *p, const SECItem *q, 440 const SECItem *gx1, const SECItem *gx3, const SECItem *gx4, 441 SECItem *base, const SECItem *x2, const SECItem *s, SECItem *x2s); 442 443 /* K = (B/g^(x2*x4*s))^x2 (mod p) 444 * 445 * K will be allocated in the arena. The arena is *not* optional so do not pass 446 * NULL for the arena parameter. The arena should be zeroed when it is freed. 447 */ 448 SECStatus 449 JPAKE_Final(PLArenaPool *arena, const SECItem *p, const SECItem *q, 450 const SECItem *x2, const SECItem *gx4, const SECItem *x2s, 451 const SECItem *B, SECItem *K); 452 453 /****************************************************** 454 ** Elliptic Curve algorithms 455 */ 456 457 /* Generates a public and private key, both of which are encoded 458 ** in a single ECPrivateKey struct. Params is input, privKey are 459 ** output. 460 */ 461 extern SECStatus EC_NewKey(ECParams *params, 462 ECPrivateKey **privKey); 463 464 extern SECStatus EC_NewKeyFromSeed(ECParams *params, 465 ECPrivateKey **privKey, 466 const unsigned char *seed, 467 int seedlen); 468 469 /* Validates an EC public key as described in Section 5.2.2 of 470 * X9.62. Such validation prevents against small subgroup attacks 471 * when the ECDH primitive is used with the cofactor. 472 */ 473 extern SECStatus EC_ValidatePublicKey(ECParams *params, 474 SECItem *publicValue); 475 476 /* 477 ** ECDH_Derive performs a scalar point multiplication of a point 478 ** representing a (peer's) public key and a large integer representing 479 ** a private key (its own). Both keys must use the same elliptic curve 480 ** parameters. If the withCofactor parameter is true, the 481 ** multiplication also uses the cofactor associated with the curve 482 ** parameters. The output of this scheme is the x-coordinate of the 483 ** resulting point. If successful, derivedSecret->data is set to the 484 ** address of the newly allocated buffer containing the derived 485 ** secret, and derivedSecret->len is the size of the secret 486 ** produced. It is the caller's responsibility to free the allocated 487 ** buffer containing the derived secret. 488 */ 489 extern SECStatus ECDH_Derive(SECItem *publicValue, 490 ECParams *params, 491 SECItem *privateValue, 492 PRBool withCofactor, 493 SECItem *derivedSecret); 494 495 /* On input, signature->len == size of buffer to hold signature. 496 ** digest->len == size of digest. 497 ** On output, signature->len == size of signature in buffer. 498 ** Uses a random seed. 499 */ 500 extern SECStatus ECDSA_SignDigest(ECPrivateKey *key, 501 SECItem *signature, 502 const SECItem *digest); 503 504 /* On input, signature->len == size of buffer to hold signature. 505 ** digest->len == size of digest. 506 */ 507 extern SECStatus ECDSA_VerifyDigest(ECPublicKey *key, 508 const SECItem *signature, 509 const SECItem *digest); 510 511 /* Uses the provided seed. */ 512 extern SECStatus ECDSA_SignDigestWithSeed(ECPrivateKey *key, 513 SECItem *signature, 514 const SECItem *digest, 515 const unsigned char *seed, 516 const int seedlen); 517 518 /******************************************/ 519 /* 520 ** RC4 symmetric stream cypher 521 */ 522 523 /* 524 ** Create a new RC4 context suitable for RC4 encryption/decryption. 525 ** "key" raw key data 526 ** "len" the number of bytes of key data 527 */ 528 extern RC4Context *RC4_CreateContext(const unsigned char *key, int len); 529 530 extern RC4Context *RC4_AllocateContext(void); 531 extern SECStatus RC4_InitContext(RC4Context *cx, 532 const unsigned char *key, 533 unsigned int keylen, 534 const unsigned char *, 535 int, 536 unsigned int, 537 unsigned int); 538 539 /* 540 ** Destroy an RC4 encryption/decryption context. 541 ** "cx" the context 542 ** "freeit" if PR_TRUE then free the object as well as its sub-objects 543 */ 544 extern void RC4_DestroyContext(RC4Context *cx, PRBool freeit); 545 546 /* 547 ** Perform RC4 encryption. 548 ** "cx" the context 549 ** "output" the output buffer to store the encrypted data. 550 ** "outputLen" how much data is stored in "output". Set by the routine 551 ** after some data is stored in output. 552 ** "maxOutputLen" the maximum amount of data that can ever be 553 ** stored in "output" 554 ** "input" the input data 555 ** "inputLen" the amount of input data 556 */ 557 extern SECStatus RC4_Encrypt(RC4Context *cx, unsigned char *output, 558 unsigned int *outputLen, unsigned int maxOutputLen, 559 const unsigned char *input, unsigned int inputLen); 560 561 /* 562 ** Perform RC4 decryption. 563 ** "cx" the context 564 ** "output" the output buffer to store the decrypted data. 565 ** "outputLen" how much data is stored in "output". Set by the routine 566 ** after some data is stored in output. 567 ** "maxOutputLen" the maximum amount of data that can ever be 568 ** stored in "output" 569 ** "input" the input data 570 ** "inputLen" the amount of input data 571 */ 572 extern SECStatus RC4_Decrypt(RC4Context *cx, unsigned char *output, 573 unsigned int *outputLen, unsigned int maxOutputLen, 574 const unsigned char *input, unsigned int inputLen); 575 576 /******************************************/ 577 /* 578 ** RC2 symmetric block cypher 579 */ 580 581 /* 582 ** Create a new RC2 context suitable for RC2 encryption/decryption. 583 ** "key" raw key data 584 ** "len" the number of bytes of key data 585 ** "iv" is the CBC initialization vector (if mode is NSS_RC2_CBC) 586 ** "mode" one of NSS_RC2 or NSS_RC2_CBC 587 ** "effectiveKeyLen" is the effective key length (as specified in 588 ** RFC 2268) in bytes (not bits). 589 ** 590 ** When mode is set to NSS_RC2_CBC the RC2 cipher is run in "cipher block 591 ** chaining" mode. 592 */ 593 extern RC2Context *RC2_CreateContext(const unsigned char *key, unsigned int len, 594 const unsigned char *iv, int mode, 595 unsigned effectiveKeyLen); 596 extern RC2Context *RC2_AllocateContext(void); 597 extern SECStatus RC2_InitContext(RC2Context *cx, 598 const unsigned char *key, 599 unsigned int keylen, 600 const unsigned char *iv, 601 int mode, 602 unsigned int effectiveKeyLen, 603 unsigned int); 604 605 /* 606 ** Destroy an RC2 encryption/decryption context. 607 ** "cx" the context 608 ** "freeit" if PR_TRUE then free the object as well as its sub-objects 609 */ 610 extern void RC2_DestroyContext(RC2Context *cx, PRBool freeit); 611 612 /* 613 ** Perform RC2 encryption. 614 ** "cx" the context 615 ** "output" the output buffer to store the encrypted data. 616 ** "outputLen" how much data is stored in "output". Set by the routine 617 ** after some data is stored in output. 618 ** "maxOutputLen" the maximum amount of data that can ever be 619 ** stored in "output" 620 ** "input" the input data 621 ** "inputLen" the amount of input data 622 */ 623 extern SECStatus RC2_Encrypt(RC2Context *cx, unsigned char *output, 624 unsigned int *outputLen, unsigned int maxOutputLen, 625 const unsigned char *input, unsigned int inputLen); 626 627 /* 628 ** Perform RC2 decryption. 629 ** "cx" the context 630 ** "output" the output buffer to store the decrypted data. 631 ** "outputLen" how much data is stored in "output". Set by the routine 632 ** after some data is stored in output. 633 ** "maxOutputLen" the maximum amount of data that can ever be 634 ** stored in "output" 635 ** "input" the input data 636 ** "inputLen" the amount of input data 637 */ 638 extern SECStatus RC2_Decrypt(RC2Context *cx, unsigned char *output, 639 unsigned int *outputLen, unsigned int maxOutputLen, 640 const unsigned char *input, unsigned int inputLen); 641 642 /******************************************/ 643 /* 644 ** RC5 symmetric block cypher -- 64-bit block size 645 */ 646 647 /* 648 ** Create a new RC5 context suitable for RC5 encryption/decryption. 649 ** "key" raw key data 650 ** "len" the number of bytes of key data 651 ** "iv" is the CBC initialization vector (if mode is NSS_RC5_CBC) 652 ** "mode" one of NSS_RC5 or NSS_RC5_CBC 653 ** 654 ** When mode is set to NSS_RC5_CBC the RC5 cipher is run in "cipher block 655 ** chaining" mode. 656 */ 657 extern RC5Context *RC5_CreateContext(const SECItem *key, unsigned int rounds, 658 unsigned int wordSize, const unsigned char *iv, int mode); 659 extern RC5Context *RC5_AllocateContext(void); 660 extern SECStatus RC5_InitContext(RC5Context *cx, 661 const unsigned char *key, 662 unsigned int keylen, 663 const unsigned char *iv, 664 int mode, 665 unsigned int rounds, 666 unsigned int wordSize); 667 668 /* 669 ** Destroy an RC5 encryption/decryption context. 670 ** "cx" the context 671 ** "freeit" if PR_TRUE then free the object as well as its sub-objects 672 */ 673 extern void RC5_DestroyContext(RC5Context *cx, PRBool freeit); 674 675 /* 676 ** Perform RC5 encryption. 677 ** "cx" the context 678 ** "output" the output buffer to store the encrypted data. 679 ** "outputLen" how much data is stored in "output". Set by the routine 680 ** after some data is stored in output. 681 ** "maxOutputLen" the maximum amount of data that can ever be 682 ** stored in "output" 683 ** "input" the input data 684 ** "inputLen" the amount of input data 685 */ 686 extern SECStatus RC5_Encrypt(RC5Context *cx, unsigned char *output, 687 unsigned int *outputLen, unsigned int maxOutputLen, 688 const unsigned char *input, unsigned int inputLen); 689 690 /* 691 ** Perform RC5 decryption. 692 ** "cx" the context 693 ** "output" the output buffer to store the decrypted data. 694 ** "outputLen" how much data is stored in "output". Set by the routine 695 ** after some data is stored in output. 696 ** "maxOutputLen" the maximum amount of data that can ever be 697 ** stored in "output" 698 ** "input" the input data 699 ** "inputLen" the amount of input data 700 */ 701 702 extern SECStatus RC5_Decrypt(RC5Context *cx, unsigned char *output, 703 unsigned int *outputLen, unsigned int maxOutputLen, 704 const unsigned char *input, unsigned int inputLen); 705 706 /******************************************/ 707 /* 708 ** DES symmetric block cypher 709 */ 710 711 /* 712 ** Create a new DES context suitable for DES encryption/decryption. 713 ** "key" raw key data 714 ** "len" the number of bytes of key data 715 ** "iv" is the CBC initialization vector (if mode is NSS_DES_CBC or 716 ** mode is DES_EDE3_CBC) 717 ** "mode" one of NSS_DES, NSS_DES_CBC, NSS_DES_EDE3 or NSS_DES_EDE3_CBC 718 ** "encrypt" is PR_TRUE if the context will be used for encryption 719 ** 720 ** When mode is set to NSS_DES_CBC or NSS_DES_EDE3_CBC then the DES 721 ** cipher is run in "cipher block chaining" mode. 722 */ 723 extern DESContext *DES_CreateContext(const unsigned char *key, 724 const unsigned char *iv, 725 int mode, PRBool encrypt); 726 extern DESContext *DES_AllocateContext(void); 727 extern SECStatus DES_InitContext(DESContext *cx, 728 const unsigned char *key, 729 unsigned int keylen, 730 const unsigned char *iv, 731 int mode, 732 unsigned int encrypt, 733 unsigned int); 734 735 /* 736 ** Destroy an DES encryption/decryption context. 737 ** "cx" the context 738 ** "freeit" if PR_TRUE then free the object as well as its sub-objects 739 */ 740 extern void DES_DestroyContext(DESContext *cx, PRBool freeit); 741 742 /* 743 ** Perform DES encryption. 744 ** "cx" the context 745 ** "output" the output buffer to store the encrypted data. 746 ** "outputLen" how much data is stored in "output". Set by the routine 747 ** after some data is stored in output. 748 ** "maxOutputLen" the maximum amount of data that can ever be 749 ** stored in "output" 750 ** "input" the input data 751 ** "inputLen" the amount of input data 752 ** 753 ** NOTE: the inputLen must be a multiple of DES_KEY_LENGTH 754 */ 755 extern SECStatus DES_Encrypt(DESContext *cx, unsigned char *output, 756 unsigned int *outputLen, unsigned int maxOutputLen, 757 const unsigned char *input, unsigned int inputLen); 758 759 /* 760 ** Perform DES decryption. 761 ** "cx" the context 762 ** "output" the output buffer to store the decrypted data. 763 ** "outputLen" how much data is stored in "output". Set by the routine 764 ** after some data is stored in output. 765 ** "maxOutputLen" the maximum amount of data that can ever be 766 ** stored in "output" 767 ** "input" the input data 768 ** "inputLen" the amount of input data 769 ** 770 ** NOTE: the inputLen must be a multiple of DES_KEY_LENGTH 771 */ 772 extern SECStatus DES_Decrypt(DESContext *cx, unsigned char *output, 773 unsigned int *outputLen, unsigned int maxOutputLen, 774 const unsigned char *input, unsigned int inputLen); 775 776 /******************************************/ 777 /* 778 ** SEED symmetric block cypher 779 */ 780 extern SEEDContext * 781 SEED_CreateContext(const unsigned char *key, const unsigned char *iv, 782 int mode, PRBool encrypt); 783 extern SEEDContext *SEED_AllocateContext(void); 784 extern SECStatus SEED_InitContext(SEEDContext *cx, 785 const unsigned char *key, 786 unsigned int keylen, 787 const unsigned char *iv, 788 int mode, unsigned int encrypt, 789 unsigned int); 790 extern void SEED_DestroyContext(SEEDContext *cx, PRBool freeit); 791 extern SECStatus 792 SEED_Encrypt(SEEDContext *cx, unsigned char *output, 793 unsigned int *outputLen, unsigned int maxOutputLen, 794 const unsigned char *input, unsigned int inputLen); 795 extern SECStatus 796 SEED_Decrypt(SEEDContext *cx, unsigned char *output, 797 unsigned int *outputLen, unsigned int maxOutputLen, 798 const unsigned char *input, unsigned int inputLen); 799 800 /******************************************/ 801 /* 802 ** AES symmetric block cypher (Rijndael) 803 */ 804 805 /* 806 ** Create a new AES context suitable for AES encryption/decryption. 807 ** "key" raw key data 808 ** "keylen" the number of bytes of key data (16, 24, or 32) 809 ** "blocklen" is the blocksize to use. NOTE: only 16 is supported! 810 */ 811 extern AESContext * 812 AES_CreateContext(const unsigned char *key, const unsigned char *iv, 813 int mode, int encrypt, 814 unsigned int keylen, unsigned int blocklen); 815 extern AESContext *AES_AllocateContext(void); 816 extern SECStatus AES_InitContext(AESContext *cx, 817 const unsigned char *key, 818 unsigned int keylen, 819 const unsigned char *iv, 820 int mode, 821 unsigned int encrypt, 822 unsigned int blocklen); 823 824 /* 825 ** Destroy a AES encryption/decryption context. 826 ** "cx" the context 827 ** "freeit" if PR_TRUE then free the object as well as its sub-objects 828 */ 829 extern void 830 AES_DestroyContext(AESContext *cx, PRBool freeit); 831 832 /* 833 ** Perform AES encryption. 834 ** "cx" the context 835 ** "output" the output buffer to store the encrypted data. 836 ** "outputLen" how much data is stored in "output". Set by the routine 837 ** after some data is stored in output. 838 ** "maxOutputLen" the maximum amount of data that can ever be 839 ** stored in "output" 840 ** "input" the input data 841 ** "inputLen" the amount of input data 842 */ 843 extern SECStatus 844 AES_Encrypt(AESContext *cx, unsigned char *output, 845 unsigned int *outputLen, unsigned int maxOutputLen, 846 const unsigned char *input, unsigned int inputLen); 847 848 /* 849 ** Perform AES decryption. 850 ** "cx" the context 851 ** "output" the output buffer to store the decrypted data. 852 ** "outputLen" how much data is stored in "output". Set by the routine 853 ** after some data is stored in output. 854 ** "maxOutputLen" the maximum amount of data that can ever be 855 ** stored in "output" 856 ** "input" the input data 857 ** "inputLen" the amount of input data 858 */ 859 extern SECStatus 860 AES_Decrypt(AESContext *cx, unsigned char *output, 861 unsigned int *outputLen, unsigned int maxOutputLen, 862 const unsigned char *input, unsigned int inputLen); 863 /* 864 ** Perform AES AEAD operation (either encrypt or decrypt), controlled by 865 ** the context. 866 ** "cx" the context 867 ** "output" the output buffer to store the encrypted data. 868 ** "outputLen" how much data is stored in "output". Set by the routine 869 ** after some data is stored in output. 870 ** "maxOutputLen" the maximum amount of data that can ever be 871 ** stored in "output" 872 ** "input" the input data 873 ** "inputLen" the amount of input data 874 ** "params" pointer to an AEAD specific param PKCS #11 param structure 875 ** "paramsLen" length of the param structure pointed to by params 876 ** "aad" addition authenticated data 877 ** "aadLen" the amount of additional authenticated data. 878 */ 879 extern SECStatus 880 AES_AEAD(AESContext *cx, unsigned char *output, 881 unsigned int *outputLen, unsigned int maxOutputLen, 882 const unsigned char *input, unsigned int inputLen, 883 void *params, unsigned int paramsLen, 884 const unsigned char *aad, unsigned int aadLen); 885 886 /******************************************/ 887 /* 888 ** AES key wrap algorithm, RFC 3394 889 */ 890 891 /* 892 ** Create a new AES context suitable for AES encryption/decryption. 893 ** "key" raw key data 894 ** "iv" The 8 byte "initial value" 895 ** "encrypt", a boolean, true for key wrapping, false for unwrapping. 896 ** "keylen" the number of bytes of key data (16, 24, or 32) 897 */ 898 extern AESKeyWrapContext * 899 AESKeyWrap_CreateContext(const unsigned char *key, const unsigned char *iv, 900 int encrypt, unsigned int keylen); 901 extern AESKeyWrapContext *AESKeyWrap_AllocateContext(void); 902 extern SECStatus 903 AESKeyWrap_InitContext(AESKeyWrapContext *cx, 904 const unsigned char *key, 905 unsigned int keylen, 906 const unsigned char *iv, 907 int, 908 unsigned int encrypt, 909 unsigned int); 910 911 /* 912 ** Destroy a AES KeyWrap context. 913 ** "cx" the context 914 ** "freeit" if PR_TRUE then free the object as well as its sub-objects 915 */ 916 extern void 917 AESKeyWrap_DestroyContext(AESKeyWrapContext *cx, PRBool freeit); 918 919 /* 920 ** Perform AES key wrap. 921 ** "cx" the context 922 ** "output" the output buffer to store the encrypted data. 923 ** "outputLen" how much data is stored in "output". Set by the routine 924 ** after some data is stored in output. 925 ** "maxOutputLen" the maximum amount of data that can ever be 926 ** stored in "output" 927 ** "input" the input data 928 ** "inputLen" the amount of input data 929 */ 930 extern SECStatus 931 AESKeyWrap_Encrypt(AESKeyWrapContext *cx, unsigned char *output, 932 unsigned int *outputLen, unsigned int maxOutputLen, 933 const unsigned char *input, unsigned int inputLen); 934 935 /* 936 ** Perform AES key unwrap. 937 ** "cx" the context 938 ** "output" the output buffer to store the decrypted data. 939 ** "outputLen" how much data is stored in "output". Set by the routine 940 ** after some data is stored in output. 941 ** "maxOutputLen" the maximum amount of data that can ever be 942 ** stored in "output" 943 ** "input" the input data 944 ** "inputLen" the amount of input data 945 */ 946 extern SECStatus 947 AESKeyWrap_Decrypt(AESKeyWrapContext *cx, unsigned char *output, 948 unsigned int *outputLen, unsigned int maxOutputLen, 949 const unsigned char *input, unsigned int inputLen); 950 951 /* 952 ** Perform AES padded key wrap. 953 ** "cx" the context 954 ** "output" the output buffer to store the encrypted data. 955 ** "outputLen" how much data is stored in "output". Set by the routine 956 ** after some data is stored in output. 957 ** "maxOutputLen" the maximum amount of data that can ever be 958 ** stored in "output" 959 ** "input" the input data 960 ** "inputLen" the amount of input data 961 */ 962 extern SECStatus 963 AESKeyWrap_EncryptKWP(AESKeyWrapContext *cx, unsigned char *output, 964 unsigned int *outputLen, unsigned int maxOutputLen, 965 const unsigned char *input, unsigned int inputLen); 966 967 /* 968 ** Perform AES padded key unwrap. 969 ** "cx" the context 970 ** "output" the output buffer to store the decrypted data. 971 ** "outputLen" how much data is stored in "output". Set by the routine 972 ** after some data is stored in output. 973 ** "maxOutputLen" the maximum amount of data that can ever be 974 ** stored in "output" 975 ** "input" the input data 976 ** "inputLen" the amount of input data 977 */ 978 extern SECStatus 979 AESKeyWrap_DecryptKWP(AESKeyWrapContext *cx, unsigned char *output, 980 unsigned int *outputLen, unsigned int maxOutputLen, 981 const unsigned char *input, unsigned int inputLen); 982 983 /******************************************/ 984 /* 985 ** Camellia symmetric block cypher 986 */ 987 988 /* 989 ** Create a new Camellia context suitable for Camellia encryption/decryption. 990 ** "key" raw key data 991 ** "keylen" the number of bytes of key data (16, 24, or 32) 992 */ 993 extern CamelliaContext * 994 Camellia_CreateContext(const unsigned char *key, const unsigned char *iv, 995 int mode, int encrypt, unsigned int keylen); 996 997 extern CamelliaContext *Camellia_AllocateContext(void); 998 extern SECStatus Camellia_InitContext(CamelliaContext *cx, 999 const unsigned char *key, 1000 unsigned int keylen, 1001 const unsigned char *iv, 1002 int mode, 1003 unsigned int encrypt, 1004 unsigned int unused); 1005 /* 1006 ** Destroy a Camellia encryption/decryption context. 1007 ** "cx" the context 1008 ** "freeit" if PR_TRUE then free the object as well as its sub-objects 1009 */ 1010 extern void 1011 Camellia_DestroyContext(CamelliaContext *cx, PRBool freeit); 1012 1013 /* 1014 ** Perform Camellia encryption. 1015 ** "cx" the context 1016 ** "output" the output buffer to store the encrypted data. 1017 ** "outputLen" how much data is stored in "output". Set by the routine 1018 ** after some data is stored in output. 1019 ** "maxOutputLen" the maximum amount of data that can ever be 1020 ** stored in "output" 1021 ** "input" the input data 1022 ** "inputLen" the amount of input data 1023 */ 1024 extern SECStatus 1025 Camellia_Encrypt(CamelliaContext *cx, unsigned char *output, 1026 unsigned int *outputLen, unsigned int maxOutputLen, 1027 const unsigned char *input, unsigned int inputLen); 1028 1029 /* 1030 ** Perform Camellia decryption. 1031 ** "cx" the context 1032 ** "output" the output buffer to store the decrypted data. 1033 ** "outputLen" how much data is stored in "output". Set by the routine 1034 ** after some data is stored in output. 1035 ** "maxOutputLen" the maximum amount of data that can ever be 1036 ** stored in "output" 1037 ** "input" the input data 1038 ** "inputLen" the amount of input data 1039 */ 1040 extern SECStatus 1041 Camellia_Decrypt(CamelliaContext *cx, unsigned char *output, 1042 unsigned int *outputLen, unsigned int maxOutputLen, 1043 const unsigned char *input, unsigned int inputLen); 1044 1045 /******************************************/ 1046 /* 1047 ** ChaCha20 block cipher 1048 */ 1049 1050 extern SECStatus ChaCha20_InitContext(ChaCha20Context *ctx, 1051 const unsigned char *key, 1052 unsigned int keyLen, 1053 const unsigned char *nonce, 1054 unsigned int nonceLen, 1055 PRUint32 ctr); 1056 1057 extern ChaCha20Context *ChaCha20_CreateContext(const unsigned char *key, 1058 unsigned int keyLen, 1059 const unsigned char *nonce, 1060 unsigned int nonceLen, 1061 PRUint32 ctr); 1062 1063 extern void ChaCha20_DestroyContext(ChaCha20Context *ctx, PRBool freeit); 1064 1065 /******************************************/ 1066 /* 1067 ** ChaCha20+Poly1305 AEAD 1068 */ 1069 1070 extern SECStatus ChaCha20Poly1305_InitContext(ChaCha20Poly1305Context *ctx, 1071 const unsigned char *key, 1072 unsigned int keyLen, 1073 unsigned int tagLen); 1074 1075 extern ChaCha20Poly1305Context *ChaCha20Poly1305_CreateContext( 1076 const unsigned char *key, unsigned int keyLen, unsigned int tagLen); 1077 1078 extern void ChaCha20Poly1305_DestroyContext(ChaCha20Poly1305Context *ctx, 1079 PRBool freeit); 1080 1081 extern SECStatus ChaCha20Poly1305_Seal( 1082 const ChaCha20Poly1305Context *ctx, unsigned char *output, 1083 unsigned int *outputLen, unsigned int maxOutputLen, 1084 const unsigned char *input, unsigned int inputLen, 1085 const unsigned char *nonce, unsigned int nonceLen, 1086 const unsigned char *ad, unsigned int adLen); 1087 1088 extern SECStatus ChaCha20Poly1305_Open( 1089 const ChaCha20Poly1305Context *ctx, unsigned char *output, 1090 unsigned int *outputLen, unsigned int maxOutputLen, 1091 const unsigned char *input, unsigned int inputLen, 1092 const unsigned char *nonce, unsigned int nonceLen, 1093 const unsigned char *ad, unsigned int adLen); 1094 1095 extern SECStatus ChaCha20Poly1305_Encrypt( 1096 const ChaCha20Poly1305Context *ctx, unsigned char *output, 1097 unsigned int *outputLen, unsigned int maxOutputLen, 1098 const unsigned char *input, unsigned int inputLen, 1099 const unsigned char *nonce, unsigned int nonceLen, 1100 const unsigned char *ad, unsigned int adLen, unsigned char *tagOut); 1101 1102 extern SECStatus ChaCha20Poly1305_Decrypt( 1103 const ChaCha20Poly1305Context *ctx, unsigned char *output, 1104 unsigned int *outputLen, unsigned int maxOutputLen, 1105 const unsigned char *input, unsigned int inputLen, 1106 const unsigned char *nonce, unsigned int nonceLen, 1107 const unsigned char *ad, unsigned int adLen, unsigned char *tagIn); 1108 1109 extern SECStatus ChaCha20_Xor( 1110 unsigned char *output, const unsigned char *block, unsigned int len, 1111 const unsigned char *k, const unsigned char *nonce, PRUint32 ctr); 1112 1113 /******************************************/ 1114 /* 1115 ** MD5 secure hash function 1116 */ 1117 1118 /* 1119 ** Hash a null terminated string "src" into "dest" using MD5 1120 */ 1121 extern SECStatus MD5_Hash(unsigned char *dest, const char *src); 1122 1123 /* 1124 ** Hash a non-null terminated string "src" into "dest" using MD5 1125 */ 1126 extern SECStatus MD5_HashBuf(unsigned char *dest, const unsigned char *src, 1127 PRUint32 src_length); 1128 1129 /* 1130 ** Create a new MD5 context 1131 */ 1132 extern MD5Context *MD5_NewContext(void); 1133 1134 /* 1135 ** Destroy an MD5 secure hash context. 1136 ** "cx" the context 1137 ** "freeit" if PR_TRUE then free the object as well as its sub-objects 1138 */ 1139 extern void MD5_DestroyContext(MD5Context *cx, PRBool freeit); 1140 1141 /* 1142 ** Reset an MD5 context, preparing it for a fresh round of hashing 1143 */ 1144 extern void MD5_Begin(MD5Context *cx); 1145 1146 /* 1147 ** Update the MD5 hash function with more data. 1148 ** "cx" the context 1149 ** "input" the data to hash 1150 ** "inputLen" the amount of data to hash 1151 */ 1152 extern void MD5_Update(MD5Context *cx, 1153 const unsigned char *input, unsigned int inputLen); 1154 1155 /* 1156 ** Finish the MD5 hash function. Produce the digested results in "digest" 1157 ** "cx" the context 1158 ** "digest" where the 16 bytes of digest data are stored 1159 ** "digestLen" where the digest length (16) is stored 1160 ** "maxDigestLen" the maximum amount of data that can ever be 1161 ** stored in "digest" 1162 */ 1163 extern void MD5_End(MD5Context *cx, unsigned char *digest, 1164 unsigned int *digestLen, unsigned int maxDigestLen); 1165 1166 /* 1167 ** Export the current state of the MD5 hash without appending the standard 1168 ** padding and length bytes. Produce the digested results in "digest" 1169 ** "cx" the context 1170 ** "digest" where the 16 bytes of digest data are stored 1171 ** "digestLen" where the digest length (16) is stored (optional) 1172 ** "maxDigestLen" the maximum amount of data that can ever be 1173 ** stored in "digest" 1174 */ 1175 extern void MD5_EndRaw(MD5Context *cx, unsigned char *digest, 1176 unsigned int *digestLen, unsigned int maxDigestLen); 1177 1178 /* 1179 * Return the the size of a buffer needed to flatten the MD5 Context into 1180 * "cx" the context 1181 * returns size; 1182 */ 1183 extern unsigned int MD5_FlattenSize(MD5Context *cx); 1184 1185 /* 1186 * Flatten the MD5 Context into a buffer: 1187 * "cx" the context 1188 * "space" the buffer to flatten to 1189 * returns status; 1190 */ 1191 extern SECStatus MD5_Flatten(MD5Context *cx, unsigned char *space); 1192 1193 /* 1194 * Resurrect a flattened context into a MD5 Context 1195 * "space" the buffer of the flattend buffer 1196 * "arg" ptr to void used by cryptographic resurrect 1197 * returns resurected context; 1198 */ 1199 extern MD5Context *MD5_Resurrect(unsigned char *space, void *arg); 1200 extern void MD5_Clone(MD5Context *dest, MD5Context *src); 1201 1202 /* 1203 ** trace the intermediate state info of the MD5 hash. 1204 */ 1205 extern void MD5_TraceState(MD5Context *cx); 1206 1207 /******************************************/ 1208 /* 1209 ** MD2 secure hash function 1210 */ 1211 1212 /* 1213 ** Hash a null terminated string "src" into "dest" using MD2 1214 */ 1215 extern SECStatus MD2_Hash(unsigned char *dest, const char *src); 1216 1217 /* 1218 ** Create a new MD2 context 1219 */ 1220 extern MD2Context *MD2_NewContext(void); 1221 1222 /* 1223 ** Destroy an MD2 secure hash context. 1224 ** "cx" the context 1225 ** "freeit" if PR_TRUE then free the object as well as its sub-objects 1226 */ 1227 extern void MD2_DestroyContext(MD2Context *cx, PRBool freeit); 1228 1229 /* 1230 ** Reset an MD2 context, preparing it for a fresh round of hashing 1231 */ 1232 extern void MD2_Begin(MD2Context *cx); 1233 1234 /* 1235 ** Update the MD2 hash function with more data. 1236 ** "cx" the context 1237 ** "input" the data to hash 1238 ** "inputLen" the amount of data to hash 1239 */ 1240 extern void MD2_Update(MD2Context *cx, 1241 const unsigned char *input, unsigned int inputLen); 1242 1243 /* 1244 ** Finish the MD2 hash function. Produce the digested results in "digest" 1245 ** "cx" the context 1246 ** "digest" where the 16 bytes of digest data are stored 1247 ** "digestLen" where the digest length (16) is stored 1248 ** "maxDigestLen" the maximum amount of data that can ever be 1249 ** stored in "digest" 1250 */ 1251 extern void MD2_End(MD2Context *cx, unsigned char *digest, 1252 unsigned int *digestLen, unsigned int maxDigestLen); 1253 1254 /* 1255 * Return the the size of a buffer needed to flatten the MD2 Context into 1256 * "cx" the context 1257 * returns size; 1258 */ 1259 extern unsigned int MD2_FlattenSize(MD2Context *cx); 1260 1261 /* 1262 * Flatten the MD2 Context into a buffer: 1263 * "cx" the context 1264 * "space" the buffer to flatten to 1265 * returns status; 1266 */ 1267 extern SECStatus MD2_Flatten(MD2Context *cx, unsigned char *space); 1268 1269 /* 1270 * Resurrect a flattened context into a MD2 Context 1271 * "space" the buffer of the flattend buffer 1272 * "arg" ptr to void used by cryptographic resurrect 1273 * returns resurected context; 1274 */ 1275 extern MD2Context *MD2_Resurrect(unsigned char *space, void *arg); 1276 extern void MD2_Clone(MD2Context *dest, MD2Context *src); 1277 1278 /******************************************/ 1279 /* 1280 ** SHA-1 secure hash function 1281 */ 1282 1283 /* 1284 ** Hash a null terminated string "src" into "dest" using SHA-1 1285 */ 1286 extern SECStatus SHA1_Hash(unsigned char *dest, const char *src); 1287 1288 /* 1289 ** Hash a non-null terminated string "src" into "dest" using SHA-1 1290 */ 1291 extern SECStatus SHA1_HashBuf(unsigned char *dest, const unsigned char *src, 1292 PRUint32 src_length); 1293 1294 /* 1295 ** Create a new SHA-1 context 1296 */ 1297 extern SHA1Context *SHA1_NewContext(void); 1298 1299 /* 1300 ** Destroy a SHA-1 secure hash context. 1301 ** "cx" the context 1302 ** "freeit" if PR_TRUE then free the object as well as its sub-objects 1303 */ 1304 extern void SHA1_DestroyContext(SHA1Context *cx, PRBool freeit); 1305 1306 /* 1307 ** Reset a SHA-1 context, preparing it for a fresh round of hashing 1308 */ 1309 extern void SHA1_Begin(SHA1Context *cx); 1310 1311 /* 1312 ** Update the SHA-1 hash function with more data. 1313 ** "cx" the context 1314 ** "input" the data to hash 1315 ** "inputLen" the amount of data to hash 1316 */ 1317 extern void SHA1_Update(SHA1Context *cx, const unsigned char *input, 1318 unsigned int inputLen); 1319 1320 /* 1321 ** Finish the SHA-1 hash function. Produce the digested results in "digest" 1322 ** "cx" the context 1323 ** "digest" where the 16 bytes of digest data are stored 1324 ** "digestLen" where the digest length (20) is stored 1325 ** "maxDigestLen" the maximum amount of data that can ever be 1326 ** stored in "digest" 1327 */ 1328 extern void SHA1_End(SHA1Context *cx, unsigned char *digest, 1329 unsigned int *digestLen, unsigned int maxDigestLen); 1330 1331 /* 1332 ** Export the current state of the SHA-1 hash without appending the standard 1333 ** padding and length bytes. Produce the digested results in "digest" 1334 ** "cx" the context 1335 ** "digest" where the 20 bytes of digest data are stored 1336 ** "digestLen" where the digest length (20) is stored (optional) 1337 ** "maxDigestLen" the maximum amount of data that can ever be 1338 ** stored in "digest" 1339 */ 1340 extern void SHA1_EndRaw(SHA1Context *cx, unsigned char *digest, 1341 unsigned int *digestLen, unsigned int maxDigestLen); 1342 1343 /* 1344 ** trace the intermediate state info of the SHA1 hash. 1345 */ 1346 extern void SHA1_TraceState(SHA1Context *cx); 1347 1348 /* 1349 * Return the the size of a buffer needed to flatten the SHA-1 Context into 1350 * "cx" the context 1351 * returns size; 1352 */ 1353 extern unsigned int SHA1_FlattenSize(SHA1Context *cx); 1354 1355 /* 1356 * Flatten the SHA-1 Context into a buffer: 1357 * "cx" the context 1358 * "space" the buffer to flatten to 1359 * returns status; 1360 */ 1361 extern SECStatus SHA1_Flatten(SHA1Context *cx, unsigned char *space); 1362 1363 /* 1364 * Resurrect a flattened context into a SHA-1 Context 1365 * "space" the buffer of the flattend buffer 1366 * "arg" ptr to void used by cryptographic resurrect 1367 * returns resurected context; 1368 */ 1369 extern SHA1Context *SHA1_Resurrect(unsigned char *space, void *arg); 1370 extern void SHA1_Clone(SHA1Context *dest, SHA1Context *src); 1371 1372 /******************************************/ 1373 1374 /******************************************/ 1375 /* 1376 ** SHA-2 secure hash function 1377 ** The SHA-2 family includes SHA224, SHA256, SHA384, and SHA512 1378 */ 1379 1380 extern SHA224Context *SHA224_NewContext(void); 1381 extern void SHA224_DestroyContext(SHA224Context *cx, PRBool freeit); 1382 extern void SHA224_Begin(SHA224Context *cx); 1383 extern void SHA224_Update(SHA224Context *cx, const unsigned char *input, 1384 unsigned int inputLen); 1385 extern void SHA224_End(SHA224Context *cx, unsigned char *digest, 1386 unsigned int *digestLen, unsigned int maxDigestLen); 1387 /* 1388 ** Export the current state of the SHA-224 hash without appending the standard 1389 ** padding and length bytes. Produce the digested results in "digest" 1390 ** "cx" the context 1391 ** "digest" where the 28 bytes of digest data are stored 1392 ** "digestLen" where the digest length (28) is stored (optional) 1393 ** "maxDigestLen" the maximum amount of data that can ever be 1394 ** stored in "digest" 1395 */ 1396 extern void SHA224_EndRaw(SHA224Context *cx, unsigned char *digest, 1397 unsigned int *digestLen, unsigned int maxDigestLen); 1398 extern SECStatus SHA224_HashBuf(unsigned char *dest, const unsigned char *src, 1399 PRUint32 src_length); 1400 extern SECStatus SHA224_Hash(unsigned char *dest, const char *src); 1401 extern void SHA224_TraceState(SHA224Context *cx); 1402 extern unsigned int SHA224_FlattenSize(SHA224Context *cx); 1403 extern SECStatus SHA224_Flatten(SHA224Context *cx, unsigned char *space); 1404 extern SHA224Context *SHA224_Resurrect(unsigned char *space, void *arg); 1405 extern void SHA224_Clone(SHA224Context *dest, SHA224Context *src); 1406 1407 /******************************************/ 1408 1409 extern SHA256Context *SHA256_NewContext(void); 1410 extern void SHA256_DestroyContext(SHA256Context *cx, PRBool freeit); 1411 extern void SHA256_Begin(SHA256Context *cx); 1412 extern void SHA256_Update(SHA256Context *cx, const unsigned char *input, 1413 unsigned int inputLen); 1414 extern void SHA256_End(SHA256Context *cx, unsigned char *digest, 1415 unsigned int *digestLen, unsigned int maxDigestLen); 1416 /* 1417 ** Export the current state of the SHA-256 hash without appending the standard 1418 ** padding and length bytes. Produce the digested results in "digest" 1419 ** "cx" the context 1420 ** "digest" where the 32 bytes of digest data are stored 1421 ** "digestLen" where the digest length (32) is stored (optional) 1422 ** "maxDigestLen" the maximum amount of data that can ever be 1423 ** stored in "digest" 1424 */ 1425 extern void SHA256_EndRaw(SHA256Context *cx, unsigned char *digest, 1426 unsigned int *digestLen, unsigned int maxDigestLen); 1427 extern SECStatus SHA256_HashBuf(unsigned char *dest, const unsigned char *src, 1428 PRUint32 src_length); 1429 extern SECStatus SHA256_Hash(unsigned char *dest, const char *src); 1430 extern void SHA256_TraceState(SHA256Context *cx); 1431 extern unsigned int SHA256_FlattenSize(SHA256Context *cx); 1432 extern SECStatus SHA256_Flatten(SHA256Context *cx, unsigned char *space); 1433 extern SHA256Context *SHA256_Resurrect(unsigned char *space, void *arg); 1434 extern void SHA256_Clone(SHA256Context *dest, SHA256Context *src); 1435 1436 /******************************************/ 1437 1438 extern SHA512Context *SHA512_NewContext(void); 1439 extern void SHA512_DestroyContext(SHA512Context *cx, PRBool freeit); 1440 extern void SHA512_Begin(SHA512Context *cx); 1441 extern void SHA512_Update(SHA512Context *cx, const unsigned char *input, 1442 unsigned int inputLen); 1443 /* 1444 ** Export the current state of the SHA-512 hash without appending the standard 1445 ** padding and length bytes. Produce the digested results in "digest" 1446 ** "cx" the context 1447 ** "digest" where the 64 bytes of digest data are stored 1448 ** "digestLen" where the digest length (64) is stored (optional) 1449 ** "maxDigestLen" the maximum amount of data that can ever be 1450 ** stored in "digest" 1451 */ 1452 extern void SHA512_EndRaw(SHA512Context *cx, unsigned char *digest, 1453 unsigned int *digestLen, unsigned int maxDigestLen); 1454 extern void SHA512_End(SHA512Context *cx, unsigned char *digest, 1455 unsigned int *digestLen, unsigned int maxDigestLen); 1456 extern SECStatus SHA512_HashBuf(unsigned char *dest, const unsigned char *src, 1457 PRUint32 src_length); 1458 extern SECStatus SHA512_Hash(unsigned char *dest, const char *src); 1459 extern void SHA512_TraceState(SHA512Context *cx); 1460 extern unsigned int SHA512_FlattenSize(SHA512Context *cx); 1461 extern SECStatus SHA512_Flatten(SHA512Context *cx, unsigned char *space); 1462 extern SHA512Context *SHA512_Resurrect(unsigned char *space, void *arg); 1463 extern void SHA512_Clone(SHA512Context *dest, SHA512Context *src); 1464 1465 /******************************************/ 1466 1467 extern SHA384Context *SHA384_NewContext(void); 1468 extern void SHA384_DestroyContext(SHA384Context *cx, PRBool freeit); 1469 extern void SHA384_Begin(SHA384Context *cx); 1470 extern void SHA384_Update(SHA384Context *cx, const unsigned char *input, 1471 unsigned int inputLen); 1472 extern void SHA384_End(SHA384Context *cx, unsigned char *digest, 1473 unsigned int *digestLen, unsigned int maxDigestLen); 1474 /* 1475 ** Export the current state of the SHA-384 hash without appending the standard 1476 ** padding and length bytes. Produce the digested results in "digest" 1477 ** "cx" the context 1478 ** "digest" where the 48 bytes of digest data are stored 1479 ** "digestLen" where the digest length (48) is stored (optional) 1480 ** "maxDigestLen" the maximum amount of data that can ever be 1481 ** stored in "digest" 1482 */ 1483 extern void SHA384_EndRaw(SHA384Context *cx, unsigned char *digest, 1484 unsigned int *digestLen, unsigned int maxDigestLen); 1485 extern SECStatus SHA384_HashBuf(unsigned char *dest, const unsigned char *src, 1486 PRUint32 src_length); 1487 extern SECStatus SHA384_Hash(unsigned char *dest, const char *src); 1488 extern void SHA384_TraceState(SHA384Context *cx); 1489 extern unsigned int SHA384_FlattenSize(SHA384Context *cx); 1490 extern SECStatus SHA384_Flatten(SHA384Context *cx, unsigned char *space); 1491 extern SHA384Context *SHA384_Resurrect(unsigned char *space, void *arg); 1492 extern void SHA384_Clone(SHA384Context *dest, SHA384Context *src); 1493 1494 /******************************************/ 1495 /* 1496 ** SHA-3 secure hash function 1497 ** The SHA-3 family includes SHA3_224, SHA3_256, SHA3_384, and SHA3_512 1498 */ 1499 1500 extern SHA3_224Context *SHA3_224_NewContext(void); 1501 extern void SHA3_224_DestroyContext(SHA3_224Context *cx, PRBool freeit); 1502 extern unsigned int SHA3_224_FlattenSize(SHA3_224Context *cx); 1503 extern void SHA3_224_Begin(SHA3_224Context *cx); 1504 extern void SHA3_224_Update(SHA3_224Context *cx, const unsigned char *input, 1505 unsigned int inputLen); 1506 extern void SHA3_224_End(SHA3_224Context *cx, unsigned char *digest, 1507 unsigned int *digestLen, unsigned int maxDigestLen); 1508 1509 extern SECStatus SHA3_224_HashBuf(unsigned char *dest, const unsigned char *src, 1510 PRUint32 src_length); 1511 extern SECStatus SHA3_224_Hash(unsigned char *dest, const char *src); 1512 1513 /******************************************/ 1514 1515 extern SHA3_256Context *SHA3_256_NewContext(void); 1516 extern void SHA3_256_DestroyContext(SHA3_256Context *cx, PRBool freeit); 1517 extern unsigned int SHA3_256_FlattenSize(SHA3_256Context *cx); 1518 extern void SHA3_256_Begin(SHA3_256Context *cx); 1519 extern void SHA3_256_Update(SHA3_256Context *cx, const unsigned char *input, 1520 unsigned int inputLen); 1521 extern void SHA3_256_End(SHA3_256Context *cx, unsigned char *digest, 1522 unsigned int *digestLen, unsigned int maxDigestLen); 1523 1524 extern SECStatus SHA3_256_HashBuf(unsigned char *dest, const unsigned char *src, 1525 PRUint32 src_length); 1526 extern SECStatus SHA3_256_Hash(unsigned char *dest, const char *src); 1527 1528 /******************************************/ 1529 1530 extern SHA3_384Context *SHA3_384_NewContext(void); 1531 extern void SHA3_384_DestroyContext(SHA3_384Context *cx, PRBool freeit); 1532 extern unsigned int SHA3_384_FlattenSize(SHA3_384Context *cx); 1533 extern void SHA3_384_Begin(SHA3_384Context *cx); 1534 extern void SHA3_384_Update(SHA3_384Context *cx, const unsigned char *input, 1535 unsigned int inputLen); 1536 extern void SHA3_384_End(SHA3_384Context *cx, unsigned char *digest, 1537 unsigned int *digestLen, unsigned int maxDigestLen); 1538 1539 extern SECStatus SHA3_384_HashBuf(unsigned char *dest, const unsigned char *src, 1540 PRUint32 src_length); 1541 extern SECStatus SHA3_384_Hash(unsigned char *dest, const char *src); 1542 1543 /******************************************/ 1544 1545 extern SHA3_512Context *SHA3_512_NewContext(void); 1546 extern void SHA3_512_DestroyContext(SHA3_512Context *cx, PRBool freeit); 1547 extern unsigned int SHA3_512_FlattenSize(SHA3_512Context *cx); 1548 extern void SHA3_512_Begin(SHA3_512Context *cx); 1549 extern void SHA3_512_Update(SHA3_512Context *cx, const unsigned char *input, 1550 unsigned int inputLen); 1551 extern void SHA3_512_End(SHA3_512Context *cx, unsigned char *digest, 1552 unsigned int *digestLen, unsigned int maxDigestLen); 1553 1554 extern SECStatus SHA3_512_HashBuf(unsigned char *dest, const unsigned char *src, 1555 PRUint32 src_length); 1556 extern SECStatus SHA3_512_Hash(unsigned char *dest, const char *src); 1557 1558 /******************************************/ 1559 /* 1560 ** SHAKE XOF functions from SHA-3 1561 ** The SHAKE family includes SHAKE_128 and SHAKE_256 1562 */ 1563 1564 extern SHAKE_128Context *SHAKE_128_NewContext(void); 1565 extern void SHAKE_128_DestroyContext(SHAKE_128Context *cx, PRBool freeit); 1566 extern void SHAKE_128_Begin(SHAKE_128Context *cx); 1567 extern void SHAKE_128_Absorb(SHAKE_128Context *cx, const unsigned char *input, 1568 unsigned int inputLen); 1569 extern void SHAKE_128_SqueezeEnd(SHAKE_128Context *cx, unsigned char *digest, 1570 unsigned int digestLen); 1571 extern SECStatus SHAKE_128_HashBuf(unsigned char *dest, unsigned int dest_len, 1572 const unsigned char *src, PRUint32 src_length); 1573 extern SECStatus SHAKE_128_Hash(unsigned char *dest, unsigned int dest_len, const char *src); 1574 1575 /******************************************/ 1576 1577 extern SHAKE_256Context *SHAKE_256_NewContext(void); 1578 extern void SHAKE_256_DestroyContext(SHAKE_256Context *cx, PRBool freeit); 1579 extern void SHAKE_256_Begin(SHAKE_256Context *cx); 1580 extern void SHAKE_256_Absorb(SHAKE_256Context *cx, const unsigned char *input, 1581 unsigned int inputLen); 1582 extern void SHAKE_256_SqueezeEnd(SHAKE_256Context *cx, unsigned char *digest, 1583 unsigned int digestLen); 1584 extern SECStatus SHAKE_256_HashBuf(unsigned char *dest, unsigned int dest_len, 1585 const unsigned char *src, PRUint32 src_length); 1586 extern SECStatus SHAKE_256_Hash(unsigned char *dest, unsigned int dest_len, const char *src); 1587 1588 /**************************************** 1589 * implement TLS 1.0 Pseudo Random Function (PRF) and TLS P_hash function 1590 */ 1591 1592 extern SECStatus 1593 TLS_PRF(const SECItem *secret, const char *label, SECItem *seed, 1594 SECItem *result, PRBool isFIPS); 1595 1596 extern SECStatus 1597 TLS_P_hash(HASH_HashType hashAlg, const SECItem *secret, const char *label, 1598 SECItem *seed, SECItem *result, PRBool isFIPS); 1599 1600 /******************************************/ 1601 /* 1602 ** Implements the Blake2b hash function. 1603 */ 1604 1605 /* 1606 ** Hash a null terminated string "src" into "dest" using Blake2b 1607 */ 1608 extern SECStatus BLAKE2B_Hash(unsigned char *dest, const char *src); 1609 1610 /* 1611 ** Hash a non-null terminated string "src" into "dest" using Blake2b 1612 */ 1613 extern SECStatus BLAKE2B_HashBuf(unsigned char *output, 1614 const unsigned char *input, PRUint32 inlen); 1615 1616 extern SECStatus BLAKE2B_MAC_HashBuf(unsigned char *output, 1617 const unsigned char *input, 1618 unsigned int inlen, 1619 const unsigned char *key, 1620 unsigned int keylen); 1621 1622 /* 1623 ** Create a new Blake2b context 1624 */ 1625 extern BLAKE2BContext *BLAKE2B_NewContext(void); 1626 1627 /* 1628 ** Destroy a Blake2b secure hash context. 1629 ** "ctx" the context 1630 ** "freeit" if PR_TRUE then free the object as well as its sub-objects 1631 */ 1632 extern void BLAKE2B_DestroyContext(BLAKE2BContext *ctx, PRBool freeit); 1633 1634 /* 1635 ** Reset a Blake2b context, preparing it for a fresh round of hashing 1636 */ 1637 extern SECStatus BLAKE2B_Begin(BLAKE2BContext *ctx); 1638 1639 extern SECStatus BLAKE2B_MAC_Begin(BLAKE2BContext *ctx, const PRUint8 *key, 1640 const size_t keylen); 1641 1642 /* 1643 ** Update the Blake hash function with more data. 1644 */ 1645 extern SECStatus BLAKE2B_Update(BLAKE2BContext *ctx, const unsigned char *in, 1646 unsigned int inlen); 1647 1648 /* 1649 ** Finish the Blake hash function. Produce the digested results in "digest" 1650 */ 1651 extern SECStatus BLAKE2B_End(BLAKE2BContext *ctx, unsigned char *out, 1652 unsigned int *digestLen, size_t maxDigestLen); 1653 1654 /* 1655 * Return the size of a buffer needed to flatten the Blake2b Context into 1656 * "ctx" the context 1657 * returns size; 1658 */ 1659 extern unsigned int BLAKE2B_FlattenSize(BLAKE2BContext *ctx); 1660 1661 /* 1662 * Flatten the Blake2b Context into a buffer: 1663 * "ctx" the context 1664 * "space" the buffer to flatten to 1665 * returns status; 1666 */ 1667 extern SECStatus BLAKE2B_Flatten(BLAKE2BContext *ctx, unsigned char *space); 1668 1669 /* 1670 * Resurrect a flattened context into a Blake2b Context 1671 * "space" the buffer of the flattend buffer 1672 * "arg" ptr to void used by cryptographic resurrect 1673 * returns resurected context 1674 */ 1675 extern BLAKE2BContext *BLAKE2B_Resurrect(unsigned char *space, void *arg); 1676 extern void BLAKE2B_Clone(BLAKE2BContext *dest, BLAKE2BContext *src); 1677 1678 /******************************************/ 1679 /* 1680 ** Pseudo Random Number Generation. FIPS compliance desirable. 1681 */ 1682 1683 /* 1684 ** Initialize the global RNG context and give it some seed input taken 1685 ** from the system. This function is thread-safe and will only allow 1686 ** the global context to be initialized once. The seed input is likely 1687 ** small, so it is imperative that RNG_RandomUpdate() be called with 1688 ** additional seed data before the generator is used. A good way to 1689 ** provide the generator with additional entropy is to call 1690 ** RNG_SystemInfoForRNG(). Note that NSS_Init() does exactly that. 1691 */ 1692 extern SECStatus RNG_RNGInit(void); 1693 1694 /* 1695 ** Update the global random number generator with more seeding 1696 ** material 1697 */ 1698 extern SECStatus RNG_RandomUpdate(const void *data, size_t bytes); 1699 1700 /* 1701 ** Generate some random bytes, using the global random number generator 1702 ** object. 1703 */ 1704 extern SECStatus RNG_GenerateGlobalRandomBytes(void *dest, size_t len); 1705 1706 /* Destroy the global RNG context. After a call to RNG_RNGShutdown() 1707 ** a call to RNG_RNGInit() is required in order to use the generator again, 1708 ** along with seed data (see the comment above RNG_RNGInit()). 1709 */ 1710 extern void RNG_RNGShutdown(void); 1711 1712 extern void RNG_SystemInfoForRNG(void); 1713 1714 /* 1715 * FIPS 186-2 Change Notice 1 RNG Algorithm 1, used both to 1716 * generate the DSA X parameter and as a generic purpose RNG. 1717 * 1718 * The following two FIPS186Change functions are needed for 1719 * NIST RNG Validation System. 1720 */ 1721 1722 /* 1723 * FIPS186Change_GenerateX is now deprecated. It will return SECFailure with 1724 * the error set to PR_NOT_IMPLEMENTED_ERROR. 1725 */ 1726 extern SECStatus 1727 FIPS186Change_GenerateX(unsigned char *XKEY, 1728 const unsigned char *XSEEDj, 1729 unsigned char *x_j); 1730 1731 /* 1732 * When generating the DSA X parameter, we generate 2*GSIZE bytes 1733 * of random output and reduce it mod q. 1734 * 1735 * Input: w, 2*GSIZE bytes 1736 * q, DSA_SUBPRIME_LEN bytes 1737 * Output: xj, DSA_SUBPRIME_LEN bytes 1738 */ 1739 extern SECStatus 1740 FIPS186Change_ReduceModQForDSA(const unsigned char *w, 1741 const unsigned char *q, 1742 unsigned char *xj); 1743 1744 /* To allow NIST KAT tests */ 1745 extern SECStatus 1746 PRNGTEST_Instantiate_Kat(const PRUint8 *entropy, unsigned int entropy_len, 1747 const PRUint8 *nonce, unsigned int nonce_len, 1748 const PRUint8 *personal_string, unsigned int ps_len); 1749 1750 /* 1751 * The following functions are for FIPS poweron self test and FIPS algorithm 1752 * testing. 1753 */ 1754 extern SECStatus 1755 PRNGTEST_Instantiate(const PRUint8 *entropy, unsigned int entropy_len, 1756 const PRUint8 *nonce, unsigned int nonce_len, 1757 const PRUint8 *personal_string, unsigned int ps_len); 1758 1759 extern SECStatus 1760 PRNGTEST_Reseed(const PRUint8 *entropy, unsigned int entropy_len, 1761 const PRUint8 *additional, unsigned int additional_len); 1762 1763 extern SECStatus 1764 PRNGTEST_Generate(PRUint8 *bytes, unsigned int bytes_len, 1765 const PRUint8 *additional, unsigned int additional_len); 1766 1767 extern SECStatus 1768 PRNGTEST_Uninstantiate(void); 1769 1770 extern SECStatus 1771 PRNGTEST_RunHealthTests(void); 1772 1773 /* Generate PQGParams and PQGVerify structs. 1774 * Length of seed and length of h both equal length of P. 1775 * All lengths are specified by "j", according to the table above. 1776 * 1777 * The verify parameters will conform to FIPS186-1. 1778 */ 1779 extern SECStatus 1780 PQG_ParamGen(unsigned int j, /* input : determines length of P. */ 1781 PQGParams **pParams, /* output: P Q and G returned here */ 1782 PQGVerify **pVfy); /* output: counter and seed. */ 1783 1784 /* Generate PQGParams and PQGVerify structs. 1785 * Length of P specified by j. Length of h will match length of P. 1786 * Length of SEED in bytes specified in seedBytes. 1787 * seedBbytes must be in the range [20..255] or an error will result. 1788 * 1789 * The verify parameters will conform to FIPS186-1. 1790 */ 1791 extern SECStatus 1792 PQG_ParamGenSeedLen( 1793 unsigned int j, /* input : determines length of P. */ 1794 unsigned int seedBytes, /* input : length of seed in bytes.*/ 1795 PQGParams **pParams, /* output: P Q and G returned here */ 1796 PQGVerify **pVfy); /* output: counter and seed. */ 1797 1798 /* Generate PQGParams and PQGVerify structs. 1799 * Length of P specified by L in bits. 1800 * Length of Q specified by N in bits. 1801 * Length of SEED in bytes specified in seedBytes. 1802 * seedBbytes must be in the range [N..L*2] or an error will result. 1803 * 1804 * Not that J uses the above table, L is the length exact. L and N must 1805 * match the table below or an error will result: 1806 * 1807 * L N 1808 * 1024 160 1809 * 2048 224 1810 * 2048 256 1811 * 3072 256 1812 * 1813 * If N or seedBytes are set to zero, then PQG_ParamGenSeedLen will 1814 * pick a default value (typically the smallest secure value for these 1815 * variables). 1816 * 1817 * The verify parameters will conform to FIPS186-3 using the smallest 1818 * permissible hash for the key strength. 1819 */ 1820 extern SECStatus 1821 PQG_ParamGenV2( 1822 unsigned int L, /* input : determines length of P. */ 1823 unsigned int N, /* input : determines length of Q. */ 1824 unsigned int seedBytes, /* input : length of seed in bytes.*/ 1825 PQGParams **pParams, /* output: P Q and G returned here */ 1826 PQGVerify **pVfy); /* output: counter and seed. */ 1827 1828 /* Test PQGParams for validity as DSS PQG values. 1829 * If vfy is non-NULL, test PQGParams to make sure they were generated 1830 * using the specified seed, counter, and h values. 1831 * 1832 * Return value indicates whether Verification operation ran successfully 1833 * to completion, but does not indicate if PQGParams are valid or not. 1834 * If return value is SECSuccess, then *pResult has these meanings: 1835 * SECSuccess: PQGParams are valid. 1836 * SECFailure: PQGParams are invalid. 1837 * 1838 * Verify the PQG againts the counter, SEED and h. 1839 * These tests are specified in FIPS 186-3 Appendix A.1.1.1, A.1.1.3, and A.2.2 1840 * PQG_VerifyParams will automatically choose the appropriate test. 1841 */ 1842 1843 extern SECStatus PQG_VerifyParams(const PQGParams *params, 1844 const PQGVerify *vfy, SECStatus *result); 1845 1846 extern void PQG_DestroyParams(PQGParams *params); 1847 1848 extern void PQG_DestroyVerify(PQGVerify *vfy); 1849 1850 /* 1851 * clean-up any global tables freebl may have allocated after it starts up. 1852 * This function is not thread safe and should be called only after the 1853 * library has been quiessed. 1854 */ 1855 extern void BL_Cleanup(void); 1856 1857 /* unload freebl shared library from memory */ 1858 extern void BL_Unload(void); 1859 1860 /************************************************************************** 1861 * Verify a given Shared library signature * 1862 **************************************************************************/ 1863 PRBool BLAPI_SHVerify(const char *name, PRFuncPtr addr); 1864 1865 /************************************************************************** 1866 * Verify a given filename's signature * 1867 **************************************************************************/ 1868 PRBool BLAPI_SHVerifyFile(const char *shName); 1869 1870 /************************************************************************** 1871 * Verify Are Own Shared library signature * 1872 **************************************************************************/ 1873 PRBool BLAPI_VerifySelf(const char *name); 1874 1875 /*********************************************************************/ 1876 extern const SECHashObject *HASH_GetRawHashObject(HASH_HashType hashType); 1877 1878 extern void BL_SetForkState(PRBool forked); 1879 1880 /* 1881 ** pepare an ECParam structure from DEREncoded params 1882 */ 1883 extern SECStatus EC_FillParams(PLArenaPool *arena, 1884 const SECItem *encodedParams, ECParams *params); 1885 extern SECStatus EC_DecodeParams(const SECItem *encodedParams, 1886 ECParams **ecparams); 1887 extern SECStatus EC_CopyParams(PLArenaPool *arena, ECParams *dstParams, 1888 const ECParams *srcParams); 1889 1890 /* 1891 * use the internal table to get the size in bytes of a single EC point 1892 */ 1893 extern int EC_GetPointSize(const ECParams *params); 1894 1895 /* 1896 * use the internal table to get the size in bytes of a single EC coordinate 1897 */ 1898 extern int EC_GetScalarSize(const ECParams *params); 1899 1900 /* Generate a Kyber key pair with parameters given by |params|. If |seed| is 1901 * null this function generates its own randomness internally, otherwise the 1902 * key is derived from |seed| using the method defined by |params|. The caller 1903 * is responsible for allocating appropriately sized `privKey` and `pubKey` 1904 * items. 1905 */ 1906 extern SECStatus Kyber_NewKey(KyberParams params, const SECItem *seed, SECItem *privKey, SECItem *pubKey); 1907 1908 /* Encapsulate a random secret to the Kyber public key `pubKey`. If `seed` is 1909 * null this function generates its own randomness internally, otherwise the 1910 * secret is derived from `seed` using the method defined by `params`. The 1911 * caller is responsible for allocating appropriately sized `ciphertext` and 1912 * `secret` items. Returns an error if any arguments' length is incompatible 1913 * with `params`. 1914 */ 1915 extern SECStatus Kyber_Encapsulate(KyberParams params, const SECItem *seed, const SECItem *pubKey, SECItem *ciphertext, SECItem *secret); 1916 1917 /* Decapsulate a secret from a Kyber ciphertext `ciphertext` using the private 1918 * key `privKey`. The caller is responsible for allocating an appropriately sized 1919 * `secret` item. Returns an error if any arguments' length is incompatible 1920 * with `params`. 1921 */ 1922 extern SECStatus Kyber_Decapsulate(KyberParams params, const SECItem *privKey, const SECItem *ciphertext, SECItem *secret); 1923 1924 /* EdDSA (only ed25519) 1925 ** On input, msg == buffer containing message to be signed. 1926 ** key == key to be used for signature. 1927 ** Output, signature == Buffer containing the signature. 1928 */ 1929 extern SECStatus ED_SignMessage(ECPrivateKey *key, SECItem *signature, 1930 const SECItem *msg); 1931 1932 /* On input, signature == buffer holding the signature. 1933 ** msg == buffer holding the message. 1934 ** key == key used to verify the signature. 1935 ** Output, whether the signature is valid or not. 1936 */ 1937 extern SECStatus ED_VerifyMessage(ECPublicKey *key, const SECItem *signature, 1938 const SECItem *msg); 1939 1940 /* EdDSA (only ed25519) 1941 * Derive the public key `publicKey` from the private key `privateKey`. 1942 */ 1943 extern SECStatus ED_DerivePublicKey(const SECItem *privateKey, SECItem *publicKey); 1944 1945 extern SECStatus X25519_DerivePublicKey(const SECItem *privateKey, SECItem *publicKey); 1946 1947 /* Public key derivation is supported only for the curves supporting pt_mul method. */ 1948 extern SECStatus EC_DerivePublicKey(const SECItem *privateKey, const ECParams *ecParams, SECItem *publicKey); 1949 1950 /* 1951 * ML_DSA functions 1952 */ 1953 SECStatus MLDSA_NewKey(CK_ML_DSA_PARAMETER_SET_TYPE paramSet, SECItem *seed, 1954 MLDSAPrivateKey *privKey, MLDSAPublicKey *pubKey); 1955 SECStatus MLDSA_SignInit(MLDSAPrivateKey *key, CK_HEDGE_TYPE hedgeType, 1956 const SECItem *sgnCtx, MLDSAContext **ctx); 1957 SECStatus MLDSA_SignUpdate(MLDSAContext *ctx, const SECItem *data); 1958 SECStatus MLDSA_SignFinal(MLDSAContext *ctx, SECItem *signature); 1959 1960 SECStatus MLDSA_VerifyInit(MLDSAPublicKey *key, const SECItem *sgnCtx, 1961 MLDSAContext **ctx); 1962 SECStatus MLDSA_VerifyUpdate(MLDSAContext *ctx, const SECItem *data); 1963 SECStatus MLDSA_VerifyFinal(MLDSAContext *ctx, const SECItem *signature); 1964 1965 /* Decompress public key. 1966 ** On input, publicCompressed == buffer containing compressed key 1967 ** Output, publicRaw == decompressed public key. The function returns true iff the 1968 ** decompressed key belongs to the appropriate curve. 1969 */ 1970 SECStatus EC_DecompressPublicKey(const SECItem *publicCompressed, const ECParams *params, SECItem *publicUncompressed); 1971 1972 SEC_END_PROTOS 1973 1974 #endif /* _BLAPI_H_ */