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 /* decrypt then PKCS #1 v1.5 depad */ |
|
17 int rsa_v15_decrypt_key(const unsigned char *in, unsigned long inlen, |
|
18 unsigned char *outkey, unsigned long keylen, |
|
19 prng_state *prng, int prng_idx, |
|
20 int *res, rsa_key *key) |
|
21 { |
|
22 unsigned long modulus_bitlen, modulus_bytelen, x; |
|
23 int err; |
|
24 unsigned char *tmp; |
|
25 |
|
26 _ARGCHK(outkey != NULL); |
|
27 _ARGCHK(key != NULL); |
|
28 _ARGCHK(res != NULL); |
|
29 |
|
30 /* default to invalid */ |
|
31 *res = 0; |
|
32 |
|
33 /* valid prng ? */ |
|
34 if ((err = prng_is_valid(prng_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 != inlen) { |
|
44 return CRYPT_INVALID_PACKET; |
|
45 } |
|
46 |
|
47 /* allocate ram */ |
|
48 tmp = XMALLOC(inlen); |
|
49 if (tmp == NULL) { |
|
50 return CRYPT_MEM; |
|
51 } |
|
52 |
|
53 /* rsa decode the packet */ |
|
54 x = inlen; |
|
55 if ((err = rsa_exptmod(in, inlen, tmp, &x, PK_PRIVATE, prng, prng_idx, key)) != CRYPT_OK) { |
|
56 XFREE(tmp); |
|
57 return err; |
|
58 } |
|
59 |
|
60 /* PKCS #1 v1.5 depad */ |
|
61 err = pkcs_1_v15_es_decode(tmp, x, modulus_bitlen, outkey, keylen, res); |
|
62 XFREE(tmp); |
|
63 return err; |
|
64 } |
|
65 |
|
66 #endif |