comparison src/pk/rsa/rsa_v15_decrypt_key.c @ 191:1c15b283127b libtomcrypt-orig

Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 May 2005 13:23:02 +0000
parents
children
comparison
equal deleted inserted replaced
143:5d99163f7e32 191:1c15b283127b
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_v15_decrypt_key.c
15 RSA PKCS v1.5 Decryption, Tom St Denis
16 */
17
18 #ifdef MRSA
19
20 /**
21 RSA decrypt then PKCS #1 v1.5 depad
22 @param in The ciphertext
23 @param inlen The length of the ciphertext (octets)
24 @param out [out] The plaintext
25 @param outlen The length of the plaintext (you have to tell this function as it's not part of PKCS #1 v1.0 padding!)
26 @param stat [out] Status of decryption, 1==valid, 0==invalid
27 @param key The corresponding private RSA key
28 @return CRYPT_OK if successful (even if invalid)
29 */
30 int rsa_v15_decrypt_key(const unsigned char *in, unsigned long inlen,
31 unsigned char *out, unsigned long outlen,
32 int *stat, rsa_key *key)
33 {
34 unsigned long modulus_bitlen, modulus_bytelen, x;
35 int err;
36 unsigned char *tmp;
37
38 LTC_ARGCHK(out != NULL);
39 LTC_ARGCHK(key != NULL);
40 LTC_ARGCHK(stat != NULL);
41
42 /* default to invalid */
43 *stat = 0;
44
45 /* get modulus len in bits */
46 modulus_bitlen = mp_count_bits(&(key->N));
47
48 /* outlen must be at least the size of the modulus */
49 modulus_bytelen = mp_unsigned_bin_size(&(key->N));
50 if (modulus_bytelen != inlen) {
51 return CRYPT_INVALID_PACKET;
52 }
53
54 /* allocate ram */
55 tmp = XMALLOC(inlen);
56 if (tmp == NULL) {
57 return CRYPT_MEM;
58 }
59
60 /* rsa decode the packet */
61 x = inlen;
62 if ((err = rsa_exptmod(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) {
63 XFREE(tmp);
64 return err;
65 }
66
67 /* PKCS #1 v1.5 depad */
68 err = pkcs_1_v15_es_decode(tmp, x, modulus_bitlen, out, outlen, stat);
69 XFREE(tmp);
70 return err;
71 }
72
73 #endif