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_make_key.c
     21   ECC Crypto, Tom St Denis
     22 */
     23 
     24 #ifdef MECC
     25 
     26 /**
     27   Make a new ECC key
     28   @param prng         An active PRNG state
     29   @param wprng        The index of the PRNG you wish to use
     30   @param keysize      The keysize for the new key (in octets from 20 to 65 bytes)
     31   @param key          [out] Destination of the newly created key
     32   @return CRYPT_OK if successful, upon error all allocated memory will be freed
     33 */
     34 int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key)
     35 {
     36    int x, err;
     37 
     38    /* find key size */
     39    for (x = 0; (keysize > ltc_ecc_sets[x].size) && (ltc_ecc_sets[x].size != 0); x++);
     40    keysize = ltc_ecc_sets[x].size;
     41 
     42    if (keysize > ECC_MAXSIZE || ltc_ecc_sets[x].size == 0) {
     43       return CRYPT_INVALID_KEYSIZE;
     44    }
     45    err = ecc_make_key_ex(prng, wprng, key, &ltc_ecc_sets[x]);
     46    key->idx = x;
     47    return err;
     48 }
     49 
     50 int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp)
     51 {
     52    int            err;
     53    ecc_point     *base;
     54    void          *prime;
     55    unsigned char *buf;
     56    int            keysize;
     57 
     58    LTC_ARGCHK(key         != NULL);
     59    LTC_ARGCHK(ltc_mp.name != NULL);
     60    LTC_ARGCHK(dp          != NULL);
     61 
     62    /* good prng? */
     63    if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
     64       return err;
     65    }
     66 
     67    key->idx = -1;
     68    key->dp  = dp;
     69    keysize  = dp->size;
     70 
     71    /* allocate ram */
     72    base = NULL;
     73    buf  = XMALLOC(ECC_MAXSIZE);
     74    if (buf == NULL) {
     75       return CRYPT_MEM;
     76    }
     77 
     78    /* make up random string */
     79    if (prng_descriptor[wprng].read(buf, (unsigned long)keysize, prng) != (unsigned long)keysize) {
     80       err = CRYPT_ERROR_READPRNG;
     81       goto ERR_BUF;
     82    }
     83 
     84    /* setup the key variables */
     85    if ((err = mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, &prime, NULL)) != CRYPT_OK) {
     86       goto ERR_BUF;
     87    }
     88    base = ltc_ecc_new_point();
     89    if (base == NULL) {
     90       err = CRYPT_MEM;
     91       goto errkey;
     92    }
     93 
     94    /* read in the specs for this key */
     95    if ((err = mp_read_radix(prime,   (char *)key->dp->prime, 16)) != CRYPT_OK)                  { goto errkey; }
     96    if ((err = mp_read_radix(base->x, (char *)key->dp->Gx, 16)) != CRYPT_OK)                     { goto errkey; }
     97    if ((err = mp_read_radix(base->y, (char *)key->dp->Gy, 16)) != CRYPT_OK)                     { goto errkey; }
     98    if ((err = mp_set(base->z, 1)) != CRYPT_OK)                                                  { goto errkey; }
     99    if ((err = mp_read_unsigned_bin(key->k, (unsigned char *)buf, keysize)) != CRYPT_OK)         { goto errkey; }
    100 
    101    /* make the public key */
    102    if ((err = ltc_mp.ecc_ptmul(key->k, base, &key->pubkey, prime, 1)) != CRYPT_OK)              { goto errkey; }
    103    key->type = PK_PRIVATE;
    104 
    105    /* free up ram */
    106    err = CRYPT_OK;
    107    goto cleanup;
    108 errkey:
    109    mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
    110 cleanup:
    111    ltc_ecc_del_point(base);
    112    mp_clear(prime);
    113 ERR_BUF:
    114 #ifdef LTC_CLEAN_STACK
    115    zeromem(buf, ECC_MAXSIZE);
    116 #endif
    117    XFREE(buf);
    118    return err;
    119 }
    120 
    121 #endif
    122 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_make_key.c,v $ */
    123 /* $Revision: 1.9 $ */
    124 /* $Date: 2006/12/04 02:50:11 $ */
    125 
    126