2
* crypto.h - public data structures and prototypes for the crypto library
4
* The contents of this file are subject to the Mozilla Public
5
* License Version 1.1 (the "License"); you may not use this file
6
* except in compliance with the License. You may obtain a copy of
7
* the License at http://www.mozilla.org/MPL/
9
* Software distributed under the License is distributed on an "AS
10
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
* implied. See the License for the specific language governing
12
* rights and limitations under the License.
14
* The Original Code is the Netscape security libraries.
16
* The Initial Developer of the Original Code is Netscape
17
* Communications Corporation. Portions created by Netscape are
18
* Copyright (C) 1994-2000 Netscape Communications Corporation. All
21
* Portions created by Sun Microsystems, Inc. are Copyright (C) 2003
22
* Sun Microsystems, Inc. All Rights Reserved.
25
* Dr Vipul Gupta <vipul.gupta@sun.com>, Sun Microsystems Laboratories
27
* Alternatively, the contents of this file may be used under the
28
* terms of the GNU General Public License Version 2 or later (the
29
* "GPL"), in which case the provisions of the GPL are applicable
30
* instead of those above. If you wish to allow use of your
31
* version of this file only under the terms of the GPL and not to
32
* allow others to use your version of this file under the MPL,
33
* indicate your decision by deleting the provisions above and
34
* replace them with the notice and other provisions required by
35
* the GPL. If you do not delete the provisions above, a recipient
36
* may use your version of this file under either the MPL or the
39
* $Id: cryptohi.h,v 1.6 2003/10/17 13:45:32 ian.mcgreer%sun.com Exp $
58
/****************************************/
60
** DER encode/decode (EC)DSA signatures
63
/* ANSI X9.57 defines DSA signatures as DER encoded data. Our DSA code (and
64
* most of the rest of the world) just generates 40 bytes of raw data. These
65
* functions convert between formats.
67
extern SECStatus DSAU_EncodeDerSig(SECItem *dest, SECItem *src);
68
extern SECItem *DSAU_DecodeDerSig(SECItem *item);
71
* Unlike DSA, raw ECDSA signatures do not have a fixed length.
72
* Rather they contain two integers r and s whose length depends
73
* on the size of the EC key used for signing.
75
* We can reuse the DSAU_EncodeDerSig interface to DER encode
76
* raw ECDSA signature keeping in mind that the length of r
77
* is the same as that of s and exactly half of src->len.
79
* For decoding, we need to pass the length of the desired
80
* raw signature (twice the key size) explicitly.
82
extern SECStatus DSAU_EncodeDerSigWithLen(SECItem *dest, SECItem *src,
84
extern SECItem *DSAU_DecodeDerSigToLen(SECItem *item, unsigned int len);
86
/****************************************/
88
** Signature creation operations
92
** Create a new signature context used for signing a data stream.
93
** "alg" the signature algorithm to use (e.g. SEC_OID_RSA_WITH_MD5)
94
** "privKey" the private key to use
96
extern SGNContext *SGN_NewContext(SECOidTag alg, SECKEYPrivateKey *privKey);
99
** Destroy a signature-context object
101
** "freeit" if PR_TRUE then free the object as well as its sub-objects
103
extern void SGN_DestroyContext(SGNContext *cx, PRBool freeit);
106
** Reset the signing context "cx" to its initial state, preparing it for
107
** another stream of data.
109
extern SECStatus SGN_Begin(SGNContext *cx);
112
** Update the signing context with more data to sign.
114
** "input" the input data to sign
115
** "inputLen" the length of the input data
117
extern SECStatus SGN_Update(SGNContext *cx, unsigned char *input,
118
unsigned int inputLen);
121
** Finish the signature process. Use either k0 or k1 to sign the data
122
** stream that was input using SGN_Update. The resulting signature is
123
** formatted using PKCS#1 and then encrypted using RSA private or public
126
** "result" the final signature data (memory is allocated)
128
extern SECStatus SGN_End(SGNContext *cx, SECItem *result);
131
** Sign a single block of data using private key encryption and given
132
** signature/hash algorithm.
133
** "result" the final signature data (memory is allocated)
134
** "buf" the input data to sign
135
** "len" the amount of data to sign
136
** "pk" the private key to encrypt with
137
** "algid" the signature/hash algorithm to sign with
138
** (must be compatible with the key type).
140
extern SECStatus SEC_SignData(SECItem *result, unsigned char *buf, int len,
141
SECKEYPrivateKey *pk, SECOidTag algid);
144
** Sign a pre-digested block of data using private key encryption, encoding
145
** The given signature/hash algorithm.
146
** "result" the final signature data (memory is allocated)
147
** "digest" the digest to sign
148
** "pk" the private key to encrypt with
149
** "algtag" The algorithm tag to encode (need for RSA only)
151
extern SECStatus SGN_Digest(SECKEYPrivateKey *privKey,
152
SECOidTag algtag, SECItem *result, SECItem *digest);
155
** DER sign a single block of data using private key encryption and the
156
** MD5 hashing algorithm. This routine first computes a digital signature
157
** using SEC_SignData, then wraps it with an CERTSignedData and then der
158
** encodes the result.
159
** "arena" is the memory arena to use to allocate data from
160
** "result" the final der encoded data (memory is allocated)
161
** "buf" the input data to sign
162
** "len" the amount of data to sign
163
** "pk" the private key to encrypt with
165
extern SECStatus SEC_DerSignData(PRArenaPool *arena, SECItem *result,
166
unsigned char *buf, int len,
167
SECKEYPrivateKey *pk, SECOidTag algid);
170
** Destroy a signed-data object.
172
** "freeit" if PR_TRUE then free the object as well as its sub-objects
174
extern void SEC_DestroySignedData(CERTSignedData *sd, PRBool freeit);
176
/****************************************/
178
** Signature verification operations
182
** Create a signature verification context.
183
** "key" the public key to verify with
184
** "sig" the encrypted signature data if sig is NULL then
185
** VFY_EndWithSignature must be called with the correct signature at
186
** the end of the processing.
187
** "algid" specifies the signing algorithm to use. This must match
189
** "wincx" void pointer to the window context
191
extern VFYContext *VFY_CreateContext(SECKEYPublicKey *key, SECItem *sig,
192
SECOidTag algid, void *wincx);
195
** Destroy a verification-context object.
196
** "cx" the context to destroy
197
** "freeit" if PR_TRUE then free the object as well as its sub-objects
199
extern void VFY_DestroyContext(VFYContext *cx, PRBool freeit);
201
extern SECStatus VFY_Begin(VFYContext *cx);
204
** Update a verification context with more input data. The input data
205
** is fed to a secure hash function (depending on what was in the
206
** encrypted signature data).
208
** "input" the input data
209
** "inputLen" the amount of input data
211
extern SECStatus VFY_Update(VFYContext *cx, unsigned char *input,
212
unsigned int inputLen);
215
** Finish the verification process. The return value is a status which
216
** indicates success or failure. On success, the SECSuccess value is
217
** returned. Otherwise, SECFailure is returned and the error code found
218
** using PORT_GetError() indicates what failure occurred.
221
extern SECStatus VFY_End(VFYContext *cx);
224
** Finish the verification process. The return value is a status which
225
** indicates success or failure. On success, the SECSuccess value is
226
** returned. Otherwise, SECFailure is returned and the error code found
227
** using PORT_GetError() indicates what failure occurred. If signature is
228
** supplied the verification uses this signature to verify, otherwise the
229
** signature passed in VFY_CreateContext() is used.
230
** VFY_EndWithSignature(cx,NULL); is identical to VFY_End(cx);.
232
** "sig" the encrypted signature data
234
extern SECStatus VFY_EndWithSignature(VFYContext *cx, SECItem *sig);
238
** Verify the signature on a block of data for which we already have
239
** the digest. The signature data is an RSA private key encrypted
240
** block of data formatted according to PKCS#1.
242
** "key" the public key to check the signature with
243
** "sig" the encrypted signature data
244
** "algid" specifies the signing algorithm to use. This must match
247
extern SECStatus VFY_VerifyDigest(SECItem *dig, SECKEYPublicKey *key,
248
SECItem *sig, SECOidTag algid, void *wincx);
251
** Verify the signature on a block of data. The signature data is an RSA
252
** private key encrypted block of data formatted according to PKCS#1.
253
** "buf" the input data
254
** "len" the length of the input data
255
** "key" the public key to check the signature with
256
** "sig" the encrypted signature data
257
** "algid" specifies the signing algorithm to use. This must match
260
extern SECStatus VFY_VerifyData(unsigned char *buf, int len,
261
SECKEYPublicKey *key, SECItem *sig,
262
SECOidTag algid, void *wincx);
267
#endif /* _CRYPTOHI_H_ */