Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / testsuite / ecdsa-keygen-test.c
blob7c25421a9e8b059068666327cb594e3f3ef998af
1 #include "testutils.h"
2 #include "knuth-lfib.h"
4 /* Check if y^2 = x^3 - 3x + b */
5 static int
6 ecc_valid_p (struct ecc_point *pub)
8 mpz_t t, x, y;
9 mpz_t lhs, rhs;
10 int res;
11 mp_size_t size;
13 size = pub->ecc->size;
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;
20 mpz_init (lhs);
21 mpz_init (rhs);
23 mpz_roinit_n (x, pub->p, size);
24 mpz_roinit_n (y, pub->p + size, size);
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));
32 res = mpz_congruent_p (lhs, rhs, mpz_roinit_n (t, pub->ecc->p, size));
34 mpz_clear (lhs);
35 mpz_clear (rhs);
37 return res;
40 void
41 test_main (void)
43 unsigned i;
44 struct knuth_lfib_ctx rctx;
45 struct dsa_signature signature;
47 struct tstring *digest;
49 knuth_lfib_init (&rctx, 4711);
50 dsa_signature_init (&signature);
52 digest = SHEX (/* sha256("abc") */
53 "BA7816BF 8F01CFEA 414140DE 5DAE2223"
54 "B00361A3 96177A9C B410FF61 F20015AD");
56 for (i = 0; ecc_curves[i]; i++)
58 const struct ecc_curve *ecc = ecc_curves[i];
59 struct ecc_point pub;
60 struct ecc_scalar key;
62 if (verbose)
63 fprintf (stderr, "Curve %d\n", ecc->bit_size);
65 ecc_point_init (&pub, ecc);
66 ecc_scalar_init (&key, ecc);
68 ecdsa_generate_keypair (&pub, &key,
69 &rctx,
70 (nettle_random_func *) knuth_lfib_random);
72 if (verbose)
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);
80 if (!ecc_valid_p (&pub))
81 die ("ecdsa_generate_keypair produced an invalid point.\n");
83 ecdsa_sign (&key,
84 &rctx, (nettle_random_func *) knuth_lfib_random,
85 digest->length, digest->data,
86 &signature);
88 if (!ecdsa_verify (&pub, digest->length, digest->data,
89 &signature))
90 die ("ecdsa_verify failed.\n");
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;
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");
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");
109 ecc_point_clear (&pub);
110 ecc_scalar_clear (&key);
112 dsa_signature_clear (&signature);