andre@0: /* andre@0: * cryptohi.h - public prototypes for the crypto library andre@0: * andre@0: * This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #ifndef _CRYPTOHI_H_ andre@0: #define _CRYPTOHI_H_ andre@0: andre@0: #include "blapit.h" andre@0: andre@0: #include "seccomon.h" andre@0: #include "secoidt.h" andre@0: #include "secdert.h" andre@0: #include "cryptoht.h" andre@0: #include "keyt.h" andre@0: #include "certt.h" andre@0: andre@0: andre@0: SEC_BEGIN_PROTOS andre@0: andre@0: andre@0: /****************************************/ andre@0: /* andre@0: ** DER encode/decode (EC)DSA signatures andre@0: */ andre@0: andre@0: /* ANSI X9.57 defines DSA signatures as DER encoded data. Our DSA1 code (and andre@0: * most of the rest of the world) just generates 40 bytes of raw data. These andre@0: * functions convert between formats. andre@0: */ andre@0: extern SECStatus DSAU_EncodeDerSig(SECItem *dest, SECItem *src); andre@0: extern SECItem *DSAU_DecodeDerSig(const SECItem *item); andre@0: andre@0: /* andre@0: * Unlike DSA1, raw DSA2 and ECDSA signatures do not have a fixed length. andre@0: * Rather they contain two integers r and s whose length depends andre@0: * on the size of q or the EC key used for signing. andre@0: * andre@0: * We can reuse the DSAU_EncodeDerSig interface to DER encode andre@0: * raw ECDSA signature keeping in mind that the length of r andre@0: * is the same as that of s and exactly half of src->len. andre@0: * andre@0: * For decoding, we need to pass the length of the desired andre@0: * raw signature (twice the key size) explicitly. andre@0: */ andre@0: extern SECStatus DSAU_EncodeDerSigWithLen(SECItem *dest, SECItem *src, andre@0: unsigned int len); andre@0: extern SECItem *DSAU_DecodeDerSigToLen(const SECItem *item, unsigned int len); andre@0: andre@0: /****************************************/ andre@0: /* andre@0: ** Signature creation operations andre@0: */ andre@0: andre@0: /* andre@0: ** Create a new signature context used for signing a data stream. andre@0: ** "alg" the signature algorithm to use (e.g. SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION) andre@0: ** "privKey" the private key to use andre@0: */ andre@0: extern SGNContext *SGN_NewContext(SECOidTag alg, SECKEYPrivateKey *privKey); andre@0: andre@0: /* andre@0: ** Destroy a signature-context object andre@0: ** "cx" the object andre@0: ** "freeit" if PR_TRUE then free the object as well as its sub-objects andre@0: */ andre@0: extern void SGN_DestroyContext(SGNContext *cx, PRBool freeit); andre@0: andre@0: /* andre@0: ** Reset the signing context "cx" to its initial state, preparing it for andre@0: ** another stream of data. andre@0: */ andre@0: extern SECStatus SGN_Begin(SGNContext *cx); andre@0: andre@0: /* andre@0: ** Update the signing context with more data to sign. andre@0: ** "cx" the context andre@0: ** "input" the input data to sign andre@0: ** "inputLen" the length of the input data andre@0: */ andre@0: extern SECStatus SGN_Update(SGNContext *cx, const unsigned char *input, andre@0: unsigned int inputLen); andre@0: andre@0: /* andre@0: ** Finish the signature process. Use either k0 or k1 to sign the data andre@0: ** stream that was input using SGN_Update. The resulting signature is andre@0: ** formatted using PKCS#1 and then encrypted using RSA private or public andre@0: ** encryption. andre@0: ** "cx" the context andre@0: ** "result" the final signature data (memory is allocated) andre@0: */ andre@0: extern SECStatus SGN_End(SGNContext *cx, SECItem *result); andre@0: andre@0: /* andre@0: ** Sign a single block of data using private key encryption and given andre@0: ** signature/hash algorithm. andre@0: ** "result" the final signature data (memory is allocated) andre@0: ** "buf" the input data to sign andre@0: ** "len" the amount of data to sign andre@0: ** "pk" the private key to encrypt with andre@0: ** "algid" the signature/hash algorithm to sign with andre@0: ** (must be compatible with the key type). andre@0: */ andre@0: extern SECStatus SEC_SignData(SECItem *result, andre@0: const unsigned char *buf, int len, andre@0: SECKEYPrivateKey *pk, SECOidTag algid); andre@0: andre@0: /* andre@0: ** Sign a pre-digested block of data using private key encryption, encoding andre@0: ** The given signature/hash algorithm. andre@0: ** "result" the final signature data (memory is allocated) andre@0: ** "digest" the digest to sign andre@0: ** "privKey" the private key to encrypt with andre@0: ** "algtag" The algorithm tag to encode (need for RSA only) andre@0: */ andre@0: extern SECStatus SGN_Digest(SECKEYPrivateKey *privKey, andre@0: SECOidTag algtag, SECItem *result, SECItem *digest); andre@0: andre@0: /* andre@0: ** DER sign a single block of data using private key encryption and the andre@0: ** MD5 hashing algorithm. This routine first computes a digital signature andre@0: ** using SEC_SignData, then wraps it with an CERTSignedData and then der andre@0: ** encodes the result. andre@0: ** "arena" is the memory arena to use to allocate data from andre@0: ** "result" the final der encoded data (memory is allocated) andre@0: ** "buf" the input data to sign andre@0: ** "len" the amount of data to sign andre@0: ** "pk" the private key to encrypt with andre@0: */ andre@0: extern SECStatus SEC_DerSignData(PLArenaPool *arena, SECItem *result, andre@0: const unsigned char *buf, int len, andre@0: SECKEYPrivateKey *pk, SECOidTag algid); andre@0: andre@0: /* andre@0: ** Destroy a signed-data object. andre@0: ** "sd" the object andre@0: ** "freeit" if PR_TRUE then free the object as well as its sub-objects andre@0: */ andre@0: extern void SEC_DestroySignedData(CERTSignedData *sd, PRBool freeit); andre@0: andre@0: /* andre@0: ** Get the signature algorithm tag number for the given key type and hash andre@0: ** algorithm tag. Returns SEC_OID_UNKNOWN if key type and hash algorithm andre@0: ** do not match or are not supported. andre@0: */ andre@0: extern SECOidTag SEC_GetSignatureAlgorithmOidTag(KeyType keyType, andre@0: SECOidTag hashAlgTag); andre@0: andre@0: /****************************************/ andre@0: /* andre@0: ** Signature verification operations andre@0: */ andre@0: andre@0: /* andre@0: ** Create a signature verification context. This version is deprecated, andre@0: ** This function is deprecated. Use VFY_CreateContextDirect or andre@0: ** VFY_CreateContextWithAlgorithmID instead. andre@0: ** "key" the public key to verify with andre@0: ** "sig" the encrypted signature data if sig is NULL then andre@0: ** VFY_EndWithSignature must be called with the correct signature at andre@0: ** the end of the processing. andre@0: ** "sigAlg" specifies the signing algorithm to use (including the andre@0: ** hash algorthim). This must match the key type. andre@0: ** "wincx" void pointer to the window context andre@0: */ andre@0: extern VFYContext *VFY_CreateContext(SECKEYPublicKey *key, SECItem *sig, andre@0: SECOidTag sigAlg, void *wincx); andre@0: /* andre@0: ** Create a signature verification context. andre@0: ** "key" the public key to verify with andre@0: ** "sig" the encrypted signature data if sig is NULL then andre@0: ** VFY_EndWithSignature must be called with the correct signature at andre@0: ** the end of the processing. andre@0: ** "pubkAlg" specifies the cryptographic signing algorithm to use (the andre@0: ** raw algorithm without any hash specified. This must match the key andre@0: ** type. andre@0: ** "hashAlg" specifies the hashing algorithm used. If the key is an andre@0: ** RSA key, and sig is not NULL, then hashAlg can be SEC_OID_UNKNOWN. andre@0: ** the hash is selected from data in the sig. andre@0: ** "hash" optional pointer to return the actual hash algorithm used. andre@0: ** in practice this should always match the passed in hashAlg (the andre@0: ** exception is the case where hashAlg is SEC_OID_UNKNOWN above). andre@0: ** If this value is NULL no, hash oid is returned. andre@0: ** "wincx" void pointer to the window context andre@0: */ andre@0: extern VFYContext *VFY_CreateContextDirect(const SECKEYPublicKey *key, andre@0: const SECItem *sig, andre@0: SECOidTag pubkAlg, andre@0: SECOidTag hashAlg, andre@0: SECOidTag *hash, void *wincx); andre@0: /* andre@0: ** Create a signature verification context from a algorithm ID. andre@0: ** "key" the public key to verify with andre@0: ** "sig" the encrypted signature data if sig is NULL then andre@0: ** VFY_EndWithSignature must be called with the correct signature at andre@0: ** the end of the processing. andre@0: ** "algid" specifies the signing algorithm and parameters to use. andre@0: ** This must match the key type. andre@0: ** "hash" optional pointer to return the oid of the actual hash used in andre@0: ** the signature. If this value is NULL no, hash oid is returned. andre@0: ** "wincx" void pointer to the window context andre@0: */ andre@0: extern VFYContext *VFY_CreateContextWithAlgorithmID(const SECKEYPublicKey *key, andre@0: const SECItem *sig, andre@0: const SECAlgorithmID *algid, andre@0: SECOidTag *hash, andre@0: void *wincx); andre@0: andre@0: /* andre@0: ** Destroy a verification-context object. andre@0: ** "cx" the context to destroy andre@0: ** "freeit" if PR_TRUE then free the object as well as its sub-objects andre@0: */ andre@0: extern void VFY_DestroyContext(VFYContext *cx, PRBool freeit); andre@0: andre@0: extern SECStatus VFY_Begin(VFYContext *cx); andre@0: andre@0: /* andre@0: ** Update a verification context with more input data. The input data andre@0: ** is fed to a secure hash function (depending on what was in the andre@0: ** encrypted signature data). andre@0: ** "cx" the context andre@0: ** "input" the input data andre@0: ** "inputLen" the amount of input data andre@0: */ andre@0: extern SECStatus VFY_Update(VFYContext *cx, const unsigned char *input, andre@0: unsigned int inputLen); andre@0: andre@0: /* andre@0: ** Finish the verification process. The return value is a status which andre@0: ** indicates success or failure. On success, the SECSuccess value is andre@0: ** returned. Otherwise, SECFailure is returned and the error code found andre@0: ** using PORT_GetError() indicates what failure occurred. andre@0: ** "cx" the context andre@0: */ andre@0: extern SECStatus VFY_End(VFYContext *cx); andre@0: andre@0: /* andre@0: ** Finish the verification process. The return value is a status which andre@0: ** indicates success or failure. On success, the SECSuccess value is andre@0: ** returned. Otherwise, SECFailure is returned and the error code found andre@0: ** using PORT_GetError() indicates what failure occurred. If signature is andre@0: ** supplied the verification uses this signature to verify, otherwise the andre@0: ** signature passed in VFY_CreateContext() is used. andre@0: ** VFY_EndWithSignature(cx,NULL); is identical to VFY_End(cx);. andre@0: ** "cx" the context andre@0: ** "sig" the encrypted signature data andre@0: */ andre@0: extern SECStatus VFY_EndWithSignature(VFYContext *cx, SECItem *sig); andre@0: andre@0: andre@0: /* andre@0: ** Verify the signature on a block of data for which we already have andre@0: ** the digest. The signature data is an RSA private key encrypted andre@0: ** block of data formatted according to PKCS#1. andre@0: ** This function is deprecated. Use VFY_VerifyDigestDirect or andre@0: ** VFY_VerifyDigestWithAlgorithmID instead. andre@0: ** "dig" the digest andre@0: ** "key" the public key to check the signature with andre@0: ** "sig" the encrypted signature data andre@0: ** "sigAlg" specifies the signing algorithm to use. This must match andre@0: ** the key type. andre@0: ** "wincx" void pointer to the window context andre@0: **/ andre@0: extern SECStatus VFY_VerifyDigest(SECItem *dig, SECKEYPublicKey *key, andre@0: SECItem *sig, SECOidTag sigAlg, void *wincx); andre@0: /* andre@0: ** Verify the signature on a block of data for which we already have andre@0: ** the digest. The signature data is an RSA private key encrypted andre@0: ** block of data formatted according to PKCS#1. andre@0: ** "dig" the digest andre@0: ** "key" the public key to check the signature with andre@0: ** "sig" the encrypted signature data andre@0: ** "pubkAlg" specifies the cryptographic signing algorithm to use (the andre@0: ** raw algorithm without any hash specified. This must match the key andre@0: ** type. andre@0: ** "hashAlg" specifies the hashing algorithm used. andre@0: ** "wincx" void pointer to the window context andre@0: **/ andre@0: extern SECStatus VFY_VerifyDigestDirect(const SECItem *dig, andre@0: const SECKEYPublicKey *key, andre@0: const SECItem *sig, SECOidTag pubkAlg, andre@0: SECOidTag hashAlg, void *wincx); andre@0: /* andre@0: ** Verify the signature on a block of data for which we already have andre@0: ** the digest. The signature data is an RSA private key encrypted andre@0: ** block of data formatted according to PKCS#1. andre@0: ** "key" the public key to verify with andre@0: ** "sig" the encrypted signature data if sig is NULL then andre@0: ** VFY_EndWithSignature must be called with the correct signature at andre@0: ** the end of the processing. andre@0: ** "algid" specifies the signing algorithm and parameters to use. andre@0: ** This must match the key type. andre@0: ** "hash" oid of the actual hash used to create digest. If this value is andre@0: ** not set to SEC_OID_UNKNOWN, it must match the hash of the signature. andre@0: ** "wincx" void pointer to the window context andre@0: */ andre@0: extern SECStatus VFY_VerifyDigestWithAlgorithmID(const SECItem *dig, andre@0: const SECKEYPublicKey *key, const SECItem *sig, andre@0: const SECAlgorithmID *algid, SECOidTag hash, andre@0: void *wincx); andre@0: andre@0: /* andre@0: ** Verify the signature on a block of data. The signature data is an RSA andre@0: ** private key encrypted block of data formatted according to PKCS#1. andre@0: ** This function is deprecated. Use VFY_VerifyDataDirect or andre@0: ** VFY_VerifyDataWithAlgorithmID instead. andre@0: ** "buf" the input data andre@0: ** "len" the length of the input data andre@0: ** "key" the public key to check the signature with andre@0: ** "sig" the encrypted signature data andre@0: ** "sigAlg" specifies the signing algorithm to use. This must match andre@0: ** the key type. andre@0: ** "wincx" void pointer to the window context andre@0: */ andre@0: extern SECStatus VFY_VerifyData(const unsigned char *buf, int len, andre@0: const SECKEYPublicKey *key, const SECItem *sig, andre@0: SECOidTag sigAlg, void *wincx); andre@0: /* andre@0: ** Verify the signature on a block of data. The signature data is an RSA andre@0: ** private key encrypted block of data formatted according to PKCS#1. andre@0: ** "buf" the input data andre@0: ** "len" the length of the input data andre@0: ** "key" the public key to check the signature with andre@0: ** "sig" the encrypted signature data andre@0: ** "pubkAlg" specifies the cryptographic signing algorithm to use (the andre@0: ** raw algorithm without any hash specified. This must match the key andre@0: ** type. andre@0: ** "hashAlg" specifies the hashing algorithm used. If the key is an andre@0: ** RSA key, and sig is not NULL, then hashAlg can be SEC_OID_UNKNOWN. andre@0: ** the hash is selected from data in the sig. andre@0: ** "hash" optional pointer to return the actual hash algorithm used. andre@0: ** in practice this should always match the passed in hashAlg (the andre@0: ** exception is the case where hashAlg is SEC_OID_UNKNOWN above). andre@0: ** If this value is NULL no, hash oid is returned. andre@0: ** "wincx" void pointer to the window context andre@0: */ andre@0: extern SECStatus VFY_VerifyDataDirect(const unsigned char *buf, int len, andre@0: const SECKEYPublicKey *key, andre@0: const SECItem *sig, andre@0: SECOidTag pubkAlg, SECOidTag hashAlg, andre@0: SECOidTag *hash, void *wincx); andre@0: andre@0: /* andre@0: ** Verify the signature on a block of data. The signature data is an RSA andre@0: ** private key encrypted block of data formatted according to PKCS#1. andre@0: ** "buf" the input data andre@0: ** "len" the length of the input data andre@0: ** "key" the public key to check the signature with andre@0: ** "sig" the encrypted signature data andre@0: ** "algid" specifies the signing algorithm and parameters to use. andre@0: ** This must match the key type. andre@0: ** "hash" optional pointer to return the oid of the actual hash used in andre@0: ** the signature. If this value is NULL no, hash oid is returned. andre@0: ** "wincx" void pointer to the window context andre@0: */ andre@0: extern SECStatus VFY_VerifyDataWithAlgorithmID(const unsigned char *buf, andre@0: int len, const SECKEYPublicKey *key, andre@0: const SECItem *sig, andre@0: const SECAlgorithmID *algid, SECOidTag *hash, andre@0: void *wincx); andre@0: andre@0: andre@0: SEC_END_PROTOS andre@0: andre@0: #endif /* _CRYPTOHI_H_ */