comparison libtomcrypt/src/pk/rsa/rsa_decrypt_key.c @ 382:0cbe8f6dbf9e

propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 2af22fb4e878750b88f80f90d439b316d229796f) to branch 'au.asn.ucc.matt.dropbear' (head 02c413252c90e9de8e03d91e9939dde3029f5c0a)
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Jan 2007 02:41:05 +0000
parents 1b9e69c058d2
children f849a5ca2efc
comparison
equal deleted inserted replaced
379:b66a00272a90 382:0cbe8f6dbf9e
4 * algorithms in a highly modular and flexible manner. 4 * algorithms in a highly modular and flexible manner.
5 * 5 *
6 * The library is free for all purposes without any express 6 * The library is free for all purposes without any express
7 * guarantee it works. 7 * guarantee it works.
8 * 8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org 9 * Tom St Denis, [email protected], http://libtomcrypt.com
10 */ 10 */
11 #include "tomcrypt.h" 11 #include "tomcrypt.h"
12 12
13 /** 13 /**
14 @file rsa_decrypt_key.c 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
16 */ 16 */
17 17
18 #ifdef MRSA 18 #ifdef MRSA
19 19
20 /** 20 /**
21 (PKCS #1 v2.0) decrypt then OAEP depad 21 PKCS #1 decrypt then v1.5 or OAEP depad
22 @param in The ciphertext 22 @param in The ciphertext
23 @param inlen The length of the ciphertext (octets) 23 @param inlen The length of the ciphertext (octets)
24 @param out [out] The plaintext 24 @param out [out] The plaintext
25 @param outlen [in/out] The max size and resulting size of the plaintext (octets) 25 @param outlen [in/out] The max size and resulting size of the plaintext (octets)
26 @param lparam The system "lparam" value 26 @param lparam The system "lparam" value
27 @param lparamlen The length of the lparam value (octets) 27 @param lparamlen The length of the lparam value (octets)
28 @param hash_idx The index of the hash desired 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 @param stat [out] Result of the decryption, 1==valid, 0==invalid 30 @param stat [out] Result of the decryption, 1==valid, 0==invalid
30 @param key The corresponding private RSA key 31 @param key The corresponding private RSA key
31 @return CRYPT_OK if succcessul (even if invalid) 32 @return CRYPT_OK if succcessul (even if invalid)
32 */ 33 */
33 int rsa_decrypt_key(const unsigned char *in, unsigned long inlen, 34 int rsa_decrypt_key_ex(const unsigned char *in, unsigned long inlen,
34 unsigned char *out, unsigned long *outlen, 35 unsigned char *out, unsigned long *outlen,
35 const unsigned char *lparam, unsigned long lparamlen, 36 const unsigned char *lparam, unsigned long lparamlen,
36 int hash_idx, int *stat, 37 int hash_idx, int padding,
37 rsa_key *key) 38 int *stat, rsa_key *key)
38 { 39 {
39 unsigned long modulus_bitlen, modulus_bytelen, x; 40 unsigned long modulus_bitlen, modulus_bytelen, x;
40 int err; 41 int err;
41 unsigned char *tmp; 42 unsigned char *tmp;
42 43
43 LTC_ARGCHK(out != NULL); 44 LTC_ARGCHK(out != NULL);
44 LTC_ARGCHK(outlen != NULL); 45 LTC_ARGCHK(outlen != NULL);
45 LTC_ARGCHK(key != NULL); 46 LTC_ARGCHK(key != NULL);
46 LTC_ARGCHK(stat != NULL); 47 LTC_ARGCHK(stat != NULL);
47 48
48 /* default to invalid */ 49 /* default to invalid */
49 *stat = 0; 50 *stat = 0;
50 51
51 /* valid hash ? */ 52 /* valid padding? */
52 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { 53
53 return err; 54 if ((padding != LTC_PKCS_1_V1_5) &&
55 (padding != LTC_PKCS_1_OAEP)) {
56 return CRYPT_PK_INVALID_PADDING;
54 } 57 }
55 58
59 if (padding == LTC_PKCS_1_OAEP) {
60 /* valid hash ? */
61 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
62 return err;
63 }
64 }
65
56 /* get modulus len in bits */ 66 /* get modulus len in bits */
57 modulus_bitlen = mp_count_bits(&(key->N)); 67 modulus_bitlen = mp_count_bits( (key->N));
58 68
59 /* outlen must be at least the size of the modulus */ 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 if (modulus_bytelen != inlen) { 71 if (modulus_bytelen != inlen) {
62 return CRYPT_INVALID_PACKET; 72 return CRYPT_INVALID_PACKET;
63 } 73 }
64 74
65 /* allocate ram */ 75 /* allocate ram */
68 return CRYPT_MEM; 78 return CRYPT_MEM;
69 } 79 }
70 80
71 /* rsa decode the packet */ 81 /* rsa decode the packet */
72 x = inlen; 82 x = inlen;
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) {
74 XFREE(tmp); 84 XFREE(tmp);
75 return err; 85 return err;
76 } 86 }
77 87
78 /* now OAEP decode the packet */ 88 if (padding == LTC_PKCS_1_OAEP) {
79 err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx, 89 /* now OAEP decode the packet */
80 out, outlen, stat); 90 err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,
91 out, outlen, stat);
92 } else {
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);
95 }
96
81 XFREE(tmp); 97 XFREE(tmp);
82 return err; 98 return err;
83 } 99 }
84 100
85 #endif /* MRSA */ 101 #endif /* MRSA */
86 102
87
88
89
90
91 /* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_decrypt_key.c,v $ */ 103 /* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_decrypt_key.c,v $ */
92 /* $Revision: 1.3 $ */ 104 /* $Revision: 1.8 $ */
93 /* $Date: 2005/05/05 14:35:59 $ */ 105 /* $Date: 2006/11/01 09:18:22 $ */