1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4
5 /**
6 @file rsa_decrypt_key.c
7 RSA PKCS #1 Decryption, Tom St Denis and Andreas Lange
8 */
9
10 #ifdef LTC_MRSA
11
12 /**
13 PKCS #1 decrypt then v1.5 or OAEP depad
14 @param in The ciphertext
15 @param inlen The length of the ciphertext (octets)
16 @param out [out] The plaintext
17 @param outlen [in/out] The max size and resulting size of the plaintext (octets)
18 @param lparam The system "lparam" value
19 @param lparamlen The length of the lparam value (octets)
20 @param hash_idx The index of the hash desired
21 @param padding Type of padding (LTC_PKCS_1_OAEP or LTC_PKCS_1_V1_5)
22 @param stat [out] Result of the decryption, 1==valid, 0==invalid
23 @param key The corresponding private RSA key
24 @return CRYPT_OK if succcessul (even if invalid)
25 */
rsa_decrypt_key_ex(const unsigned char * in,unsigned long inlen,unsigned char * out,unsigned long * outlen,const unsigned char * lparam,unsigned long lparamlen,int hash_idx,int padding,int * stat,const rsa_key * key)26 int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen,
27 unsigned char *out, unsigned long *outlen,
28 const unsigned char *lparam, unsigned long lparamlen,
29 int hash_idx, int padding,
30 int *stat, const rsa_key *key)
31 {
32 unsigned long modulus_bitlen, modulus_bytelen, x;
33 int err;
34 unsigned char *tmp;
35
36 LTC_ARGCHK(out != NULL);
37 LTC_ARGCHK(outlen != NULL);
38 LTC_ARGCHK(key != NULL);
39 LTC_ARGCHK(stat != NULL);
40
41 /* default to invalid */
42 *stat = 0;
43
44 /* valid padding? */
45
46 if ((padding != LTC_PKCS_1_V1_5) &&
47 (padding != LTC_PKCS_1_OAEP)) {
48 return CRYPT_PK_INVALID_PADDING;
49 }
50
51 if (padding == LTC_PKCS_1_OAEP) {
52 /* valid hash ? */
53 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
54 return err;
55 }
56 }
57
58 /* get modulus len in bits */
59 modulus_bitlen = mp_count_bits( (key->N));
60
61 /* outlen must be at least the size of the modulus */
62 modulus_bytelen = mp_unsigned_bin_size( (key->N));
63 if (modulus_bytelen != inlen) {
64 return CRYPT_INVALID_PACKET;
65 }
66
67 /* allocate ram */
68 tmp = XMALLOC(inlen);
69 if (tmp == NULL) {
70 return CRYPT_MEM;
71 }
72
73 /* rsa decode the packet */
74 x = inlen;
75 if ((err = ltc_mp.rsa_me(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) {
76 XFREE(tmp);
77 return err;
78 }
79
80 if (padding == LTC_PKCS_1_OAEP) {
81 /* now OAEP decode the packet */
82 err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,
83 out, outlen, stat);
84 } else {
85 /* now PKCS #1 v1.5 depad the packet */
86 err = pkcs_1_v1_5_decode(tmp, x, LTC_PKCS_1_EME, modulus_bitlen, out, outlen, stat);
87 }
88
89 XFREE(tmp);
90 return err;
91 }
92
93 #endif /* LTC_MRSA */
94