Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/tomcrypt/src/pk/rsa/rsa_decrypt_key.c
4396 views
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
#include "tomcrypt.h"
10
11
/**
12
@file rsa_decrypt_key.c
13
RSA PKCS #1 Decryption, Tom St Denis and Andreas Lange
14
*/
15
16
#ifdef LTC_MRSA
17
18
/**
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)
31
*/
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)
37
{
38
unsigned long modulus_bitlen, modulus_bytelen, x;
39
int err;
40
unsigned char *tmp;
41
42
LTC_ARGCHK(out != NULL);
43
LTC_ARGCHK(outlen != NULL);
44
LTC_ARGCHK(key != NULL);
45
LTC_ARGCHK(stat != NULL);
46
47
/* default to invalid */
48
*stat = 0;
49
50
/* valid padding? */
51
52
if ((padding != LTC_PKCS_1_V1_5) &&
53
(padding != LTC_PKCS_1_OAEP)) {
54
return CRYPT_PK_INVALID_PADDING;
55
}
56
57
if (padding == LTC_PKCS_1_OAEP) {
58
/* valid hash ? */
59
if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
60
return err;
61
}
62
}
63
64
/* get modulus len in bits */
65
modulus_bitlen = mp_count_bits( (key->N));
66
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;
71
}
72
73
/* allocate ram */
74
tmp = XMALLOC(inlen);
75
if (tmp == NULL) {
76
return CRYPT_MEM;
77
}
78
79
/* rsa decode the packet */
80
x = inlen;
81
if ((err = ltc_mp.rsa_me(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) {
82
XFREE(tmp);
83
return err;
84
}
85
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,
89
out, outlen, stat);
90
} else {
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);
93
}
94
95
XFREE(tmp);
96
return err;
97
}
98
99
#endif /* LTC_MRSA */
100
101