a57ba0f14f2065d227d9f380da0ade762c829595
[openssl.git] / crypto / ecdsa / ecdsa.h
1 /* crypto/ecdsa/ecdsa.h */
2 /**
3  * \file   crypto/ecdsa/ecdsa.h Include file for the OpenSSL ECDSA functions
4  * \author Written by Nils Larsch for the OpenSSL project
5  */
6 /* ====================================================================
7  * Copyright (c) 2000-2003 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer. 
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59 #ifndef HEADER_ECDSA_H
60 #define HEADER_ECDSA_H
61
62 #include <openssl/opensslconf.h>
63
64 #ifdef OPENSSL_NO_ECDSA
65 #error ECDSA is disabled.
66 #endif
67
68 #include <openssl/bn.h>
69 #include <openssl/ec.h>
70 #include <openssl/ossl_typ.h>
71
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75
76 /* Already defined in ossl_typ.h */
77 /* typedef struct ecdsa_method ECDSA_METHOD; */
78
79 typedef struct ECDSA_SIG_st
80         {
81         BIGNUM *r;
82         BIGNUM *s;
83         } ECDSA_SIG;
84
85 struct ecdsa_method 
86         {
87         const char *name;
88         ECDSA_SIG *(*ecdsa_do_sign)(const unsigned char *dgst, int dgst_len, 
89                         EC_KEY *eckey);
90         int (*ecdsa_sign_setup)(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, 
91                         BIGNUM **r);
92         int (*ecdsa_do_verify)(const unsigned char *dgst, int dgst_len, 
93                         ECDSA_SIG *sig, EC_KEY *eckey);
94 #if 0
95         int (*init)(EC_KEY *eckey);
96         int (*finish)(EC_KEY *eckey);
97 #endif
98         int flags;
99         char *app_data;
100         };
101
102 typedef struct ecdsa_data_st {
103         /* EC_KEY_METH_DATA part */
104         int (*init)(EC_KEY *);
105         void (*finish)(EC_KEY *);
106         /* method (ECDSA) specific part */
107         BIGNUM  *kinv;  /* signing pre-calc */
108         BIGNUM  *r;     /* signing pre-calc */
109         ENGINE  *engine;
110         int     flags;
111         const ECDSA_METHOD *meth;
112         CRYPTO_EX_DATA ex_data;
113 } ECDSA_DATA; 
114
115 /** ECDSA_SIG *ECDSA_SIG_new(void)
116  * allocates and initialize a ECDSA_SIG structure
117  * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
118  */
119 ECDSA_SIG *ECDSA_SIG_new(void);
120
121 /** ECDSA_SIG_free
122  * frees a ECDSA_SIG structure
123  * \param a pointer to the ECDSA_SIG structure
124  */
125 void      ECDSA_SIG_free(ECDSA_SIG *a);
126
127 /** i2d_ECDSA_SIG
128  * DER encode content of ECDSA_SIG object (note: this function modifies *pp
129  * (*pp += length of the DER encoded signature)).
130  * \param a  pointer to the ECDSA_SIG object
131  * \param pp pointer to a unsigned char pointer for the output or NULL
132  * \return the length of the DER encoded ECDSA_SIG object or 0 
133  */
134 int       i2d_ECDSA_SIG(const ECDSA_SIG *a, unsigned char **pp);
135
136 /** d2i_ECDSA_SIG
137  * decodes a DER encoded ECDSA signature (note: this function changes *pp
138  * (*pp += len)). 
139  * \param v pointer to ECDSA_SIG pointer (may be NULL)
140  * \param pp buffer with the DER encoded signature
141  * \param len bufferlength
142  * \return pointer to the decoded ECDSA_SIG structure (or NULL)
143  */
144 ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **v, const unsigned char **pp, long len);
145
146 /** ECDSA_DATA_new
147  * creates a new ECDSA_DATA object
148  * \return pointer to a newly allocated (and initialized) ECDSA_DATA object
149  */
150 ECDSA_DATA *ECDSA_DATA_new(void);
151
152 /** ECDSA_DATA_new_method
153  * creates a new ECDSA_DATA object using a specified ENGINE
154  * \param eng pointer to a ENGINE structure
155  * \return pointer to a newly allocated (and initialized) ECDSA_DATA object
156  */
157 ECDSA_DATA *ECDSA_DATA_new_method(ENGINE *eng);
158
159 /** ECDSA_DATA_free
160  * frees ECDSA_DATA structure
161  * \param data pointer to a ECDSA_DATA structure
162  */
163 void ECDSA_DATA_free(ECDSA_DATA *data);
164
165 /** ecdsa_check
166  * checks whether ECKEY->meth_data is a pointer to a ECDSA_DATA structure
167  * and if not it removes the old meth_data and creates a ECDSA_DATA structure.
168  * \param  eckey pointer to a EC_KEY object
169  * \return pointer to a ECDSA_DATA structure
170  */
171 ECDSA_DATA *ecdsa_check(EC_KEY *eckey);
172
173 /** ECDSA_do_sign
174  * computes the ECDSA signature of the given hash value using
175  * the supplied private key and returns the created signature.
176  * \param dgst pointer to the hash value
177  * \param dgst_len length of the hash value
178  * \param eckey pointer to the EC_KEY object containing a private EC key
179  * \return pointer to a ECDSA_SIG structure or NULL
180  */
181 ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst,int dgst_len,EC_KEY *eckey);
182
183 /** ECDSA_do_verify
184  * verifies that the supplied signature is a valid ECDSA
185  * signature of the supplied hash value using the supplied public key.
186  * \param dgst pointer to the hash value
187  * \param dgst_len length of the hash value
188  * \param sig  pointer to the ECDSA_SIG structure
189  * \param eckey pointer to the EC_KEY object containing a public EC key
190  * \return 1 if the signature is valid, 0 if the signature is invalid and -1 on error
191  */
192 int       ECDSA_do_verify(const unsigned char *dgst, int dgst_len, ECDSA_SIG 
193                 *sig, EC_KEY* eckey);
194
195 const ECDSA_METHOD *ECDSA_OpenSSL(void);
196
197 /** ECDSA_set_default_method
198  * sets the default ECDSA method
199  * \param meth the new default ECDSA_METHOD
200  */
201 void      ECDSA_set_default_method(const ECDSA_METHOD *meth);
202
203 /** ECDSA_get_default_method
204  * returns the default ECDSA method
205  * \return pointer to ECDSA_METHOD structure containing the default method
206  */
207 const ECDSA_METHOD *ECDSA_get_default_method(void);
208
209 /** ECDSA_set_method
210  * sets method to be used for the ECDSA operations
211  * \param eckey pointer to the EC_KEY object
212  * \param meth  pointer to the new method
213  * \return 1 on success and 0 otherwise 
214  */
215 int       ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth);
216
217 /** ECDSA_size
218  * returns the maximum length of the DER encoded signature
219  * \param  eckey pointer to a EC_KEY object
220  * \return numbers of bytes required for the DER encoded signature
221  */
222 int       ECDSA_size(const EC_KEY *eckey);
223
224 /** ECDSA_sign_setup
225  * precompute parts of the signing operation (the computed values may be
226  * passed to ECDSA_DATA->kinv and ECDSA_DATA->r for a later signature
227  * computation).
228  * \param eckey pointer to the EC_KEY object containing a private EC key
229  * \param ctx  pointer to a BN_CTX object (may be NULL)
230  * \param kinv pointer to a BIGNUM pointer for the inverse of k
231  * \param rp   pointer to a BIGNUM pointer for x coordinate of k * generator
232  * \return 1 on success and 0 otherwise
233  */
234 int       ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, 
235                 BIGNUM **rp);
236
237 /** ECDSA_sign
238  * computes ECDSA signature of a given hash value using the supplied
239  * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
240  * \param type this parameter is ignored
241  * \param dgst pointer to the hash value to sign
242  * \param dgstlen length of the hash value
243  * \param sig buffer to hold the DER encoded signature
244  * \param siglen pointer to the length of the returned signature
245  * \param eckey pointer to the EC_KEY object containing a private EC key
246  * \return 1 on success and 0 otherwise
247  */
248 int       ECDSA_sign(int type, const unsigned char *dgst, int dgstlen, 
249                 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
250
251 /** ECDSA_verify
252  * verifies that the given signature is valid ECDSA signature
253  * of the supplied hash value using the specified public key.
254  * \param type this parameter is ignored
255  * \param dgst pointer to the hash value 
256  * \param dgstlen length of the hash value
257  * \param sig  pointer to the DER encoded signature
258  * \param siglen length of the DER encoded signature
259  * \param eckey pointer to the EC_KEY object containing a public EC key
260  * \return 1 if the signature is valid, 0 if the signature is invalid and -1 on error
261  */
262 int       ECDSA_verify(int type, const unsigned char *dgst, int dgstlen, 
263                 const unsigned char *sig, int siglen, EC_KEY *eckey);
264
265 /* the standard ex_data functions */
266 int       ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new 
267                 *new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
268 int       ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg);
269 void      *ECDSA_get_ex_data(EC_KEY *d, int idx);
270
271
272 /* BEGIN ERROR CODES */
273 /* The following lines are auto generated by the script mkerr.pl. Any changes
274  * made after this point may be overwritten when the script is next run.
275  */
276 void ERR_load_ECDSA_strings(void);
277
278 /* Error codes for the ECDSA functions. */
279
280 /* Function codes. */
281 #define ECDSA_F_ECDSA_DATA_NEW                           100
282 #define ECDSA_F_ECDSA_DO_SIGN                            101
283 #define ECDSA_F_ECDSA_DO_VERIFY                          102
284 #define ECDSA_F_ECDSA_SIGN_SETUP                         103
285
286 /* Reason codes. */
287 #define ECDSA_R_BAD_SIGNATURE                            100
288 #define ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE              101
289 #define ECDSA_R_ERR_EC_LIB                               102
290 #define ECDSA_R_MISSING_PARAMETERS                       103
291 #define ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED          104
292 #define ECDSA_R_SIGNATURE_MALLOC_FAILED                  105
293
294 #ifdef  __cplusplus
295 }
296 #endif
297 #endif