comparison libtomcrypt/src/pk/pkcs1/pkcs_1_v1_5_encode.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 /*! \file pkcs_1_v1_5_encode.c 11 /*! \file pkcs_1_v1_5_encode.c
14 * 12 *
15 * LTC_PKCS #1 v1.5 Padding (Andreas Lange) 13 * PKCS #1 v1.5 Padding (Andreas Lange)
16 */ 14 */
17 15
18 #ifdef LTC_PKCS_1 16 #ifdef LTC_PKCS_1
19 17
20 /*! \brief LTC_PKCS #1 v1.5 encode. 18 /*! \brief PKCS #1 v1.5 encode.
21 * 19 *
22 * \param msg The data to encode 20 * \param msg The data to encode
23 * \param msglen The length of the data to encode (octets) 21 * \param msglen The length of the data to encode (octets)
24 * \param block_type Block type to use in padding (\sa ltc_pkcs_1_v1_5_blocks) 22 * \param block_type Block type to use in padding (\sa ltc_pkcs_1_v1_5_blocks)
25 * \param modulus_bitlen The bit length of the RSA modulus 23 * \param modulus_bitlen The bit length of the RSA modulus
26 * \param prng An active PRNG state (only for LTC_LTC_PKCS_1_EME) 24 * \param prng An active PRNG state (only for LTC_PKCS_1_EME)
27 * \param prng_idx The index of the PRNG desired (only for LTC_LTC_PKCS_1_EME) 25 * \param prng_idx The index of the PRNG desired (only for LTC_PKCS_1_EME)
28 * \param out [out] The destination for the encoded data 26 * \param out [out] The destination for the encoded data
29 * \param outlen [in/out] The max size and resulting size of the encoded data 27 * \param outlen [in/out] The max size and resulting size of the encoded data
30 * 28 *
31 * \return CRYPT_OK if successful 29 * \return CRYPT_OK if successful
32 */ 30 */
33 int pkcs_1_v1_5_encode(const unsigned char *msg, 31 int pkcs_1_v1_5_encode(const unsigned char *msg,
34 unsigned long msglen, 32 unsigned long msglen,
35 int block_type, 33 int block_type,
36 unsigned long modulus_bitlen, 34 unsigned long modulus_bitlen,
37 prng_state *prng, 35 prng_state *prng,
38 int prng_idx, 36 int prng_idx,
39 unsigned char *out, 37 unsigned char *out,
40 unsigned long *outlen) 38 unsigned long *outlen)
41 { 39 {
42 unsigned long modulus_len, ps_len, i; 40 unsigned long modulus_len, ps_len, i;
43 unsigned char *ps; 41 unsigned char *ps;
44 int result; 42 int result;
45 43
46 /* valid block_type? */ 44 /* valid block_type? */
47 if ((block_type != LTC_LTC_PKCS_1_EMSA) && 45 if ((block_type != LTC_PKCS_1_EMSA) &&
48 (block_type != LTC_LTC_PKCS_1_EME)) { 46 (block_type != LTC_PKCS_1_EME)) {
49 return CRYPT_PK_INVALID_PADDING; 47 return CRYPT_PK_INVALID_PADDING;
50 } 48 }
51 49
52 if (block_type == LTC_LTC_PKCS_1_EME) { /* encryption padding, we need a valid PRNG */ 50 if (block_type == LTC_PKCS_1_EME) { /* encryption padding, we need a valid PRNG */
53 if ((result = prng_is_valid(prng_idx)) != CRYPT_OK) { 51 if ((result = prng_is_valid(prng_idx)) != CRYPT_OK) {
54 return result; 52 return result;
55 } 53 }
56 } 54 }
57 55
70 68
71 /* generate an octets string PS */ 69 /* generate an octets string PS */
72 ps = &out[2]; 70 ps = &out[2];
73 ps_len = modulus_len - msglen - 3; 71 ps_len = modulus_len - msglen - 3;
74 72
75 if (block_type == LTC_LTC_PKCS_1_EME) { 73 if (block_type == LTC_PKCS_1_EME) {
76 /* now choose a random ps */ 74 /* now choose a random ps */
77 if (prng_descriptor[prng_idx].read(ps, ps_len, prng) != ps_len) { 75 if (prng_descriptor[prng_idx].read(ps, ps_len, prng) != ps_len) {
78 result = CRYPT_ERROR_READPRNG; 76 result = CRYPT_ERROR_READPRNG;
79 goto bail; 77 goto bail;
80 } 78 }
104 return result; 102 return result;
105 } /* pkcs_1_v1_5_encode */ 103 } /* pkcs_1_v1_5_encode */
106 104
107 #endif /* #ifdef LTC_PKCS_1 */ 105 #endif /* #ifdef LTC_PKCS_1 */
108 106
109 /* $Source$ */ 107 /* ref: $Format:%D$ */
110 /* $Revision$ */ 108 /* git commit: $Format:%H$ */
111 /* $Date$ */ 109 /* commit time: $Format:%ai$ */