1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
16
/* PKCS #1 v1.5 pad then sign */
17
int rsa_v15_sign_hash(const unsigned char *msghash, unsigned long msghashlen,
18
unsigned char *sig, unsigned long *siglen,
19
prng_state *prng, int prng_idx,
20
int hash_idx, rsa_key *key)
22
unsigned long modulus_bitlen, modulus_bytelen, x;
25
_ARGCHK(msghash != NULL);
27
_ARGCHK(siglen != NULL);
30
/* valid prng and hash ? */
31
if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
34
if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
38
/* get modulus len in bits */
39
modulus_bitlen = mp_count_bits(&(key->N));
41
/* outlen must be at least the size of the modulus */
42
modulus_bytelen = mp_unsigned_bin_size(&(key->N));
43
if (modulus_bytelen > *siglen) {
44
return CRYPT_BUFFER_OVERFLOW;
47
/* PKCS #1 v1.5 pad the key */
49
if ((err = pkcs_1_v15_sa_encode(msghash, msghashlen, hash_idx, modulus_bitlen, sig, &x)) != CRYPT_OK) {
54
return rsa_exptmod(sig, x, sig, siglen, PK_PRIVATE, prng, prng_idx, key);