1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
11 /** @file pkcs_1_v1_5_decode.c
13 * PKCS #1 v1.5 Padding. (Andreas Lange)
18 /** @brief PKCS #1 v1.5 decode.
20 * @param msg The encoded data to decode
21 * @param msglen The length of the encoded data (octets)
22 * @param block_type Block type to use in padding (\sa ltc_pkcs_1_v1_5_blocks)
23 * @param modulus_bitlen The bit length of the RSA modulus
24 * @param out [out] Destination of decoding
25 * @param outlen [in/out] The max size and resulting size of the decoding
26 * @param is_valid [out] Boolean whether the padding was valid
28 * @return CRYPT_OK if successful
30 int pkcs_1_v1_5_decode(const unsigned char *msg,
33 unsigned long modulus_bitlen,
35 unsigned long *outlen,
38 unsigned long modulus_len, ps_len, i;
41 /* default to invalid packet */
44 modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
46 /* test message size */
48 if ((msglen > modulus_len) || (modulus_len < 11)) {
49 return CRYPT_PK_INVALID_SIZE;
54 /* separate encoded message */
56 if ((msg[0] != 0x00) || (msg[1] != (unsigned char)block_type)) {
57 result = CRYPT_INVALID_PACKET;
60 if (block_type == LTC_PKCS_1_EME) {
61 for (i = 2; i < modulus_len; i++) {
63 if (msg[i] == 0x00) { break; }
67 if (i >= modulus_len) {
68 /* There was no octet with hexadecimal value 0x00 to separate ps from m.
70 result = CRYPT_INVALID_PACKET;
73 for (i = 2; i < modulus_len - 1; i++) {
74 if (msg[i] != 0xFF) { break; }
79 /* There was no octet with hexadecimal value 0x00 to separate ps from m. */
80 result = CRYPT_INVALID_PACKET;
88 /* The length of ps is less than 8 octets.
90 result = CRYPT_INVALID_PACKET;
93 if (*outlen < (msglen - (2 + ps_len + 1))) {
94 result = CRYPT_INVALID_PACKET;
97 if (result == CRYPT_OK) {
98 *outlen = (msglen - (2 + ps_len + 1));
99 XMEMCPY(out, &msg[2 + ps_len + 1], *outlen);
106 } /* pkcs_1_v1_5_decode */
108 #endif /* #ifdef LTC_PKCS_1 */
110 /* ref: $Format:%D$ */
111 /* git commit: $Format:%H$ */
112 /* commit time: $Format:%ai$ */