143
|
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 /* 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) |
|
21 { |
|
22 unsigned long modulus_bitlen, modulus_bytelen, x; |
|
23 int err; |
|
24 |
|
25 _ARGCHK(msghash != NULL); |
|
26 _ARGCHK(sig != NULL); |
|
27 _ARGCHK(siglen != NULL); |
|
28 _ARGCHK(key != NULL); |
|
29 |
|
30 /* valid prng and hash ? */ |
|
31 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { |
|
32 return err; |
|
33 } |
|
34 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { |
|
35 return err; |
|
36 } |
|
37 |
|
38 /* get modulus len in bits */ |
|
39 modulus_bitlen = mp_count_bits(&(key->N)); |
|
40 |
|
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; |
|
45 } |
|
46 |
|
47 /* PKCS #1 v1.5 pad the key */ |
|
48 x = *siglen; |
|
49 if ((err = pkcs_1_v15_sa_encode(msghash, msghashlen, hash_idx, modulus_bitlen, sig, &x)) != CRYPT_OK) { |
|
50 return err; |
|
51 } |
|
52 |
|
53 /* RSA encode it */ |
|
54 return rsa_exptmod(sig, x, sig, siglen, PK_PRIVATE, prng, prng_idx, key); |
|
55 } |
|
56 |
|
57 #endif |