1 /* crypto/ecdsa/ecdsa.h */
\r
3 * \file crypto/ecdsa/ecdsa.h Include file for the OpenSSL ECDSA functions
\r
4 * \author Written by Nils Larsch for the OpenSSL project
\r
6 /* ====================================================================
\r
7 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
\r
9 * Redistribution and use in source and binary forms, with or without
\r
10 * modification, are permitted provided that the following conditions
\r
13 * 1. Redistributions of source code must retain the above copyright
\r
14 * notice, this list of conditions and the following disclaimer.
\r
16 * 2. Redistributions in binary form must reproduce the above copyright
\r
17 * notice, this list of conditions and the following disclaimer in
\r
18 * the documentation and/or other materials provided with the
\r
21 * 3. All advertising materials mentioning features or use of this
\r
22 * software must display the following acknowledgment:
\r
23 * "This product includes software developed by the OpenSSL Project
\r
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
\r
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
\r
27 * endorse or promote products derived from this software without
\r
28 * prior written permission. For written permission, please contact
\r
29 * licensing@OpenSSL.org.
\r
31 * 5. Products derived from this software may not be called "OpenSSL"
\r
32 * nor may "OpenSSL" appear in their names without prior written
\r
33 * permission of the OpenSSL Project.
\r
35 * 6. Redistributions of any form whatsoever must retain the following
\r
37 * "This product includes software developed by the OpenSSL Project
\r
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
\r
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
\r
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
\r
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
\r
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
\r
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
\r
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
\r
52 * ====================================================================
\r
54 * This product includes cryptographic software written by Eric Young
\r
55 * (eay@cryptsoft.com). This product includes software written by Tim
\r
56 * Hudson (tjh@cryptsoft.com).
\r
59 #ifndef HEADER_ECDSA_H
\r
60 # define HEADER_ECDSA_H
\r
62 # include <openssl/opensslconf.h>
\r
64 # ifdef OPENSSL_NO_ECDSA
\r
65 # error ECDSA is disabled.
\r
68 # include <openssl/ec.h>
\r
69 # include <openssl/ossl_typ.h>
\r
70 # ifndef OPENSSL_NO_DEPRECATED
\r
71 # include <openssl/bn.h>
\r
78 typedef struct ECDSA_SIG_st {
\r
83 /** Allocates and initialize a ECDSA_SIG structure
\r
84 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
\r
86 ECDSA_SIG *ECDSA_SIG_new(void);
\r
88 /** frees a ECDSA_SIG structure
\r
89 * \param sig pointer to the ECDSA_SIG structure
\r
91 void ECDSA_SIG_free(ECDSA_SIG *sig);
\r
93 /** DER encode content of ECDSA_SIG object (note: this function modifies *pp
\r
94 * (*pp += length of the DER encoded signature)).
\r
95 * \param sig pointer to the ECDSA_SIG object
\r
96 * \param pp pointer to a unsigned char pointer for the output or NULL
\r
97 * \return the length of the DER encoded ECDSA_SIG object or 0
\r
99 int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp);
\r
101 /** Decodes a DER encoded ECDSA signature (note: this function changes *pp
\r
103 * \param sig pointer to ECDSA_SIG pointer (may be NULL)
\r
104 * \param pp memory buffer with the DER encoded signature
\r
105 * \param len length of the buffer
\r
106 * \return pointer to the decoded ECDSA_SIG structure (or NULL)
\r
108 ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len);
\r
110 /** Computes the ECDSA signature of the given hash value using
\r
111 * the supplied private key and returns the created signature.
\r
112 * \param dgst pointer to the hash value
\r
113 * \param dgst_len length of the hash value
\r
114 * \param eckey EC_KEY object containing a private EC key
\r
115 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
\r
117 ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len,
\r
120 /** Computes ECDSA signature of a given hash value using the supplied
\r
121 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
\r
122 * \param dgst pointer to the hash value to sign
\r
123 * \param dgstlen length of the hash value
\r
124 * \param kinv BIGNUM with a pre-computed inverse k (optional)
\r
125 * \param rp BIGNUM with a pre-computed rp value (optioanl),
\r
126 * see ECDSA_sign_setup
\r
127 * \param eckey EC_KEY object containing a private EC key
\r
128 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
\r
130 ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
\r
131 const BIGNUM *kinv, const BIGNUM *rp,
\r
134 /** Verifies that the supplied signature is a valid ECDSA
\r
135 * signature of the supplied hash value using the supplied public key.
\r
136 * \param dgst pointer to the hash value
\r
137 * \param dgst_len length of the hash value
\r
138 * \param sig ECDSA_SIG structure
\r
139 * \param eckey EC_KEY object containing a public EC key
\r
140 * \return 1 if the signature is valid, 0 if the signature is invalid
\r
143 int ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
\r
144 const ECDSA_SIG *sig, EC_KEY *eckey);
\r
146 const ECDSA_METHOD *ECDSA_OpenSSL(void);
\r
148 /** Sets the default ECDSA method
\r
149 * \param meth new default ECDSA_METHOD
\r
151 void ECDSA_set_default_method(const ECDSA_METHOD *meth);
\r
153 /** Returns the default ECDSA method
\r
154 * \return pointer to ECDSA_METHOD structure containing the default method
\r
156 const ECDSA_METHOD *ECDSA_get_default_method(void);
\r
158 /** Sets method to be used for the ECDSA operations
\r
159 * \param eckey EC_KEY object
\r
160 * \param meth new method
\r
161 * \return 1 on success and 0 otherwise
\r
163 int ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth);
\r
165 /** Returns the maximum length of the DER encoded signature
\r
166 * \param eckey EC_KEY object
\r
167 * \return numbers of bytes required for the DER encoded signature
\r
169 int ECDSA_size(const EC_KEY *eckey);
\r
171 /** Precompute parts of the signing operation
\r
172 * \param eckey EC_KEY object containing a private EC key
\r
173 * \param ctx BN_CTX object (optional)
\r
174 * \param kinv BIGNUM pointer for the inverse of k
\r
175 * \param rp BIGNUM pointer for x coordinate of k * generator
\r
176 * \return 1 on success and 0 otherwise
\r
178 int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp);
\r
180 /** Computes ECDSA signature of a given hash value using the supplied
\r
181 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
\r
182 * \param type this parameter is ignored
\r
183 * \param dgst pointer to the hash value to sign
\r
184 * \param dgstlen length of the hash value
\r
185 * \param sig memory for the DER encoded created signature
\r
186 * \param siglen pointer to the length of the returned signature
\r
187 * \param eckey EC_KEY object containing a private EC key
\r
188 * \return 1 on success and 0 otherwise
\r
190 int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen,
\r
191 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
\r
193 /** Computes ECDSA signature of a given hash value using the supplied
\r
194 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
\r
195 * \param type this parameter is ignored
\r
196 * \param dgst pointer to the hash value to sign
\r
197 * \param dgstlen length of the hash value
\r
198 * \param sig buffer to hold the DER encoded signature
\r
199 * \param siglen pointer to the length of the returned signature
\r
200 * \param kinv BIGNUM with a pre-computed inverse k (optional)
\r
201 * \param rp BIGNUM with a pre-computed rp value (optioanl),
\r
202 * see ECDSA_sign_setup
\r
203 * \param eckey EC_KEY object containing a private EC key
\r
204 * \return 1 on success and 0 otherwise
\r
206 int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen,
\r
207 unsigned char *sig, unsigned int *siglen,
\r
208 const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey);
\r
210 /** Verifies that the given signature is valid ECDSA signature
\r
211 * of the supplied hash value using the specified public key.
\r
212 * \param type this parameter is ignored
\r
213 * \param dgst pointer to the hash value
\r
214 * \param dgstlen length of the hash value
\r
215 * \param sig pointer to the DER encoded signature
\r
216 * \param siglen length of the DER encoded signature
\r
217 * \param eckey EC_KEY object containing a public EC key
\r
218 * \return 1 if the signature is valid, 0 if the signature is invalid
\r
221 int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen,
\r
222 const unsigned char *sig, int siglen, EC_KEY *eckey);
\r
224 /* the standard ex_data functions */
\r
225 int ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new
\r
226 *new_func, CRYPTO_EX_dup *dup_func,
\r
227 CRYPTO_EX_free *free_func);
\r
228 int ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg);
\r
229 void *ECDSA_get_ex_data(EC_KEY *d, int idx);
\r
231 /** Allocates and initialize a ECDSA_METHOD structure
\r
232 * \param ecdsa_method pointer to ECDSA_METHOD to copy. (May be NULL)
\r
233 * \return pointer to a ECDSA_METHOD structure or NULL if an error occurred
\r
236 ECDSA_METHOD *ECDSA_METHOD_new(ECDSA_METHOD *ecdsa_method);
\r
238 /** frees a ECDSA_METHOD structure
\r
239 * \param ecdsa_method pointer to the ECDSA_METHOD structure
\r
241 void ECDSA_METHOD_free(ECDSA_METHOD *ecdsa_method);
\r
243 /** Sets application specific data in the ECDSA_METHOD
\r
244 * \param ecdsa_method pointer to existing ECDSA_METHOD
\r
245 * \param app application specific data to set
\r
248 void ECDSA_METHOD_set_app_data(ECDSA_METHOD *ecdsa_method, void *app);
\r
250 /** Returns application specific data from a ECDSA_METHOD structure
\r
251 * \param ecdsa_method pointer to ECDSA_METHOD structure
\r
252 * \return pointer to application specific data.
\r
255 void *ECDSA_METHOD_get_app_data(ECDSA_METHOD *ecdsa_method);
\r
257 /** Set the ECDSA_do_sign function in the ECDSA_METHOD
\r
258 * \param ecdsa_method pointer to existing ECDSA_METHOD
\r
259 * \param ecdsa_do_sign a funtion of type ECDSA_do_sign
\r
262 void ECDSA_METHOD_set_sign(ECDSA_METHOD *ecdsa_method,
\r
263 ECDSA_SIG *(*ecdsa_do_sign) (const unsigned char
\r
264 *dgst, int dgst_len,
\r
269 /** Set the ECDSA_sign_setup function in the ECDSA_METHOD
\r
270 * \param ecdsa_method pointer to existing ECDSA_METHOD
\r
271 * \param ecdsa_sign_setup a funtion of type ECDSA_sign_setup
\r
274 void ECDSA_METHOD_set_sign_setup(ECDSA_METHOD *ecdsa_method,
\r
275 int (*ecdsa_sign_setup) (EC_KEY *eckey,
\r
280 /** Set the ECDSA_do_verify function in the ECDSA_METHOD
\r
281 * \param ecdsa_method pointer to existing ECDSA_METHOD
\r
282 * \param ecdsa_do_verify a funtion of type ECDSA_do_verify
\r
285 void ECDSA_METHOD_set_verify(ECDSA_METHOD *ecdsa_method,
\r
286 int (*ecdsa_do_verify) (const unsigned char
\r
287 *dgst, int dgst_len,
\r
288 const ECDSA_SIG *sig,
\r
291 void ECDSA_METHOD_set_flags(ECDSA_METHOD *ecdsa_method, int flags);
\r
293 /** Set the flags field in the ECDSA_METHOD
\r
294 * \param ecdsa_method pointer to existing ECDSA_METHOD
\r
295 * \param flags flags value to set
\r
298 void ECDSA_METHOD_set_name(ECDSA_METHOD *ecdsa_method, char *name);
\r
300 /** Set the name field in the ECDSA_METHOD
\r
301 * \param ecdsa_method pointer to existing ECDSA_METHOD
\r
302 * \param name name to set
\r
305 /* BEGIN ERROR CODES */
\r
307 * The following lines are auto generated by the script mkerr.pl. Any changes
\r
308 * made after this point may be overwritten when the script is next run.
\r
310 void ERR_load_ECDSA_strings(void);
\r
312 /* Error codes for the ECDSA functions. */
\r
314 /* Function codes. */
\r
315 # define ECDSA_F_ECDSA_CHECK 104
\r
316 # define ECDSA_F_ECDSA_DATA_NEW_METHOD 100
\r
317 # define ECDSA_F_ECDSA_DO_SIGN 101
\r
318 # define ECDSA_F_ECDSA_DO_VERIFY 102
\r
319 # define ECDSA_F_ECDSA_METHOD_NEW 105
\r
320 # define ECDSA_F_ECDSA_SIGN_SETUP 103
\r
322 /* Reason codes. */
\r
323 # define ECDSA_R_BAD_SIGNATURE 100
\r
324 # define ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 101
\r
325 # define ECDSA_R_ERR_EC_LIB 102
\r
326 # define ECDSA_R_MISSING_PARAMETERS 103
\r
327 # define ECDSA_R_NEED_NEW_SETUP_VALUES 106
\r
328 # define ECDSA_R_NON_FIPS_METHOD 107
\r
329 # define ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED 104
\r
330 # define ECDSA_R_SIGNATURE_MALLOC_FAILED 105
\r