Path: blob/master/libs/tomcrypt/src/pk/dsa/dsa_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*/8#include "tomcrypt.h"910/**11@file dsa_sign_hash.c12DSA implementation, sign a hash, Tom St Denis13*/1415#ifdef LTC_MDSA1617/**18Sign a hash with DSA19@param in The hash to sign20@param inlen The length of the hash to sign21@param r The "r" integer of the signature (caller must initialize with mp_init() first)22@param s The "s" integer of the signature (caller must initialize with mp_init() first)23@param prng An active PRNG state24@param wprng The index of the PRNG desired25@param key A private DSA key26@return CRYPT_OK if successful27*/28int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,29void *r, void *s,30prng_state *prng, int wprng, dsa_key *key)31{32void *k, *kinv, *tmp;33unsigned char *buf;34int err, qbits;3536LTC_ARGCHK(in != NULL);37LTC_ARGCHK(r != NULL);38LTC_ARGCHK(s != NULL);39LTC_ARGCHK(key != NULL);4041if ((err = prng_is_valid(wprng)) != CRYPT_OK) {42return err;43}44if (key->type != PK_PRIVATE) {45return CRYPT_PK_NOT_PRIVATE;46}4748/* check group order size */49if (key->qord >= LTC_MDSA_MAX_GROUP) {50return CRYPT_INVALID_ARG;51}5253buf = XMALLOC(LTC_MDSA_MAX_GROUP);54if (buf == NULL) {55return CRYPT_MEM;56}5758/* Init our temps */59if ((err = mp_init_multi(&k, &kinv, &tmp, NULL)) != CRYPT_OK) { goto ERRBUF; }6061qbits = mp_count_bits(key->q);62retry:6364do {65/* gen random k */66if ((err = rand_bn_bits(k, qbits, prng, wprng)) != CRYPT_OK) { goto error; }6768/* k should be from range: 1 <= k <= q-1 (see FIPS 186-4 B.2.2) */69if (mp_cmp_d(k, 0) != LTC_MP_GT || mp_cmp(k, key->q) != LTC_MP_LT) { goto retry; }7071/* test gcd */72if ((err = mp_gcd(k, key->q, tmp)) != CRYPT_OK) { goto error; }73} while (mp_cmp_d(tmp, 1) != LTC_MP_EQ);7475/* now find 1/k mod q */76if ((err = mp_invmod(k, key->q, kinv)) != CRYPT_OK) { goto error; }7778/* now find r = g^k mod p mod q */79if ((err = mp_exptmod(key->g, k, key->p, r)) != CRYPT_OK) { goto error; }80if ((err = mp_mod(r, key->q, r)) != CRYPT_OK) { goto error; }8182if (mp_iszero(r) == LTC_MP_YES) { goto retry; }8384/* FIPS 186-4 4.6: use leftmost min(bitlen(q), bitlen(hash)) bits of 'hash'*/85inlen = MIN(inlen, (unsigned long)(key->qord));8687/* now find s = (in + xr)/k mod q */88if ((err = mp_read_unsigned_bin(tmp, (unsigned char *)in, inlen)) != CRYPT_OK) { goto error; }89if ((err = mp_mul(key->x, r, s)) != CRYPT_OK) { goto error; }90if ((err = mp_add(s, tmp, s)) != CRYPT_OK) { goto error; }91if ((err = mp_mulmod(s, kinv, key->q, s)) != CRYPT_OK) { goto error; }9293if (mp_iszero(s) == LTC_MP_YES) { goto retry; }9495err = CRYPT_OK;96error:97mp_clear_multi(k, kinv, tmp, NULL);98ERRBUF:99#ifdef LTC_CLEAN_STACK100zeromem(buf, LTC_MDSA_MAX_GROUP);101#endif102XFREE(buf);103return err;104}105106/**107Sign a hash with DSA108@param in The hash to sign109@param inlen The length of the hash to sign110@param out [out] Where to store the signature111@param outlen [in/out] The max size and resulting size of the signature112@param prng An active PRNG state113@param wprng The index of the PRNG desired114@param key A private DSA key115@return CRYPT_OK if successful116*/117int dsa_sign_hash(const unsigned char *in, unsigned long inlen,118unsigned char *out, unsigned long *outlen,119prng_state *prng, int wprng, dsa_key *key)120{121void *r, *s;122int err;123124LTC_ARGCHK(in != NULL);125LTC_ARGCHK(out != NULL);126LTC_ARGCHK(outlen != NULL);127LTC_ARGCHK(key != NULL);128129if (mp_init_multi(&r, &s, NULL) != CRYPT_OK) {130return CRYPT_MEM;131}132133if ((err = dsa_sign_hash_raw(in, inlen, r, s, prng, wprng, key)) != CRYPT_OK) {134goto error;135}136137err = der_encode_sequence_multi(out, outlen,138LTC_ASN1_INTEGER, 1UL, r,139LTC_ASN1_INTEGER, 1UL, s,140LTC_ASN1_EOL, 0UL, NULL);141142error:143mp_clear_multi(r, s, NULL);144return err;145}146147#endif148149150