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/* ====================================================================
7238405Sjkim * Copyright (c) 2000-2005 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
14160814Ssimon *    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
60160814Ssimon#define HEADER_ECDSA_H
61160814Ssimon
62160814Ssimon#include <openssl/opensslconf.h>
63160814Ssimon
64160814Ssimon#ifdef OPENSSL_NO_ECDSA
65160814Ssimon#error ECDSA is disabled.
66160814Ssimon#endif
67160814Ssimon
68160814Ssimon#include <openssl/ec.h>
69160814Ssimon#include <openssl/ossl_typ.h>
70160814Ssimon#ifndef OPENSSL_NO_DEPRECATED
71160814Ssimon#include <openssl/bn.h>
72160814Ssimon#endif
73160814Ssimon
74160814Ssimon#ifdef __cplusplus
75160814Ssimonextern "C" {
76160814Ssimon#endif
77160814Ssimon
78160814Ssimontypedef struct ECDSA_SIG_st
79160814Ssimon	{
80160814Ssimon	BIGNUM *r;
81160814Ssimon	BIGNUM *s;
82160814Ssimon	} ECDSA_SIG;
83160814Ssimon
84238405Sjkim/** Allocates and initialize a ECDSA_SIG structure
85238405Sjkim *  \return pointer to a ECDSA_SIG structure or NULL if an error occurred
86160814Ssimon */
87160814SsimonECDSA_SIG *ECDSA_SIG_new(void);
88160814Ssimon
89238405Sjkim/** frees a ECDSA_SIG structure
90238405Sjkim *  \param  sig  pointer to the ECDSA_SIG structure
91160814Ssimon */
92238405Sjkimvoid	  ECDSA_SIG_free(ECDSA_SIG *sig);
93160814Ssimon
94238405Sjkim/** DER encode content of ECDSA_SIG object (note: this function modifies *pp
95238405Sjkim *  (*pp += length of the DER encoded signature)).
96238405Sjkim *  \param  sig  pointer to the ECDSA_SIG object
97238405Sjkim *  \param  pp   pointer to a unsigned char pointer for the output or NULL
98238405Sjkim *  \return the length of the DER encoded ECDSA_SIG object or 0
99160814Ssimon */
100238405Sjkimint	  i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp);
101160814Ssimon
102238405Sjkim/** Decodes a DER encoded ECDSA signature (note: this function changes *pp
103238405Sjkim *  (*pp += len)).
104238405Sjkim *  \param  sig  pointer to ECDSA_SIG pointer (may be NULL)
105238405Sjkim *  \param  pp   memory buffer with the DER encoded signature
106238405Sjkim *  \param  len  length of the buffer
107238405Sjkim *  \return pointer to the decoded ECDSA_SIG structure (or NULL)
108160814Ssimon */
109238405SjkimECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len);
110160814Ssimon
111238405Sjkim/** Computes the ECDSA signature of the given hash value using
112238405Sjkim *  the supplied private key and returns the created signature.
113238405Sjkim *  \param  dgst      pointer to the hash value
114238405Sjkim *  \param  dgst_len  length of the hash value
115238405Sjkim *  \param  eckey     EC_KEY object containing a private EC key
116238405Sjkim *  \return pointer to a ECDSA_SIG structure or NULL if an error occurred
117160814Ssimon */
118160814SsimonECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst,int dgst_len,EC_KEY *eckey);
119160814Ssimon
120238405Sjkim/** Computes ECDSA signature of a given hash value using the supplied
121238405Sjkim *  private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
122238405Sjkim *  \param  dgst     pointer to the hash value to sign
123238405Sjkim *  \param  dgstlen  length of the hash value
124238405Sjkim *  \param  kinv     BIGNUM with a pre-computed inverse k (optional)
125238405Sjkim *  \param  rp       BIGNUM with a pre-computed rp value (optioanl),
126238405Sjkim *                   see ECDSA_sign_setup
127238405Sjkim *  \param  eckey    EC_KEY object containing a private EC key
128238405Sjkim *  \return pointer to a ECDSA_SIG structure or NULL if an error occurred
129160814Ssimon */
130160814SsimonECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
131160814Ssimon		const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey);
132160814Ssimon
133238405Sjkim/** Verifies that the supplied signature is a valid ECDSA
134238405Sjkim *  signature of the supplied hash value using the supplied public key.
135238405Sjkim *  \param  dgst      pointer to the hash value
136238405Sjkim *  \param  dgst_len  length of the hash value
137238405Sjkim *  \param  sig       ECDSA_SIG structure
138238405Sjkim *  \param  eckey     EC_KEY object containing a public EC key
139238405Sjkim *  \return 1 if the signature is valid, 0 if the signature is invalid
140238405Sjkim *          and -1 on error
141160814Ssimon */
142160814Ssimonint	  ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
143160814Ssimon		const ECDSA_SIG *sig, EC_KEY* eckey);
144160814Ssimon
145160814Ssimonconst ECDSA_METHOD *ECDSA_OpenSSL(void);
146160814Ssimon
147238405Sjkim/** Sets the default ECDSA method
148238405Sjkim *  \param  meth  new default ECDSA_METHOD
149160814Ssimon */
150160814Ssimonvoid	  ECDSA_set_default_method(const ECDSA_METHOD *meth);
151160814Ssimon
152238405Sjkim/** Returns the default ECDSA method
153238405Sjkim *  \return pointer to ECDSA_METHOD structure containing the default method
154160814Ssimon */
155160814Ssimonconst ECDSA_METHOD *ECDSA_get_default_method(void);
156160814Ssimon
157238405Sjkim/** Sets method to be used for the ECDSA operations
158238405Sjkim *  \param  eckey  EC_KEY object
159238405Sjkim *  \param  meth   new method
160238405Sjkim *  \return 1 on success and 0 otherwise
161160814Ssimon */
162160814Ssimonint 	  ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth);
163160814Ssimon
164238405Sjkim/** Returns the maximum length of the DER encoded signature
165238405Sjkim *  \param  eckey  EC_KEY object
166238405Sjkim *  \return numbers of bytes required for the DER encoded signature
167160814Ssimon */
168160814Ssimonint	  ECDSA_size(const EC_KEY *eckey);
169160814Ssimon
170238405Sjkim/** Precompute parts of the signing operation
171238405Sjkim *  \param  eckey  EC_KEY object containing a private EC key
172238405Sjkim *  \param  ctx    BN_CTX object (optional)
173238405Sjkim *  \param  kinv   BIGNUM pointer for the inverse of k
174238405Sjkim *  \param  rp     BIGNUM pointer for x coordinate of k * generator
175238405Sjkim *  \return 1 on success and 0 otherwise
176160814Ssimon */
177160814Ssimonint 	  ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv,
178160814Ssimon		BIGNUM **rp);
179160814Ssimon
180238405Sjkim/** Computes ECDSA signature of a given hash value using the supplied
181238405Sjkim *  private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
182238405Sjkim *  \param  type     this parameter is ignored
183238405Sjkim *  \param  dgst     pointer to the hash value to sign
184238405Sjkim *  \param  dgstlen  length of the hash value
185238405Sjkim *  \param  sig      memory for the DER encoded created signature
186238405Sjkim *  \param  siglen   pointer to the length of the returned signature
187238405Sjkim *  \param  eckey    EC_KEY object containing a private EC key
188238405Sjkim *  \return 1 on success and 0 otherwise
189160814Ssimon */
190160814Ssimonint	  ECDSA_sign(int type, const unsigned char *dgst, int dgstlen,
191160814Ssimon		unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
192160814Ssimon
193160814Ssimon
194238405Sjkim/** Computes ECDSA signature of a given hash value using the supplied
195238405Sjkim *  private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
196238405Sjkim *  \param  type     this parameter is ignored
197238405Sjkim *  \param  dgst     pointer to the hash value to sign
198238405Sjkim *  \param  dgstlen  length of the hash value
199238405Sjkim *  \param  sig      buffer to hold the DER encoded signature
200238405Sjkim *  \param  siglen   pointer to the length of the returned signature
201238405Sjkim *  \param  kinv     BIGNUM with a pre-computed inverse k (optional)
202238405Sjkim *  \param  rp       BIGNUM with a pre-computed rp value (optioanl),
203238405Sjkim *                   see ECDSA_sign_setup
204238405Sjkim *  \param  eckey    EC_KEY object containing a private EC key
205238405Sjkim *  \return 1 on success and 0 otherwise
206160814Ssimon */
207160814Ssimonint	  ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen,
208160814Ssimon		unsigned char *sig, unsigned int *siglen, const BIGNUM *kinv,
209160814Ssimon		const BIGNUM *rp, EC_KEY *eckey);
210160814Ssimon
211238405Sjkim/** Verifies that the given signature is valid ECDSA signature
212238405Sjkim *  of the supplied hash value using the specified public key.
213238405Sjkim *  \param  type     this parameter is ignored
214238405Sjkim *  \param  dgst     pointer to the hash value
215238405Sjkim *  \param  dgstlen  length of the hash value
216238405Sjkim *  \param  sig      pointer to the DER encoded signature
217238405Sjkim *  \param  siglen   length of the DER encoded signature
218238405Sjkim *  \param  eckey    EC_KEY object containing a public EC key
219238405Sjkim *  \return 1 if the signature is valid, 0 if the signature is invalid
220238405Sjkim *          and -1 on error
221160814Ssimon */
222160814Ssimonint 	  ECDSA_verify(int type, const unsigned char *dgst, int dgstlen,
223160814Ssimon		const unsigned char *sig, int siglen, EC_KEY *eckey);
224160814Ssimon
225160814Ssimon/* the standard ex_data functions */
226160814Ssimonint 	  ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new
227160814Ssimon		*new_func, CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
228160814Ssimonint 	  ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg);
229160814Ssimonvoid 	  *ECDSA_get_ex_data(EC_KEY *d, int idx);
230160814Ssimon
231160814Ssimon
232160814Ssimon/* BEGIN ERROR CODES */
233160814Ssimon/* The following lines are auto generated by the script mkerr.pl. Any changes
234160814Ssimon * made after this point may be overwritten when the script is next run.
235160814Ssimon */
236160814Ssimonvoid ERR_load_ECDSA_strings(void);
237160814Ssimon
238160814Ssimon/* Error codes for the ECDSA functions. */
239160814Ssimon
240160814Ssimon/* Function codes. */
241238405Sjkim#define ECDSA_F_ECDSA_CHECK				 104
242160814Ssimon#define ECDSA_F_ECDSA_DATA_NEW_METHOD			 100
243160814Ssimon#define ECDSA_F_ECDSA_DO_SIGN				 101
244160814Ssimon#define ECDSA_F_ECDSA_DO_VERIFY				 102
245160814Ssimon#define ECDSA_F_ECDSA_SIGN_SETUP			 103
246160814Ssimon
247160814Ssimon/* Reason codes. */
248160814Ssimon#define ECDSA_R_BAD_SIGNATURE				 100
249160814Ssimon#define ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE		 101
250160814Ssimon#define ECDSA_R_ERR_EC_LIB				 102
251160814Ssimon#define ECDSA_R_MISSING_PARAMETERS			 103
252167612Ssimon#define ECDSA_R_NEED_NEW_SETUP_VALUES			 106
253238405Sjkim#define ECDSA_R_NON_FIPS_METHOD				 107
254160814Ssimon#define ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED		 104
255160814Ssimon#define ECDSA_R_SIGNATURE_MALLOC_FAILED			 105
256160814Ssimon
257160814Ssimon#ifdef  __cplusplus
258160814Ssimon}
259160814Ssimon#endif
260160814Ssimon#endif
261