15
|
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 * Tom St Denis, [email protected], http://libtomcrypt.org |
|
10 */ |
|
11 |
|
12 #include "mycrypt.h" |
|
13 |
|
14 #ifdef MRSA |
|
15 |
143
|
16 /* (PKCS #1 v2.0) OAEP pad then encrypt */ |
15
|
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) |
|
21 { |
|
22 unsigned long modulus_bitlen, modulus_bytelen, x; |
|
23 int err; |
|
24 |
|
25 _ARGCHK(inkey != NULL); |
|
26 _ARGCHK(outkey != NULL); |
|
27 _ARGCHK(outlen != NULL); |
|
28 _ARGCHK(key != NULL); |
|
29 |
|
30 /* valid prng and hash ? */ |
|
31 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { |
|
32 return err; |
|
33 } |
|
34 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { |
|
35 return err; |
|
36 } |
|
37 |
|
38 /* get modulus len in bits */ |
|
39 modulus_bitlen = mp_count_bits(&(key->N)); |
|
40 |
|
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; |
|
45 } |
|
46 |
|
47 /* OAEP pad the key */ |
|
48 x = *outlen; |
|
49 if ((err = pkcs_1_oaep_encode(inkey, inlen, lparam, |
|
50 lparamlen, modulus_bitlen, prng, prng_idx, hash_idx, |
|
51 outkey, &x)) != CRYPT_OK) { |
|
52 return err; |
|
53 } |
|
54 |
|
55 /* rsa exptmod the OAEP pad */ |
|
56 return rsa_exptmod(outkey, x, outkey, outlen, PK_PUBLIC, prng, prng_idx, key); |
|
57 } |
|
58 |
|
59 #endif /* MRSA */ |