1*2139Sjp161948 /* crypto/ecdsa/ecdsatest.c */
2*2139Sjp161948 /*
3*2139Sjp161948 * Written by Nils Larsch for the OpenSSL project.
4*2139Sjp161948 */
5*2139Sjp161948 /* ====================================================================
6*2139Sjp161948 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
7*2139Sjp161948 *
8*2139Sjp161948 * Redistribution and use in source and binary forms, with or without
9*2139Sjp161948 * modification, are permitted provided that the following conditions
10*2139Sjp161948 * are met:
11*2139Sjp161948 *
12*2139Sjp161948 * 1. Redistributions of source code must retain the above copyright
13*2139Sjp161948 * notice, this list of conditions and the following disclaimer.
14*2139Sjp161948 *
15*2139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright
16*2139Sjp161948 * notice, this list of conditions and the following disclaimer in
17*2139Sjp161948 * the documentation and/or other materials provided with the
18*2139Sjp161948 * distribution.
19*2139Sjp161948 *
20*2139Sjp161948 * 3. All advertising materials mentioning features or use of this
21*2139Sjp161948 * software must display the following acknowledgment:
22*2139Sjp161948 * "This product includes software developed by the OpenSSL Project
23*2139Sjp161948 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*2139Sjp161948 *
25*2139Sjp161948 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*2139Sjp161948 * endorse or promote products derived from this software without
27*2139Sjp161948 * prior written permission. For written permission, please contact
28*2139Sjp161948 * licensing@OpenSSL.org.
29*2139Sjp161948 *
30*2139Sjp161948 * 5. Products derived from this software may not be called "OpenSSL"
31*2139Sjp161948 * nor may "OpenSSL" appear in their names without prior written
32*2139Sjp161948 * permission of the OpenSSL Project.
33*2139Sjp161948 *
34*2139Sjp161948 * 6. Redistributions of any form whatsoever must retain the following
35*2139Sjp161948 * acknowledgment:
36*2139Sjp161948 * "This product includes software developed by the OpenSSL Project
37*2139Sjp161948 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*2139Sjp161948 *
39*2139Sjp161948 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*2139Sjp161948 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*2139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*2139Sjp161948 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*2139Sjp161948 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*2139Sjp161948 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*2139Sjp161948 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*2139Sjp161948 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*2139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*2139Sjp161948 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*2139Sjp161948 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*2139Sjp161948 * OF THE POSSIBILITY OF SUCH DAMAGE.
51*2139Sjp161948 * ====================================================================
52*2139Sjp161948 *
53*2139Sjp161948 * This product includes cryptographic software written by Eric Young
54*2139Sjp161948 * (eay@cryptsoft.com). This product includes software written by Tim
55*2139Sjp161948 * Hudson (tjh@cryptsoft.com).
56*2139Sjp161948 *
57*2139Sjp161948 */
58*2139Sjp161948 /* ====================================================================
59*2139Sjp161948 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60*2139Sjp161948 *
61*2139Sjp161948 * Portions of the attached software ("Contribution") are developed by
62*2139Sjp161948 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63*2139Sjp161948 *
64*2139Sjp161948 * The Contribution is licensed pursuant to the OpenSSL open source
65*2139Sjp161948 * license provided above.
66*2139Sjp161948 *
67*2139Sjp161948 * The elliptic curve binary polynomial software is originally written by
68*2139Sjp161948 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69*2139Sjp161948 *
70*2139Sjp161948 */
71*2139Sjp161948
72*2139Sjp161948 #include <stdio.h>
73*2139Sjp161948 #include <stdlib.h>
74*2139Sjp161948 #include <string.h>
75*2139Sjp161948
76*2139Sjp161948 #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_ECDSA is defined */
77*2139Sjp161948
78*2139Sjp161948 #ifdef OPENSSL_NO_ECDSA
main(int argc,char * argv[])79*2139Sjp161948 int main(int argc, char * argv[])
80*2139Sjp161948 {
81*2139Sjp161948 puts("Elliptic curves are disabled.");
82*2139Sjp161948 return 0;
83*2139Sjp161948 }
84*2139Sjp161948 #else
85*2139Sjp161948
86*2139Sjp161948 #include <openssl/crypto.h>
87*2139Sjp161948 #include <openssl/bio.h>
88*2139Sjp161948 #include <openssl/evp.h>
89*2139Sjp161948 #include <openssl/bn.h>
90*2139Sjp161948 #include <openssl/ecdsa.h>
91*2139Sjp161948 #ifndef OPENSSL_NO_ENGINE
92*2139Sjp161948 #include <openssl/engine.h>
93*2139Sjp161948 #endif
94*2139Sjp161948 #include <openssl/err.h>
95*2139Sjp161948 #include <openssl/rand.h>
96*2139Sjp161948
97*2139Sjp161948 static const char rnd_seed[] = "string to make the random number generator "
98*2139Sjp161948 "think it has entropy";
99*2139Sjp161948
100*2139Sjp161948 /* declaration of the test functions */
101*2139Sjp161948 int x9_62_tests(BIO *);
102*2139Sjp161948 int x9_62_test_internal(BIO *out, int nid, const char *r, const char *s);
103*2139Sjp161948 int test_builtin(BIO *);
104*2139Sjp161948
105*2139Sjp161948 /* functions to change the RAND_METHOD */
106*2139Sjp161948 int change_rand(void);
107*2139Sjp161948 int restore_rand(void);
108*2139Sjp161948 int fbytes(unsigned char *buf, int num);
109*2139Sjp161948
110*2139Sjp161948 RAND_METHOD fake_rand;
111*2139Sjp161948 const RAND_METHOD *old_rand;
112*2139Sjp161948
change_rand(void)113*2139Sjp161948 int change_rand(void)
114*2139Sjp161948 {
115*2139Sjp161948 /* save old rand method */
116*2139Sjp161948 if ((old_rand = RAND_get_rand_method()) == NULL)
117*2139Sjp161948 return 0;
118*2139Sjp161948
119*2139Sjp161948 fake_rand.seed = old_rand->seed;
120*2139Sjp161948 fake_rand.cleanup = old_rand->cleanup;
121*2139Sjp161948 fake_rand.add = old_rand->add;
122*2139Sjp161948 fake_rand.status = old_rand->status;
123*2139Sjp161948 /* use own random function */
124*2139Sjp161948 fake_rand.bytes = fbytes;
125*2139Sjp161948 fake_rand.pseudorand = old_rand->bytes;
126*2139Sjp161948 /* set new RAND_METHOD */
127*2139Sjp161948 if (!RAND_set_rand_method(&fake_rand))
128*2139Sjp161948 return 0;
129*2139Sjp161948 return 1;
130*2139Sjp161948 }
131*2139Sjp161948
restore_rand(void)132*2139Sjp161948 int restore_rand(void)
133*2139Sjp161948 {
134*2139Sjp161948 if (!RAND_set_rand_method(old_rand))
135*2139Sjp161948 return 0;
136*2139Sjp161948 else
137*2139Sjp161948 return 1;
138*2139Sjp161948 }
139*2139Sjp161948
140*2139Sjp161948 static int fbytes_counter = 0;
141*2139Sjp161948 static const char *numbers[8] = {
142*2139Sjp161948 "651056770906015076056810763456358567190100156695615665659",
143*2139Sjp161948 "6140507067065001063065065565667405560006161556565665656654",
144*2139Sjp161948 "8763001015071075675010661307616710783570106710677817767166"
145*2139Sjp161948 "71676178726717",
146*2139Sjp161948 "7000000175690566466555057817571571075705015757757057795755"
147*2139Sjp161948 "55657156756655",
148*2139Sjp161948 "1275552191113212300012030439187146164646146646466749494799",
149*2139Sjp161948 "1542725565216523985789236956265265265235675811949404040041",
150*2139Sjp161948 "1456427555219115346513212300075341203043918714616464614664"
151*2139Sjp161948 "64667494947990",
152*2139Sjp161948 "1712787255652165239672857892369562652652652356758119494040"
153*2139Sjp161948 "40041670216363"};
154*2139Sjp161948
fbytes(unsigned char * buf,int num)155*2139Sjp161948 int fbytes(unsigned char *buf, int num)
156*2139Sjp161948 {
157*2139Sjp161948 int ret;
158*2139Sjp161948 BIGNUM *tmp = NULL;
159*2139Sjp161948
160*2139Sjp161948 if (fbytes_counter >= 8)
161*2139Sjp161948 return 0;
162*2139Sjp161948 tmp = BN_new();
163*2139Sjp161948 if (!tmp)
164*2139Sjp161948 return 0;
165*2139Sjp161948 if (!BN_dec2bn(&tmp, numbers[fbytes_counter]))
166*2139Sjp161948 {
167*2139Sjp161948 BN_free(tmp);
168*2139Sjp161948 return 0;
169*2139Sjp161948 }
170*2139Sjp161948 fbytes_counter ++;
171*2139Sjp161948 ret = BN_bn2bin(tmp, buf);
172*2139Sjp161948 if (ret == 0 || ret != num)
173*2139Sjp161948 ret = 0;
174*2139Sjp161948 else
175*2139Sjp161948 ret = 1;
176*2139Sjp161948 if (tmp)
177*2139Sjp161948 BN_free(tmp);
178*2139Sjp161948 return ret;
179*2139Sjp161948 }
180*2139Sjp161948
181*2139Sjp161948 /* some tests from the X9.62 draft */
x9_62_test_internal(BIO * out,int nid,const char * r_in,const char * s_in)182*2139Sjp161948 int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in)
183*2139Sjp161948 {
184*2139Sjp161948 int ret = 0;
185*2139Sjp161948 const char message[] = "abc";
186*2139Sjp161948 unsigned char digest[20];
187*2139Sjp161948 unsigned int dgst_len = 0;
188*2139Sjp161948 EVP_MD_CTX md_ctx;
189*2139Sjp161948 EC_KEY *key = NULL;
190*2139Sjp161948 ECDSA_SIG *signature = NULL;
191*2139Sjp161948 BIGNUM *r = NULL, *s = NULL;
192*2139Sjp161948
193*2139Sjp161948 EVP_MD_CTX_init(&md_ctx);
194*2139Sjp161948 /* get the message digest */
195*2139Sjp161948 EVP_DigestInit(&md_ctx, EVP_ecdsa());
196*2139Sjp161948 EVP_DigestUpdate(&md_ctx, (const void*)message, 3);
197*2139Sjp161948 EVP_DigestFinal(&md_ctx, digest, &dgst_len);
198*2139Sjp161948
199*2139Sjp161948 BIO_printf(out, "testing %s: ", OBJ_nid2sn(nid));
200*2139Sjp161948 /* create the key */
201*2139Sjp161948 if ((key = EC_KEY_new_by_curve_name(nid)) == NULL)
202*2139Sjp161948 goto x962_int_err;
203*2139Sjp161948 if (!EC_KEY_generate_key(key))
204*2139Sjp161948 goto x962_int_err;
205*2139Sjp161948 BIO_printf(out, ".");
206*2139Sjp161948 BIO_flush(out);
207*2139Sjp161948 /* create the signature */
208*2139Sjp161948 signature = ECDSA_do_sign(digest, 20, key);
209*2139Sjp161948 if (signature == NULL)
210*2139Sjp161948 goto x962_int_err;
211*2139Sjp161948 BIO_printf(out, ".");
212*2139Sjp161948 BIO_flush(out);
213*2139Sjp161948 /* compare the created signature with the expected signature */
214*2139Sjp161948 if ((r = BN_new()) == NULL || (s = BN_new()) == NULL)
215*2139Sjp161948 goto x962_int_err;
216*2139Sjp161948 if (!BN_dec2bn(&r, r_in) ||
217*2139Sjp161948 !BN_dec2bn(&s, s_in))
218*2139Sjp161948 goto x962_int_err;
219*2139Sjp161948 if (BN_cmp(signature->r ,r) || BN_cmp(signature->s, s))
220*2139Sjp161948 goto x962_int_err;
221*2139Sjp161948 BIO_printf(out, ".");
222*2139Sjp161948 BIO_flush(out);
223*2139Sjp161948 /* verify the signature */
224*2139Sjp161948 if (ECDSA_do_verify(digest, 20, signature, key) != 1)
225*2139Sjp161948 goto x962_int_err;
226*2139Sjp161948 BIO_printf(out, ".");
227*2139Sjp161948 BIO_flush(out);
228*2139Sjp161948
229*2139Sjp161948 BIO_printf(out, " ok\n");
230*2139Sjp161948 ret = 1;
231*2139Sjp161948 x962_int_err:
232*2139Sjp161948 if (!ret)
233*2139Sjp161948 BIO_printf(out, " failed\n");
234*2139Sjp161948 if (key)
235*2139Sjp161948 EC_KEY_free(key);
236*2139Sjp161948 if (signature)
237*2139Sjp161948 ECDSA_SIG_free(signature);
238*2139Sjp161948 if (r)
239*2139Sjp161948 BN_free(r);
240*2139Sjp161948 if (s)
241*2139Sjp161948 BN_free(s);
242*2139Sjp161948 EVP_MD_CTX_cleanup(&md_ctx);
243*2139Sjp161948 return ret;
244*2139Sjp161948 }
245*2139Sjp161948
x9_62_tests(BIO * out)246*2139Sjp161948 int x9_62_tests(BIO *out)
247*2139Sjp161948 {
248*2139Sjp161948 int ret = 0;
249*2139Sjp161948
250*2139Sjp161948 BIO_printf(out, "some tests from X9.62:\n");
251*2139Sjp161948
252*2139Sjp161948 /* set own rand method */
253*2139Sjp161948 if (!change_rand())
254*2139Sjp161948 goto x962_err;
255*2139Sjp161948
256*2139Sjp161948 if (!x9_62_test_internal(out, NID_X9_62_prime192v1,
257*2139Sjp161948 "3342403536405981729393488334694600415596881826869351677613",
258*2139Sjp161948 "5735822328888155254683894997897571951568553642892029982342"))
259*2139Sjp161948 goto x962_err;
260*2139Sjp161948 if (!x9_62_test_internal(out, NID_X9_62_prime239v1,
261*2139Sjp161948 "3086361431751678114926225473006680188549593787585317781474"
262*2139Sjp161948 "62058306432176",
263*2139Sjp161948 "3238135532097973577080787768312505059318910517550078427819"
264*2139Sjp161948 "78505179448783"))
265*2139Sjp161948 goto x962_err;
266*2139Sjp161948 if (!x9_62_test_internal(out, NID_X9_62_c2tnb191v1,
267*2139Sjp161948 "87194383164871543355722284926904419997237591535066528048",
268*2139Sjp161948 "308992691965804947361541664549085895292153777025772063598"))
269*2139Sjp161948 goto x962_err;
270*2139Sjp161948 if (!x9_62_test_internal(out, NID_X9_62_c2tnb239v1,
271*2139Sjp161948 "2159633321041961198501834003903461262881815148684178964245"
272*2139Sjp161948 "5876922391552",
273*2139Sjp161948 "1970303740007316867383349976549972270528498040721988191026"
274*2139Sjp161948 "49413465737174"))
275*2139Sjp161948 goto x962_err;
276*2139Sjp161948
277*2139Sjp161948 ret = 1;
278*2139Sjp161948 x962_err:
279*2139Sjp161948 if (!restore_rand())
280*2139Sjp161948 ret = 0;
281*2139Sjp161948 return ret;
282*2139Sjp161948 }
283*2139Sjp161948
test_builtin(BIO * out)284*2139Sjp161948 int test_builtin(BIO *out)
285*2139Sjp161948 {
286*2139Sjp161948 EC_builtin_curve *curves = NULL;
287*2139Sjp161948 size_t crv_len = 0, n = 0;
288*2139Sjp161948 EC_KEY *eckey = NULL, *wrong_eckey = NULL;
289*2139Sjp161948 EC_GROUP *group;
290*2139Sjp161948 unsigned char digest[20], wrong_digest[20];
291*2139Sjp161948 unsigned char *signature = NULL;
292*2139Sjp161948 unsigned int sig_len;
293*2139Sjp161948 int nid, ret = 0;
294*2139Sjp161948
295*2139Sjp161948 /* fill digest values with some random data */
296*2139Sjp161948 if (!RAND_pseudo_bytes(digest, 20) ||
297*2139Sjp161948 !RAND_pseudo_bytes(wrong_digest, 20))
298*2139Sjp161948 {
299*2139Sjp161948 BIO_printf(out, "ERROR: unable to get random data\n");
300*2139Sjp161948 goto builtin_err;
301*2139Sjp161948 }
302*2139Sjp161948
303*2139Sjp161948 /* create and verify a ecdsa signature with every availble curve
304*2139Sjp161948 * (with ) */
305*2139Sjp161948 BIO_printf(out, "\ntesting ECDSA_sign() and ECDSA_verify() "
306*2139Sjp161948 "with some internal curves:\n");
307*2139Sjp161948
308*2139Sjp161948 /* get a list of all internal curves */
309*2139Sjp161948 crv_len = EC_get_builtin_curves(NULL, 0);
310*2139Sjp161948
311*2139Sjp161948 curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
312*2139Sjp161948
313*2139Sjp161948 if (curves == NULL)
314*2139Sjp161948 {
315*2139Sjp161948 BIO_printf(out, "malloc error\n");
316*2139Sjp161948 goto builtin_err;
317*2139Sjp161948 }
318*2139Sjp161948
319*2139Sjp161948 if (!EC_get_builtin_curves(curves, crv_len))
320*2139Sjp161948 {
321*2139Sjp161948 BIO_printf(out, "unable to get internal curves\n");
322*2139Sjp161948 goto builtin_err;
323*2139Sjp161948 }
324*2139Sjp161948
325*2139Sjp161948 /* now create and verify a signature for every curve */
326*2139Sjp161948 for (n = 0; n < crv_len; n++)
327*2139Sjp161948 {
328*2139Sjp161948 unsigned char dirt, offset;
329*2139Sjp161948
330*2139Sjp161948 nid = curves[n].nid;
331*2139Sjp161948 if (nid == NID_ipsec4)
332*2139Sjp161948 continue;
333*2139Sjp161948 /* create new ecdsa key (== EC_KEY) */
334*2139Sjp161948 if ((eckey = EC_KEY_new()) == NULL)
335*2139Sjp161948 goto builtin_err;
336*2139Sjp161948 group = EC_GROUP_new_by_curve_name(nid);
337*2139Sjp161948 if (group == NULL)
338*2139Sjp161948 goto builtin_err;
339*2139Sjp161948 if (EC_KEY_set_group(eckey, group) == 0)
340*2139Sjp161948 goto builtin_err;
341*2139Sjp161948 EC_GROUP_free(group);
342*2139Sjp161948 if (EC_GROUP_get_degree(EC_KEY_get0_group(eckey)) < 160)
343*2139Sjp161948 /* drop the curve */
344*2139Sjp161948 {
345*2139Sjp161948 EC_KEY_free(eckey);
346*2139Sjp161948 eckey = NULL;
347*2139Sjp161948 continue;
348*2139Sjp161948 }
349*2139Sjp161948 BIO_printf(out, "%s: ", OBJ_nid2sn(nid));
350*2139Sjp161948 /* create key */
351*2139Sjp161948 if (!EC_KEY_generate_key(eckey))
352*2139Sjp161948 {
353*2139Sjp161948 BIO_printf(out, " failed\n");
354*2139Sjp161948 goto builtin_err;
355*2139Sjp161948 }
356*2139Sjp161948 /* create second key */
357*2139Sjp161948 if ((wrong_eckey = EC_KEY_new()) == NULL)
358*2139Sjp161948 goto builtin_err;
359*2139Sjp161948 group = EC_GROUP_new_by_curve_name(nid);
360*2139Sjp161948 if (group == NULL)
361*2139Sjp161948 goto builtin_err;
362*2139Sjp161948 if (EC_KEY_set_group(wrong_eckey, group) == 0)
363*2139Sjp161948 goto builtin_err;
364*2139Sjp161948 EC_GROUP_free(group);
365*2139Sjp161948 if (!EC_KEY_generate_key(wrong_eckey))
366*2139Sjp161948 {
367*2139Sjp161948 BIO_printf(out, " failed\n");
368*2139Sjp161948 goto builtin_err;
369*2139Sjp161948 }
370*2139Sjp161948
371*2139Sjp161948 BIO_printf(out, ".");
372*2139Sjp161948 BIO_flush(out);
373*2139Sjp161948 /* check key */
374*2139Sjp161948 if (!EC_KEY_check_key(eckey))
375*2139Sjp161948 {
376*2139Sjp161948 BIO_printf(out, " failed\n");
377*2139Sjp161948 goto builtin_err;
378*2139Sjp161948 }
379*2139Sjp161948 BIO_printf(out, ".");
380*2139Sjp161948 BIO_flush(out);
381*2139Sjp161948 /* create signature */
382*2139Sjp161948 sig_len = ECDSA_size(eckey);
383*2139Sjp161948 if ((signature = OPENSSL_malloc(sig_len)) == NULL)
384*2139Sjp161948 goto builtin_err;
385*2139Sjp161948 if (!ECDSA_sign(0, digest, 20, signature, &sig_len, eckey))
386*2139Sjp161948 {
387*2139Sjp161948 BIO_printf(out, " failed\n");
388*2139Sjp161948 goto builtin_err;
389*2139Sjp161948 }
390*2139Sjp161948 BIO_printf(out, ".");
391*2139Sjp161948 BIO_flush(out);
392*2139Sjp161948 /* verify signature */
393*2139Sjp161948 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) != 1)
394*2139Sjp161948 {
395*2139Sjp161948 BIO_printf(out, " failed\n");
396*2139Sjp161948 goto builtin_err;
397*2139Sjp161948 }
398*2139Sjp161948 BIO_printf(out, ".");
399*2139Sjp161948 BIO_flush(out);
400*2139Sjp161948 /* verify signature with the wrong key */
401*2139Sjp161948 if (ECDSA_verify(0, digest, 20, signature, sig_len,
402*2139Sjp161948 wrong_eckey) == 1)
403*2139Sjp161948 {
404*2139Sjp161948 BIO_printf(out, " failed\n");
405*2139Sjp161948 goto builtin_err;
406*2139Sjp161948 }
407*2139Sjp161948 BIO_printf(out, ".");
408*2139Sjp161948 BIO_flush(out);
409*2139Sjp161948 /* wrong digest */
410*2139Sjp161948 if (ECDSA_verify(0, wrong_digest, 20, signature, sig_len,
411*2139Sjp161948 eckey) == 1)
412*2139Sjp161948 {
413*2139Sjp161948 BIO_printf(out, " failed\n");
414*2139Sjp161948 goto builtin_err;
415*2139Sjp161948 }
416*2139Sjp161948 BIO_printf(out, ".");
417*2139Sjp161948 BIO_flush(out);
418*2139Sjp161948 /* modify a single byte of the signature */
419*2139Sjp161948 offset = signature[10] % sig_len;
420*2139Sjp161948 dirt = signature[11];
421*2139Sjp161948 signature[offset] ^= dirt ? dirt : 1;
422*2139Sjp161948 if (ECDSA_verify(0, digest, 20, signature, sig_len, eckey) == 1)
423*2139Sjp161948 {
424*2139Sjp161948 BIO_printf(out, " failed\n");
425*2139Sjp161948 goto builtin_err;
426*2139Sjp161948 }
427*2139Sjp161948 BIO_printf(out, ".");
428*2139Sjp161948 BIO_flush(out);
429*2139Sjp161948
430*2139Sjp161948 BIO_printf(out, " ok\n");
431*2139Sjp161948 /* cleanup */
432*2139Sjp161948 OPENSSL_free(signature);
433*2139Sjp161948 signature = NULL;
434*2139Sjp161948 EC_KEY_free(eckey);
435*2139Sjp161948 eckey = NULL;
436*2139Sjp161948 EC_KEY_free(wrong_eckey);
437*2139Sjp161948 wrong_eckey = NULL;
438*2139Sjp161948 }
439*2139Sjp161948
440*2139Sjp161948 ret = 1;
441*2139Sjp161948 builtin_err:
442*2139Sjp161948 if (eckey)
443*2139Sjp161948 EC_KEY_free(eckey);
444*2139Sjp161948 if (wrong_eckey)
445*2139Sjp161948 EC_KEY_free(wrong_eckey);
446*2139Sjp161948 if (signature)
447*2139Sjp161948 OPENSSL_free(signature);
448*2139Sjp161948 if (curves)
449*2139Sjp161948 OPENSSL_free(curves);
450*2139Sjp161948
451*2139Sjp161948 return ret;
452*2139Sjp161948 }
453*2139Sjp161948
main(void)454*2139Sjp161948 int main(void)
455*2139Sjp161948 {
456*2139Sjp161948 int ret = 1;
457*2139Sjp161948 BIO *out;
458*2139Sjp161948
459*2139Sjp161948 out = BIO_new_fp(stdout, BIO_NOCLOSE);
460*2139Sjp161948
461*2139Sjp161948 /* enable memory leak checking unless explicitly disabled */
462*2139Sjp161948 if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) &&
463*2139Sjp161948 (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
464*2139Sjp161948 {
465*2139Sjp161948 CRYPTO_malloc_debug_init();
466*2139Sjp161948 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
467*2139Sjp161948 }
468*2139Sjp161948 else
469*2139Sjp161948 {
470*2139Sjp161948 /* OPENSSL_DEBUG_MEMORY=off */
471*2139Sjp161948 CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
472*2139Sjp161948 }
473*2139Sjp161948 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
474*2139Sjp161948
475*2139Sjp161948 ERR_load_crypto_strings();
476*2139Sjp161948
477*2139Sjp161948 /* initialize the prng */
478*2139Sjp161948 RAND_seed(rnd_seed, sizeof(rnd_seed));
479*2139Sjp161948
480*2139Sjp161948 /* the tests */
481*2139Sjp161948 if (!x9_62_tests(out)) goto err;
482*2139Sjp161948 if (!test_builtin(out)) goto err;
483*2139Sjp161948
484*2139Sjp161948 ret = 0;
485*2139Sjp161948 err:
486*2139Sjp161948 if (ret)
487*2139Sjp161948 BIO_printf(out, "\nECDSA test failed\n");
488*2139Sjp161948 else
489*2139Sjp161948 BIO_printf(out, "\nECDSA test passed\n");
490*2139Sjp161948 if (ret)
491*2139Sjp161948 ERR_print_errors(out);
492*2139Sjp161948 CRYPTO_cleanup_all_ex_data();
493*2139Sjp161948 ERR_remove_state(0);
494*2139Sjp161948 ERR_free_strings();
495*2139Sjp161948 CRYPTO_mem_leaks(out);
496*2139Sjp161948 if (out != NULL)
497*2139Sjp161948 BIO_free(out);
498*2139Sjp161948 return ret;
499*2139Sjp161948 }
500*2139Sjp161948 #endif
501