1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 @file katja_decrypt_key.c
13 Katja PKCS #1 OAEP Decryption, Tom St Denis
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)
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,
37 unsigned long modulus_bitlen, modulus_bytelen, x;
41 LTC_ARGCHK(out != NULL);
42 LTC_ARGCHK(outlen != NULL);
43 LTC_ARGCHK(key != NULL);
44 LTC_ARGCHK(stat != NULL);
46 /* default to invalid */
50 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
54 /* get modulus len in bits */
55 modulus_bitlen = mp_count_bits( (key->N));
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);
60 /* round down to next byte */
61 modulus_bitlen -= (modulus_bitlen & 7) + 8;
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;
75 /* rsa decode the packet */
77 if ((err = katja_exptmod(in, inlen, tmp, &x, PK_PRIVATE, key)) != CRYPT_OK) {
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))];
87 /* now OAEP decode the packet */
88 err = pkcs_1_oaep_decode(tmp, x, lparam, lparamlen, modulus_bitlen, hash_idx,
101 /* ref: $Format:%D$ */
102 /* git commit: $Format:%H$ */
103 /* commit time: $Format:%ai$ */