summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/ecdsa
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/ecdsa')
-rw-r--r--src/lib/libcrypto/ecdsa/ecdsa.h193
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_err.c2
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_ossl.c54
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_sign.c2
4 files changed, 117 insertions, 134 deletions
diff --git a/src/lib/libcrypto/ecdsa/ecdsa.h b/src/lib/libcrypto/ecdsa/ecdsa.h
index f20c8ee738..e61c539812 100644
--- a/src/lib/libcrypto/ecdsa/ecdsa.h
+++ b/src/lib/libcrypto/ecdsa/ecdsa.h
@@ -4,7 +4,7 @@
4 * \author Written by Nils Larsch for the OpenSSL project 4 * \author Written by Nils Larsch for the OpenSSL project
5 */ 5 */
6/* ==================================================================== 6/* ====================================================================
7 * Copyright (c) 2000-2003 The OpenSSL Project. All rights reserved. 7 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
@@ -81,156 +81,143 @@ typedef struct ECDSA_SIG_st
81 BIGNUM *s; 81 BIGNUM *s;
82 } ECDSA_SIG; 82 } ECDSA_SIG;
83 83
84/** ECDSA_SIG *ECDSA_SIG_new(void) 84/** Allocates and initialize a ECDSA_SIG structure
85 * allocates and initialize a ECDSA_SIG structure 85 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
86 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
87 */ 86 */
88ECDSA_SIG *ECDSA_SIG_new(void); 87ECDSA_SIG *ECDSA_SIG_new(void);
89 88
90/** ECDSA_SIG_free 89/** frees a ECDSA_SIG structure
91 * frees a ECDSA_SIG structure 90 * \param sig pointer to the ECDSA_SIG structure
92 * \param a pointer to the ECDSA_SIG structure
93 */ 91 */
94void ECDSA_SIG_free(ECDSA_SIG *a); 92void ECDSA_SIG_free(ECDSA_SIG *sig);
95 93
96/** i2d_ECDSA_SIG 94/** DER encode content of ECDSA_SIG object (note: this function modifies *pp
97 * DER encode content of ECDSA_SIG object (note: this function modifies *pp 95 * (*pp += length of the DER encoded signature)).
98 * (*pp += length of the DER encoded signature)). 96 * \param sig pointer to the ECDSA_SIG object
99 * \param a pointer to the ECDSA_SIG object 97 * \param pp pointer to a unsigned char pointer for the output or NULL
100 * \param pp pointer to a unsigned char pointer for the output or NULL 98 * \return the length of the DER encoded ECDSA_SIG object or 0
101 * \return the length of the DER encoded ECDSA_SIG object or 0
102 */ 99 */
103int i2d_ECDSA_SIG(const ECDSA_SIG *a, unsigned char **pp); 100int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp);
104 101
105/** d2i_ECDSA_SIG 102/** Decodes a DER encoded ECDSA signature (note: this function changes *pp
106 * decodes a DER encoded ECDSA signature (note: this function changes *pp 103 * (*pp += len)).
107 * (*pp += len)). 104 * \param sig pointer to ECDSA_SIG pointer (may be NULL)
108 * \param v pointer to ECDSA_SIG pointer (may be NULL) 105 * \param pp memory buffer with the DER encoded signature
109 * \param pp buffer with the DER encoded signature 106 * \param len length of the buffer
110 * \param len bufferlength 107 * \return pointer to the decoded ECDSA_SIG structure (or NULL)
111 * \return pointer to the decoded ECDSA_SIG structure (or NULL)
112 */ 108 */
113ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **v, const unsigned char **pp, long len); 109ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len);
114 110
115/** ECDSA_do_sign 111/** Computes the ECDSA signature of the given hash value using
116 * computes the ECDSA signature of the given hash value using 112 * the supplied private key and returns the created signature.
117 * the supplied private key and returns the created signature. 113 * \param dgst pointer to the hash value
118 * \param dgst pointer to the hash value 114 * \param dgst_len length of the hash value
119 * \param dgst_len length of the hash value 115 * \param eckey EC_KEY object containing a private EC key
120 * \param eckey pointer to the EC_KEY object containing a private EC key 116 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
121 * \return pointer to a ECDSA_SIG structure or NULL
122 */ 117 */
123ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst,int dgst_len,EC_KEY *eckey); 118ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst,int dgst_len,EC_KEY *eckey);
124 119
125/** ECDSA_do_sign_ex 120/** Computes ECDSA signature of a given hash value using the supplied
126 * computes ECDSA signature of a given hash value using the supplied 121 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
127 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 122 * \param dgst pointer to the hash value to sign
128 * \param dgst pointer to the hash value to sign 123 * \param dgstlen length of the hash value
129 * \param dgstlen length of the hash value 124 * \param kinv BIGNUM with a pre-computed inverse k (optional)
130 * \param kinv optional pointer to a pre-computed inverse k 125 * \param rp BIGNUM with a pre-computed rp value (optioanl),
131 * \param rp optional pointer to the pre-computed rp value (see 126 * see ECDSA_sign_setup
132 * ECDSA_sign_setup 127 * \param eckey EC_KEY object containing a private EC key
133 * \param eckey pointer to the EC_KEY object containing a private EC key 128 * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
134 * \return pointer to a ECDSA_SIG structure or NULL
135 */ 129 */
136ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen, 130ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
137 const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey); 131 const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey);
138 132
139/** ECDSA_do_verify 133/** Verifies that the supplied signature is a valid ECDSA
140 * verifies that the supplied signature is a valid ECDSA 134 * signature of the supplied hash value using the supplied public key.
141 * signature of the supplied hash value using the supplied public key. 135 * \param dgst pointer to the hash value
142 * \param dgst pointer to the hash value 136 * \param dgst_len length of the hash value
143 * \param dgst_len length of the hash value 137 * \param sig ECDSA_SIG structure
144 * \param sig pointer to the ECDSA_SIG structure 138 * \param eckey EC_KEY object containing a public EC key
145 * \param eckey pointer to the EC_KEY object containing a public EC key 139 * \return 1 if the signature is valid, 0 if the signature is invalid
146 * \return 1 if the signature is valid, 0 if the signature is invalid and -1 on error 140 * and -1 on error
147 */ 141 */
148int ECDSA_do_verify(const unsigned char *dgst, int dgst_len, 142int ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
149 const ECDSA_SIG *sig, EC_KEY* eckey); 143 const ECDSA_SIG *sig, EC_KEY* eckey);
150 144
151const ECDSA_METHOD *ECDSA_OpenSSL(void); 145const ECDSA_METHOD *ECDSA_OpenSSL(void);
152 146
153/** ECDSA_set_default_method 147/** Sets the default ECDSA method
154 * sets the default ECDSA method 148 * \param meth new default ECDSA_METHOD
155 * \param meth the new default ECDSA_METHOD
156 */ 149 */
157void ECDSA_set_default_method(const ECDSA_METHOD *meth); 150void ECDSA_set_default_method(const ECDSA_METHOD *meth);
158 151
159/** ECDSA_get_default_method 152/** Returns the default ECDSA method
160 * returns the default ECDSA method 153 * \return pointer to ECDSA_METHOD structure containing the default method
161 * \return pointer to ECDSA_METHOD structure containing the default method
162 */ 154 */
163const ECDSA_METHOD *ECDSA_get_default_method(void); 155const ECDSA_METHOD *ECDSA_get_default_method(void);
164 156
165/** ECDSA_set_method 157/** Sets method to be used for the ECDSA operations
166 * sets method to be used for the ECDSA operations 158 * \param eckey EC_KEY object
167 * \param eckey pointer to the EC_KEY object 159 * \param meth new method
168 * \param meth pointer to the new method 160 * \return 1 on success and 0 otherwise
169 * \return 1 on success and 0 otherwise
170 */ 161 */
171int ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth); 162int ECDSA_set_method(EC_KEY *eckey, const ECDSA_METHOD *meth);
172 163
173/** ECDSA_size 164/** Returns the maximum length of the DER encoded signature
174 * returns the maximum length of the DER encoded signature 165 * \param eckey EC_KEY object
175 * \param eckey pointer to a EC_KEY object 166 * \return numbers of bytes required for the DER encoded signature
176 * \return numbers of bytes required for the DER encoded signature
177 */ 167 */
178int ECDSA_size(const EC_KEY *eckey); 168int ECDSA_size(const EC_KEY *eckey);
179 169
180/** ECDSA_sign_setup 170/** Precompute parts of the signing operation
181 * precompute parts of the signing operation. 171 * \param eckey EC_KEY object containing a private EC key
182 * \param eckey pointer to the EC_KEY object containing a private EC key 172 * \param ctx BN_CTX object (optional)
183 * \param ctx pointer to a BN_CTX object (may be NULL) 173 * \param kinv BIGNUM pointer for the inverse of k
184 * \param kinv pointer to a BIGNUM pointer for the inverse of k 174 * \param rp BIGNUM pointer for x coordinate of k * generator
185 * \param rp pointer to a BIGNUM pointer for x coordinate of k * generator 175 * \return 1 on success and 0 otherwise
186 * \return 1 on success and 0 otherwise
187 */ 176 */
188int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, 177int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv,
189 BIGNUM **rp); 178 BIGNUM **rp);
190 179
191/** ECDSA_sign 180/** Computes ECDSA signature of a given hash value using the supplied
192 * computes ECDSA signature of a given hash value using the supplied 181 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
193 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 182 * \param type this parameter is ignored
194 * \param type this parameter is ignored 183 * \param dgst pointer to the hash value to sign
195 * \param dgst pointer to the hash value to sign 184 * \param dgstlen length of the hash value
196 * \param dgstlen length of the hash value 185 * \param sig memory for the DER encoded created signature
197 * \param sig buffer to hold the DER encoded signature 186 * \param siglen pointer to the length of the returned signature
198 * \param siglen pointer to the length of the returned signature 187 * \param eckey EC_KEY object containing a private EC key
199 * \param eckey pointer to the EC_KEY object containing a private EC key 188 * \return 1 on success and 0 otherwise
200 * \return 1 on success and 0 otherwise
201 */ 189 */
202int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen, 190int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen,
203 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey); 191 unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
204 192
205 193
206/** ECDSA_sign_ex 194/** Computes ECDSA signature of a given hash value using the supplied
207 * computes ECDSA signature of a given hash value using the supplied 195 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
208 * private key (note: sig must point to ECDSA_size(eckey) bytes of memory). 196 * \param type this parameter is ignored
209 * \param type this parameter is ignored 197 * \param dgst pointer to the hash value to sign
210 * \param dgst pointer to the hash value to sign 198 * \param dgstlen length of the hash value
211 * \param dgstlen length of the hash value 199 * \param sig buffer to hold the DER encoded signature
212 * \param sig buffer to hold the DER encoded signature 200 * \param siglen pointer to the length of the returned signature
213 * \param siglen pointer to the length of the returned signature 201 * \param kinv BIGNUM with a pre-computed inverse k (optional)
214 * \param kinv optional pointer to a pre-computed inverse k 202 * \param rp BIGNUM with a pre-computed rp value (optioanl),
215 * \param rp optional pointer to the pre-computed rp value (see 203 * see ECDSA_sign_setup
216 * ECDSA_sign_setup 204 * \param eckey EC_KEY object containing a private EC key
217 * \param eckey pointer to the EC_KEY object containing a private EC key 205 * \return 1 on success and 0 otherwise
218 * \return 1 on success and 0 otherwise
219 */ 206 */
220int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen, 207int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen,
221 unsigned char *sig, unsigned int *siglen, const BIGNUM *kinv, 208 unsigned char *sig, unsigned int *siglen, const BIGNUM *kinv,
222 const BIGNUM *rp, EC_KEY *eckey); 209 const BIGNUM *rp, EC_KEY *eckey);
223 210
224/** ECDSA_verify 211/** Verifies that the given signature is valid ECDSA signature
225 * verifies that the given signature is valid ECDSA signature 212 * of the supplied hash value using the specified public key.
226 * of the supplied hash value using the specified public key. 213 * \param type this parameter is ignored
227 * \param type this parameter is ignored 214 * \param dgst pointer to the hash value
228 * \param dgst pointer to the hash value 215 * \param dgstlen length of the hash value
229 * \param dgstlen length of the hash value 216 * \param sig pointer to the DER encoded signature
230 * \param sig pointer to the DER encoded signature 217 * \param siglen length of the DER encoded signature
231 * \param siglen length of the DER encoded signature 218 * \param eckey EC_KEY object containing a public EC key
232 * \param eckey pointer to the EC_KEY object containing a public EC key 219 * \return 1 if the signature is valid, 0 if the signature is invalid
233 * \return 1 if the signature is valid, 0 if the signature is invalid and -1 on error 220 * and -1 on error
234 */ 221 */
235int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen, 222int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen,
236 const unsigned char *sig, int siglen, EC_KEY *eckey); 223 const unsigned char *sig, int siglen, EC_KEY *eckey);
diff --git a/src/lib/libcrypto/ecdsa/ecs_err.c b/src/lib/libcrypto/ecdsa/ecs_err.c
index d2a53730ea..98e38d537f 100644
--- a/src/lib/libcrypto/ecdsa/ecs_err.c
+++ b/src/lib/libcrypto/ecdsa/ecs_err.c
@@ -1,6 +1,6 @@
1/* crypto/ecdsa/ecs_err.c */ 1/* crypto/ecdsa/ecs_err.c */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999-2006 The OpenSSL Project. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
diff --git a/src/lib/libcrypto/ecdsa/ecs_ossl.c b/src/lib/libcrypto/ecdsa/ecs_ossl.c
index 3ead1af94e..551cf5068f 100644
--- a/src/lib/libcrypto/ecdsa/ecs_ossl.c
+++ b/src/lib/libcrypto/ecdsa/ecs_ossl.c
@@ -212,7 +212,7 @@ err:
212static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len, 212static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
213 const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey) 213 const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey)
214{ 214{
215 int ok = 0; 215 int ok = 0, i;
216 BIGNUM *kinv=NULL, *s, *m=NULL,*tmp=NULL,*order=NULL; 216 BIGNUM *kinv=NULL, *s, *m=NULL,*tmp=NULL,*order=NULL;
217 const BIGNUM *ckinv; 217 const BIGNUM *ckinv;
218 BN_CTX *ctx = NULL; 218 BN_CTX *ctx = NULL;
@@ -251,22 +251,19 @@ static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
251 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB); 251 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
252 goto err; 252 goto err;
253 } 253 }
254 if (8 * dgst_len > BN_num_bits(order)) 254 i = BN_num_bits(order);
255 /* Need to truncate digest if it is too long: first truncate whole
256 * bytes.
257 */
258 if (8 * dgst_len > i)
259 dgst_len = (i + 7)/8;
260 if (!BN_bin2bn(dgst, dgst_len, m))
255 { 261 {
256 /* XXX 262 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
257 *
258 * Should provide for optional hash truncation:
259 * Keep the BN_num_bits(order) leftmost bits of dgst
260 * (see March 2006 FIPS 186-3 draft, which has a few
261 * confusing errors in this part though)
262 */
263
264 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
265 ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
266 goto err; 263 goto err;
267 } 264 }
268 265 /* If still too long truncate remaining bits with a shift */
269 if (!BN_bin2bn(dgst, dgst_len, m)) 266 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7)))
270 { 267 {
271 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB); 268 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
272 goto err; 269 goto err;
@@ -346,7 +343,7 @@ err:
346static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len, 343static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
347 const ECDSA_SIG *sig, EC_KEY *eckey) 344 const ECDSA_SIG *sig, EC_KEY *eckey)
348{ 345{
349 int ret = -1; 346 int ret = -1, i;
350 BN_CTX *ctx; 347 BN_CTX *ctx;
351 BIGNUM *order, *u1, *u2, *m, *X; 348 BIGNUM *order, *u1, *u2, *m, *X;
352 EC_POINT *point = NULL; 349 EC_POINT *point = NULL;
@@ -384,21 +381,6 @@ static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
384 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB); 381 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
385 goto err; 382 goto err;
386 } 383 }
387 if (8 * dgst_len > BN_num_bits(order))
388 {
389 /* XXX
390 *
391 * Should provide for optional hash truncation:
392 * Keep the BN_num_bits(order) leftmost bits of dgst
393 * (see March 2006 FIPS 186-3 draft, which has a few
394 * confusing errors in this part though)
395 */
396
397 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY,
398 ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
399 ret = 0;
400 goto err;
401 }
402 384
403 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || 385 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
404 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) || 386 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
@@ -415,11 +397,23 @@ static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
415 goto err; 397 goto err;
416 } 398 }
417 /* digest -> m */ 399 /* digest -> m */
400 i = BN_num_bits(order);
401 /* Need to truncate digest if it is too long: first truncate whole
402 * bytes.
403 */
404 if (8 * dgst_len > i)
405 dgst_len = (i + 7)/8;
418 if (!BN_bin2bn(dgst, dgst_len, m)) 406 if (!BN_bin2bn(dgst, dgst_len, m))
419 { 407 {
420 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB); 408 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
421 goto err; 409 goto err;
422 } 410 }
411 /* If still too long truncate remaining bits with a shift */
412 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7)))
413 {
414 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
415 goto err;
416 }
423 /* u1 = m * tmp mod order */ 417 /* u1 = m * tmp mod order */
424 if (!BN_mod_mul(u1, m, u2, order, ctx)) 418 if (!BN_mod_mul(u1, m, u2, order, ctx))
425 { 419 {
diff --git a/src/lib/libcrypto/ecdsa/ecs_sign.c b/src/lib/libcrypto/ecdsa/ecs_sign.c
index 74b1fe8caf..353d5af514 100644
--- a/src/lib/libcrypto/ecdsa/ecs_sign.c
+++ b/src/lib/libcrypto/ecdsa/ecs_sign.c
@@ -57,6 +57,7 @@
57#ifndef OPENSSL_NO_ENGINE 57#ifndef OPENSSL_NO_ENGINE
58#include <openssl/engine.h> 58#include <openssl/engine.h>
59#endif 59#endif
60#include <openssl/rand.h>
60 61
61ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey) 62ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey)
62{ 63{
@@ -83,6 +84,7 @@ int ECDSA_sign_ex(int type, const unsigned char *dgst, int dlen, unsigned char
83 EC_KEY *eckey) 84 EC_KEY *eckey)
84{ 85{
85 ECDSA_SIG *s; 86 ECDSA_SIG *s;
87 RAND_seed(dgst, dlen);
86 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey); 88 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
87 if (s == NULL) 89 if (s == NULL)
88 { 90 {