comparison rsa_decrypt_key.c @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents 6362d3854bb4
children
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
11 11
12 #include "mycrypt.h" 12 #include "mycrypt.h"
13 13
14 #ifdef MRSA 14 #ifdef MRSA
15 15
16 /* decrypt then OAEP depad */ 16 /* (PKCS #1 v2.0) decrypt then OAEP depad */
17 int rsa_decrypt_key(const unsigned char *in, unsigned long inlen, 17 int rsa_decrypt_key(const unsigned char *in, unsigned long inlen,
18 unsigned char *outkey, unsigned long *keylen, 18 unsigned char *outkey, unsigned long *keylen,
19 const unsigned char *lparam, unsigned long lparamlen, 19 const unsigned char *lparam, unsigned long lparamlen,
20 prng_state *prng, int prng_idx, 20 prng_state *prng, int prng_idx,
21 int hash_idx, int *res, 21 int hash_idx, int *res,
22 rsa_key *key) 22 rsa_key *key)
23 { 23 {
24 unsigned long modulus_bitlen, modulus_bytelen, x; 24 unsigned long modulus_bitlen, modulus_bytelen, x;
25 int err; 25 int err;
26 unsigned char *tmp;
26 27
27 _ARGCHK(outkey != NULL); 28 _ARGCHK(outkey != NULL);
28 _ARGCHK(keylen != NULL); 29 _ARGCHK(keylen != NULL);
29 _ARGCHK(key != NULL); 30 _ARGCHK(key != NULL);
30 _ARGCHK(res != NULL); 31 _ARGCHK(res != NULL);
31 32
32 /* valid hash ? */ 33 /* default to invalid */
34 *res = 0;
35
36 /* valid hash/prng ? */
37 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
38 return err;
39 }
33 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { 40 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
34 return err; 41 return err;
35 } 42 }
36 43
37 /* get modulus len in bits */ 44 /* get modulus len in bits */
41 modulus_bytelen = mp_unsigned_bin_size(&(key->N)); 48 modulus_bytelen = mp_unsigned_bin_size(&(key->N));
42 if (modulus_bytelen != inlen) { 49 if (modulus_bytelen != inlen) {
43 return CRYPT_INVALID_PACKET; 50 return CRYPT_INVALID_PACKET;
44 } 51 }
45 52
53 /* allocate ram */
54 tmp = XMALLOC(inlen);
55 if (tmp == NULL) {
56 return CRYPT_MEM;
57 }
58
46 /* rsa decode the packet */ 59 /* rsa decode the packet */
47 x = *keylen; 60 x = inlen;
48 if ((err = rsa_exptmod(in, inlen, outkey, &x, PK_PRIVATE, prng, prng_idx, key)) != CRYPT_OK) { 61 if ((err = rsa_exptmod(in, inlen, tmp, &x, PK_PRIVATE, prng, prng_idx, key)) != CRYPT_OK) {
62 XFREE(tmp);
49 return err; 63 return err;
50 } 64 }
51 65
52 /* now OAEP decode the packet */ 66 /* now OAEP decode the packet */
53 return pkcs_1_oaep_decode(outkey, x, lparam, lparamlen, modulus_bitlen, hash_idx, 67 err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,
54 outkey, keylen, res); 68 outkey, keylen, res);
69 XFREE(tmp);
70 return err;
55 } 71 }
56 72
57 #endif /* MRSA */ 73 #endif /* MRSA */
58 74
59 75