]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/ecc/ecc_test.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / pk / ecc / ecc_test.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9
10 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
11  *
12  * All curves taken from NIST recommendation paper of July 1999
13  * Available at http://csrc.nist.gov/cryptval/dss.htm
14  */
15 #include "tomcrypt.h"
16
17 /**
18   @file ecc_test.c
19   ECC Crypto, Tom St Denis
20 */
21
22 #ifdef LTC_MECC
23
24 /**
25   Perform on the ECC system
26   @return CRYPT_OK if successful
27 */
28 int ecc_test(void)
29 {
30    void     *modulus, *order;
31    ecc_point  *G, *GG;
32    int i, err, primality;
33
34    if ((err = mp_init_multi(&modulus, &order, NULL)) != CRYPT_OK) {
35       return err;
36    }
37
38    G   = ltc_ecc_new_point();
39    GG  = ltc_ecc_new_point();
40    if (G == NULL || GG == NULL) {
41       mp_clear_multi(modulus, order, NULL);
42       ltc_ecc_del_point(G);
43       ltc_ecc_del_point(GG);
44       return CRYPT_MEM;
45    }
46
47    for (i = 0; ltc_ecc_sets[i].size; i++) {
48        #if 0
49           printf("Testing %d\n", ltc_ecc_sets[i].size);
50        #endif
51        if ((err = mp_read_radix(modulus, (char *)ltc_ecc_sets[i].prime, 16)) != CRYPT_OK)   { goto done; }
52        if ((err = mp_read_radix(order, (char *)ltc_ecc_sets[i].order, 16)) != CRYPT_OK)     { goto done; }
53
54        /* is prime actually prime? */
55        if ((err = mp_prime_is_prime(modulus, 8, &primality)) != CRYPT_OK)                   { goto done; }
56        if (primality == 0) {
57           err = CRYPT_FAIL_TESTVECTOR;
58           goto done;
59        }
60
61        /* is order prime ? */
62        if ((err = mp_prime_is_prime(order, 8, &primality)) != CRYPT_OK)                     { goto done; }
63        if (primality == 0) {
64           err = CRYPT_FAIL_TESTVECTOR;
65           goto done;
66        }
67
68        if ((err = mp_read_radix(G->x, (char *)ltc_ecc_sets[i].Gx, 16)) != CRYPT_OK)         { goto done; }
69        if ((err = mp_read_radix(G->y, (char *)ltc_ecc_sets[i].Gy, 16)) != CRYPT_OK)         { goto done; }
70        mp_set(G->z, 1);
71
72        /* then we should have G == (order + 1)G */
73        if ((err = mp_add_d(order, 1, order)) != CRYPT_OK)                                   { goto done; }
74        if ((err = ltc_mp.ecc_ptmul(order, G, GG, modulus, 1)) != CRYPT_OK)                  { goto done; }
75        if (mp_cmp(G->x, GG->x) != LTC_MP_EQ || mp_cmp(G->y, GG->y) != LTC_MP_EQ) {
76           err = CRYPT_FAIL_TESTVECTOR;
77           goto done;
78        }
79    }
80    err = CRYPT_OK;
81 done:
82    ltc_ecc_del_point(GG);
83    ltc_ecc_del_point(G);
84    mp_clear_multi(order, modulus, NULL);
85    return err;
86 }
87
88 #endif
89
90 /* ref:         $Format:%D$ */
91 /* git commit:  $Format:%H$ */
92 /* commit time: $Format:%ai$ */
93