Path: blob/master/libs/tomcrypt/src/pk/ecc/ecc_sign_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_sign_hash.c15ECC Crypto, Tom St Denis16*/1718static int _ecc_sign_hash(const unsigned char *in, unsigned long inlen,19unsigned char *out, unsigned long *outlen,20prng_state *prng, int wprng, ecc_key *key, int sigformat)21{22ecc_key pubkey;23void *r, *s, *e, *p, *b;24int err, max_iterations = LTC_PK_MAX_RETRIES;25unsigned long pbits, pbytes, i, shift_right;26unsigned char ch, buf[MAXBLOCKSIZE];2728LTC_ARGCHK(in != NULL);29LTC_ARGCHK(out != NULL);30LTC_ARGCHK(outlen != NULL);31LTC_ARGCHK(key != NULL);3233/* is this a private key? */34if (key->type != PK_PRIVATE) {35return CRYPT_PK_NOT_PRIVATE;36}3738/* is the IDX valid ? */39if (ltc_ecc_is_valid_idx(key->idx) != 1) {40return CRYPT_PK_INVALID_TYPE;41}4243if ((err = prng_is_valid(wprng)) != CRYPT_OK) {44return err;45}4647/* init the bignums */48if ((err = mp_init_multi(&r, &s, &p, &e, &b, NULL)) != CRYPT_OK) {49return err;50}51if ((err = mp_read_radix(p, (char *)key->dp->order, 16)) != CRYPT_OK) { goto errnokey; }5253/* get the hash and load it as a bignum into 'e' */54pbits = mp_count_bits(p);55pbytes = (pbits+7) >> 3;56if (pbits > inlen*8) {57if ((err = mp_read_unsigned_bin(e, (unsigned char *)in, inlen)) != CRYPT_OK) { goto errnokey; }58}59else if (pbits % 8 == 0) {60if ((err = mp_read_unsigned_bin(e, (unsigned char *)in, pbytes)) != CRYPT_OK) { goto errnokey; }61}62else {63shift_right = 8 - pbits % 8;64for (i=0, ch=0; i<pbytes; i++) {65buf[i] = ch;66ch = (in[i] << (8-shift_right));67buf[i] = buf[i] ^ (in[i] >> shift_right);68}69if ((err = mp_read_unsigned_bin(e, (unsigned char *)buf, pbytes)) != CRYPT_OK) { goto errnokey; }70}7172/* make up a key and export the public copy */73do {74if ((err = ecc_make_key_ex(prng, wprng, &pubkey, key->dp)) != CRYPT_OK) {75goto errnokey;76}7778/* find r = x1 mod n */79if ((err = mp_mod(pubkey.pubkey.x, p, r)) != CRYPT_OK) { goto error; }8081if (mp_iszero(r) == LTC_MP_YES) {82ecc_free(&pubkey);83} else {84if ((err = rand_bn_upto(b, p, prng, wprng)) != CRYPT_OK) { goto error; } /* b = blinding value */85/* find s = (e + xr)/k */86if ((err = mp_mulmod(pubkey.k, b, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = kb */87if ((err = mp_invmod(pubkey.k, p, pubkey.k)) != CRYPT_OK) { goto error; } /* k = 1/kb */88if ((err = mp_mulmod(key->k, r, p, s)) != CRYPT_OK) { goto error; } /* s = xr */89if ((err = mp_mulmod(pubkey.k, s, p, s)) != CRYPT_OK) { goto error; } /* s = xr/kb */90if ((err = mp_mulmod(pubkey.k, e, p, e)) != CRYPT_OK) { goto error; } /* e = e/kb */91if ((err = mp_add(e, s, s)) != CRYPT_OK) { goto error; } /* s = e/kb + xr/kb */92if ((err = mp_mulmod(s, b, p, s)) != CRYPT_OK) { goto error; } /* s = b(e/kb + xr/kb) = (e + xr)/k */93ecc_free(&pubkey);94if (mp_iszero(s) == LTC_MP_NO) {95break;96}97}98} while (--max_iterations > 0);99100if (max_iterations == 0) {101goto errnokey;102}103104if (sigformat == 1) {105/* RFC7518 format */106if (*outlen < 2*pbytes) { err = CRYPT_MEM; goto errnokey; }107zeromem(out, 2*pbytes);108i = mp_unsigned_bin_size(r);109if ((err = mp_to_unsigned_bin(r, out + (pbytes - i))) != CRYPT_OK) { goto errnokey; }110i = mp_unsigned_bin_size(s);111if ((err = mp_to_unsigned_bin(s, out + (2*pbytes - i))) != CRYPT_OK) { goto errnokey; }112*outlen = 2*pbytes;113err = CRYPT_OK;114}115else {116/* store as ASN.1 SEQUENCE { r, s -- integer } */117err = der_encode_sequence_multi(out, outlen,118LTC_ASN1_INTEGER, 1UL, r,119LTC_ASN1_INTEGER, 1UL, s,120LTC_ASN1_EOL, 0UL, NULL);121}122goto errnokey;123error:124ecc_free(&pubkey);125errnokey:126mp_clear_multi(r, s, p, e, b, NULL);127return err;128}129130/**131Sign a message digest132@param in The message digest to sign133@param inlen The length of the digest134@param out [out] The destination for the signature135@param outlen [in/out] The max size and resulting size of the signature136@param prng An active PRNG state137@param wprng The index of the PRNG you wish to use138@param key A private ECC key139@return CRYPT_OK if successful140*/141int ecc_sign_hash(const unsigned char *in, unsigned long inlen,142unsigned char *out, unsigned long *outlen,143prng_state *prng, int wprng, ecc_key *key)144{145return _ecc_sign_hash(in, inlen, out, outlen, prng, wprng, key, 0);146}147148/**149Sign a message digest in RFC7518 format150@param in The message digest to sign151@param inlen The length of the digest152@param out [out] The destination for the signature153@param outlen [in/out] The max size and resulting size of the signature154@param prng An active PRNG state155@param wprng The index of the PRNG you wish to use156@param key A private ECC key157@return CRYPT_OK if successful158*/159int ecc_sign_hash_rfc7518(const unsigned char *in, unsigned long inlen,160unsigned char *out, unsigned long *outlen,161prng_state *prng, int wprng, ecc_key *key)162{163return _ecc_sign_hash(in, inlen, out, outlen, prng, wprng, key, 1);164}165166#endif167168169