1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
10 /* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
12 * All curves taken from NIST recommendation paper of July 1999
13 * Available at http://csrc.nist.gov/cryptval/dss.htm
19 ECC Crypto, Tom St Denis
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
32 int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key)
37 for (x = 0; (keysize > ltc_ecc_sets[x].size) && (ltc_ecc_sets[x].size != 0); x++);
38 keysize = ltc_ecc_sets[x].size;
40 if (keysize > ECC_MAXSIZE || ltc_ecc_sets[x].size == 0) {
41 return CRYPT_INVALID_KEYSIZE;
43 err = ecc_make_key_ex(prng, wprng, key, <c_ecc_sets[x]);
48 int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp)
56 LTC_ARGCHK(key != NULL);
57 LTC_ARGCHK(ltc_mp.name != NULL);
58 LTC_ARGCHK(dp != NULL);
61 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
71 buf = XMALLOC(ECC_MAXSIZE);
76 /* make up random string */
77 if (prng_descriptor[wprng].read(buf, (unsigned long)keysize, prng) != (unsigned long)keysize) {
78 err = CRYPT_ERROR_READPRNG;
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) {
86 base = ltc_ecc_new_point();
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; }
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; }
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;
112 mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);
114 ltc_ecc_del_point(base);
115 mp_clear_multi(prime, order, NULL);
117 #ifdef LTC_CLEAN_STACK
118 zeromem(buf, ECC_MAXSIZE);
125 /* ref: $Format:%D$ */
126 /* git commit: $Format:%H$ */
127 /* commit time: $Format:%ai$ */