Mercurial > dropbear
comparison src/pk/rsa/rsa_encrypt_key.c @ 380:d5faf4814ddb libtomcrypt-orig libtomcrypt-1.16
Update to LibTomCrypt 1.16
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 11 Jan 2007 02:22:00 +0000 |
parents | 59400faa4b44 |
children |
comparison
equal
deleted
inserted
replaced
280:59400faa4b44 | 380:d5faf4814ddb |
---|---|
4 * algorithms in a highly modular and flexible manner. | 4 * algorithms in a highly modular and flexible manner. |
5 * | 5 * |
6 * The library is free for all purposes without any express | 6 * The library is free for all purposes without any express |
7 * guarantee it works. | 7 * guarantee it works. |
8 * | 8 * |
9 * Tom St Denis, [email protected], http://libtomcrypt.org | 9 * Tom St Denis, [email protected], http://libtomcrypt.com |
10 */ | 10 */ |
11 #include "tomcrypt.h" | 11 #include "tomcrypt.h" |
12 | 12 |
13 /** | 13 /** |
14 @file rsa_encrypt_key.c | 14 @file rsa_encrypt_key.c |
15 RSA PKCS OAEP encryption, Tom St Denis | 15 RSA PKCS #1 encryption, Tom St Denis and Andreas Lange |
16 */ | 16 */ |
17 | 17 |
18 #ifdef MRSA | 18 #ifdef MRSA |
19 | 19 |
20 /** | 20 /** |
21 (PKCS #1 v2.0) OAEP pad then encrypt | 21 (PKCS #1 v2.0) OAEP pad then encrypt |
26 @param lparam The system "lparam" for the encryption | 26 @param lparam The system "lparam" for the encryption |
27 @param lparamlen The length of lparam (octets) | 27 @param lparamlen The length of lparam (octets) |
28 @param prng An active PRNG | 28 @param prng An active PRNG |
29 @param prng_idx The index of the desired prng | 29 @param prng_idx The index of the desired prng |
30 @param hash_idx The index of the desired hash | 30 @param hash_idx The index of the desired hash |
31 @param padding Type of padding (LTC_PKCS_1_OAEP or LTC_PKCS_1_V1_5) | |
31 @param key The RSA key to encrypt to | 32 @param key The RSA key to encrypt to |
32 @return CRYPT_OK if successful | 33 @return CRYPT_OK if successful |
33 */ | 34 */ |
34 int rsa_encrypt_key(const unsigned char *in, unsigned long inlen, | 35 int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen, |
35 unsigned char *out, unsigned long *outlen, | 36 unsigned char *out, unsigned long *outlen, |
36 const unsigned char *lparam, unsigned long lparamlen, | 37 const unsigned char *lparam, unsigned long lparamlen, |
37 prng_state *prng, int prng_idx, int hash_idx, rsa_key *key) | 38 prng_state *prng, int prng_idx, int hash_idx, int padding, rsa_key *key) |
38 { | 39 { |
39 unsigned long modulus_bitlen, modulus_bytelen, x; | 40 unsigned long modulus_bitlen, modulus_bytelen, x; |
40 int err; | 41 int err; |
41 | 42 |
42 LTC_ARGCHK(in != NULL); | 43 LTC_ARGCHK(in != NULL); |
43 LTC_ARGCHK(out != NULL); | 44 LTC_ARGCHK(out != NULL); |
44 LTC_ARGCHK(outlen != NULL); | 45 LTC_ARGCHK(outlen != NULL); |
45 LTC_ARGCHK(key != NULL); | 46 LTC_ARGCHK(key != NULL); |
46 | 47 |
47 /* valid prng and hash ? */ | 48 /* valid padding? */ |
49 if ((padding != LTC_PKCS_1_V1_5) && | |
50 (padding != LTC_PKCS_1_OAEP)) { | |
51 return CRYPT_PK_INVALID_PADDING; | |
52 } | |
53 | |
54 /* valid prng? */ | |
48 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { | 55 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { |
49 return err; | 56 return err; |
50 } | 57 } |
51 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { | 58 |
52 return err; | 59 if (padding == LTC_PKCS_1_OAEP) { |
60 /* valid hash? */ | |
61 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { | |
62 return err; | |
63 } | |
53 } | 64 } |
54 | 65 |
55 /* get modulus len in bits */ | 66 /* get modulus len in bits */ |
56 modulus_bitlen = mp_count_bits(&(key->N)); | 67 modulus_bitlen = mp_count_bits( (key->N)); |
57 | 68 |
58 /* outlen must be at least the size of the modulus */ | 69 /* outlen must be at least the size of the modulus */ |
59 modulus_bytelen = mp_unsigned_bin_size(&(key->N)); | 70 modulus_bytelen = mp_unsigned_bin_size( (key->N)); |
60 if (modulus_bytelen > *outlen) { | 71 if (modulus_bytelen > *outlen) { |
72 *outlen = modulus_bytelen; | |
61 return CRYPT_BUFFER_OVERFLOW; | 73 return CRYPT_BUFFER_OVERFLOW; |
62 } | 74 } |
63 | |
64 /* OAEP pad the key */ | |
65 x = *outlen; | |
66 if ((err = pkcs_1_oaep_encode(in, inlen, lparam, | |
67 lparamlen, modulus_bitlen, prng, prng_idx, hash_idx, | |
68 out, &x)) != CRYPT_OK) { | |
69 return err; | |
70 } | |
71 | 75 |
72 /* rsa exptmod the OAEP pad */ | 76 if (padding == LTC_PKCS_1_OAEP) { |
73 return rsa_exptmod(out, x, out, outlen, PK_PUBLIC, key); | 77 /* OAEP pad the key */ |
78 x = *outlen; | |
79 if ((err = pkcs_1_oaep_encode(in, inlen, lparam, | |
80 lparamlen, modulus_bitlen, prng, prng_idx, hash_idx, | |
81 out, &x)) != CRYPT_OK) { | |
82 return err; | |
83 } | |
84 } else { | |
85 /* PKCS #1 v1.5 pad the key */ | |
86 x = *outlen; | |
87 if ((err = pkcs_1_v1_5_encode(in, inlen, LTC_PKCS_1_EME, | |
88 modulus_bitlen, prng, prng_idx, | |
89 out, &x)) != CRYPT_OK) { | |
90 return err; | |
91 } | |
92 } | |
93 | |
94 /* rsa exptmod the OAEP or PKCS #1 v1.5 pad */ | |
95 return ltc_mp.rsa_me(out, x, out, outlen, PK_PUBLIC, key); | |
74 } | 96 } |
75 | 97 |
76 #endif /* MRSA */ | 98 #endif /* MRSA */ |
77 | 99 |
78 /* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_encrypt_key.c,v $ */ | 100 /* $Source: /cvs/libtom/libtomcrypt/src/pk/rsa/rsa_encrypt_key.c,v $ */ |
79 /* $Revision: 1.3 $ */ | 101 /* $Revision: 1.8 $ */ |
80 /* $Date: 2005/05/05 14:35:59 $ */ | 102 /* $Date: 2006/11/01 09:18:22 $ */ |