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
9 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
14 @file rsa_decrypt_key.c
15 RSA PKCS #1 Decryption, Tom St Denis and Andreas Lange
21 PKCS #1 decrypt then v1.5 or OAEP depad
22 @param in The ciphertext
23 @param inlen The length of the ciphertext (octets)
24 @param out [out] The plaintext
25 @param outlen [in/out] The max size and resulting size of the plaintext (octets)
26 @param lparam The system "lparam" value
27 @param lparamlen The length of the lparam value (octets)
28 @param hash_idx The index of the hash desired
29 @param padding Type of padding (LTC_PKCS_1_OAEP or LTC_PKCS_1_V1_5)
30 @param stat [out] Result of the decryption, 1==valid, 0==invalid
31 @param key The corresponding private RSA key
32 @return CRYPT_OK if succcessul (even if invalid)
34 int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen,
35 unsigned char *out, unsigned long *outlen,
36 const unsigned char *lparam, unsigned long lparamlen,
37 int hash_idx, int padding,
38 int *stat, rsa_key *key)
40 unsigned long modulus_bitlen, modulus_bytelen, x;
44 LTC_ARGCHK(out != NULL);
45 LTC_ARGCHK(outlen != NULL);
46 LTC_ARGCHK(key != NULL);
47 LTC_ARGCHK(stat != NULL);
49 /* default to invalid */
54 if ((padding != LTC_PKCS_1_V1_5) &&
55 (padding != LTC_PKCS_1_OAEP)) {
56 return CRYPT_PK_INVALID_PADDING;
59 if (padding == LTC_PKCS_1_OAEP) {
61 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
66 /* get modulus len in bits */
67 modulus_bitlen = mp_count_bits( (key->N));
69 /* outlen must be at least the size of the modulus */
70 modulus_bytelen = mp_unsigned_bin_size( (key->N));
71 if (modulus_bytelen != inlen) {
72 return CRYPT_INVALID_PACKET;
81 /* rsa decode the packet */
83 if ((err = ltc_mp.rsa_me(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) {
88 if (padding == LTC_PKCS_1_OAEP) {
89 /* now OAEP decode the packet */
90 err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,
93 /* now PKCS #1 v1.5 depad the packet */
94 err = pkcs_1_v1_5_decode(tmp, x, LTC_PKCS_1_EME, modulus_bitlen, out, outlen, stat);
103 /* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_decrypt_key.c,v $ */
104 /* $Revision: 1.8 $ */
105 /* $Date: 2006/11/01 09:18:22 $ */