comparison libtomcrypt/src/pk/rsa/rsa_decrypt_key.c @ 297:79bf1023cf11 agent-client

propagate from branch 'au.asn.ucc.matt.dropbear' (head 0501e6f661b5415eb76f3b312d183c3adfbfb712) to branch 'au.asn.ucc.matt.dropbear.cli-agent' (head 01038174ec27245b51bd43a66c01ad930880f67b)
author Matt Johnston <matt@ucc.asn.au>
date Tue, 21 Mar 2006 16:20:59 +0000
parents 1b9e69c058d2
children 0cbe8f6dbf9e
comparison
equal deleted inserted replaced
225:ca7e76d981d9 297:79bf1023cf11
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 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11 #include "tomcrypt.h"
12
13 /**
14 @file rsa_decrypt_key.c
15 RSA PKCS #1 OAEP Decryption, Tom St Denis
16 */
17
18 #ifdef MRSA
19
20 /**
21 (PKCS #1 v2.0) decrypt then 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 stat [out] Result of the decryption, 1==valid, 0==invalid
30 @param key The corresponding private RSA key
31 @return CRYPT_OK if succcessul (even if invalid)
32 */
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,
37 rsa_key *key)
38 {
39 unsigned long modulus_bitlen, modulus_bytelen, x;
40 int err;
41 unsigned char *tmp;
42
43 LTC_ARGCHK(out != NULL);
44 LTC_ARGCHK(outlen != NULL);
45 LTC_ARGCHK(key != NULL);
46 LTC_ARGCHK(stat != NULL);
47
48 /* default to invalid */
49 *stat = 0;
50
51 /* valid hash ? */
52 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
53 return err;
54 }
55
56 /* get modulus len in bits */
57 modulus_bitlen = mp_count_bits(&(key->N));
58
59 /* outlen must be at least the size of the modulus */
60 modulus_bytelen = mp_unsigned_bin_size(&(key->N));
61 if (modulus_bytelen != inlen) {
62 return CRYPT_INVALID_PACKET;
63 }
64
65 /* allocate ram */
66 tmp = XMALLOC(inlen);
67 if (tmp == NULL) {
68 return CRYPT_MEM;
69 }
70
71 /* rsa decode the packet */
72 x = inlen;
73 if ((err = rsa_exptmod(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) {
74 XFREE(tmp);
75 return err;
76 }
77
78 /* now OAEP decode the packet */
79 err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,
80 out, outlen, stat);
81 XFREE(tmp);
82 return err;
83 }
84
85 #endif /* MRSA */
86
87
88
89
90
91 /* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_decrypt_key.c,v $ */
92 /* $Revision: 1.3 $ */
93 /* $Date: 2005/05/05 14:35:59 $ */