1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 @file rsa_decrypt_key.c
13 RSA PKCS #1 Decryption, Tom St Denis and Andreas Lange
19 PKCS #1 decrypt then v1.5 or OAEP depad
20 @param in The ciphertext
21 @param inlen The length of the ciphertext (octets)
22 @param out [out] The plaintext
23 @param outlen [in/out] The max size and resulting size of the plaintext (octets)
24 @param lparam The system "lparam" value
25 @param lparamlen The length of the lparam value (octets)
26 @param hash_idx The index of the hash desired
27 @param padding Type of padding (LTC_PKCS_1_OAEP or LTC_PKCS_1_V1_5)
28 @param stat [out] Result of the decryption, 1==valid, 0==invalid
29 @param key The corresponding private RSA key
30 @return CRYPT_OK if succcessul (even if invalid)
32 int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen,
33 unsigned char *out, unsigned long *outlen,
34 const unsigned char *lparam, unsigned long lparamlen,
35 int hash_idx, int padding,
36 int *stat, rsa_key *key)
38 unsigned long modulus_bitlen, modulus_bytelen, x;
42 LTC_ARGCHK(out != NULL);
43 LTC_ARGCHK(outlen != NULL);
44 LTC_ARGCHK(key != NULL);
45 LTC_ARGCHK(stat != NULL);
47 /* default to invalid */
52 if ((padding != LTC_PKCS_1_V1_5) &&
53 (padding != LTC_PKCS_1_OAEP)) {
54 return CRYPT_PK_INVALID_PADDING;
57 if (padding == LTC_PKCS_1_OAEP) {
59 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
64 /* get modulus len in bits */
65 modulus_bitlen = mp_count_bits( (key->N));
67 /* outlen must be at least the size of the modulus */
68 modulus_bytelen = mp_unsigned_bin_size( (key->N));
69 if (modulus_bytelen != inlen) {
70 return CRYPT_INVALID_PACKET;
79 /* rsa decode the packet */
81 if ((err = ltc_mp.rsa_me(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) {
86 if (padding == LTC_PKCS_1_OAEP) {
87 /* now OAEP decode the packet */
88 err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,
91 /* now PKCS #1 v1.5 depad the packet */
92 err = pkcs_1_v1_5_decode(tmp, x, LTC_PKCS_1_EME, modulus_bitlen, out, outlen, stat);
101 /* ref: $Format:%D$ */
102 /* git commit: $Format:%H$ */
103 /* commit time: $Format:%ai$ */