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@iahu.ca, http://libtomcrypt.org
16
/* (PKCS #1 v2.0) OAEP pad then encrypt */
17
int rsa_encrypt_key(const unsigned char *inkey, unsigned long inlen,
18
unsigned char *outkey, unsigned long *outlen,
19
const unsigned char *lparam, unsigned long lparamlen,
20
prng_state *prng, int prng_idx, int hash_idx, rsa_key *key)
22
unsigned long modulus_bitlen, modulus_bytelen, x;
25
_ARGCHK(inkey != NULL);
26
_ARGCHK(outkey != NULL);
27
_ARGCHK(outlen != NULL);
30
/* valid prng and hash ? */
31
if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
34
if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
38
/* get modulus len in bits */
39
modulus_bitlen = mp_count_bits(&(key->N));
41
/* outlen must be at least the size of the modulus */
42
modulus_bytelen = mp_unsigned_bin_size(&(key->N));
43
if (modulus_bytelen > *outlen) {
44
return CRYPT_BUFFER_OVERFLOW;
47
/* OAEP pad the key */
49
if ((err = pkcs_1_oaep_encode(inkey, inlen, lparam,
50
lparamlen, modulus_bitlen, prng, prng_idx, hash_idx,
51
outkey, &x)) != CRYPT_OK) {
55
/* rsa exptmod the OAEP pad */
56
return rsa_exptmod(outkey, x, outkey, outlen, PK_PUBLIC, prng, prng_idx, key);