]> pd.if.org Git - zpackage/blob - libtomcrypt/src/pk/katja/katja_encrypt_key.c
remove md2 md4 md5 hashes
[zpackage] / libtomcrypt / src / pk / katja / katja_encrypt_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_encrypt_key.c
13   Katja PKCS-style OAEP encryption, Tom St Denis
14 */
15
16 #ifdef LTC_MKAT
17
18 /**
19     (PKCS #1 v2.0) OAEP pad then encrypt
20     @param in          The plaintext
21     @param inlen       The length of the plaintext (octets)
22     @param out         [out] The ciphertext
23     @param outlen      [in/out] The max size and resulting size of the ciphertext
24     @param lparam      The system "lparam" for the encryption
25     @param lparamlen   The length of lparam (octets)
26     @param prng        An active PRNG
27     @param prng_idx    The index of the desired prng
28     @param hash_idx    The index of the desired hash
29     @param key         The Katja key to encrypt to
30     @return CRYPT_OK if successful
31 */
32 int katja_encrypt_key(const unsigned char *in,     unsigned long inlen,
33                           unsigned char *out,    unsigned long *outlen,
34                     const unsigned char *lparam, unsigned long lparamlen,
35                     prng_state *prng, int prng_idx, int hash_idx, katja_key *key)
36 {
37   unsigned long modulus_bitlen, modulus_bytelen, x;
38   int           err;
39
40   LTC_ARGCHK(in     != NULL);
41   LTC_ARGCHK(out    != NULL);
42   LTC_ARGCHK(outlen != NULL);
43   LTC_ARGCHK(key    != NULL);
44
45   /* valid prng and hash ? */
46   if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
47      return err;
48   }
49   if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
50      return err;
51   }
52
53   /* get modulus len in bits */
54   modulus_bitlen = mp_count_bits((key->N));
55
56   /* payload is upto pq, so we know q is 1/3rd the size of N and therefore pq is 2/3th the size */
57   modulus_bitlen = ((modulus_bitlen << 1) / 3);
58
59   /* round down to next byte */
60   modulus_bitlen -= (modulus_bitlen & 7) + 8;
61
62   /* outlen must be at least the size of the modulus */
63   modulus_bytelen = mp_unsigned_bin_size((key->N));
64   if (modulus_bytelen > *outlen) {
65      *outlen = modulus_bytelen;
66      return CRYPT_BUFFER_OVERFLOW;
67   }
68
69   /* OAEP pad the key */
70   x = *outlen;
71   if ((err = pkcs_1_oaep_encode(in, inlen, lparam,
72                                 lparamlen, modulus_bitlen, prng, prng_idx, hash_idx,
73                                 out, &x)) != CRYPT_OK) {
74      return err;
75   }
76
77   /* Katja exptmod the OAEP pad */
78   return katja_exptmod(out, x, out, outlen, PK_PUBLIC, key);
79 }
80
81 #endif /* LTC_MRSA */
82
83 /* ref:         $Format:%D$ */
84 /* git commit:  $Format:%H$ */
85 /* commit time: $Format:%ai$ */