Path: blob/master/libs/tomcrypt/src/pk/ecc/ecc_verify_hash.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#include "tomcrypt.h"1011#ifdef LTC_MECC1213/**14@file ecc_verify_hash.c15ECC Crypto, Tom St Denis16*/1718static int _ecc_verify_hash(const unsigned char *sig, unsigned long siglen,19const unsigned char *hash, unsigned long hashlen,20int *stat, ecc_key *key, int sigformat)21{22ecc_point *mG, *mQ;23void *r, *s, *v, *w, *u1, *u2, *e, *p, *m;24void *mp;25int err;26unsigned long pbits, pbytes, i, shift_right;27unsigned char ch, buf[MAXBLOCKSIZE];2829LTC_ARGCHK(sig != NULL);30LTC_ARGCHK(hash != NULL);31LTC_ARGCHK(stat != NULL);32LTC_ARGCHK(key != NULL);3334/* default to invalid signature */35*stat = 0;36mp = NULL;3738/* is the IDX valid ? */39if (ltc_ecc_is_valid_idx(key->idx) != 1) {40return CRYPT_PK_INVALID_TYPE;41}4243/* allocate ints */44if ((err = mp_init_multi(&r, &s, &v, &w, &u1, &u2, &p, &e, &m, NULL)) != CRYPT_OK) {45return CRYPT_MEM;46}4748/* allocate points */49mG = ltc_ecc_new_point();50mQ = ltc_ecc_new_point();51if (mQ == NULL || mG == NULL) {52err = CRYPT_MEM;53goto error;54}5556if (sigformat == 1) {57/* RFC7518 format */58if ((siglen % 2) == 1) {59err = CRYPT_INVALID_PACKET;60goto error;61}62i = siglen / 2;63if ((err = mp_read_unsigned_bin(r, (unsigned char *)sig, i)) != CRYPT_OK) { goto error; }64if ((err = mp_read_unsigned_bin(s, (unsigned char *)sig+i, i)) != CRYPT_OK) { goto error; }65}66else {67/* ASN.1 format */68if ((err = der_decode_sequence_multi(sig, siglen,69LTC_ASN1_INTEGER, 1UL, r,70LTC_ASN1_INTEGER, 1UL, s,71LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { goto error; }72}7374/* get the order */75if ((err = mp_read_radix(p, (char *)key->dp->order, 16)) != CRYPT_OK) { goto error; }7677/* get the modulus */78if ((err = mp_read_radix(m, (char *)key->dp->prime, 16)) != CRYPT_OK) { goto error; }7980/* check for zero */81if (mp_iszero(r) || mp_iszero(s) || mp_cmp(r, p) != LTC_MP_LT || mp_cmp(s, p) != LTC_MP_LT) {82err = CRYPT_INVALID_PACKET;83goto error;84}8586/* read hash - truncate if needed */87pbits = mp_count_bits(p);88pbytes = (pbits+7) >> 3;89if (pbits > hashlen*8) {90if ((err = mp_read_unsigned_bin(e, (unsigned char *)hash, hashlen)) != CRYPT_OK) { goto error; }91}92else if (pbits % 8 == 0) {93if ((err = mp_read_unsigned_bin(e, (unsigned char *)hash, pbytes)) != CRYPT_OK) { goto error; }94}95else {96shift_right = 8 - pbits % 8;97for (i=0, ch=0; i<pbytes; i++) {98buf[i] = ch;99ch = (hash[i] << (8-shift_right));100buf[i] = buf[i] ^ (hash[i] >> shift_right);101}102if ((err = mp_read_unsigned_bin(e, (unsigned char *)buf, pbytes)) != CRYPT_OK) { goto error; }103}104105/* w = s^-1 mod n */106if ((err = mp_invmod(s, p, w)) != CRYPT_OK) { goto error; }107108/* u1 = ew */109if ((err = mp_mulmod(e, w, p, u1)) != CRYPT_OK) { goto error; }110111/* u2 = rw */112if ((err = mp_mulmod(r, w, p, u2)) != CRYPT_OK) { goto error; }113114/* find mG and mQ */115if ((err = mp_read_radix(mG->x, (char *)key->dp->Gx, 16)) != CRYPT_OK) { goto error; }116if ((err = mp_read_radix(mG->y, (char *)key->dp->Gy, 16)) != CRYPT_OK) { goto error; }117if ((err = mp_set(mG->z, 1)) != CRYPT_OK) { goto error; }118119if ((err = mp_copy(key->pubkey.x, mQ->x)) != CRYPT_OK) { goto error; }120if ((err = mp_copy(key->pubkey.y, mQ->y)) != CRYPT_OK) { goto error; }121if ((err = mp_copy(key->pubkey.z, mQ->z)) != CRYPT_OK) { goto error; }122123/* compute u1*mG + u2*mQ = mG */124if (ltc_mp.ecc_mul2add == NULL) {125if ((err = ltc_mp.ecc_ptmul(u1, mG, mG, m, 0)) != CRYPT_OK) { goto error; }126if ((err = ltc_mp.ecc_ptmul(u2, mQ, mQ, m, 0)) != CRYPT_OK) { goto error; }127128/* find the montgomery mp */129if ((err = mp_montgomery_setup(m, &mp)) != CRYPT_OK) { goto error; }130131/* add them */132if ((err = ltc_mp.ecc_ptadd(mQ, mG, mG, m, mp)) != CRYPT_OK) { goto error; }133134/* reduce */135if ((err = ltc_mp.ecc_map(mG, m, mp)) != CRYPT_OK) { goto error; }136} else {137/* use Shamir's trick to compute u1*mG + u2*mQ using half of the doubles */138if ((err = ltc_mp.ecc_mul2add(mG, u1, mQ, u2, mG, m)) != CRYPT_OK) { goto error; }139}140141/* v = X_x1 mod n */142if ((err = mp_mod(mG->x, p, v)) != CRYPT_OK) { goto error; }143144/* does v == r */145if (mp_cmp(v, r) == LTC_MP_EQ) {146*stat = 1;147}148149/* clear up and return */150err = CRYPT_OK;151error:152ltc_ecc_del_point(mG);153ltc_ecc_del_point(mQ);154mp_clear_multi(r, s, v, w, u1, u2, p, e, m, NULL);155if (mp != NULL) {156mp_montgomery_free(mp);157}158return err;159}160161/**162Verify an ECC signature163@param sig The signature to verify164@param siglen The length of the signature (octets)165@param hash The hash (message digest) that was signed166@param hashlen The length of the hash (octets)167@param stat Result of signature, 1==valid, 0==invalid168@param key The corresponding public ECC key169@return CRYPT_OK if successful (even if the signature is not valid)170*/171int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,172const unsigned char *hash, unsigned long hashlen,173int *stat, ecc_key *key)174{175return _ecc_verify_hash(sig, siglen, hash, hashlen, stat, key, 0);176}177178/**179Verify an ECC signature in RFC7518 format180@param sig The signature to verify181@param siglen The length of the signature (octets)182@param hash The hash (message digest) that was signed183@param hashlen The length of the hash (octets)184@param stat Result of signature, 1==valid, 0==invalid185@param key The corresponding public ECC key186@return CRYPT_OK if successful (even if the signature is not valid)187*/188int ecc_verify_hash_rfc7518(const unsigned char *sig, unsigned long siglen,189const unsigned char *hash, unsigned long hashlen,190int *stat, ecc_key *key)191{192return _ecc_verify_hash(sig, siglen, hash, hashlen, stat, key, 1);193}194195#endif196197198