143
|
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 |
|
16 /* PKCS #1 v1.5 pad then encrypt */ |
|
17 int rsa_v15_encrypt_key(const unsigned char *inkey, unsigned long inlen, |
|
18 unsigned char *outkey, unsigned long *outlen, |
|
19 prng_state *prng, int prng_idx, |
|
20 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? */ |
|
31 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { |
|
32 return err; |
|
33 } |
|
34 |
|
35 /* get modulus len in bits */ |
|
36 modulus_bitlen = mp_count_bits(&(key->N)); |
|
37 |
|
38 /* outlen must be at least the size of the modulus */ |
|
39 modulus_bytelen = mp_unsigned_bin_size(&(key->N)); |
|
40 if (modulus_bytelen > *outlen) { |
|
41 return CRYPT_BUFFER_OVERFLOW; |
|
42 } |
|
43 |
|
44 /* pad it */ |
|
45 x = *outlen; |
|
46 if ((err = pkcs_1_v15_es_encode(inkey, inlen, modulus_bitlen, prng, prng_idx, outkey, &x)) != CRYPT_OK) { |
|
47 return err; |
|
48 } |
|
49 |
|
50 /* encrypt it */ |
|
51 return rsa_exptmod(outkey, x, outkey, outlen, PK_PUBLIC, prng, prng_idx, key); |
|
52 } |
|
53 |
|
54 #endif |