1160814Ssimon/* crypto/ecdsa/ecdsa.h */
2160814Ssimon/**
3160814Ssimon * \file   crypto/ecdsa/ecdsa.h Include file for the OpenSSL ECDSA functions
4160814Ssimon * \author Written by Nils Larsch for the OpenSSL project
5160814Ssimon */
6160814Ssimon/* ====================================================================
7160814Ssimon * Copyright (c) 2000-2003 The OpenSSL Project.  All rights reserved.
8160814Ssimon *
9160814Ssimon * Redistribution and use in source and binary forms, with or without
10160814Ssimon * modification, are permitted provided that the following conditions
11160814Ssimon * are met:
12160814Ssimon *
13160814Ssimon * 1. Redistributions of source code must retain the above copyright
14296465Sdelphij *    notice, this list of conditions and the following disclaimer.
15160814Ssimon *
16160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
17160814Ssimon *    notice, this list of conditions and the following disclaimer in
18160814Ssimon *    the documentation and/or other materials provided with the
19160814Ssimon *    distribution.
20160814Ssimon *
21160814Ssimon * 3. All advertising materials mentioning features or use of this
22160814Ssimon *    software must display the following acknowledgment:
23160814Ssimon *    "This product includes software developed by the OpenSSL Project
24160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25160814Ssimon *
26160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27160814Ssimon *    endorse or promote products derived from this software without
28160814Ssimon *    prior written permission. For written permission, please contact
29160814Ssimon *    licensing@OpenSSL.org.
30160814Ssimon *
31160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
32160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
33160814Ssimon *    permission of the OpenSSL Project.
34160814Ssimon *
35160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
36160814Ssimon *    acknowledgment:
37160814Ssimon *    "This product includes software developed by the OpenSSL Project
38160814Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39160814Ssimon *
40160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
52160814Ssimon * ====================================================================
53160814Ssimon *
54160814Ssimon * This product includes cryptographic software written by Eric Young
55160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
56160814Ssimon * Hudson (tjh@cryptsoft.com).
57160814Ssimon *
58160814Ssimon */
59160814Ssimon#ifndef HEADER_ECDSA_H
60296465Sdelphij# define HEADER_ECDSA_H
61160814Ssimon
62296465Sdelphij# include <openssl/opensslconf.h>
63160814Ssimon
64296465Sdelphij# ifdef OPENSSL_NO_ECDSA
65296465Sdelphij#  error ECDSA is disabled.
66296465Sdelphij# endif
67160814Ssimon
68296465Sdelphij# include <openssl/ec.h>
69296465Sdelphij# include <openssl/ossl_typ.h>
70296465Sdelphij# ifndef OPENSSL_NO_DEPRECATED
71296465Sdelphij#  include <openssl/bn.h>
72296465Sdelphij# endif
73160814Ssimon
74160814Ssimon#ifdef __cplusplus
75160814Ssimonextern "C" {
76160814Ssimon#endif
77160814Ssimon
78296465Sdelphijtypedef struct ECDSA_SIG_st {
79296465Sdelphij    BIGNUM *r;
80296465Sdelphij    BIGNUM *s;
81296465Sdelphij} ECDSA_SIG;
82160814Ssimon
83160814Ssimon/** ECDSA_SIG *ECDSA_SIG_new(void)
84160814Ssimon * allocates and initialize a ECDSA_SIG structure
85160814Ssimon * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
86160814Ssimon */
87160814SsimonECDSA_SIG *ECDSA_SIG_new(void);
88160814Ssimon
89160814Ssimon/** ECDSA_SIG_free
90160814Ssimon * frees a ECDSA_SIG structure
91160814Ssimon * \param a pointer to the ECDSA_SIG structure
92160814Ssimon */
93296465Sdelphijvoid ECDSA_SIG_free(ECDSA_SIG *a);
94160814Ssimon
95160814Ssimon/** i2d_ECDSA_SIG
96160814Ssimon * DER encode content of ECDSA_SIG object (note: this function modifies *pp
97160814Ssimon * (*pp += length of the DER encoded signature)).
98160814Ssimon * \param a  pointer to the ECDSA_SIG object
99160814Ssimon * \param pp pointer to a unsigned char pointer for the output or NULL
100296465Sdelphij * \return the length of the DER encoded ECDSA_SIG object or 0
101160814Ssimon */
102296465Sdelphijint i2d_ECDSA_SIG(const ECDSA_SIG *a, unsigned char **pp);
103160814Ssimon
104160814Ssimon/** d2i_ECDSA_SIG
105160814Ssimon * decodes a DER encoded ECDSA signature (note: this function changes *pp
106296465Sdelphij * (*pp += len)).
107160814Ssimon * \param v pointer to ECDSA_SIG pointer (may be NULL)
108160814Ssimon * \param pp buffer with the DER encoded signature
109160814Ssimon * \param len bufferlength
110160814Ssimon * \return pointer to the decoded ECDSA_SIG structure (or NULL)
111160814Ssimon */
112160814SsimonECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **v, const unsigned char **pp, long len);
113160814Ssimon
114160814Ssimon/** ECDSA_do_sign
115160814Ssimon * computes the ECDSA signature of the given hash value using
116160814Ssimon * the supplied private key and returns the created signature.
117160814Ssimon * \param dgst pointer to the hash value
118160814Ssimon * \param dgst_len length of the hash value
119160814Ssimon * \param eckey pointer to the EC_KEY object containing a private EC key
120160814Ssimon * \return pointer to a ECDSA_SIG structure or NULL
121160814Ssimon */
122296465SdelphijECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len,
123296465Sdelphij                         EC_KEY *eckey);
124160814Ssimon
125160814Ssimon/** ECDSA_do_sign_ex
126160814Ssimon * computes ECDSA signature of a given hash value using the supplied
127160814Ssimon * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
128160814Ssimon * \param dgst pointer to the hash value to sign
129160814Ssimon * \param dgstlen length of the hash value
130160814Ssimon * \param kinv optional pointer to a pre-computed inverse k
131296465Sdelphij * \param rp optional pointer to the pre-computed rp value (see
132160814Ssimon *        ECDSA_sign_setup
133160814Ssimon * \param eckey pointer to the EC_KEY object containing a private EC key
134160814Ssimon * \return pointer to a ECDSA_SIG structure or NULL
135160814Ssimon */
136296465SdelphijECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
137296465Sdelphij                            const BIGNUM *kinv, const BIGNUM *rp,
138296465Sdelphij                            EC_KEY *eckey);
139160814Ssimon
140160814Ssimon/** ECDSA_do_verify
141160814Ssimon * verifies that the supplied signature is a valid ECDSA
142160814Ssimon * signature of the supplied hash value using the supplied public key.
143160814Ssimon * \param dgst pointer to the hash value
144160814Ssimon * \param dgst_len length of the hash value
145160814Ssimon * \param sig  pointer to the ECDSA_SIG structure
146160814Ssimon * \param eckey pointer to the EC_KEY object containing a public EC key
147160814Ssimon * \return 1 if the signature is valid, 0 if the signature is invalid and -1 on error
148160814Ssimon */
149296465Sdelphijint ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
150296465Sdelphij                    const ECDSA_SIG *sig, EC_KEY *eckey);
151160814Ssimon
152160814Ssimonconst ECDSA_METHOD *ECDSA_OpenSSL(void);
153160814Ssimon
154160814Ssimon/** ECDSA_set_default_method
155160814Ssimon * sets the default ECDSA method
156160814Ssimon * \param meth the new default ECDSA_METHOD
157160814Ssimon */
158296465Sdelphijvoid ECDSA_set_default_method(const ECDSA_METHOD *meth);
159160814Ssimon
160160814Ssimon/** ECDSA_get_default_method
161160814Ssimon * returns the default ECDSA method
162160814Ssimon * \return pointer to ECDSA_METHOD structure containing the default method
163160814Ssimon */
164160814Ssimonconst ECDSA_METHOD *ECDSA_get_default_method(void);
165160814Ssimon
166160814Ssimon/** ECDSA_set_method
167160814Ssimon * sets method to be used for the ECDSA operations
168160814Ssimon * \param eckey pointer to the EC_KEY object
169160814Ssimon * \param meth  pointer to the new method
170296465Sdelphij * \return 1 on success and 0 otherwise
171160814Ssimon */
172296465Sdelphijint ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth);
173160814Ssimon
174160814Ssimon/** ECDSA_size
175160814Ssimon * returns the maximum length of the DER encoded signature
176160814Ssimon * \param  eckey pointer to a EC_KEY object
177160814Ssimon * \return numbers of bytes required for the DER encoded signature
178160814Ssimon */
179296465Sdelphijint ECDSA_size(const EC_KEY *eckey);
180160814Ssimon
181160814Ssimon/** ECDSA_sign_setup
182296465Sdelphij * precompute parts of the signing operation.
183160814Ssimon * \param eckey pointer to the EC_KEY object containing a private EC key
184160814Ssimon * \param ctx  pointer to a BN_CTX object (may be NULL)
185160814Ssimon * \param kinv pointer to a BIGNUM pointer for the inverse of k
186160814Ssimon * \param rp   pointer to a BIGNUM pointer for x coordinate of k * generator
187160814Ssimon * \return 1 on success and 0 otherwise
188160814Ssimon */
189296465Sdelphijint ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp);
190160814Ssimon
191160814Ssimon/** ECDSA_sign
192160814Ssimon * computes ECDSA signature of a given hash value using the supplied
193160814Ssimon * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
194160814Ssimon * \param type this parameter is ignored
195160814Ssimon * \param dgst pointer to the hash value to sign
196160814Ssimon * \param dgstlen length of the hash value
197160814Ssimon * \param sig buffer to hold the DER encoded signature
198160814Ssimon * \param siglen pointer to the length of the returned signature
199160814Ssimon * \param eckey pointer to the EC_KEY object containing a private EC key
200160814Ssimon * \return 1 on success and 0 otherwise
201160814Ssimon */
202296465Sdelphijint ECDSA_sign(int type, const unsigned char *dgst, int dgstlen,
203296465Sdelphij               unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
204160814Ssimon
205160814Ssimon/** ECDSA_sign_ex
206160814Ssimon * computes ECDSA signature of a given hash value using the supplied
207160814Ssimon * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
208160814Ssimon * \param type this parameter is ignored
209160814Ssimon * \param dgst pointer to the hash value to sign
210160814Ssimon * \param dgstlen length of the hash value
211160814Ssimon * \param sig buffer to hold the DER encoded signature
212160814Ssimon * \param siglen pointer to the length of the returned signature
213160814Ssimon * \param kinv optional pointer to a pre-computed inverse k
214296465Sdelphij * \param rp optional pointer to the pre-computed rp value (see
215160814Ssimon *        ECDSA_sign_setup
216160814Ssimon * \param eckey pointer to the EC_KEY object containing a private EC key
217160814Ssimon * \return 1 on success and 0 otherwise
218160814Ssimon */
219296465Sdelphijint ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen,
220296465Sdelphij                  unsigned char *sig, unsigned int *siglen,
221296465Sdelphij                  const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey);
222160814Ssimon
223160814Ssimon/** ECDSA_verify
224160814Ssimon * verifies that the given signature is valid ECDSA signature
225160814Ssimon * of the supplied hash value using the specified public key.
226160814Ssimon * \param type this parameter is ignored
227296465Sdelphij * \param dgst pointer to the hash value
228160814Ssimon * \param dgstlen length of the hash value
229160814Ssimon * \param sig  pointer to the DER encoded signature
230160814Ssimon * \param siglen length of the DER encoded signature
231160814Ssimon * \param eckey pointer to the EC_KEY object containing a public EC key
232160814Ssimon * \return 1 if the signature is valid, 0 if the signature is invalid and -1 on error
233160814Ssimon */
234296465Sdelphijint ECDSA_verify(int type, const unsigned char *dgst, int dgstlen,
235296465Sdelphij                 const unsigned char *sig, int siglen, EC_KEY *eckey);
236160814Ssimon
237160814Ssimon/* the standard ex_data functions */
238296465Sdelphijint ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new
239296465Sdelphij                           *new_func, CRYPTO_EX_dup *dup_func,
240296465Sdelphij                           CRYPTO_EX_free *free_func);
241296465Sdelphijint ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg);
242296465Sdelphijvoid *ECDSA_get_ex_data(EC_KEY *d, int idx);
243160814Ssimon
244160814Ssimon/* BEGIN ERROR CODES */
245296465Sdelphij/*
246296465Sdelphij * The following lines are auto generated by the script mkerr.pl. Any changes
247160814Ssimon * made after this point may be overwritten when the script is next run.
248160814Ssimon */
249160814Ssimonvoid ERR_load_ECDSA_strings(void);
250160814Ssimon
251160814Ssimon/* Error codes for the ECDSA functions. */
252160814Ssimon
253160814Ssimon/* Function codes. */
254296465Sdelphij# define ECDSA_F_ECDSA_DATA_NEW_METHOD                    100
255296465Sdelphij# define ECDSA_F_ECDSA_DO_SIGN                            101
256296465Sdelphij# define ECDSA_F_ECDSA_DO_VERIFY                          102
257296465Sdelphij# define ECDSA_F_ECDSA_SIGN_SETUP                         103
258160814Ssimon
259160814Ssimon/* Reason codes. */
260296465Sdelphij# define ECDSA_R_BAD_SIGNATURE                            100
261296465Sdelphij# define ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE              101
262296465Sdelphij# define ECDSA_R_ERR_EC_LIB                               102
263296465Sdelphij# define ECDSA_R_MISSING_PARAMETERS                       103
264296465Sdelphij# define ECDSA_R_NEED_NEW_SETUP_VALUES                    106
265296465Sdelphij# define ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED          104
266296465Sdelphij# define ECDSA_R_SIGNATURE_MALLOC_FAILED                  105
267160814Ssimon
268160814Ssimon#ifdef  __cplusplus
269160814Ssimon}
270160814Ssimon#endif
271160814Ssimon#endif
272