summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ecdsa
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2016-07-23 19:31:36 +0000
committercvs2svn <admin@example.com>2016-07-23 19:31:36 +0000
commit86c49b31af735796dfde37aa29473a30d36367db (patch)
treee9a354a92a348338fe2b361e2eda703cae23cfab /src/lib/libcrypto/ecdsa
parent19d5fe348e8926bac4521c5807aa64c45b8f7a41 (diff)
downloadopenbsd-OPENBSD_6_0_BASE.tar.gz
openbsd-OPENBSD_6_0_BASE.tar.bz2
openbsd-OPENBSD_6_0_BASE.zip
This commit was manufactured by cvs2git to create tag 'OPENBSD_6_0_BASE'.OPENBSD_6_0_BASE
Diffstat (limited to 'src/lib/libcrypto/ecdsa')
-rw-r--r--src/lib/libcrypto/ecdsa/ecdsa.h286
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_asn1.c115
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_err.c106
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_lib.c264
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_locl.h90
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_ossl.c430
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_sign.c111
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_vrf.c116
8 files changed, 0 insertions, 1518 deletions
diff --git a/src/lib/libcrypto/ecdsa/ecdsa.h b/src/lib/libcrypto/ecdsa/ecdsa.h
deleted file mode 100644
index 530ab265bb..0000000000
--- a/src/lib/libcrypto/ecdsa/ecdsa.h
+++ /dev/null
@@ -1,286 +0,0 @@
1/* $OpenBSD: ecdsa.h,v 1.4 2015/02/08 13:35:06 jsing Exp $ */
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-2005 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/ec.h>
69#include <openssl/ossl_typ.h>
70#ifndef OPENSSL_NO_DEPRECATED
71#include <openssl/bn.h>
72#endif
73
74#ifdef __cplusplus
75extern "C" {
76#endif
77
78typedef struct ECDSA_SIG_st ECDSA_SIG;
79
80struct ecdsa_method {
81 const char *name;
82 ECDSA_SIG *(*ecdsa_do_sign)(const unsigned char *dgst, int dgst_len,
83 const BIGNUM *inv, const BIGNUM *rp, EC_KEY *eckey);
84 int (*ecdsa_sign_setup)(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv,
85 BIGNUM **r);
86 int (*ecdsa_do_verify)(const unsigned char *dgst, int dgst_len,
87 const ECDSA_SIG *sig, EC_KEY *eckey);
88#if 0
89 int (*init)(EC_KEY *eckey);
90 int (*finish)(EC_KEY *eckey);
91#endif
92 int flags;
93 char *app_data;
94};
95
96/* If this flag is set the ECDSA method is FIPS compliant and can be used
97 * in FIPS mode. This is set in the validated module method. If an
98 * application sets this flag in its own methods it is its responsibility
99 * to ensure the result is compliant.
100 */
101
102#define ECDSA_FLAG_FIPS_METHOD 0x1
103
104struct ECDSA_SIG_st {
105 BIGNUM *r;
106 BIGNUM *s;
107};
108
109/** Allocates and initialize a ECDSA_SIG structure
110 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
111 */
112ECDSA_SIG *ECDSA_SIG_new(void);
113
114/** frees a ECDSA_SIG structure
115 * \param sig pointer to the ECDSA_SIG structure
116 */
117void ECDSA_SIG_free(ECDSA_SIG *sig);
118
119/** DER encode content of ECDSA_SIG object (note: this function modifies *pp
120 * (*pp += length of the DER encoded signature)).
121 * \param sig pointer to the ECDSA_SIG object
122 * \param pp pointer to a unsigned char pointer for the output or NULL
123 * \return the length of the DER encoded ECDSA_SIG object or 0
124 */
125int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp);
126
127/** Decodes a DER encoded ECDSA signature (note: this function changes *pp
128 * (*pp += len)).
129 * \param sig pointer to ECDSA_SIG pointer (may be NULL)
130 * \param pp memory buffer with the DER encoded signature
131 * \param len length of the buffer
132 * \return pointer to the decoded ECDSA_SIG structure (or NULL)
133 */
134ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len);
135
136/** Computes the ECDSA signature of the given hash value using
137 * the supplied private key and returns the created signature.
138 * \param dgst pointer to the hash value
139 * \param dgst_len length of the hash value
140 * \param eckey EC_KEY object containing a private EC key
141 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
142 */
143ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len,
144 EC_KEY *eckey);
145
146/** Computes ECDSA signature of a given hash value using the supplied
147 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
148 * \param dgst pointer to the hash value to sign
149 * \param dgstlen length of the hash value
150 * \param kinv BIGNUM with a pre-computed inverse k (optional)
151 * \param rp BIGNUM with a pre-computed rp value (optioanl),
152 * see ECDSA_sign_setup
153 * \param eckey EC_KEY object containing a private EC key
154 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
155 */
156ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
157 const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey);
158
159/** Verifies that the supplied signature is a valid ECDSA
160 * signature of the supplied hash value using the supplied public key.
161 * \param dgst pointer to the hash value
162 * \param dgst_len length of the hash value
163 * \param sig ECDSA_SIG structure
164 * \param eckey EC_KEY object containing a public EC key
165 * \return 1 if the signature is valid, 0 if the signature is invalid
166 * and -1 on error
167 */
168int ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
169 const ECDSA_SIG *sig, EC_KEY* eckey);
170
171const ECDSA_METHOD *ECDSA_OpenSSL(void);
172
173/** Sets the default ECDSA method
174 * \param meth new default ECDSA_METHOD
175 */
176void ECDSA_set_default_method(const ECDSA_METHOD *meth);
177
178/** Returns the default ECDSA method
179 * \return pointer to ECDSA_METHOD structure containing the default method
180 */
181const ECDSA_METHOD *ECDSA_get_default_method(void);
182
183/** Sets method to be used for the ECDSA operations
184 * \param eckey EC_KEY object
185 * \param meth new method
186 * \return 1 on success and 0 otherwise
187 */
188int ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth);
189
190/** Returns the maximum length of the DER encoded signature
191 * \param eckey EC_KEY object
192 * \return numbers of bytes required for the DER encoded signature
193 */
194int ECDSA_size(const EC_KEY *eckey);
195
196/** Precompute parts of the signing operation
197 * \param eckey EC_KEY object containing a private EC key
198 * \param ctx BN_CTX object (optional)
199 * \param kinv BIGNUM pointer for the inverse of k
200 * \param rp BIGNUM pointer for x coordinate of k * generator
201 * \return 1 on success and 0 otherwise
202 */
203int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv,
204 BIGNUM **rp);
205
206/** Computes ECDSA signature of a given hash value using the supplied
207 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
208 * \param type this parameter is ignored
209 * \param dgst pointer to the hash value to sign
210 * \param dgstlen length of the hash value
211 * \param sig memory for the DER encoded created signature
212 * \param siglen pointer to the length of the returned signature
213 * \param eckey EC_KEY object containing a private EC key
214 * \return 1 on success and 0 otherwise
215 */
216int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen,
217 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
218
219
220/** Computes ECDSA signature of a given hash value using the supplied
221 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
222 * \param type this parameter is ignored
223 * \param dgst pointer to the hash value to sign
224 * \param dgstlen length of the hash value
225 * \param sig buffer to hold the DER encoded signature
226 * \param siglen pointer to the length of the returned signature
227 * \param kinv BIGNUM with a pre-computed inverse k (optional)
228 * \param rp BIGNUM with a pre-computed rp value (optioanl),
229 * see ECDSA_sign_setup
230 * \param eckey EC_KEY object containing a private EC key
231 * \return 1 on success and 0 otherwise
232 */
233int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen,
234 unsigned char *sig, unsigned int *siglen, const BIGNUM *kinv,
235 const BIGNUM *rp, EC_KEY *eckey);
236
237/** Verifies that the given signature is valid ECDSA signature
238 * of the supplied hash value using the specified public key.
239 * \param type this parameter is ignored
240 * \param dgst pointer to the hash value
241 * \param dgstlen length of the hash value
242 * \param sig pointer to the DER encoded signature
243 * \param siglen length of the DER encoded signature
244 * \param eckey EC_KEY object containing a public EC key
245 * \return 1 if the signature is valid, 0 if the signature is invalid
246 * and -1 on error
247 */
248int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen,
249 const unsigned char *sig, int siglen, EC_KEY *eckey);
250
251/* the standard ex_data functions */
252int ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
253 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
254int ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg);
255void *ECDSA_get_ex_data(EC_KEY *d, int idx);
256
257
258/* BEGIN ERROR CODES */
259/* The following lines are auto generated by the script mkerr.pl. Any changes
260 * made after this point may be overwritten when the script is next run.
261 */
262void ERR_load_ECDSA_strings(void);
263
264/* Error codes for the ECDSA functions. */
265
266/* Function codes. */
267#define ECDSA_F_ECDSA_CHECK 104
268#define ECDSA_F_ECDSA_DATA_NEW_METHOD 100
269#define ECDSA_F_ECDSA_DO_SIGN 101
270#define ECDSA_F_ECDSA_DO_VERIFY 102
271#define ECDSA_F_ECDSA_SIGN_SETUP 103
272
273/* Reason codes. */
274#define ECDSA_R_BAD_SIGNATURE 100
275#define ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 101
276#define ECDSA_R_ERR_EC_LIB 102
277#define ECDSA_R_MISSING_PARAMETERS 103
278#define ECDSA_R_NEED_NEW_SETUP_VALUES 106
279#define ECDSA_R_NON_FIPS_METHOD 107
280#define ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED 104
281#define ECDSA_R_SIGNATURE_MALLOC_FAILED 105
282
283#ifdef __cplusplus
284}
285#endif
286#endif
diff --git a/src/lib/libcrypto/ecdsa/ecs_asn1.c b/src/lib/libcrypto/ecdsa/ecs_asn1.c
deleted file mode 100644
index 725fe44a36..0000000000
--- a/src/lib/libcrypto/ecdsa/ecs_asn1.c
+++ /dev/null
@@ -1,115 +0,0 @@
1/* $OpenBSD: ecs_asn1.c,v 1.8 2015/10/16 15:15:39 jsing Exp $ */
2/* ====================================================================
3 * Copyright (c) 2000-2002 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * licensing@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56#include "ecs_locl.h"
57#include <openssl/err.h>
58#include <openssl/asn1t.h>
59
60static const ASN1_TEMPLATE ECDSA_SIG_seq_tt[] = {
61 {
62 .flags = 0,
63 .tag = 0,
64 .offset = offsetof(ECDSA_SIG, r),
65 .field_name = "r",
66 .item = &CBIGNUM_it,
67 },
68 {
69 .flags = 0,
70 .tag = 0,
71 .offset = offsetof(ECDSA_SIG, s),
72 .field_name = "s",
73 .item = &CBIGNUM_it,
74 },
75};
76
77const ASN1_ITEM ECDSA_SIG_it = {
78 .itype = ASN1_ITYPE_SEQUENCE,
79 .utype = V_ASN1_SEQUENCE,
80 .templates = ECDSA_SIG_seq_tt,
81 .tcount = sizeof(ECDSA_SIG_seq_tt) / sizeof(ASN1_TEMPLATE),
82 .funcs = NULL,
83 .size = sizeof(ECDSA_SIG),
84 .sname = "ECDSA_SIG",
85};
86
87ECDSA_SIG *ECDSA_SIG_new(void);
88void ECDSA_SIG_free(ECDSA_SIG *a);
89ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **a, const unsigned char **in, long len);
90int i2d_ECDSA_SIG(const ECDSA_SIG *a, unsigned char **out);
91
92ECDSA_SIG *
93d2i_ECDSA_SIG(ECDSA_SIG **a, const unsigned char **in, long len)
94{
95 return (ECDSA_SIG *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
96 &ECDSA_SIG_it);
97}
98
99int
100i2d_ECDSA_SIG(const ECDSA_SIG *a, unsigned char **out)
101{
102 return ASN1_item_i2d((ASN1_VALUE *)a, out, &ECDSA_SIG_it);
103}
104
105ECDSA_SIG *
106ECDSA_SIG_new(void)
107{
108 return (ECDSA_SIG *)ASN1_item_new(&ECDSA_SIG_it);
109}
110
111void
112ECDSA_SIG_free(ECDSA_SIG *a)
113{
114 ASN1_item_free((ASN1_VALUE *)a, &ECDSA_SIG_it);
115}
diff --git a/src/lib/libcrypto/ecdsa/ecs_err.c b/src/lib/libcrypto/ecdsa/ecs_err.c
deleted file mode 100644
index 26efc135e3..0000000000
--- a/src/lib/libcrypto/ecdsa/ecs_err.c
+++ /dev/null
@@ -1,106 +0,0 @@
1/* $OpenBSD: ecs_err.c,v 1.4 2015/02/08 13:35:07 jsing Exp $ */
2/* ====================================================================
3 * Copyright (c) 1999-2011 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56/* NOTE: this file was auto generated by the mkerr.pl script: any changes
57 * made to it will be overwritten when the script next updates this file,
58 * only reason strings will be preserved.
59 */
60
61#include <stdio.h>
62
63#include <openssl/opensslconf.h>
64
65#include <openssl/err.h>
66#include <openssl/ecdsa.h>
67
68/* BEGIN ERROR CODES */
69#ifndef OPENSSL_NO_ERR
70
71#define ERR_FUNC(func) ERR_PACK(ERR_LIB_ECDSA,func,0)
72#define ERR_REASON(reason) ERR_PACK(ERR_LIB_ECDSA,0,reason)
73
74static ERR_STRING_DATA ECDSA_str_functs[]= {
75 {ERR_FUNC(ECDSA_F_ECDSA_CHECK), "ECDSA_CHECK"},
76 {ERR_FUNC(ECDSA_F_ECDSA_DATA_NEW_METHOD), "ECDSA_DATA_NEW_METHOD"},
77 {ERR_FUNC(ECDSA_F_ECDSA_DO_SIGN), "ECDSA_do_sign"},
78 {ERR_FUNC(ECDSA_F_ECDSA_DO_VERIFY), "ECDSA_do_verify"},
79 {ERR_FUNC(ECDSA_F_ECDSA_SIGN_SETUP), "ECDSA_sign_setup"},
80 {0, NULL}
81};
82
83static ERR_STRING_DATA ECDSA_str_reasons[]= {
84 {ERR_REASON(ECDSA_R_BAD_SIGNATURE) , "bad signature"},
85 {ERR_REASON(ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE), "data too large for key size"},
86 {ERR_REASON(ECDSA_R_ERR_EC_LIB) , "err ec lib"},
87 {ERR_REASON(ECDSA_R_MISSING_PARAMETERS) , "missing parameters"},
88 {ERR_REASON(ECDSA_R_NEED_NEW_SETUP_VALUES), "need new setup values"},
89 {ERR_REASON(ECDSA_R_NON_FIPS_METHOD) , "non fips method"},
90 {ERR_REASON(ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED), "random number generation failed"},
91 {ERR_REASON(ECDSA_R_SIGNATURE_MALLOC_FAILED), "signature malloc failed"},
92 {0, NULL}
93};
94
95#endif
96
97void
98ERR_load_ECDSA_strings(void)
99{
100#ifndef OPENSSL_NO_ERR
101 if (ERR_func_error_string(ECDSA_str_functs[0].error) == NULL) {
102 ERR_load_strings(0, ECDSA_str_functs);
103 ERR_load_strings(0, ECDSA_str_reasons);
104 }
105#endif
106}
diff --git a/src/lib/libcrypto/ecdsa/ecs_lib.c b/src/lib/libcrypto/ecdsa/ecs_lib.c
deleted file mode 100644
index 1ba788b4f0..0000000000
--- a/src/lib/libcrypto/ecdsa/ecs_lib.c
+++ /dev/null
@@ -1,264 +0,0 @@
1/* $OpenBSD: ecs_lib.c,v 1.10 2015/09/10 15:56:25 jsing Exp $ */
2/* ====================================================================
3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56#include <string.h>
57
58#include <openssl/opensslconf.h>
59
60#include "ecs_locl.h"
61#ifndef OPENSSL_NO_ENGINE
62#include <openssl/engine.h>
63#endif
64#include <openssl/err.h>
65#include <openssl/bn.h>
66
67static const ECDSA_METHOD *default_ECDSA_method = NULL;
68
69static void *ecdsa_data_new(void);
70static void *ecdsa_data_dup(void *);
71static void ecdsa_data_free(void *);
72
73void
74ECDSA_set_default_method(const ECDSA_METHOD *meth)
75{
76 default_ECDSA_method = meth;
77}
78
79const ECDSA_METHOD *
80ECDSA_get_default_method(void)
81{
82 if (!default_ECDSA_method) {
83 default_ECDSA_method = ECDSA_OpenSSL();
84 }
85 return default_ECDSA_method;
86}
87
88int
89ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth)
90{
91 ECDSA_DATA *ecdsa;
92
93 ecdsa = ecdsa_check(eckey);
94
95 if (ecdsa == NULL)
96 return 0;
97
98#ifndef OPENSSL_NO_ENGINE
99 if (ecdsa->engine) {
100 ENGINE_finish(ecdsa->engine);
101 ecdsa->engine = NULL;
102 }
103#endif
104 ecdsa->meth = meth;
105
106 return 1;
107}
108
109static ECDSA_DATA *
110ECDSA_DATA_new_method(ENGINE *engine)
111{
112 ECDSA_DATA *ret;
113
114 ret = malloc(sizeof(ECDSA_DATA));
115 if (ret == NULL) {
116 ECDSAerr(ECDSA_F_ECDSA_DATA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
117 return (NULL);
118 }
119
120 ret->init = NULL;
121
122 ret->meth = ECDSA_get_default_method();
123 ret->engine = engine;
124#ifndef OPENSSL_NO_ENGINE
125 if (!ret->engine)
126 ret->engine = ENGINE_get_default_ECDSA();
127 if (ret->engine) {
128 ret->meth = ENGINE_get_ECDSA(ret->engine);
129 if (!ret->meth) {
130 ECDSAerr(ECDSA_F_ECDSA_DATA_NEW_METHOD,
131 ERR_R_ENGINE_LIB);
132 ENGINE_finish(ret->engine);
133 free(ret);
134 return NULL;
135 }
136 }
137#endif
138
139 ret->flags = ret->meth->flags;
140 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ECDSA, ret, &ret->ex_data);
141 return (ret);
142}
143
144static void *
145ecdsa_data_new(void)
146{
147 return (void *)ECDSA_DATA_new_method(NULL);
148}
149
150static void *
151ecdsa_data_dup(void *data)
152{
153 ECDSA_DATA *r = (ECDSA_DATA *)data;
154
155 /* XXX: dummy operation */
156 if (r == NULL)
157 return NULL;
158
159 return ecdsa_data_new();
160}
161
162static void
163ecdsa_data_free(void *data)
164{
165 ECDSA_DATA *r = (ECDSA_DATA *)data;
166
167#ifndef OPENSSL_NO_ENGINE
168 if (r->engine)
169 ENGINE_finish(r->engine);
170#endif
171 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDSA, r, &r->ex_data);
172
173 explicit_bzero((void *)r, sizeof(ECDSA_DATA));
174
175 free(r);
176}
177
178ECDSA_DATA *
179ecdsa_check(EC_KEY *key)
180{
181 ECDSA_DATA *ecdsa_data;
182
183 void *data = EC_KEY_get_key_method_data(key, ecdsa_data_dup,
184 ecdsa_data_free, ecdsa_data_free);
185 if (data == NULL) {
186 ecdsa_data = (ECDSA_DATA *)ecdsa_data_new();
187 if (ecdsa_data == NULL)
188 return NULL;
189 data = EC_KEY_insert_key_method_data(key, (void *)ecdsa_data,
190 ecdsa_data_dup, ecdsa_data_free, ecdsa_data_free);
191 if (data != NULL) {
192 /* Another thread raced us to install the key_method
193 * data and won. */
194 ecdsa_data_free(ecdsa_data);
195 ecdsa_data = (ECDSA_DATA *)data;
196 }
197 } else
198 ecdsa_data = (ECDSA_DATA *)data;
199
200 return ecdsa_data;
201}
202
203int
204ECDSA_size(const EC_KEY *r)
205{
206 int ret, i;
207 ASN1_INTEGER bs;
208 BIGNUM *order = NULL;
209 unsigned char buf[4];
210 const EC_GROUP *group;
211
212 if (r == NULL)
213 return 0;
214 group = EC_KEY_get0_group(r);
215 if (group == NULL)
216 return 0;
217
218 if ((order = BN_new()) == NULL)
219 return 0;
220 if (!EC_GROUP_get_order(group, order, NULL)) {
221 BN_clear_free(order);
222 return 0;
223 }
224 i = BN_num_bits(order);
225 bs.length = (i + 7) / 8;
226 bs.data = buf;
227 bs.type = V_ASN1_INTEGER;
228 /* If the top bit is set the asn1 encoding is 1 larger. */
229 buf[0] = 0xff;
230
231 i = i2d_ASN1_INTEGER(&bs, NULL);
232 i += i; /* r and s */
233 ret = ASN1_object_size(1, i, V_ASN1_SEQUENCE);
234 BN_clear_free(order);
235 return (ret);
236}
237
238int
239ECDSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
240 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
241{
242 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ECDSA, argl, argp,
243 new_func, dup_func, free_func);
244}
245
246int
247ECDSA_set_ex_data(EC_KEY *d, int idx, void *arg)
248{
249 ECDSA_DATA *ecdsa;
250 ecdsa = ecdsa_check(d);
251 if (ecdsa == NULL)
252 return 0;
253 return (CRYPTO_set_ex_data(&ecdsa->ex_data, idx, arg));
254}
255
256void *
257ECDSA_get_ex_data(EC_KEY *d, int idx)
258{
259 ECDSA_DATA *ecdsa;
260 ecdsa = ecdsa_check(d);
261 if (ecdsa == NULL)
262 return NULL;
263 return (CRYPTO_get_ex_data(&ecdsa->ex_data, idx));
264}
diff --git a/src/lib/libcrypto/ecdsa/ecs_locl.h b/src/lib/libcrypto/ecdsa/ecs_locl.h
deleted file mode 100644
index 24d866ae64..0000000000
--- a/src/lib/libcrypto/ecdsa/ecs_locl.h
+++ /dev/null
@@ -1,90 +0,0 @@
1/* $OpenBSD: ecs_locl.h,v 1.4 2015/02/08 13:35:07 jsing 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
59#ifndef HEADER_ECS_LOCL_H
60#define HEADER_ECS_LOCL_H
61
62#include <openssl/ecdsa.h>
63
64#ifdef __cplusplus
65extern "C" {
66#endif
67
68typedef struct ecdsa_data_st {
69 /* EC_KEY_METH_DATA part */
70 int (*init)(EC_KEY *);
71 /* method (ECDSA) specific part */
72 ENGINE *engine;
73 int flags;
74 const ECDSA_METHOD *meth;
75 CRYPTO_EX_DATA ex_data;
76} ECDSA_DATA;
77
78/** ecdsa_check
79 * checks whether ECKEY->meth_data is a pointer to a ECDSA_DATA structure
80 * and if not it removes the old meth_data and creates a ECDSA_DATA structure.
81 * \param eckey pointer to a EC_KEY object
82 * \return pointer to a ECDSA_DATA structure
83 */
84ECDSA_DATA *ecdsa_check(EC_KEY *eckey);
85
86#ifdef __cplusplus
87}
88#endif
89
90#endif /* HEADER_ECS_LOCL_H */
diff --git a/src/lib/libcrypto/ecdsa/ecs_ossl.c b/src/lib/libcrypto/ecdsa/ecs_ossl.c
deleted file mode 100644
index 31102138c0..0000000000
--- a/src/lib/libcrypto/ecdsa/ecs_ossl.c
+++ /dev/null
@@ -1,430 +0,0 @@
1/* $OpenBSD: ecs_ossl.c,v 1.6 2015/02/08 13:35:07 jsing Exp $ */
2/*
3 * Written by Nils Larsch for the OpenSSL project
4 */
5/* ====================================================================
6 * Copyright (c) 1998-2004 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 * openssl-core@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
59#include <openssl/opensslconf.h>
60
61#include "ecs_locl.h"
62#include <openssl/err.h>
63#include <openssl/obj_mac.h>
64#include <openssl/bn.h>
65
66static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen,
67 const BIGNUM *, const BIGNUM *, EC_KEY *eckey);
68static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
69 BIGNUM **rp);
70static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
71 const ECDSA_SIG *sig, EC_KEY *eckey);
72
73static ECDSA_METHOD openssl_ecdsa_meth = {
74 .name = "OpenSSL ECDSA method",
75 .ecdsa_do_sign = ecdsa_do_sign,
76 .ecdsa_sign_setup = ecdsa_sign_setup,
77 .ecdsa_do_verify = ecdsa_do_verify
78};
79
80const ECDSA_METHOD *
81ECDSA_OpenSSL(void)
82{
83 return &openssl_ecdsa_meth;
84}
85
86static int
87ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
88{
89 BN_CTX *ctx = NULL;
90 BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
91 EC_POINT *tmp_point = NULL;
92 const EC_GROUP *group;
93 int ret = 0;
94
95 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
96 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
97 return 0;
98 }
99
100 if (ctx_in == NULL) {
101 if ((ctx = BN_CTX_new()) == NULL) {
102 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
103 ERR_R_MALLOC_FAILURE);
104 return 0;
105 }
106 } else
107 ctx = ctx_in;
108
109 k = BN_new(); /* this value is later returned in *kinvp */
110 r = BN_new(); /* this value is later returned in *rp */
111 order = BN_new();
112 X = BN_new();
113 if (!k || !r || !order || !X) {
114 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
115 goto err;
116 }
117 if ((tmp_point = EC_POINT_new(group)) == NULL) {
118 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
119 goto err;
120 }
121 if (!EC_GROUP_get_order(group, order, ctx)) {
122 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
123 goto err;
124 }
125
126 do {
127 /* get random k */
128 do
129 if (!BN_rand_range(k, order)) {
130 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
131 ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
132 goto err;
133 }
134 while (BN_is_zero(k));
135
136 /* We do not want timing information to leak the length of k,
137 * so we compute G*k using an equivalent scalar of fixed
138 * bit-length. */
139 if (!BN_add(k, k, order))
140 goto err;
141 if (BN_num_bits(k) <= BN_num_bits(order))
142 if (!BN_add(k, k, order))
143 goto err;
144
145 /* compute r the x-coordinate of generator * k */
146 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
147 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
148 goto err;
149 }
150 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
151 NID_X9_62_prime_field) {
152 if (!EC_POINT_get_affine_coordinates_GFp(group,
153 tmp_point, X, NULL, ctx)) {
154 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
155 ERR_R_EC_LIB);
156 goto err;
157 }
158 }
159#ifndef OPENSSL_NO_EC2M
160 else /* NID_X9_62_characteristic_two_field */
161 {
162 if (!EC_POINT_get_affine_coordinates_GF2m(group,
163 tmp_point, X, NULL, ctx)) {
164 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
165 ERR_R_EC_LIB);
166 goto err;
167 }
168 }
169#endif
170 if (!BN_nnmod(r, X, order, ctx)) {
171 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
172 goto err;
173 }
174 } while (BN_is_zero(r));
175
176 /* compute the inverse of k */
177 if (!BN_mod_inverse(k, k, order, ctx)) {
178 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
179 goto err;
180 }
181 /* clear old values if necessary */
182 BN_clear_free(*rp);
183 BN_clear_free(*kinvp);
184 /* save the pre-computed values */
185 *rp = r;
186 *kinvp = k;
187 ret = 1;
188
189err:
190 if (!ret) {
191 BN_clear_free(k);
192 BN_clear_free(r);
193 }
194 if (ctx_in == NULL)
195 BN_CTX_free(ctx);
196 BN_free(order);
197 EC_POINT_free(tmp_point);
198 BN_clear_free(X);
199 return (ret);
200}
201
202
203static ECDSA_SIG *
204ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
205 const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey)
206{
207 int ok = 0, i;
208 BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
209 const BIGNUM *ckinv;
210 BN_CTX *ctx = NULL;
211 const EC_GROUP *group;
212 ECDSA_SIG *ret;
213 ECDSA_DATA *ecdsa;
214 const BIGNUM *priv_key;
215
216 ecdsa = ecdsa_check(eckey);
217 group = EC_KEY_get0_group(eckey);
218 priv_key = EC_KEY_get0_private_key(eckey);
219
220 if (group == NULL || priv_key == NULL || ecdsa == NULL) {
221 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
222 return NULL;
223 }
224
225 ret = ECDSA_SIG_new();
226 if (!ret) {
227 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
228 return NULL;
229 }
230 s = ret->s;
231
232 if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
233 (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
234 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
235 goto err;
236 }
237
238 if (!EC_GROUP_get_order(group, order, ctx)) {
239 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
240 goto err;
241 }
242 i = BN_num_bits(order);
243 /* Need to truncate digest if it is too long: first truncate whole
244 * bytes.
245 */
246 if (8 * dgst_len > i)
247 dgst_len = (i + 7)/8;
248 if (!BN_bin2bn(dgst, dgst_len, m)) {
249 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
250 goto err;
251 }
252 /* If still too long truncate remaining bits with a shift */
253 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
254 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
255 goto err;
256 }
257 do {
258 if (in_kinv == NULL || in_r == NULL) {
259 if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r)) {
260 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
261 ERR_R_ECDSA_LIB);
262 goto err;
263 }
264 ckinv = kinv;
265 } else {
266 ckinv = in_kinv;
267 if (BN_copy(ret->r, in_r) == NULL) {
268 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
269 ERR_R_MALLOC_FAILURE);
270 goto err;
271 }
272 }
273
274 if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
275 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
276 goto err;
277 }
278 if (!BN_mod_add_quick(s, tmp, m, order)) {
279 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
280 goto err;
281 }
282 if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
283 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
284 goto err;
285 }
286 if (BN_is_zero(s)) {
287 /* if kinv and r have been supplied by the caller
288 * don't to generate new kinv and r values */
289 if (in_kinv != NULL && in_r != NULL) {
290 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
291 ECDSA_R_NEED_NEW_SETUP_VALUES);
292 goto err;
293 }
294 } else
295 /* s != 0 => we have a valid signature */
296 break;
297 } while (1);
298
299 ok = 1;
300
301err:
302 if (!ok) {
303 ECDSA_SIG_free(ret);
304 ret = NULL;
305 }
306 BN_CTX_free(ctx);
307 BN_clear_free(m);
308 BN_clear_free(tmp);
309 BN_free(order);
310 BN_clear_free(kinv);
311 return ret;
312}
313
314static int
315ecdsa_do_verify(const unsigned char *dgst, int dgst_len, const ECDSA_SIG *sig,
316 EC_KEY *eckey)
317{
318 int ret = -1, i;
319 BN_CTX *ctx;
320 BIGNUM *order, *u1, *u2, *m, *X;
321 EC_POINT *point = NULL;
322 const EC_GROUP *group;
323 const EC_POINT *pub_key;
324
325 /* check input values */
326 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
327 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
328 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
329 return -1;
330 }
331
332 ctx = BN_CTX_new();
333 if (!ctx) {
334 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
335 return -1;
336 }
337 BN_CTX_start(ctx);
338 order = BN_CTX_get(ctx);
339 u1 = BN_CTX_get(ctx);
340 u2 = BN_CTX_get(ctx);
341 m = BN_CTX_get(ctx);
342 X = BN_CTX_get(ctx);
343 if (!X) {
344 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
345 goto err;
346 }
347
348 if (!EC_GROUP_get_order(group, order, ctx)) {
349 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
350 goto err;
351 }
352
353 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
354 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
355 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
356 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
357 ret = 0; /* signature is invalid */
358 goto err;
359 }
360 /* calculate tmp1 = inv(S) mod order */
361 if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
362 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
363 goto err;
364 }
365 /* digest -> m */
366 i = BN_num_bits(order);
367 /* Need to truncate digest if it is too long: first truncate whole
368 * bytes.
369 */
370 if (8 * dgst_len > i)
371 dgst_len = (i + 7)/8;
372 if (!BN_bin2bn(dgst, dgst_len, m)) {
373 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
374 goto err;
375 }
376 /* If still too long truncate remaining bits with a shift */
377 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
378 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
379 goto err;
380 }
381 /* u1 = m * tmp mod order */
382 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
383 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
384 goto err;
385 }
386 /* u2 = r * w mod q */
387 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
388 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
389 goto err;
390 }
391
392 if ((point = EC_POINT_new(group)) == NULL) {
393 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
394 goto err;
395 }
396 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
397 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
398 goto err;
399 }
400 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
401 NID_X9_62_prime_field) {
402 if (!EC_POINT_get_affine_coordinates_GFp(group,
403 point, X, NULL, ctx)) {
404 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
405 goto err;
406 }
407 }
408#ifndef OPENSSL_NO_EC2M
409 else /* NID_X9_62_characteristic_two_field */
410 {
411 if (!EC_POINT_get_affine_coordinates_GF2m(group,
412 point, X, NULL, ctx)) {
413 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
414 goto err;
415 }
416 }
417#endif
418 if (!BN_nnmod(u1, X, order, ctx)) {
419 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
420 goto err;
421 }
422 /* if the signature is correct u1 is equal to sig->r */
423 ret = (BN_ucmp(u1, sig->r) == 0);
424
425err:
426 BN_CTX_end(ctx);
427 BN_CTX_free(ctx);
428 EC_POINT_free(point);
429 return ret;
430}
diff --git a/src/lib/libcrypto/ecdsa/ecs_sign.c b/src/lib/libcrypto/ecdsa/ecs_sign.c
deleted file mode 100644
index 029a0cb562..0000000000
--- a/src/lib/libcrypto/ecdsa/ecs_sign.c
+++ /dev/null
@@ -1,111 +0,0 @@
1/* $OpenBSD: ecs_sign.c,v 1.6 2015/02/08 13:35:07 jsing Exp $ */
2/* ====================================================================
3 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56#include <openssl/opensslconf.h>
57
58#include "ecs_locl.h"
59#ifndef OPENSSL_NO_ENGINE
60#include <openssl/engine.h>
61#endif
62
63ECDSA_SIG *
64ECDSA_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
65{
66 return ECDSA_do_sign_ex(dgst, dlen, NULL, NULL, eckey);
67}
68
69ECDSA_SIG *
70ECDSA_do_sign_ex(const unsigned char *dgst, int dlen, const BIGNUM *kinv,
71 const BIGNUM *rp, EC_KEY *eckey)
72{
73 ECDSA_DATA *ecdsa = ecdsa_check(eckey);
74
75 if (ecdsa == NULL)
76 return NULL;
77 return ecdsa->meth->ecdsa_do_sign(dgst, dlen, kinv, rp, eckey);
78}
79
80int
81ECDSA_sign(int type, const unsigned char *dgst, int dlen, unsigned char *sig,
82 unsigned int *siglen, EC_KEY *eckey)
83{
84 return ECDSA_sign_ex(type, dgst, dlen, sig, siglen, NULL, NULL, eckey);
85}
86
87int
88ECDSA_sign_ex(int type, const unsigned char *dgst, int dlen, unsigned char *sig,
89 unsigned int *siglen, const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
90{
91 ECDSA_SIG *s;
92
93 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
94 if (s == NULL) {
95 *siglen = 0;
96 return 0;
97 }
98 *siglen = i2d_ECDSA_SIG(s, &sig);
99 ECDSA_SIG_free(s);
100 return 1;
101}
102
103int
104ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
105{
106 ECDSA_DATA *ecdsa = ecdsa_check(eckey);
107
108 if (ecdsa == NULL)
109 return 0;
110 return ecdsa->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
111}
diff --git a/src/lib/libcrypto/ecdsa/ecs_vrf.c b/src/lib/libcrypto/ecdsa/ecs_vrf.c
deleted file mode 100644
index a4b627ace6..0000000000
--- a/src/lib/libcrypto/ecdsa/ecs_vrf.c
+++ /dev/null
@@ -1,116 +0,0 @@
1/* $OpenBSD: ecs_vrf.c,v 1.5 2015/02/08 13:35:07 jsing Exp $ */
2/*
3 * Written by Nils Larsch for the OpenSSL project
4 */
5/* ====================================================================
6 * Copyright (c) 1998-2002 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 * openssl-core@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
59#include <string.h>
60#include <openssl/opensslconf.h>
61
62#include "ecs_locl.h"
63#ifndef OPENSSL_NO_ENGINE
64#include <openssl/engine.h>
65#endif
66
67/* returns
68 * 1: correct signature
69 * 0: incorrect signature
70 * -1: error
71 */
72int
73ECDSA_do_verify(const unsigned char *dgst, int dgst_len, const ECDSA_SIG *sig,
74 EC_KEY *eckey)
75{
76 ECDSA_DATA *ecdsa = ecdsa_check(eckey);
77
78 if (ecdsa == NULL)
79 return 0;
80 return ecdsa->meth->ecdsa_do_verify(dgst, dgst_len, sig, eckey);
81}
82
83/* returns
84 * 1: correct signature
85 * 0: incorrect signature
86 * -1: error
87 */
88int
89ECDSA_verify(int type, const unsigned char *dgst, int dgst_len,
90 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
91{
92 ECDSA_SIG *s;
93 unsigned char *der = NULL;
94 const unsigned char *p = sigbuf;
95 int derlen = -1;
96 int ret = -1;
97
98 s = ECDSA_SIG_new();
99 if (s == NULL)
100 return (ret);
101 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
102 goto err;
103 /* Ensure signature uses DER and doesn't have trailing garbage */
104 derlen = i2d_ECDSA_SIG(s, &der);
105 if (derlen != sig_len || memcmp(sigbuf, der, derlen))
106 goto err;
107 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
108
109err:
110 if (derlen > 0) {
111 explicit_bzero(der, derlen);
112 free(der);
113 }
114 ECDSA_SIG_free(s);
115 return (ret);
116}