comparison libtomcrypt/src/pk/pkcs1/pkcs_1_oaep_decode.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 pkcs_1_oaep_decode.c 12 @file pkcs_1_oaep_decode.c
15 OAEP Padding for LTC_PKCS #1, Tom St Denis 13 OAEP Padding for PKCS #1, Tom St Denis
16 */ 14 */
17 15
18 #ifdef LTC_PKCS_1 16 #ifdef LTC_PKCS_1
19 17
20 /** 18 /**
21 LTC_PKCS #1 v2.00 OAEP decode 19 PKCS #1 v2.00 OAEP decode
22 @param msg The encoded data to decode 20 @param msg The encoded data to decode
23 @param msglen The length of the encoded data (octets) 21 @param msglen The length of the encoded data (octets)
24 @param lparam The session or system data (can be NULL) 22 @param lparam The session or system data (can be NULL)
25 @param lparamlen The length of the lparam 23 @param lparamlen The length of the lparam
26 @param modulus_bitlen The bit length of the RSA modulus 24 @param modulus_bitlen The bit length of the RSA modulus
27 @param hash_idx The index of the hash desired 25 @param hash_idx The index of the hash desired
28 @param out [out] Destination of decoding 26 @param out [out] Destination of decoding
29 @param outlen [in/out] The max size and resulting size of the decoding 27 @param outlen [in/out] The max size and resulting size of the decoding
30 @param res [out] Result of decoding, 1==valid, 0==invalid 28 @param res [out] Result of decoding, 1==valid, 0==invalid
31 @return CRYPT_OK if successful (even if invalid) 29 @return CRYPT_OK if successful
32 */ 30 */
33 int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen, 31 int pkcs_1_oaep_decode(const unsigned char *msg, unsigned long msglen,
34 const unsigned char *lparam, unsigned long lparamlen, 32 const unsigned char *lparam, unsigned long lparamlen,
35 unsigned long modulus_bitlen, int hash_idx, 33 unsigned long modulus_bitlen, int hash_idx,
36 unsigned char *out, unsigned long *outlen, 34 unsigned char *out, unsigned long *outlen,
37 int *res) 35 int *res)
38 { 36 {
39 unsigned char *DB, *seed, *mask; 37 unsigned char *DB, *seed, *mask;
40 unsigned long hLen, x, y, modulus_len; 38 unsigned long hLen, x, y, modulus_len;
41 int err; 39 int err, ret;
42 40
43 LTC_ARGCHK(msg != NULL); 41 LTC_ARGCHK(msg != NULL);
44 LTC_ARGCHK(out != NULL); 42 LTC_ARGCHK(out != NULL);
45 LTC_ARGCHK(outlen != NULL); 43 LTC_ARGCHK(outlen != NULL);
46 LTC_ARGCHK(res != NULL); 44 LTC_ARGCHK(res != NULL);
47 45
48 /* default to invalid packet */ 46 /* default to invalid packet */
49 *res = 0; 47 *res = 0;
50 48
51 /* test valid hash */ 49 /* test valid hash */
52 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) { 50 if ((err = hash_is_valid(hash_idx)) != CRYPT_OK) {
53 return err; 51 return err;
54 } 52 }
55 hLen = hash_descriptor[hash_idx].hashsize; 53 hLen = hash_descriptor[hash_idx].hashsize;
56 modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0); 54 modulus_len = (modulus_bitlen >> 3) + (modulus_bitlen & 7 ? 1 : 0);
57 55
76 } 74 }
77 return CRYPT_MEM; 75 return CRYPT_MEM;
78 } 76 }
79 77
80 /* ok so it's now in the form 78 /* ok so it's now in the form
81 79
82 0x00 || maskedseed || maskedDB 80 0x00 || maskedseed || maskedDB
83 81
84 1 || hLen || modulus_len - hLen - 1 82 1 || hLen || modulus_len - hLen - 1
85 83
86 */ 84 */
85
86 ret = CRYPT_OK;
87 87
88 /* must have leading 0x00 byte */ 88 /* must have leading 0x00 byte */
89 if (msg[0] != 0x00) { 89 if (msg[0] != 0x00) {
90 err = CRYPT_OK; 90 ret = CRYPT_INVALID_PACKET;
91 goto LBL_ERR;
92 } 91 }
93 92
94 /* now read the masked seed */ 93 /* now read the masked seed */
95 x = 1; 94 x = 1;
96 XMEMCPY(seed, msg + x, hLen); 95 XMEMCPY(seed, msg + x, hLen);
98 97
99 /* now read the masked DB */ 98 /* now read the masked DB */
100 XMEMCPY(DB, msg + x, modulus_len - hLen - 1); 99 XMEMCPY(DB, msg + x, modulus_len - hLen - 1);
101 x += modulus_len - hLen - 1; 100 x += modulus_len - hLen - 1;
102 101
103 /* compute MGF1 of maskedDB (hLen) */ 102 /* compute MGF1 of maskedDB (hLen) */
104 if ((err = pkcs_1_mgf1(hash_idx, DB, modulus_len - hLen - 1, mask, hLen)) != CRYPT_OK) { 103 if ((err = pkcs_1_mgf1(hash_idx, DB, modulus_len - hLen - 1, mask, hLen)) != CRYPT_OK) {
105 goto LBL_ERR; 104 goto LBL_ERR;
106 } 105 }
107 106
108 /* XOR against seed */ 107 /* XOR against seed */
115 goto LBL_ERR; 114 goto LBL_ERR;
116 } 115 }
117 116
118 /* xor against DB */ 117 /* xor against DB */
119 for (y = 0; y < (modulus_len - hLen - 1); y++) { 118 for (y = 0; y < (modulus_len - hLen - 1); y++) {
120 DB[y] ^= mask[y]; 119 DB[y] ^= mask[y];
121 } 120 }
122 121
123 /* now DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */ 122 /* now DB == lhash || PS || 0x01 || M, PS == k - mlen - 2hlen - 2 zeroes */
124 123
125 /* compute lhash and store it in seed [reuse temps!] */ 124 /* compute lhash and store it in seed [reuse temps!] */
134 goto LBL_ERR; 133 goto LBL_ERR;
135 } 134 }
136 } 135 }
137 136
138 /* compare the lhash'es */ 137 /* compare the lhash'es */
139 if (XMEMCMP(seed, DB, hLen) != 0) { 138 if (XMEM_NEQ(seed, DB, hLen) != 0) {
140 err = CRYPT_OK; 139 ret = CRYPT_INVALID_PACKET;
141 goto LBL_ERR;
142 } 140 }
143 141
144 /* now zeroes before a 0x01 */ 142 /* now zeroes before a 0x01 */
145 for (x = hLen; x < (modulus_len - hLen - 1) && DB[x] == 0x00; x++) { 143 for (x = hLen; x < (modulus_len - hLen - 1) && DB[x] == 0x00; x++) {
146 /* step... */ 144 /* step... */
147 } 145 }
148 146
149 /* error out if wasn't 0x01 */ 147 /* error if wasn't 0x01 */
150 if (x == (modulus_len - hLen - 1) || DB[x] != 0x01) { 148 if (x == (modulus_len - hLen - 1) || DB[x] != 0x01) {
151 err = CRYPT_INVALID_PACKET; 149 ret = CRYPT_INVALID_PACKET;
152 goto LBL_ERR;
153 } 150 }
154 151
155 /* rest is the message (and skip 0x01) */ 152 /* rest is the message (and skip 0x01) */
156 if ((modulus_len - hLen - 1 - ++x) > *outlen) { 153 if ((modulus_len - hLen - 1 - ++x) > *outlen) {
157 *outlen = modulus_len - hLen - 1 - x; 154 ret = CRYPT_INVALID_PACKET;
158 err = CRYPT_BUFFER_OVERFLOW;
159 goto LBL_ERR;
160 } 155 }
161 156
162 /* copy message */ 157 if (ret == CRYPT_OK) {
163 *outlen = modulus_len - hLen - 1 - x; 158 /* copy message */
164 XMEMCPY(out, DB + x, modulus_len - hLen - 1 - x); 159 *outlen = modulus_len - hLen - 1 - x;
165 x += modulus_len - hLen - 1; 160 XMEMCPY(out, DB + x, modulus_len - hLen - 1 - x);
166 161
167 /* valid packet */ 162 /* valid packet */
168 *res = 1; 163 *res = 1;
164 }
165 err = ret;
169 166
170 err = CRYPT_OK;
171 LBL_ERR: 167 LBL_ERR:
172 #ifdef LTC_CLEAN_STACK 168 #ifdef LTC_CLEAN_STACK
173 zeromem(DB, modulus_len); 169 zeromem(DB, modulus_len);
174 zeromem(seed, hLen); 170 zeromem(seed, hLen);
175 zeromem(mask, modulus_len); 171 zeromem(mask, modulus_len);
182 return err; 178 return err;
183 } 179 }
184 180
185 #endif /* LTC_PKCS_1 */ 181 #endif /* LTC_PKCS_1 */
186 182
187 /* $Source$ */ 183 /* ref: $Format:%D$ */
188 /* $Revision$ */ 184 /* git commit: $Format:%H$ */
189 /* $Date$ */ 185 /* commit time: $Format:%ai$ */