security/nss/lib/cryptohi/cryptohi.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * cryptohi.h - public prototypes for the crypto library
michael@0 3 *
michael@0 4 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 5 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 7
michael@0 8 #ifndef _CRYPTOHI_H_
michael@0 9 #define _CRYPTOHI_H_
michael@0 10
michael@0 11 #include "blapit.h"
michael@0 12
michael@0 13 #include "seccomon.h"
michael@0 14 #include "secoidt.h"
michael@0 15 #include "secdert.h"
michael@0 16 #include "cryptoht.h"
michael@0 17 #include "keyt.h"
michael@0 18 #include "certt.h"
michael@0 19
michael@0 20
michael@0 21 SEC_BEGIN_PROTOS
michael@0 22
michael@0 23
michael@0 24 /****************************************/
michael@0 25 /*
michael@0 26 ** DER encode/decode (EC)DSA signatures
michael@0 27 */
michael@0 28
michael@0 29 /* ANSI X9.57 defines DSA signatures as DER encoded data. Our DSA1 code (and
michael@0 30 * most of the rest of the world) just generates 40 bytes of raw data. These
michael@0 31 * functions convert between formats.
michael@0 32 */
michael@0 33 extern SECStatus DSAU_EncodeDerSig(SECItem *dest, SECItem *src);
michael@0 34 extern SECItem *DSAU_DecodeDerSig(const SECItem *item);
michael@0 35
michael@0 36 /*
michael@0 37 * Unlike DSA1, raw DSA2 and ECDSA signatures do not have a fixed length.
michael@0 38 * Rather they contain two integers r and s whose length depends
michael@0 39 * on the size of q or the EC key used for signing.
michael@0 40 *
michael@0 41 * We can reuse the DSAU_EncodeDerSig interface to DER encode
michael@0 42 * raw ECDSA signature keeping in mind that the length of r
michael@0 43 * is the same as that of s and exactly half of src->len.
michael@0 44 *
michael@0 45 * For decoding, we need to pass the length of the desired
michael@0 46 * raw signature (twice the key size) explicitly.
michael@0 47 */
michael@0 48 extern SECStatus DSAU_EncodeDerSigWithLen(SECItem *dest, SECItem *src,
michael@0 49 unsigned int len);
michael@0 50 extern SECItem *DSAU_DecodeDerSigToLen(const SECItem *item, unsigned int len);
michael@0 51
michael@0 52 /****************************************/
michael@0 53 /*
michael@0 54 ** Signature creation operations
michael@0 55 */
michael@0 56
michael@0 57 /*
michael@0 58 ** Create a new signature context used for signing a data stream.
michael@0 59 ** "alg" the signature algorithm to use (e.g. SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION)
michael@0 60 ** "privKey" the private key to use
michael@0 61 */
michael@0 62 extern SGNContext *SGN_NewContext(SECOidTag alg, SECKEYPrivateKey *privKey);
michael@0 63
michael@0 64 /*
michael@0 65 ** Destroy a signature-context object
michael@0 66 ** "cx" the object
michael@0 67 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
michael@0 68 */
michael@0 69 extern void SGN_DestroyContext(SGNContext *cx, PRBool freeit);
michael@0 70
michael@0 71 /*
michael@0 72 ** Reset the signing context "cx" to its initial state, preparing it for
michael@0 73 ** another stream of data.
michael@0 74 */
michael@0 75 extern SECStatus SGN_Begin(SGNContext *cx);
michael@0 76
michael@0 77 /*
michael@0 78 ** Update the signing context with more data to sign.
michael@0 79 ** "cx" the context
michael@0 80 ** "input" the input data to sign
michael@0 81 ** "inputLen" the length of the input data
michael@0 82 */
michael@0 83 extern SECStatus SGN_Update(SGNContext *cx, const unsigned char *input,
michael@0 84 unsigned int inputLen);
michael@0 85
michael@0 86 /*
michael@0 87 ** Finish the signature process. Use either k0 or k1 to sign the data
michael@0 88 ** stream that was input using SGN_Update. The resulting signature is
michael@0 89 ** formatted using PKCS#1 and then encrypted using RSA private or public
michael@0 90 ** encryption.
michael@0 91 ** "cx" the context
michael@0 92 ** "result" the final signature data (memory is allocated)
michael@0 93 */
michael@0 94 extern SECStatus SGN_End(SGNContext *cx, SECItem *result);
michael@0 95
michael@0 96 /*
michael@0 97 ** Sign a single block of data using private key encryption and given
michael@0 98 ** signature/hash algorithm.
michael@0 99 ** "result" the final signature data (memory is allocated)
michael@0 100 ** "buf" the input data to sign
michael@0 101 ** "len" the amount of data to sign
michael@0 102 ** "pk" the private key to encrypt with
michael@0 103 ** "algid" the signature/hash algorithm to sign with
michael@0 104 ** (must be compatible with the key type).
michael@0 105 */
michael@0 106 extern SECStatus SEC_SignData(SECItem *result,
michael@0 107 const unsigned char *buf, int len,
michael@0 108 SECKEYPrivateKey *pk, SECOidTag algid);
michael@0 109
michael@0 110 /*
michael@0 111 ** Sign a pre-digested block of data using private key encryption, encoding
michael@0 112 ** The given signature/hash algorithm.
michael@0 113 ** "result" the final signature data (memory is allocated)
michael@0 114 ** "digest" the digest to sign
michael@0 115 ** "privKey" the private key to encrypt with
michael@0 116 ** "algtag" The algorithm tag to encode (need for RSA only)
michael@0 117 */
michael@0 118 extern SECStatus SGN_Digest(SECKEYPrivateKey *privKey,
michael@0 119 SECOidTag algtag, SECItem *result, SECItem *digest);
michael@0 120
michael@0 121 /*
michael@0 122 ** DER sign a single block of data using private key encryption and the
michael@0 123 ** MD5 hashing algorithm. This routine first computes a digital signature
michael@0 124 ** using SEC_SignData, then wraps it with an CERTSignedData and then der
michael@0 125 ** encodes the result.
michael@0 126 ** "arena" is the memory arena to use to allocate data from
michael@0 127 ** "result" the final der encoded data (memory is allocated)
michael@0 128 ** "buf" the input data to sign
michael@0 129 ** "len" the amount of data to sign
michael@0 130 ** "pk" the private key to encrypt with
michael@0 131 */
michael@0 132 extern SECStatus SEC_DerSignData(PLArenaPool *arena, SECItem *result,
michael@0 133 const unsigned char *buf, int len,
michael@0 134 SECKEYPrivateKey *pk, SECOidTag algid);
michael@0 135
michael@0 136 /*
michael@0 137 ** Destroy a signed-data object.
michael@0 138 ** "sd" the object
michael@0 139 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
michael@0 140 */
michael@0 141 extern void SEC_DestroySignedData(CERTSignedData *sd, PRBool freeit);
michael@0 142
michael@0 143 /*
michael@0 144 ** Get the signature algorithm tag number for the given key type and hash
michael@0 145 ** algorithm tag. Returns SEC_OID_UNKNOWN if key type and hash algorithm
michael@0 146 ** do not match or are not supported.
michael@0 147 */
michael@0 148 extern SECOidTag SEC_GetSignatureAlgorithmOidTag(KeyType keyType,
michael@0 149 SECOidTag hashAlgTag);
michael@0 150
michael@0 151 /****************************************/
michael@0 152 /*
michael@0 153 ** Signature verification operations
michael@0 154 */
michael@0 155
michael@0 156 /*
michael@0 157 ** Create a signature verification context. This version is deprecated,
michael@0 158 ** This function is deprecated. Use VFY_CreateContextDirect or
michael@0 159 ** VFY_CreateContextWithAlgorithmID instead.
michael@0 160 ** "key" the public key to verify with
michael@0 161 ** "sig" the encrypted signature data if sig is NULL then
michael@0 162 ** VFY_EndWithSignature must be called with the correct signature at
michael@0 163 ** the end of the processing.
michael@0 164 ** "sigAlg" specifies the signing algorithm to use (including the
michael@0 165 ** hash algorthim). This must match the key type.
michael@0 166 ** "wincx" void pointer to the window context
michael@0 167 */
michael@0 168 extern VFYContext *VFY_CreateContext(SECKEYPublicKey *key, SECItem *sig,
michael@0 169 SECOidTag sigAlg, void *wincx);
michael@0 170 /*
michael@0 171 ** Create a signature verification context.
michael@0 172 ** "key" the public key to verify with
michael@0 173 ** "sig" the encrypted signature data if sig is NULL then
michael@0 174 ** VFY_EndWithSignature must be called with the correct signature at
michael@0 175 ** the end of the processing.
michael@0 176 ** "pubkAlg" specifies the cryptographic signing algorithm to use (the
michael@0 177 ** raw algorithm without any hash specified. This must match the key
michael@0 178 ** type.
michael@0 179 ** "hashAlg" specifies the hashing algorithm used. If the key is an
michael@0 180 ** RSA key, and sig is not NULL, then hashAlg can be SEC_OID_UNKNOWN.
michael@0 181 ** the hash is selected from data in the sig.
michael@0 182 ** "hash" optional pointer to return the actual hash algorithm used.
michael@0 183 ** in practice this should always match the passed in hashAlg (the
michael@0 184 ** exception is the case where hashAlg is SEC_OID_UNKNOWN above).
michael@0 185 ** If this value is NULL no, hash oid is returned.
michael@0 186 ** "wincx" void pointer to the window context
michael@0 187 */
michael@0 188 extern VFYContext *VFY_CreateContextDirect(const SECKEYPublicKey *key,
michael@0 189 const SECItem *sig,
michael@0 190 SECOidTag pubkAlg,
michael@0 191 SECOidTag hashAlg,
michael@0 192 SECOidTag *hash, void *wincx);
michael@0 193 /*
michael@0 194 ** Create a signature verification context from a algorithm ID.
michael@0 195 ** "key" the public key to verify with
michael@0 196 ** "sig" the encrypted signature data if sig is NULL then
michael@0 197 ** VFY_EndWithSignature must be called with the correct signature at
michael@0 198 ** the end of the processing.
michael@0 199 ** "algid" specifies the signing algorithm and parameters to use.
michael@0 200 ** This must match the key type.
michael@0 201 ** "hash" optional pointer to return the oid of the actual hash used in
michael@0 202 ** the signature. If this value is NULL no, hash oid is returned.
michael@0 203 ** "wincx" void pointer to the window context
michael@0 204 */
michael@0 205 extern VFYContext *VFY_CreateContextWithAlgorithmID(const SECKEYPublicKey *key,
michael@0 206 const SECItem *sig,
michael@0 207 const SECAlgorithmID *algid,
michael@0 208 SECOidTag *hash,
michael@0 209 void *wincx);
michael@0 210
michael@0 211 /*
michael@0 212 ** Destroy a verification-context object.
michael@0 213 ** "cx" the context to destroy
michael@0 214 ** "freeit" if PR_TRUE then free the object as well as its sub-objects
michael@0 215 */
michael@0 216 extern void VFY_DestroyContext(VFYContext *cx, PRBool freeit);
michael@0 217
michael@0 218 extern SECStatus VFY_Begin(VFYContext *cx);
michael@0 219
michael@0 220 /*
michael@0 221 ** Update a verification context with more input data. The input data
michael@0 222 ** is fed to a secure hash function (depending on what was in the
michael@0 223 ** encrypted signature data).
michael@0 224 ** "cx" the context
michael@0 225 ** "input" the input data
michael@0 226 ** "inputLen" the amount of input data
michael@0 227 */
michael@0 228 extern SECStatus VFY_Update(VFYContext *cx, const unsigned char *input,
michael@0 229 unsigned int inputLen);
michael@0 230
michael@0 231 /*
michael@0 232 ** Finish the verification process. The return value is a status which
michael@0 233 ** indicates success or failure. On success, the SECSuccess value is
michael@0 234 ** returned. Otherwise, SECFailure is returned and the error code found
michael@0 235 ** using PORT_GetError() indicates what failure occurred.
michael@0 236 ** "cx" the context
michael@0 237 */
michael@0 238 extern SECStatus VFY_End(VFYContext *cx);
michael@0 239
michael@0 240 /*
michael@0 241 ** Finish the verification process. The return value is a status which
michael@0 242 ** indicates success or failure. On success, the SECSuccess value is
michael@0 243 ** returned. Otherwise, SECFailure is returned and the error code found
michael@0 244 ** using PORT_GetError() indicates what failure occurred. If signature is
michael@0 245 ** supplied the verification uses this signature to verify, otherwise the
michael@0 246 ** signature passed in VFY_CreateContext() is used.
michael@0 247 ** VFY_EndWithSignature(cx,NULL); is identical to VFY_End(cx);.
michael@0 248 ** "cx" the context
michael@0 249 ** "sig" the encrypted signature data
michael@0 250 */
michael@0 251 extern SECStatus VFY_EndWithSignature(VFYContext *cx, SECItem *sig);
michael@0 252
michael@0 253
michael@0 254 /*
michael@0 255 ** Verify the signature on a block of data for which we already have
michael@0 256 ** the digest. The signature data is an RSA private key encrypted
michael@0 257 ** block of data formatted according to PKCS#1.
michael@0 258 ** This function is deprecated. Use VFY_VerifyDigestDirect or
michael@0 259 ** VFY_VerifyDigestWithAlgorithmID instead.
michael@0 260 ** "dig" the digest
michael@0 261 ** "key" the public key to check the signature with
michael@0 262 ** "sig" the encrypted signature data
michael@0 263 ** "sigAlg" specifies the signing algorithm to use. This must match
michael@0 264 ** the key type.
michael@0 265 ** "wincx" void pointer to the window context
michael@0 266 **/
michael@0 267 extern SECStatus VFY_VerifyDigest(SECItem *dig, SECKEYPublicKey *key,
michael@0 268 SECItem *sig, SECOidTag sigAlg, void *wincx);
michael@0 269 /*
michael@0 270 ** Verify the signature on a block of data for which we already have
michael@0 271 ** the digest. The signature data is an RSA private key encrypted
michael@0 272 ** block of data formatted according to PKCS#1.
michael@0 273 ** "dig" the digest
michael@0 274 ** "key" the public key to check the signature with
michael@0 275 ** "sig" the encrypted signature data
michael@0 276 ** "pubkAlg" specifies the cryptographic signing algorithm to use (the
michael@0 277 ** raw algorithm without any hash specified. This must match the key
michael@0 278 ** type.
michael@0 279 ** "hashAlg" specifies the hashing algorithm used.
michael@0 280 ** "wincx" void pointer to the window context
michael@0 281 **/
michael@0 282 extern SECStatus VFY_VerifyDigestDirect(const SECItem *dig,
michael@0 283 const SECKEYPublicKey *key,
michael@0 284 const SECItem *sig, SECOidTag pubkAlg,
michael@0 285 SECOidTag hashAlg, void *wincx);
michael@0 286 /*
michael@0 287 ** Verify the signature on a block of data for which we already have
michael@0 288 ** the digest. The signature data is an RSA private key encrypted
michael@0 289 ** block of data formatted according to PKCS#1.
michael@0 290 ** "key" the public key to verify with
michael@0 291 ** "sig" the encrypted signature data if sig is NULL then
michael@0 292 ** VFY_EndWithSignature must be called with the correct signature at
michael@0 293 ** the end of the processing.
michael@0 294 ** "algid" specifies the signing algorithm and parameters to use.
michael@0 295 ** This must match the key type.
michael@0 296 ** "hash" oid of the actual hash used to create digest. If this value is
michael@0 297 ** not set to SEC_OID_UNKNOWN, it must match the hash of the signature.
michael@0 298 ** "wincx" void pointer to the window context
michael@0 299 */
michael@0 300 extern SECStatus VFY_VerifyDigestWithAlgorithmID(const SECItem *dig,
michael@0 301 const SECKEYPublicKey *key, const SECItem *sig,
michael@0 302 const SECAlgorithmID *algid, SECOidTag hash,
michael@0 303 void *wincx);
michael@0 304
michael@0 305 /*
michael@0 306 ** Verify the signature on a block of data. The signature data is an RSA
michael@0 307 ** private key encrypted block of data formatted according to PKCS#1.
michael@0 308 ** This function is deprecated. Use VFY_VerifyDataDirect or
michael@0 309 ** VFY_VerifyDataWithAlgorithmID instead.
michael@0 310 ** "buf" the input data
michael@0 311 ** "len" the length of the input data
michael@0 312 ** "key" the public key to check the signature with
michael@0 313 ** "sig" the encrypted signature data
michael@0 314 ** "sigAlg" specifies the signing algorithm to use. This must match
michael@0 315 ** the key type.
michael@0 316 ** "wincx" void pointer to the window context
michael@0 317 */
michael@0 318 extern SECStatus VFY_VerifyData(const unsigned char *buf, int len,
michael@0 319 const SECKEYPublicKey *key, const SECItem *sig,
michael@0 320 SECOidTag sigAlg, void *wincx);
michael@0 321 /*
michael@0 322 ** Verify the signature on a block of data. The signature data is an RSA
michael@0 323 ** private key encrypted block of data formatted according to PKCS#1.
michael@0 324 ** "buf" the input data
michael@0 325 ** "len" the length of the input data
michael@0 326 ** "key" the public key to check the signature with
michael@0 327 ** "sig" the encrypted signature data
michael@0 328 ** "pubkAlg" specifies the cryptographic signing algorithm to use (the
michael@0 329 ** raw algorithm without any hash specified. This must match the key
michael@0 330 ** type.
michael@0 331 ** "hashAlg" specifies the hashing algorithm used. If the key is an
michael@0 332 ** RSA key, and sig is not NULL, then hashAlg can be SEC_OID_UNKNOWN.
michael@0 333 ** the hash is selected from data in the sig.
michael@0 334 ** "hash" optional pointer to return the actual hash algorithm used.
michael@0 335 ** in practice this should always match the passed in hashAlg (the
michael@0 336 ** exception is the case where hashAlg is SEC_OID_UNKNOWN above).
michael@0 337 ** If this value is NULL no, hash oid is returned.
michael@0 338 ** "wincx" void pointer to the window context
michael@0 339 */
michael@0 340 extern SECStatus VFY_VerifyDataDirect(const unsigned char *buf, int len,
michael@0 341 const SECKEYPublicKey *key,
michael@0 342 const SECItem *sig,
michael@0 343 SECOidTag pubkAlg, SECOidTag hashAlg,
michael@0 344 SECOidTag *hash, void *wincx);
michael@0 345
michael@0 346 /*
michael@0 347 ** Verify the signature on a block of data. The signature data is an RSA
michael@0 348 ** private key encrypted block of data formatted according to PKCS#1.
michael@0 349 ** "buf" the input data
michael@0 350 ** "len" the length of the input data
michael@0 351 ** "key" the public key to check the signature with
michael@0 352 ** "sig" the encrypted signature data
michael@0 353 ** "algid" specifies the signing algorithm and parameters to use.
michael@0 354 ** This must match the key type.
michael@0 355 ** "hash" optional pointer to return the oid of the actual hash used in
michael@0 356 ** the signature. If this value is NULL no, hash oid is returned.
michael@0 357 ** "wincx" void pointer to the window context
michael@0 358 */
michael@0 359 extern SECStatus VFY_VerifyDataWithAlgorithmID(const unsigned char *buf,
michael@0 360 int len, const SECKEYPublicKey *key,
michael@0 361 const SECItem *sig,
michael@0 362 const SECAlgorithmID *algid, SECOidTag *hash,
michael@0 363 void *wincx);
michael@0 364
michael@0 365
michael@0 366 SEC_END_PROTOS
michael@0 367
michael@0 368 #endif /* _CRYPTOHI_H_ */

mercurial