~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/rsa_decrypt_key.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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, tomstdenis@iahu.ca, http://libtomcrypt.org
10
 
 */
11
 
 
12
 
#include "mycrypt.h"
13
 
 
14
 
#ifdef MRSA
15
 
 
16
 
/* (PKCS #1 v2.0) decrypt then OAEP depad  */
17
 
int rsa_decrypt_key(const unsigned char *in,     unsigned long inlen,
18
 
                          unsigned char *outkey, unsigned long *keylen, 
19
 
                    const unsigned char *lparam, unsigned long lparamlen,
20
 
                          prng_state    *prng,   int           prng_idx,
21
 
                          int            hash_idx, int *res,
22
 
                          rsa_key       *key)
23
 
{
24
 
  unsigned long modulus_bitlen, modulus_bytelen, x;
25
 
  int           err;
26
 
  unsigned char *tmp;
27
 
  
28
 
  _ARGCHK(outkey != NULL);
29
 
  _ARGCHK(keylen != NULL);
30
 
  _ARGCHK(key    != NULL);
31
 
  _ARGCHK(res    != NULL);
32
 
 
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
 
  }
40
 
  if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
41
 
     return err;
42
 
  }
43
 
  
44
 
  /* get modulus len in bits */
45
 
  modulus_bitlen = mp_count_bits(&(key->N));
46
 
 
47
 
  /* outlen must be at least the size of the modulus */
48
 
  modulus_bytelen = mp_unsigned_bin_size(&(key->N));
49
 
  if (modulus_bytelen != inlen) {
50
 
     return CRYPT_INVALID_PACKET;
51
 
  }
52
 
 
53
 
  /* allocate ram */
54
 
  tmp = XMALLOC(inlen);
55
 
  if (tmp == NULL) {
56
 
     return CRYPT_MEM;
57
 
  }
58
 
 
59
 
  /* rsa decode the packet */
60
 
  x = inlen;
61
 
  if ((err = rsa_exptmod(in, inlen, tmp, &x, PK_PRIVATE, prng, prng_idx, key)) != CRYPT_OK) {
62
 
     XFREE(tmp);
63
 
     return err;
64
 
  }
65
 
 
66
 
  /* now OAEP decode the packet */
67
 
  err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,
68
 
                           outkey, keylen, res);
69
 
  XFREE(tmp);
70
 
  return err;
71
 
}
72
 
 
73
 
#endif /* MRSA */
74
 
 
75
 
 
76
 
 
77