Home | History | Annotate | Download | only in ecc
      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  * Tom St Denis, tomstdenis (at) gmail.com, http://libtomcrypt.com
     10  */
     11 
     12 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
     13  *
     14  * All curves taken from NIST recommendation paper of July 1999
     15  * Available at http://csrc.nist.gov/cryptval/dss.htm
     16  */
     17 #include "tomcrypt.h"
     18 
     19 /**
     20   @file ecc_test.c
     21   ECC Crypto, Tom St Denis
     22 */
     23 
     24 #ifdef MECC
     25 
     26 /**
     27   Perform on the ECC system
     28   @return CRYPT_OK if successful
     29 */
     30 int ecc_test(void)
     31 {
     32    void     *modulus, *order;
     33    ecc_point  *G, *GG;
     34    int i, err, primality;
     35 
     36    if ((err = mp_init_multi(&modulus, &order, NULL)) != CRYPT_OK) {
     37       return err;
     38    }
     39 
     40    G   = ltc_ecc_new_point();
     41    GG  = ltc_ecc_new_point();
     42    if (G == NULL || GG == NULL) {
     43       mp_clear_multi(modulus, order, NULL);
     44       ltc_ecc_del_point(G);
     45       ltc_ecc_del_point(GG);
     46       return CRYPT_MEM;
     47    }
     48 
     49    for (i = 0; ltc_ecc_sets[i].size; i++) {
     50        #if 0
     51           printf("Testing %d\n", ltc_ecc_sets[i].size);
     52        #endif
     53        if ((err = mp_read_radix(modulus, (char *)ltc_ecc_sets[i].prime, 16)) != CRYPT_OK)   { goto done; }
     54        if ((err = mp_read_radix(order, (char *)ltc_ecc_sets[i].order, 16)) != CRYPT_OK)     { goto done; }
     55 
     56        /* is prime actually prime? */
     57        if ((err = mp_prime_is_prime(modulus, 8, &primality)) != CRYPT_OK)                   { goto done; }
     58        if (primality == 0) {
     59           err = CRYPT_FAIL_TESTVECTOR;
     60           goto done;
     61        }
     62 
     63        /* is order prime ? */
     64        if ((err = mp_prime_is_prime(order, 8, &primality)) != CRYPT_OK)                     { goto done; }
     65        if (primality == 0) {
     66           err = CRYPT_FAIL_TESTVECTOR;
     67           goto done;
     68        }
     69 
     70        if ((err = mp_read_radix(G->x, (char *)ltc_ecc_sets[i].Gx, 16)) != CRYPT_OK)         { goto done; }
     71        if ((err = mp_read_radix(G->y, (char *)ltc_ecc_sets[i].Gy, 16)) != CRYPT_OK)         { goto done; }
     72        mp_set(G->z, 1);
     73 
     74        /* then we should have G == (order + 1)G */
     75        if ((err = mp_add_d(order, 1, order)) != CRYPT_OK)                                   { goto done; }
     76        if ((err = ltc_mp.ecc_ptmul(order, G, GG, modulus, 1)) != CRYPT_OK)                  { goto done; }
     77        if (mp_cmp(G->x, GG->x) != LTC_MP_EQ || mp_cmp(G->y, GG->y) != LTC_MP_EQ) {
     78           err = CRYPT_FAIL_TESTVECTOR;
     79           goto done;
     80        }
     81    }
     82    err = CRYPT_OK;
     83 done:
     84    ltc_ecc_del_point(GG);
     85    ltc_ecc_del_point(G);
     86    mp_clear_multi(order, modulus, NULL);
     87    return err;
     88 }
     89 
     90 #endif
     91 
     92 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_test.c,v $ */
     93 /* $Revision: 1.10 $ */
     94 /* $Date: 2006/12/04 02:19:48 $ */
     95 
     96