15
|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
|
2 * |
|
3 * LibTomCrypt is a library that provides various cryptographic |
|
4 * algorithms in a highly modular and flexible manner. |
|
5 * |
|
6 * The library is free for all purposes without any express |
|
7 * guarantee it works. |
|
8 * |
|
9 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 |
|
12 #include "mycrypt.h" |
|
13 |
|
14 #ifdef MRSA |
|
15 |
|
16 /* PSS pad then sign */ |
|
17 int rsa_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, unsigned long saltlen, |
|
21 rsa_key *key) |
|
22 { |
|
23 unsigned long modulus_bitlen, modulus_bytelen, x; |
|
24 int err; |
|
25 |
|
26 _ARGCHK(msghash != NULL); |
|
27 _ARGCHK(sig != NULL); |
|
28 _ARGCHK(siglen != NULL); |
|
29 _ARGCHK(key != NULL); |
|
30 |
|
31 /* valid prng and hash ? */ |
|
32 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { |
|
33 return err; |
|
34 } |
|
35 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { |
|
36 return err; |
|
37 } |
|
38 |
|
39 /* get modulus len in bits */ |
|
40 modulus_bitlen = mp_count_bits(&(key->N)); |
|
41 |
|
42 /* outlen must be at least the size of the modulus */ |
|
43 modulus_bytelen = mp_unsigned_bin_size(&(key->N)); |
|
44 if (modulus_bytelen > *siglen) { |
|
45 return CRYPT_BUFFER_OVERFLOW; |
|
46 } |
|
47 |
|
48 /* PSS pad the key */ |
|
49 x = *siglen; |
|
50 if ((err = pkcs_1_pss_encode(msghash, msghashlen, saltlen, prng, prng_idx, |
|
51 hash_idx, modulus_bitlen, sig, &x)) != CRYPT_OK) { |
|
52 return err; |
|
53 } |
|
54 |
|
55 /* RSA encode it */ |
|
56 return rsa_exptmod(sig, x, sig, siglen, PK_PRIVATE, prng, prng_idx, key); |
|
57 } |
|
58 |
|
59 #endif /* MRSA */ |