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 |
143
|
16 /* (PKCS #1 v2.0) decrypt then OAEP depad */ |
15
|
17 int rsa_decrypt_key(const unsigned char *in, unsigned long inlen, |
|
18 unsigned char *outkey, unsigned long *keylen, |
|
19 const unsigned char *lparam, unsigned long lparamlen, |
|
20 prng_state *prng, int prng_idx, |
|
21 int hash_idx, int *res, |
|
22 rsa_key *key) |
|
23 { |
|
24 unsigned long modulus_bitlen, modulus_bytelen, x; |
|
25 int err; |
143
|
26 unsigned char *tmp; |
15
|
27 |
|
28 _ARGCHK(outkey != NULL); |
|
29 _ARGCHK(keylen != NULL); |
|
30 _ARGCHK(key != NULL); |
|
31 _ARGCHK(res != NULL); |
143
|
32 |
|
33 /* default to invalid */ |
|
34 *res = 0; |
|
35 |
|
36 /* valid hash/prng ? */ |
|
37 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { |
|
38 return err; |
|
39 } |
15
|
40 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { |
|
41 return err; |
|
42 } |
|
43 |
|
44 /* get modulus len in bits */ |
|
45 modulus_bitlen = mp_count_bits(&(key->N)); |
|
46 |
|
47 /* outlen must be at least the size of the modulus */ |
|
48 modulus_bytelen = mp_unsigned_bin_size(&(key->N)); |
|
49 if (modulus_bytelen != inlen) { |
|
50 return CRYPT_INVALID_PACKET; |
|
51 } |
|
52 |
143
|
53 /* allocate ram */ |
|
54 tmp = XMALLOC(inlen); |
|
55 if (tmp == NULL) { |
|
56 return CRYPT_MEM; |
|
57 } |
|
58 |
15
|
59 /* rsa decode the packet */ |
143
|
60 x = inlen; |
|
61 if ((err = rsa_exptmod(in, inlen, tmp, &x, PK_PRIVATE, prng, prng_idx, key)) != CRYPT_OK) { |
|
62 XFREE(tmp); |
15
|
63 return err; |
|
64 } |
|
65 |
|
66 /* now OAEP decode the packet */ |
143
|
67 err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx, |
|
68 outkey, keylen, res); |
|
69 XFREE(tmp); |
|
70 return err; |
15
|
71 } |
|
72 |
|
73 #endif /* MRSA */ |
|
74 |
|
75 |
|
76 |
|
77 |