Path: blob/master/libs/tomcrypt/src/pk/ecc/ecc_make_key.c
4396 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/89/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b10*11* All curves taken from NIST recommendation paper of July 199912* Available at http://csrc.nist.gov/cryptval/dss.htm13*/14#include "tomcrypt.h"1516/**17@file ecc_make_key.c18ECC Crypto, Tom St Denis19*/2021#ifdef LTC_MECC2223/**24Make a new ECC key25@param prng An active PRNG state26@param wprng The index of the PRNG you wish to use27@param keysize The keysize for the new key (in octets from 20 to 65 bytes)28@param key [out] Destination of the newly created key29@return CRYPT_OK if successful, upon error all allocated memory will be freed30*/31int ecc_make_key(prng_state *prng, int wprng, int keysize, ecc_key *key)32{33int x, err;3435/* find key size */36for (x = 0; (keysize > ltc_ecc_sets[x].size) && (ltc_ecc_sets[x].size != 0); x++);37keysize = ltc_ecc_sets[x].size;3839if (keysize > ECC_MAXSIZE || ltc_ecc_sets[x].size == 0) {40return CRYPT_INVALID_KEYSIZE;41}42err = ecc_make_key_ex(prng, wprng, key, <c_ecc_sets[x]);43key->idx = x;44return err;45}4647int ecc_make_key_ex(prng_state *prng, int wprng, ecc_key *key, const ltc_ecc_set_type *dp)48{49int err;50ecc_point *base;51void *prime, *order;52unsigned char *buf;53int keysize;5455LTC_ARGCHK(key != NULL);56LTC_ARGCHK(ltc_mp.name != NULL);57LTC_ARGCHK(dp != NULL);5859/* good prng? */60if ((err = prng_is_valid(wprng)) != CRYPT_OK) {61return err;62}6364key->idx = -1;65key->dp = dp;66keysize = dp->size;6768/* allocate ram */69base = NULL;70buf = XMALLOC(ECC_MAXSIZE);71if (buf == NULL) {72return CRYPT_MEM;73}7475/* make up random string */76if (prng_descriptor[wprng].read(buf, (unsigned long)keysize, prng) != (unsigned long)keysize) {77err = CRYPT_ERROR_READPRNG;78goto ERR_BUF;79}8081/* setup the key variables */82if ((err = mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, &prime, &order, NULL)) != CRYPT_OK) {83goto ERR_BUF;84}85base = ltc_ecc_new_point();86if (base == NULL) {87err = CRYPT_MEM;88goto errkey;89}9091/* read in the specs for this key */92if ((err = mp_read_radix(prime, (char *)key->dp->prime, 16)) != CRYPT_OK) { goto errkey; }93if ((err = mp_read_radix(order, (char *)key->dp->order, 16)) != CRYPT_OK) { goto errkey; }94if ((err = mp_read_radix(base->x, (char *)key->dp->Gx, 16)) != CRYPT_OK) { goto errkey; }95if ((err = mp_read_radix(base->y, (char *)key->dp->Gy, 16)) != CRYPT_OK) { goto errkey; }96if ((err = mp_set(base->z, 1)) != CRYPT_OK) { goto errkey; }97if ((err = mp_read_unsigned_bin(key->k, (unsigned char *)buf, keysize)) != CRYPT_OK) { goto errkey; }9899/* the key should be smaller than the order of base point */100if (mp_cmp(key->k, order) != LTC_MP_LT) {101if((err = mp_mod(key->k, order, key->k)) != CRYPT_OK) { goto errkey; }102}103/* make the public key */104if ((err = ltc_mp.ecc_ptmul(key->k, base, &key->pubkey, prime, 1)) != CRYPT_OK) { goto errkey; }105key->type = PK_PRIVATE;106107/* free up ram */108err = CRYPT_OK;109goto cleanup;110errkey:111mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);112cleanup:113ltc_ecc_del_point(base);114mp_clear_multi(prime, order, NULL);115ERR_BUF:116#ifdef LTC_CLEAN_STACK117zeromem(buf, ECC_MAXSIZE);118#endif119XFREE(buf);120return err;121}122123#endif124125126