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
9
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
14
@file katja_encrypt_key.c
15
Katja PKCS-style OAEP encryption, Tom St Denis
21
(PKCS #1 v2.0) OAEP pad then encrypt
22
@param in The plaintext
23
@param inlen The length of the plaintext (octets)
24
@param out [out] The ciphertext
25
@param outlen [in/out] The max size and resulting size of the ciphertext
26
@param lparam The system "lparam" for the encryption
27
@param lparamlen The length of lparam (octets)
28
@param prng An active PRNG
29
@param prng_idx The index of the desired prng
30
@param hash_idx The index of the desired hash
31
@param key The Katja key to encrypt to
32
@return CRYPT_OK if successful
34
int katja_encrypt_key(const unsigned char *in, unsigned long inlen,
35
unsigned char *out, unsigned long *outlen,
36
const unsigned char *lparam, unsigned long lparamlen,
37
prng_state *prng, int prng_idx, int hash_idx, katja_key *key)
39
unsigned long modulus_bitlen, modulus_bytelen, x;
42
LTC_ARGCHK(in != NULL);
43
LTC_ARGCHK(out != NULL);
44
LTC_ARGCHK(outlen != NULL);
45
LTC_ARGCHK(key != NULL);
47
/* valid prng and hash ? */
48
if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
51
if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
55
/* get modulus len in bits */
56
modulus_bitlen = mp_count_bits((key->N));
58
/* payload is upto pq, so we know q is 1/3rd the size of N and therefore pq is 2/3th the size */
59
modulus_bitlen = ((modulus_bitlen << 1) / 3);
61
/* round down to next byte */
62
modulus_bitlen -= (modulus_bitlen & 7) + 8;
64
/* outlen must be at least the size of the modulus */
65
modulus_bytelen = mp_unsigned_bin_size((key->N));
66
if (modulus_bytelen > *outlen) {
67
*outlen = modulus_bytelen;
68
return CRYPT_BUFFER_OVERFLOW;
71
/* OAEP pad the key */
73
if ((err = pkcs_1_oaep_encode(in, inlen, lparam,
74
lparamlen, modulus_bitlen, prng, prng_idx, hash_idx,
75
out, &x)) != CRYPT_OK) {
79
/* Katja exptmod the OAEP pad */
80
return katja_exptmod(out, x, out, outlen, PK_PUBLIC, key);
85
/* $Source: /cvs/libtom/libtomcrypt/src/pk/katja/katja_encrypt_key.c,v $ */
86
/* $Revision: 1.5 $ */
87
/* $Date: 2006/06/16 21:53:41 $ */