comparison libtomcrypt/src/pk/rsa/rsa_encrypt_key.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children
comparison
equal deleted inserted replaced
1470:8bba51a55704 1471:6dba84798cd5
3 * LibTomCrypt is a library that provides various cryptographic 3 * LibTomCrypt is a library that provides various cryptographic
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 *
9 * Tom St Denis, [email protected], http://libtom.org
10 */ 8 */
11 #include "tomcrypt.h" 9 #include "tomcrypt.h"
12 10
13 /** 11 /**
14 @file rsa_encrypt_key.c 12 @file rsa_encrypt_key.c
15 RSA LTC_PKCS #1 encryption, Tom St Denis and Andreas Lange 13 RSA PKCS #1 encryption, Tom St Denis and Andreas Lange
16 */ 14 */
17 15
18 #ifdef LTC_MRSA 16 #ifdef LTC_MRSA
19 17
20 /** 18 /**
21 (LTC_PKCS #1 v2.0) OAEP pad then encrypt 19 (PKCS #1 v2.0) OAEP pad then encrypt
22 @param in The plaintext 20 @param in The plaintext
23 @param inlen The length of the plaintext (octets) 21 @param inlen The length of the plaintext (octets)
24 @param out [out] The ciphertext 22 @param out [out] The ciphertext
25 @param outlen [in/out] The max size and resulting size of the ciphertext 23 @param outlen [in/out] The max size and resulting size of the ciphertext
26 @param lparam The system "lparam" for the encryption 24 @param lparam The system "lparam" for the encryption
27 @param lparamlen The length of lparam (octets) 25 @param lparamlen The length of lparam (octets)
28 @param prng An active PRNG 26 @param prng An active PRNG
29 @param prng_idx The index of the desired prng 27 @param prng_idx The index of the desired prng
30 @param hash_idx The index of the desired hash 28 @param hash_idx The index of the desired hash
31 @param padding Type of padding (LTC_LTC_PKCS_1_OAEP or LTC_LTC_PKCS_1_V1_5) 29 @param padding Type of padding (LTC_PKCS_1_OAEP or LTC_PKCS_1_V1_5)
32 @param key The RSA key to encrypt to 30 @param key The RSA key to encrypt to
33 @return CRYPT_OK if successful 31 @return CRYPT_OK if successful
34 */ 32 */
35 int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen, 33 int rsa_encrypt_key_ex(const unsigned char *in, unsigned long inlen,
36 unsigned char *out, unsigned long *outlen, 34 unsigned char *out, unsigned long *outlen,
44 LTC_ARGCHK(out != NULL); 42 LTC_ARGCHK(out != NULL);
45 LTC_ARGCHK(outlen != NULL); 43 LTC_ARGCHK(outlen != NULL);
46 LTC_ARGCHK(key != NULL); 44 LTC_ARGCHK(key != NULL);
47 45
48 /* valid padding? */ 46 /* valid padding? */
49 if ((padding != LTC_LTC_PKCS_1_V1_5) && 47 if ((padding != LTC_PKCS_1_V1_5) &&
50 (padding != LTC_LTC_PKCS_1_OAEP)) { 48 (padding != LTC_PKCS_1_OAEP)) {
51 return CRYPT_PK_INVALID_PADDING; 49 return CRYPT_PK_INVALID_PADDING;
52 } 50 }
53 51
54 /* valid prng? */ 52 /* valid prng? */
55 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) { 53 if ((err = prng_is_valid(prng_idx)) != CRYPT_OK) {
56 return err; 54 return err;
57 } 55 }
58 56
59 if (padding == LTC_LTC_PKCS_1_OAEP) { 57 if (padding == LTC_PKCS_1_OAEP) {
60 /* valid hash? */ 58 /* valid hash? */
61 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { 59 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
62 return err; 60 return err;
63 } 61 }
64 } 62 }
71 if (modulus_bytelen > *outlen) { 69 if (modulus_bytelen > *outlen) {
72 *outlen = modulus_bytelen; 70 *outlen = modulus_bytelen;
73 return CRYPT_BUFFER_OVERFLOW; 71 return CRYPT_BUFFER_OVERFLOW;
74 } 72 }
75 73
76 if (padding == LTC_LTC_PKCS_1_OAEP) { 74 if (padding == LTC_PKCS_1_OAEP) {
77 /* OAEP pad the key */ 75 /* OAEP pad the key */
78 x = *outlen; 76 x = *outlen;
79 if ((err = pkcs_1_oaep_encode(in, inlen, lparam, 77 if ((err = pkcs_1_oaep_encode(in, inlen, lparam,
80 lparamlen, modulus_bitlen, prng, prng_idx, hash_idx, 78 lparamlen, modulus_bitlen, prng, prng_idx, hash_idx,
81 out, &x)) != CRYPT_OK) { 79 out, &x)) != CRYPT_OK) {
82 return err; 80 return err;
83 } 81 }
84 } else { 82 } else {
85 /* LTC_PKCS #1 v1.5 pad the key */ 83 /* PKCS #1 v1.5 pad the key */
86 x = *outlen; 84 x = *outlen;
87 if ((err = pkcs_1_v1_5_encode(in, inlen, LTC_LTC_PKCS_1_EME, 85 if ((err = pkcs_1_v1_5_encode(in, inlen, LTC_PKCS_1_EME,
88 modulus_bitlen, prng, prng_idx, 86 modulus_bitlen, prng, prng_idx,
89 out, &x)) != CRYPT_OK) { 87 out, &x)) != CRYPT_OK) {
90 return err; 88 return err;
91 } 89 }
92 } 90 }
93 91
94 /* rsa exptmod the OAEP or LTC_PKCS #1 v1.5 pad */ 92 /* rsa exptmod the OAEP or PKCS #1 v1.5 pad */
95 return ltc_mp.rsa_me(out, x, out, outlen, PK_PUBLIC, key); 93 return ltc_mp.rsa_me(out, x, out, outlen, PK_PUBLIC, key);
96 } 94 }
97 95
98 #endif /* LTC_MRSA */ 96 #endif /* LTC_MRSA */
99 97
100 /* $Source$ */ 98 /* ref: $Format:%D$ */
101 /* $Revision$ */ 99 /* git commit: $Format:%H$ */
102 /* $Date$ */ 100 /* commit time: $Format:%ai$ */