Path: blob/master/libs/tomcrypt/src/pk/ecc/ecc_import.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_import.c18ECC Crypto, Tom St Denis19*/2021#ifdef LTC_MECC2223static int _is_point(ecc_key *key)24{25void *prime, *b, *t1, *t2;26int err;2728if ((err = mp_init_multi(&prime, &b, &t1, &t2, NULL)) != CRYPT_OK) {29return err;30}3132/* load prime and b */33if ((err = mp_read_radix(prime, key->dp->prime, 16)) != CRYPT_OK) { goto error; }34if ((err = mp_read_radix(b, key->dp->B, 16)) != CRYPT_OK) { goto error; }3536/* compute y^2 */37if ((err = mp_sqr(key->pubkey.y, t1)) != CRYPT_OK) { goto error; }3839/* compute x^3 */40if ((err = mp_sqr(key->pubkey.x, t2)) != CRYPT_OK) { goto error; }41if ((err = mp_mod(t2, prime, t2)) != CRYPT_OK) { goto error; }42if ((err = mp_mul(key->pubkey.x, t2, t2)) != CRYPT_OK) { goto error; }4344/* compute y^2 - x^3 */45if ((err = mp_sub(t1, t2, t1)) != CRYPT_OK) { goto error; }4647/* compute y^2 - x^3 + 3x */48if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }49if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }50if ((err = mp_add(t1, key->pubkey.x, t1)) != CRYPT_OK) { goto error; }51if ((err = mp_mod(t1, prime, t1)) != CRYPT_OK) { goto error; }52while (mp_cmp_d(t1, 0) == LTC_MP_LT) {53if ((err = mp_add(t1, prime, t1)) != CRYPT_OK) { goto error; }54}55while (mp_cmp(t1, prime) != LTC_MP_LT) {56if ((err = mp_sub(t1, prime, t1)) != CRYPT_OK) { goto error; }57}5859/* compare to b */60if (mp_cmp(t1, b) != LTC_MP_EQ) {61err = CRYPT_INVALID_PACKET;62} else {63err = CRYPT_OK;64}6566error:67mp_clear_multi(prime, b, t1, t2, NULL);68return err;69}7071/**72Import an ECC key from a binary packet73@param in The packet to import74@param inlen The length of the packet75@param key [out] The destination of the import76@return CRYPT_OK if successful, upon error all allocated memory will be freed77*/78int ecc_import(const unsigned char *in, unsigned long inlen, ecc_key *key)79{80return ecc_import_ex(in, inlen, key, NULL);81}8283/**84Import an ECC key from a binary packet, using user supplied domain params rather than one of the NIST ones85@param in The packet to import86@param inlen The length of the packet87@param key [out] The destination of the import88@param dp pointer to user supplied params; must be the same as the params used when exporting89@return CRYPT_OK if successful, upon error all allocated memory will be freed90*/91int ecc_import_ex(const unsigned char *in, unsigned long inlen, ecc_key *key, const ltc_ecc_set_type *dp)92{93unsigned long key_size;94unsigned char flags[1];95int err;9697LTC_ARGCHK(in != NULL);98LTC_ARGCHK(key != NULL);99LTC_ARGCHK(ltc_mp.name != NULL);100101/* init key */102if (mp_init_multi(&key->pubkey.x, &key->pubkey.y, &key->pubkey.z, &key->k, NULL) != CRYPT_OK) {103return CRYPT_MEM;104}105106/* find out what type of key it is */107err = der_decode_sequence_multi(in, inlen, LTC_ASN1_BIT_STRING, 1UL, flags,108LTC_ASN1_EOL, 0UL, NULL);109if (err != CRYPT_OK && err != CRYPT_INPUT_TOO_LONG) {110goto done;111}112113114if (flags[0] == 1) {115/* private key */116key->type = PK_PRIVATE;117if ((err = der_decode_sequence_multi(in, inlen,118LTC_ASN1_BIT_STRING, 1UL, flags,119LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,120LTC_ASN1_INTEGER, 1UL, key->pubkey.x,121LTC_ASN1_INTEGER, 1UL, key->pubkey.y,122LTC_ASN1_INTEGER, 1UL, key->k,123LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {124goto done;125}126} else if (flags[0] == 0) {127/* public key */128key->type = PK_PUBLIC;129if ((err = der_decode_sequence_multi(in, inlen,130LTC_ASN1_BIT_STRING, 1UL, flags,131LTC_ASN1_SHORT_INTEGER, 1UL, &key_size,132LTC_ASN1_INTEGER, 1UL, key->pubkey.x,133LTC_ASN1_INTEGER, 1UL, key->pubkey.y,134LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {135goto done;136}137}138else {139err = CRYPT_INVALID_PACKET;140goto done;141}142143if (dp == NULL) {144/* find the idx */145for (key->idx = 0; ltc_ecc_sets[key->idx].size && (unsigned long)ltc_ecc_sets[key->idx].size != key_size; ++key->idx);146if (ltc_ecc_sets[key->idx].size == 0) {147err = CRYPT_INVALID_PACKET;148goto done;149}150key->dp = <c_ecc_sets[key->idx];151} else {152key->idx = -1;153key->dp = dp;154}155/* set z */156if ((err = mp_set(key->pubkey.z, 1)) != CRYPT_OK) { goto done; }157158/* is it a point on the curve? */159if ((err = _is_point(key)) != CRYPT_OK) {160goto done;161}162163/* we're good */164return CRYPT_OK;165done:166mp_clear_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z, key->k, NULL);167return err;168}169#endif170171172