xref: /openbsd-src/lib/libcrypto/ecdsa/ecdsa.h (revision 521ba2f2ab0e0e89d1776559874b3ecc227442fc)
1 /* $OpenBSD: ecdsa.h,v 1.16 2023/06/19 09:12:41 tb Exp $ */
2 /*
3  * Written by Nils Larsch for the OpenSSL project
4  */
5 /* ====================================================================
6  * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 #ifndef HEADER_ECDSA_H
59 #define HEADER_ECDSA_H
60 
61 #include <openssl/opensslconf.h>
62 
63 #ifdef OPENSSL_NO_ECDSA
64 #error ECDSA is disabled.
65 #endif
66 
67 #include <openssl/bn.h>
68 #include <openssl/ec.h>
69 
70 #include <openssl/ossl_typ.h>
71 
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75 
76 typedef struct ECDSA_SIG_st ECDSA_SIG;
77 
78 struct ecdsa_method {
79 	const char *name;
80 	ECDSA_SIG *(*ecdsa_do_sign)(const unsigned char *dgst, int dgst_len,
81 	    const BIGNUM *inv, const BIGNUM *rp, EC_KEY *eckey);
82 	int (*ecdsa_sign_setup)(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv,
83 	    BIGNUM **r);
84 	int (*ecdsa_do_verify)(const unsigned char *dgst, int dgst_len,
85 	    const ECDSA_SIG *sig, EC_KEY *eckey);
86 	int flags;
87 	char *app_data;
88 };
89 
90 /*
91  * If this flag is set, the ECDSA method is FIPS compliant and can be used
92  * in FIPS mode. This is set in the validated module method. If an
93  * application sets this flag in its own methods it is its responsibility
94  * to ensure the result is compliant.
95  */
96 
97 #define ECDSA_FLAG_FIPS_METHOD  0x1
98 
99 ECDSA_SIG *ECDSA_SIG_new(void);
100 void ECDSA_SIG_free(ECDSA_SIG *sig);
101 int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp);
102 ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len);
103 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
104 
105 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig);
106 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig);
107 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
108 
109 ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len,
110     EC_KEY *eckey);
111 ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
112     const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey);
113 int ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
114     const ECDSA_SIG *sig, EC_KEY* eckey);
115 
116 const ECDSA_METHOD *ECDSA_OpenSSL(void);
117 void ECDSA_set_default_method(const ECDSA_METHOD *meth);
118 const ECDSA_METHOD *ECDSA_get_default_method(void);
119 int ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth);
120 int ECDSA_size(const EC_KEY *eckey);
121 
122 int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv,
123     BIGNUM **rp);
124 int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen,
125     unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
126 int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen,
127     unsigned char *sig, unsigned int *siglen, const BIGNUM *kinv,
128     const BIGNUM *rp, EC_KEY *eckey);
129 int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen,
130     const unsigned char *sig, int siglen, EC_KEY *eckey);
131 
132 int ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
133     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
134 int ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg);
135 void *ECDSA_get_ex_data(EC_KEY *d, int idx);
136 
137 /* XXX should be in ec.h, but needs ECDSA_SIG */
138 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
139     int (*sign)(int type, const unsigned char *dgst,
140 	int dlen, unsigned char *sig, unsigned int *siglen,
141 	const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey),
142     int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
143 	BIGNUM **kinvp, BIGNUM **rp),
144     ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
145 	int dgst_len, const BIGNUM *in_kinv, const BIGNUM *in_r,
146 	EC_KEY *eckey));
147 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
148     int (*verify)(int type, const unsigned char *dgst, int dgst_len,
149 	const unsigned char *sigbuf, int sig_len, EC_KEY *eckey),
150     int (*verify_sig)(const unsigned char *dgst, int dgst_len,
151 	const ECDSA_SIG *sig, EC_KEY *eckey));
152 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
153     int (**psign)(int type, const unsigned char *dgst,
154 	int dlen, unsigned char *sig, unsigned int *siglen,
155 	const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey),
156     int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
157 	BIGNUM **kinvp, BIGNUM **rp),
158     ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
159 	int dgst_len, const BIGNUM *in_kinv, const BIGNUM *in_r,
160 	EC_KEY *eckey));
161 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
162     int (**pverify)(int type, const unsigned char *dgst, int dgst_len,
163 	const unsigned char *sigbuf, int sig_len, EC_KEY *eckey),
164     int (**pverify_sig)(const unsigned char *dgst, int dgst_len,
165 	const ECDSA_SIG *sig, EC_KEY *eckey));
166 
167 void ERR_load_ECDSA_strings(void);
168 
169 /* Error codes for the ECDSA functions. */
170 
171 /* Function codes. */
172 #define ECDSA_F_ECDSA_CHECK				 104
173 #define ECDSA_F_ECDSA_DATA_NEW_METHOD			 100
174 #define ECDSA_F_ECDSA_DO_SIGN				 101
175 #define ECDSA_F_ECDSA_DO_VERIFY				 102
176 #define ECDSA_F_ECDSA_SIGN_SETUP			 103
177 
178 /* Reason codes. */
179 #define ECDSA_R_BAD_SIGNATURE				 100
180 #define ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE		 101
181 #define ECDSA_R_ERR_EC_LIB				 102
182 #define ECDSA_R_MISSING_PARAMETERS			 103
183 #define ECDSA_R_NEED_NEW_SETUP_VALUES			 106
184 #define ECDSA_R_NON_FIPS_METHOD				 107
185 #define ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED		 104
186 #define ECDSA_R_SIGNATURE_MALLOC_FAILED			 105
187 
188 #ifdef  __cplusplus
189 }
190 #endif
191 #endif
192