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_encrypt_key.c
13 Katja PKCS-style OAEP encryption, Tom St Denis
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
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)
37 unsigned long modulus_bitlen, modulus_bytelen, x;
40 LTC_ARGCHK(in != NULL);
41 LTC_ARGCHK(out != NULL);
42 LTC_ARGCHK(outlen != NULL);
43 LTC_ARGCHK(key != NULL);
45 /* valid prng and hash ? */
46 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
49 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
53 /* get modulus len in bits */
54 modulus_bitlen = mp_count_bits((key->N));
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);
59 /* round down to next byte */
60 modulus_bitlen -= (modulus_bitlen & 7) + 8;
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;
69 /* OAEP pad the key */
71 if ((err = pkcs_1_oaep_encode(in, inlen, lparam,
72 lparamlen, modulus_bitlen, prng, prng_idx, hash_idx,
73 out, &x)) != CRYPT_OK) {
77 /* Katja exptmod the OAEP pad */
78 return katja_exptmod(out, x, out, outlen, PK_PUBLIC, key);
83 /* ref: $Format:%D$ */
84 /* git commit: $Format:%H$ */
85 /* commit time: $Format:%ai$ */