6
6
* The library is free for all purposes without any express
7
7
* guarantee it works.
9
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
9
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
11
11
#include "tomcrypt.h"
14
14
@file rsa_decrypt_key.c
15
RSA PKCS #1 OAEP Decryption, Tom St Denis
15
RSA PKCS #1 Decryption, Tom St Denis and Andreas Lange
21
(PKCS #1 v2.0) decrypt then OAEP depad
21
PKCS #1 decrypt then v1.5 or OAEP depad
22
22
@param in The ciphertext
23
23
@param inlen The length of the ciphertext (octets)
24
24
@param out [out] The plaintext
26
26
@param lparam The system "lparam" value
27
27
@param lparamlen The length of the lparam value (octets)
28
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)
29
30
@param stat [out] Result of the decryption, 1==valid, 0==invalid
30
31
@param key The corresponding private RSA key
31
32
@return CRYPT_OK if succcessul (even if invalid)
33
int rsa_decrypt_key(const unsigned char *in, unsigned long inlen,
34
unsigned char *out, unsigned long *outlen,
35
const unsigned char *lparam, unsigned long lparamlen,
36
int hash_idx, int *stat,
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)
39
40
unsigned long modulus_bitlen, modulus_bytelen, x;
41
42
unsigned char *tmp;
43
44
LTC_ARGCHK(out != NULL);
44
45
LTC_ARGCHK(outlen != NULL);
45
46
LTC_ARGCHK(key != NULL);
48
49
/* default to invalid */
52
if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
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) {
56
66
/* get modulus len in bits */
57
modulus_bitlen = mp_count_bits(&(key->N));
67
modulus_bitlen = mp_count_bits( (key->N));
59
69
/* outlen must be at least the size of the modulus */
60
modulus_bytelen = mp_unsigned_bin_size(&(key->N));
70
modulus_bytelen = mp_unsigned_bin_size( (key->N));
61
71
if (modulus_bytelen != inlen) {
62
72
return CRYPT_INVALID_PACKET;
71
81
/* rsa decode the packet */
73
if ((err = rsa_exptmod(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) {
83
if ((err = ltc_mp.rsa_me(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) {
78
/* now OAEP decode the packet */
79
err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,
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);
91
103
/* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_decrypt_key.c,v $ */
92
/* $Revision: 1.3 $ */
93
/* $Date: 2005/05/05 14:35:59 $ */
104
/* $Revision: 1.8 $ */
105
/* $Date: 2006/11/01 09:18:22 $ */