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