Debian Codegraph @

src/pkcs1_decode.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2021, Helder Eijs <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include "common.h"
33
34 FAKE_INIT(pkcs1_decode)
35
36 #define SIZE_T_MAX ((size_t)-1)
37 #define SIZE_T_LEN (sizeof(size_t))
38
39 STATIC uint8_t rol8(uint8_t x)
40 {
41 return (uint8_t)((x << 1) | (x >> 7));
42 }
43
44 /*
45 * Return 0 if x is 0, otherwise SIZE_T_MAX (all bits set to 1)
46 */
47 STATIC size_t propagate_ones(uint8_t x)
48 {
49 unsigned i;
50 uint8_t result8;
51 size_t result;
52
53 result8 = x;
54 for (i=0; i<8; i++) {
55 x = rol8(x);
56 result8 |= x;
57 }
58 result = 0;
59 for (i=0; i<sizeof(result); i++) {
60 result |= ((size_t)result8) << (i*8);
61 }
62
63 return result;
64 }
65
66 /*
67 * Set all bits to 1 in the flag if term1 == term2
68 * or leave the flag untouched otherwise.
69 */
70 STATIC void set_if_match(uint8_t *flag, size_t term1, size_t term2)
71 {
72 unsigned i;
73 uint8_t x;
74
75 x = 0;
76 for (i=0; i<sizeof(size_t); i++) {
77 x |= (uint8_t)((term1 ^ term2) >> (i*8));
78 }
79 *flag |= (uint8_t)~propagate_ones(x);
80 }
81
82 /*
83 * Set all bits to 1 in the flag if term1 != term2
84 * or leave the flag untouched otherwise.
85 */
86 STATIC void set_if_no_match(uint8_t *flag, size_t term1, size_t term2)
87 {
88 size_t i;
89 uint8_t x;
90
91 x = 0;
92 for (i=0; i<sizeof(size_t); i++) {
93 x |= (uint8_t)((term1 ^ term2) >> (i*8));
94 }
95 *flag |= (uint8_t)propagate_ones(x);
96 }
97
98 /*
99 * Copy in1[] into out[] if choice is 0, otherwise copy in2[]
100 */
101 STATIC void safe_select(const uint8_t *in1, const uint8_t *in2, uint8_t *out, uint8_t choice, size_t len)
102 {
103 size_t i;
104 uint8_t mask1, mask2;
105
106 mask1 = (uint8_t)propagate_ones(choice);
107 mask2 = (uint8_t)~mask1;
108 for (i=0; i<len; i++) {
109 out[i] = (in1[i] & mask2) | (in2[i] & mask1);
110 /* yes, these rol8s are redundant, but we try to avoid compiler optimizations */
111 mask1 = rol8(mask1);
112 mask2 = rol8(mask2);
113 }
114 }
115
116 /*
117 * Return in1 if choice is 0, in2 otherwise.
118 */
119 STATIC size_t safe_select_idx(size_t in1, size_t in2, uint8_t choice)
120 {
121 size_t mask;
122
123 mask = propagate_ones(choice);
124 return (in1 & ~mask) | (in2 & mask);
125 }
126
127 /*
128 * Return 0 if all these conditions hold:
129 * - in1[] is equal to in2[] where eq_mask[] is 0xFF,
130 * - in1[] is NOT equal to in2[] where neq_mask[] is 0xFF.
131 * Return non-zero otherwise.
132 */
133 STATIC uint8_t safe_cmp_masks(const uint8_t *in1, const uint8_t *in2,
134 const uint8_t *eq_mask, const uint8_t *neq_mask,
135 size_t len)
136 {
137 size_t i;
138 uint8_t c, result;
139
140 result = 0;
141 for (i=0; i<len; i++) {
142 c = (uint8_t)propagate_ones(*in1++ ^ *in2++);
143 result |= (uint8_t)(c & *eq_mask++); /* Set all bits to 1 if *in1 and *in2 differed
144 and eq_mask was 0xff */
145 result |= (uint8_t)(~c & *neq_mask++); /* Set all bits to 1 if *in1 and *in2 matched
146 and neq_mask was 0xff */
147 }
148
149 return result;
150 }
151
152 /*
153 * Return the index of the first byte with value c,
154 * the length of in1[] when c is not present,
155 * or SIZE_T_MAX in case of problems.
156 */
157 STATIC size_t safe_search(const uint8_t *in1, uint8_t c, size_t len)
158 {
159 size_t result, mask1, mask2, i;
160 uint8_t *in1_c;
161
162 if (NULL == in1 || 0 == len) {
163 return SIZE_T_MAX;
164 }
165
166 /*
167 * Create a second byte string and put c at the end,
168 * so that at least we will find it there.
169 */
170 in1_c = (uint8_t*) malloc(len + 1);
171 if (NULL == in1_c) {
172 return SIZE_T_MAX;
173 }
174 memcpy(in1_c, in1, len);
175 in1_c[len] = c;
176
177 result = 0;
178 mask2 = 0;
179 for (i=0; i<(len+1); i++) {
180 mask1 = ~mask2 & ~propagate_ones(in1_c[i] ^ c); /* Set mask1 to 0xff if there is a match
181 and it is the first one. */
182 result |= i & mask1;
183 mask2 |= mask1;
184 }
185
186 free(in1_c);
187 return result;
188 }
189
190 #define PKCS1_PREFIX_LEN 10
191
192 /*
193 * Decode and verify the PKCS#1 padding, then put either the plaintext
194 * or the sentinel value into the output buffer, all in constant time.
195 *
196 * The output is a buffer of equal length as the encoded message (em).
197 *
198 * The sentinel is put into the buffer when decryption fails.
199 *
200 * The caller may already know the expected length of the plaintext M,
201 * which they should put into expected_pt_len.
202 * Otherwise, expected_pt_len must be set to 0.
203 *
204 * Either the plaintext or the sentinel will be put into the buffer
205 * with padding on the left.
206 *
207 * The function returns the number of bytes to ignore at the beginning
208 * of the output buffer, or -1 in case of problems.
209 */
210 EXPORT_SYM int pkcs1_decode(const uint8_t *em, size_t len_em_output,
211 const uint8_t *sentinel, size_t len_sentinel,
212 size_t expected_pt_len,
213 uint8_t *output)
214 {
215 size_t pos;
216 uint8_t match, selector;
217 uint8_t *padded_sentinel;
218 int result;
219
220 result = -1;
221
222 if (NULL == em || NULL == output || NULL == sentinel) {
223 return -1;
224 }
225 if (len_em_output < (PKCS1_PREFIX_LEN + 2)) {
226 return -1;
227 }
228 if (len_sentinel > len_em_output) {
229 return -1;
230 }
231 if (expected_pt_len > 0 && expected_pt_len > (len_em_output - PKCS1_PREFIX_LEN - 1)) {
232 return -1;
233 }
234
235 /** Pad sentinel (on the left) so that it matches em in length **/
236 padded_sentinel = (uint8_t*) calloc(1, len_em_output);
237 if (NULL == padded_sentinel) {
238 return -1;
239 }
240 memcpy(padded_sentinel + (len_em_output - len_sentinel), sentinel, len_sentinel);
241
242 /** The first 10 bytes must follow the pattern **/
243 match = safe_cmp_masks(em,
244 (const uint8_t*)"\x00\x02" "\x00\x00\x00\x00\x00\x00\x00\x00",
245 (const uint8_t*)"\xFF\xFF" "\x00\x00\x00\x00\x00\x00\x00\x00",
246 (const uint8_t*)"\x00\x00" "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
247 10);
248
249 /*
250 * pos is the index of the first 0 byte. It is followed by the plaintext M.
251 * It can be (len_em_output-1) when the 0 is at the end (empty M).
252 * It can be len_em_output when the 0 is not present.
253 * It can SIZE_T_MAX in case of other errors.
254 */
255 pos = safe_search(em + 10, 0, len_em_output - 10) + 10;
256 if (pos == SIZE_T_MAX) {
257 result = -1;
258 goto end;
259 }
260
261 /*
262 * selector is 0 if:
263 * - there is a match for the first 10 bytes AND
264 * - a 0 byte is found in the remainder of em, AND
265 * - the length of the plaintext matches the expectation (if there is one)
266 */
267 selector = match;
268 set_if_match(&selector, pos, len_em_output);
269 if (expected_pt_len > 0) {
270 size_t pt_len;
271
272 pt_len = len_em_output - pos - 1;
273 set_if_no_match(&selector, pt_len, expected_pt_len);
274 }
275
276 /** Select the correct data to output **/
277 safe_select(em, padded_sentinel, output, selector, len_em_output);
278
279 /** Select the number of bytes that the caller will skip in output **/
280 result = (int)safe_select_idx(pos + 1, len_em_output - len_sentinel, selector);
281
282 end:
283 free(padded_sentinel);
284 return result;
285 }
286
287 /*
288 * Decode and verify the OAEP padding in constant time.
289 *
290 * The function returns the number of bytes to ignore at the beginning
291 * of db (the rest is the plaintext), or -1 in case of problems.
292 */
293
294 EXPORT_SYM int oaep_decode(const uint8_t *em,
295 size_t em_len,
296 const uint8_t *lHash,
297 size_t hLen,
298 const uint8_t *db,
299 size_t db_len) /* em_len - 1 - hLen */
300 {
301 int result;
302 size_t one_pos, search_len, i;
303 uint8_t wrong_padding;
304 uint8_t *eq_mask = NULL;
305 uint8_t *neq_mask = NULL;
306 uint8_t *target_db = NULL;
307
308 if (NULL == em || NULL == lHash || NULL == db) {
309 return -1;
310 }
311
312 if (em_len < 2*hLen+2 || db_len != em_len-1-hLen) {
313 return -1;
314 }
315
316 /* Allocate */
317 eq_mask = (uint8_t*) calloc(1, db_len);
318 neq_mask = (uint8_t*) calloc(1, db_len);
319 target_db = (uint8_t*) calloc(1, db_len);
320 if (NULL == eq_mask || NULL == neq_mask || NULL == target_db) {
321 result = -1;
322 goto cleanup;
323 }
324
325 /* Step 3g */
326 search_len = db_len - hLen;
327
328 one_pos = safe_search(db + hLen, 0x01, search_len);
329 if (SIZE_T_MAX == one_pos) {
330 result = -1;
331 goto cleanup;
332 }
333
334 memset(eq_mask, 0xAA, db_len);
335 memcpy(target_db, lHash, hLen);
336 memset(eq_mask, 0xFF, hLen);
337
338 for (i=0; i<search_len; i++) {
339 eq_mask[hLen + i] = propagate_ones(i < one_pos);
340 }
341
342 wrong_padding = em[0];
343 wrong_padding |= safe_cmp_masks(db, target_db, eq_mask, neq_mask, db_len);
344 set_if_match(&wrong_padding, one_pos, search_len);
345
346 result = wrong_padding ? -1 : (int)(hLen + 1 + one_pos);
347
348 cleanup:
349 free(eq_mask);
350 free(neq_mask);
351 free(target_db);
352
353 return result;
354 }