]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/katja/katja_decrypt_key.c
72009b0fb172e54121139e9b544258a0393d8f86
[zpackage] / libtomcrypt / src / pk / katja / katja_decrypt_key.c
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 #include "tomcrypt.h"
10
11 /**
12   @file katja_decrypt_key.c
13   Katja PKCS #1 OAEP Decryption, Tom St Denis
14 */
15
16 #ifdef LTC_MKAT
17
18 /**
19    (PKCS #1 v2.0) decrypt then OAEP depad
20    @param in          The ciphertext
21    @param inlen       The length of the ciphertext (octets)
22    @param out         [out] The plaintext
23    @param outlen      [in/out] The max size and resulting size of the plaintext (octets)
24    @param lparam      The system "lparam" value
25    @param lparamlen   The length of the lparam value (octets)
26    @param hash_idx    The index of the hash desired
27    @param stat        [out] Result of the decryption, 1==valid, 0==invalid
28    @param key         The corresponding private Katja key
29    @return CRYPT_OK if succcessul (even if invalid)
30 */
31 int katja_decrypt_key(const unsigned char *in,       unsigned long  inlen,
32                           unsigned char *out,      unsigned long *outlen,
33                     const unsigned char *lparam,   unsigned long  lparamlen,
34                           int            hash_idx, int           *stat,
35                           katja_key       *key)
36 {
37   unsigned long modulus_bitlen, modulus_bytelen, x;
38   int           err;
39   unsigned char *tmp;
40
41   LTC_ARGCHK(out    != NULL);
42   LTC_ARGCHK(outlen != NULL);
43   LTC_ARGCHK(key    != NULL);
44   LTC_ARGCHK(stat   != NULL);
45
46   /* default to invalid */
47   *stat = 0;
48
49   /* valid hash ? */
50   if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
51      return err;
52   }
53
54   /* get modulus len in bits */
55   modulus_bitlen = mp_count_bits( (key->N));
56
57   /* payload is upto pq, so we know q is 1/3rd the size of N and therefore pq is 2/3th the size */
58  modulus_bitlen = ((modulus_bitlen << 1) / 3);
59
60   /* round down to next byte */
61   modulus_bitlen -= (modulus_bitlen & 7) + 8;
62
63   /* outlen must be at least the size of the modulus */
64   modulus_bytelen = mp_unsigned_bin_size( (key->N));
65   if (modulus_bytelen != inlen) {
66      return CRYPT_INVALID_PACKET;
67   }
68
69   /* allocate ram */
70   tmp = XMALLOC(inlen);
71   if (tmp == NULL) {
72      return CRYPT_MEM;
73   }
74
75   /* rsa decode the packet */
76   x = inlen;
77   if ((err = katja_exptmod(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) {
78      XFREE(tmp);
79      return err;
80   }
81
82   /* shift right by modulus_bytelen - modulus_bitlen/8  bytes */
83   for (x = 0; x < (modulus_bitlen >> 3); x++) {
84      tmp[x] = tmp[x+(modulus_bytelen-(modulus_bitlen>>3))];
85   }
86
87   /* now OAEP decode the packet */
88   err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,
89                            out, outlen, stat);
90
91   XFREE(tmp);
92   return err;
93 }
94
95 #endif /* LTC_MRSA */
96
97
98
99
100
101 /* ref:         $Format:%D$ */
102 /* git commit:  $Format:%H$ */
103 /* commit time: $Format:%ai$ */