Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / testsuite / ecdsa-keygen-test.c
1 #include "testutils.h"
2 #include "knuth-lfib.h"
3
4 /* Check if y^2 = x^3 - 3x + b */
5 static int
6 ecc_valid_p (struct ecc_point *pub)
7 {
8   mpz_t t, x, y;
9   mpz_t lhs, rhs;
10   int res;
11   mp_size_t size;
12
13   size = pub->ecc->size;
14
15   /* First check range */
16   if (mpn_cmp (pub->p, pub->ecc->p, size) >= 0
17       || mpn_cmp (pub->p + size, pub->ecc->p, size) >= 0)
18     return 0;
19
20   mpz_init (lhs);
21   mpz_init (rhs);
22
23   mpz_roinit_n (x, pub->p, size);
24   mpz_roinit_n (y, pub->p + size, size);
25
26   mpz_mul (lhs, y, y);
27   mpz_mul (rhs, x, x);
28   mpz_sub_ui (rhs, rhs, 3);
29   mpz_mul (rhs, rhs, x);
30   mpz_add (rhs, rhs, mpz_roinit_n (t, pub->ecc->b, size));
31
32   res = mpz_congruent_p (lhs, rhs, mpz_roinit_n (t, pub->ecc->p, size));
33   
34   mpz_clear (lhs);
35   mpz_clear (rhs);
36
37   return res;
38 }
39
40 void
41 test_main (void)
42 {
43   unsigned i;
44   struct knuth_lfib_ctx rctx;
45   struct dsa_signature signature;
46
47   struct tstring *digest;
48
49   knuth_lfib_init (&rctx, 4711);
50   dsa_signature_init (&signature);
51
52   digest = SHEX (/* sha256("abc") */
53                  "BA7816BF 8F01CFEA 414140DE 5DAE2223"
54                  "B00361A3 96177A9C B410FF61 F20015AD");
55
56   for (i = 0; ecc_curves[i]; i++)
57     {
58       const struct ecc_curve *ecc = ecc_curves[i];
59       struct ecc_point pub;
60       struct ecc_scalar key;
61
62       if (verbose)
63         fprintf (stderr, "Curve %d\n", ecc->bit_size);
64
65       ecc_point_init (&pub, ecc);
66       ecc_scalar_init (&key, ecc);
67
68       ecdsa_generate_keypair (&pub, &key,
69                               &rctx,
70                               (nettle_random_func *) knuth_lfib_random);
71
72       if (verbose)
73         {
74           gmp_fprintf (stderr,
75                        "Public key:\nx = %Nx\ny = %Nx\n",
76                        pub.p, ecc->size, pub.p + ecc->size, ecc->size);
77           gmp_fprintf (stderr,
78                        "Private key: %Nx\n", key.p, ecc->size);
79         }
80       if (!ecc_valid_p (&pub))
81         die ("ecdsa_generate_keypair produced an invalid point.\n");
82
83       ecdsa_sign (&key,
84                   &rctx, (nettle_random_func *) knuth_lfib_random,
85                   digest->length, digest->data,
86                   &signature);
87
88       if (!ecdsa_verify (&pub, digest->length, digest->data,
89                           &signature))
90         die ("ecdsa_verify failed.\n");
91
92       digest->data[3] ^= 17;
93       if (ecdsa_verify (&pub, digest->length, digest->data,
94                          &signature))
95         die ("ecdsa_verify  returned success with invalid digest.\n");
96       digest->data[3] ^= 17;
97
98       mpz_combit (signature.r, 117);
99       if (ecdsa_verify (&pub, digest->length, digest->data,
100                          &signature))
101         die ("ecdsa_verify  returned success with invalid signature.r.\n");
102
103       mpz_combit (signature.r, 117);
104       mpz_combit (signature.s, 93);
105       if (ecdsa_verify (&pub, digest->length, digest->data,
106                          &signature))
107         die ("ecdsa_verify  returned success with invalid signature.s.\n");
108
109       ecc_point_clear (&pub);
110       ecc_scalar_clear (&key);
111     }
112   dsa_signature_clear (&signature);
113 }