Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/base64/base64.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/base64.h>
5
6 #include <assert.h>
7 #include <limits.h>
8 #include <string.h>
9
10 #include <openssl/type_check.h>
11
12 #include "../internal.h"
13
14
15 // constant_time_lt_args_8 behaves like |constant_time_lt_8| but takes |uint8_t|
16 // arguments for a slightly simpler implementation.
17 static inline uint8_t constant_time_lt_args_8(uint8_t a, uint8_t b) {
18 crypto_word_t aw = a;
19 crypto_word_t bw = b;
20 // |crypto_word_t| is larger than |uint8_t|, so |aw| and |bw| have the same
21 // MSB. |aw| < |bw| iff MSB(|aw| - |bw|) is 1.
22 return constant_time_msb_w(aw - bw);
23 }
24
25 // constant_time_in_range_8 returns |CONSTTIME_TRUE_8| if |min| <= |a| <= |max|
26 // and |CONSTTIME_FALSE_8| otherwise.
27 static inline uint8_t constant_time_in_range_8(uint8_t a, uint8_t min,
28 uint8_t max) {
29 a -= min;
30 return constant_time_lt_args_8(a, max - min + 1);
31 }
32
33 // Encoding.
34
35 static uint8_t conv_bin2ascii(uint8_t a) {
36 // Since PEM is sometimes used to carry private keys, we encode base64 data
37 // itself in constant-time.
38 a &= 0x3f;
39 uint8_t ret = constant_time_select_8(constant_time_eq_8(a, 62), '+', '/');
40 ret =
41 constant_time_select_8(constant_time_lt_args_8(a, 62), a - 52 + '0', ret);
42 ret =
43 constant_time_select_8(constant_time_lt_args_8(a, 52), a - 26 + 'a', ret);
44 ret = constant_time_select_8(constant_time_lt_args_8(a, 26), a + 'A', ret);
45 return ret;
46 }
47
48 OPENSSL_STATIC_ASSERT(sizeof(((EVP_ENCODE_CTX *)(NULL))->data) % 3 == 0,
49 _data_length_must_be_a_multiple_of_base64_chunk_size)
50
51 int EVP_EncodedLength(size_t *out_len, size_t len) {
52 if (len + 2 < len) {
53 return 0;
54 }
55 len += 2;
56 len /= 3;
57
58 if (((len << 2) >> 2) != len) {
59 return 0;
60 }
61 len <<= 2;
62
63 if (len + 1 < len) {
64 return 0;
65 }
66 len++;
67
68 *out_len = len;
69 return 1;
70 }
71
72 EVP_ENCODE_CTX *EVP_ENCODE_CTX_new(void) {
73 return OPENSSL_zalloc(sizeof(EVP_ENCODE_CTX));
74 }
75
76 void EVP_ENCODE_CTX_free(EVP_ENCODE_CTX *ctx) {
77 OPENSSL_free(ctx);
78 }
79
80 void EVP_EncodeInit(EVP_ENCODE_CTX *ctx) {
81 OPENSSL_memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
82 }
83
84 int EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
85 const uint8_t *in, size_t in_len) {
86 size_t total = 0;
87
88 *out_len = 0;
89 if (in_len == 0) {
90 return 0;
91 }
92
93 assert(ctx->data_used < sizeof(ctx->data));
94
95 if (sizeof(ctx->data) - ctx->data_used > in_len) {
96 OPENSSL_memcpy(&ctx->data[ctx->data_used], in, in_len);
97 ctx->data_used += (unsigned)in_len;
98 return 1;
99 }
100
101 if (ctx->data_used != 0) {
102 const size_t todo = sizeof(ctx->data) - ctx->data_used;
103 OPENSSL_memcpy(&ctx->data[ctx->data_used], in, todo);
104 in += todo;
105 in_len -= todo;
106
107 size_t encoded = EVP_EncodeBlock(out, ctx->data, sizeof(ctx->data));
108 ctx->data_used = 0;
109
110 out += encoded;
111 *(out++) = '\n';
112 *out = '\0';
113
114 total = encoded + 1;
115 }
116
117 while (in_len >= sizeof(ctx->data)) {
118 size_t encoded = EVP_EncodeBlock(out, in, sizeof(ctx->data));
119 in += sizeof(ctx->data);
120 in_len -= sizeof(ctx->data);
121
122 out += encoded;
123 *(out++) = '\n';
124 *out = '\0';
125
126 if (total + encoded + 1 < total) {
127 *out_len = 0;
128 return 0;
129 }
130
131 total += encoded + 1;
132 }
133
134 if (in_len != 0) {
135 OPENSSL_memcpy(ctx->data, in, in_len);
136 }
137
138 ctx->data_used = (unsigned)in_len;
139
140 if (total > INT_MAX) {
141 // We cannot signal an error, but we can at least avoid making *out_len
142 // negative.
143 *out_len = 0;
144 return 0;
145 }
146 *out_len = (int)total;
147
148 return 1;
149 }
150
151 void EVP_EncodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len) {
152 if (ctx->data_used == 0) {
153 *out_len = 0;
154 return;
155 }
156
157 size_t encoded = EVP_EncodeBlock(out, ctx->data, ctx->data_used);
158 out[encoded++] = '\n';
159 out[encoded] = '\0';
160 ctx->data_used = 0;
161
162 // ctx->data_used is bounded by sizeof(ctx->data), so this does not
163 // overflow.
164 assert(encoded <= INT_MAX);
165 *out_len = (int)encoded;
166 }
167
168 size_t EVP_EncodeBlock(uint8_t *dst, const uint8_t *src, size_t src_len) {
169 uint32_t l;
170 size_t remaining = src_len, ret = 0;
171
172 while (remaining) {
173 if (remaining >= 3) {
174 l = (((uint32_t)src[0]) << 16L) | (((uint32_t)src[1]) << 8L) | src[2];
175 *(dst++) = conv_bin2ascii(l >> 18L);
176 *(dst++) = conv_bin2ascii(l >> 12L);
177 *(dst++) = conv_bin2ascii(l >> 6L);
178 *(dst++) = conv_bin2ascii(l);
179 remaining -= 3;
180 } else {
181 l = ((uint32_t)src[0]) << 16L;
182 if (remaining == 2) {
183 l |= ((uint32_t)src[1] << 8L);
184 }
185
186 *(dst++) = conv_bin2ascii(l >> 18L);
187 *(dst++) = conv_bin2ascii(l >> 12L);
188 *(dst++) = (remaining == 1) ? '=' : conv_bin2ascii(l >> 6L);
189 *(dst++) = '=';
190 remaining = 0;
191 }
192 ret += 4;
193 src += 3;
194 }
195
196 *dst = '\0';
197 return ret;
198 }
199
200
201 // Decoding.
202
203 int EVP_DecodedLength(size_t *out_len, size_t len) {
204 if (len % 4 != 0) {
205 return 0;
206 }
207
208 *out_len = (len / 4) * 3;
209 return 1;
210 }
211
212 void EVP_DecodeInit(EVP_ENCODE_CTX *ctx) {
213 OPENSSL_memset(ctx, 0, sizeof(EVP_ENCODE_CTX));
214 }
215
216 static uint8_t base64_ascii_to_bin(uint8_t a) {
217 // Since PEM is sometimes used to carry private keys, we decode base64 data
218 // itself in constant-time.
219 const uint8_t is_upper = constant_time_in_range_8(a, 'A', 'Z');
220 const uint8_t is_lower = constant_time_in_range_8(a, 'a', 'z');
221 const uint8_t is_digit = constant_time_in_range_8(a, '0', '9');
222 const uint8_t is_plus = constant_time_eq_8(a, '+');
223 const uint8_t is_slash = constant_time_eq_8(a, '/');
224 const uint8_t is_equals = constant_time_eq_8(a, '=');
225
226 uint8_t ret = 0;
227 ret |= is_upper & (a - 'A'); // [0,26)
228 ret |= is_lower & (a - 'a' + 26); // [26,52)
229 ret |= is_digit & (a - '0' + 52); // [52,62)
230 ret |= is_plus & 62;
231 ret |= is_slash & 63;
232 // Invalid inputs, 'A', and '=' have all been mapped to zero. Map invalid
233 // inputs to 0xff. Note '=' is padding and handled separately by the caller.
234 const uint8_t is_valid =
235 is_upper | is_lower | is_digit | is_plus | is_slash | is_equals;
236 ret |= ~is_valid;
237 return ret;
238 }
239
240 // base64_decode_quad decodes a single “quad” (i.e. four characters) of base64
241 // data and writes up to three bytes to |out|. It sets |*out_num_bytes| to the
242 // number of bytes written, which will be less than three if the quad ended
243 // with padding. It returns one on success or zero on error.
244 static int base64_decode_quad(uint8_t *out, size_t *out_num_bytes,
245 const uint8_t *in) {
246 const uint8_t a = base64_ascii_to_bin(in[0]);
247 const uint8_t b = base64_ascii_to_bin(in[1]);
248 const uint8_t c = base64_ascii_to_bin(in[2]);
249 const uint8_t d = base64_ascii_to_bin(in[3]);
250 if (a == 0xff || b == 0xff || c == 0xff || d == 0xff) {
251 return 0;
252 }
253
254 const uint32_t v = ((uint32_t)a) << 18 | ((uint32_t)b) << 12 |
255 ((uint32_t)c) << 6 | (uint32_t)d;
256
257 const unsigned padding_pattern = (in[0] == '=') << 3 |
258 (in[1] == '=') << 2 |
259 (in[2] == '=') << 1 |
260 (in[3] == '=');
261
262 switch (padding_pattern) {
263 case 0:
264 // The common case of no padding.
265 *out_num_bytes = 3;
266 out[0] = v >> 16;
267 out[1] = v >> 8;
268 out[2] = v;
269 break;
270
271 case 1: // xxx=
272 *out_num_bytes = 2;
273 out[0] = v >> 16;
274 out[1] = v >> 8;
275 break;
276
277 case 3: // xx==
278 *out_num_bytes = 1;
279 out[0] = v >> 16;
280 break;
281
282 default:
283 return 0;
284 }
285
286 return 1;
287 }
288
289 int EVP_DecodeUpdate(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len,
290 const uint8_t *in, size_t in_len) {
291 *out_len = 0;
292
293 if (ctx->error_encountered) {
294 return -1;
295 }
296
297 size_t bytes_out = 0, i;
298 for (i = 0; i < in_len; i++) {
299 const char c = in[i];
300 switch (c) {
301 case ' ':
302 case '\t':
303 case '\r':
304 case '\n':
305 continue;
306 }
307
308 if (ctx->eof_seen) {
309 ctx->error_encountered = 1;
310 return -1;
311 }
312
313 ctx->data[ctx->data_used++] = c;
314 if (ctx->data_used == 4) {
315 size_t num_bytes_resulting;
316 if (!base64_decode_quad(out, &num_bytes_resulting, ctx->data)) {
317 ctx->error_encountered = 1;
318 return -1;
319 }
320
321 ctx->data_used = 0;
322 bytes_out += num_bytes_resulting;
323 out += num_bytes_resulting;
324
325 if (num_bytes_resulting < 3) {
326 ctx->eof_seen = 1;
327 }
328 }
329 }
330
331 if (bytes_out > INT_MAX) {
332 ctx->error_encountered = 1;
333 *out_len = 0;
334 return -1;
335 }
336 *out_len = (int)bytes_out;
337
338 if (ctx->eof_seen) {
339 return 0;
340 }
341
342 return 1;
343 }
344
345 int EVP_DecodeFinal(EVP_ENCODE_CTX *ctx, uint8_t *out, int *out_len) {
346 *out_len = 0;
347 if (ctx->error_encountered || ctx->data_used != 0) {
348 return -1;
349 }
350
351 return 1;
352 }
353
354 int EVP_DecodeBase64(uint8_t *out, size_t *out_len, size_t max_out,
355 const uint8_t *in, size_t in_len) {
356 *out_len = 0;
357
358 if (in_len % 4 != 0) {
359 return 0;
360 }
361
362 size_t max_len;
363 if (!EVP_DecodedLength(&max_len, in_len) ||
364 max_out < max_len) {
365 return 0;
366 }
367
368 size_t i, bytes_out = 0;
369 for (i = 0; i < in_len; i += 4) {
370 size_t num_bytes_resulting;
371
372 if (!base64_decode_quad(out, &num_bytes_resulting, &in[i])) {
373 return 0;
374 }
375
376 bytes_out += num_bytes_resulting;
377 out += num_bytes_resulting;
378 if (num_bytes_resulting != 3 && i != in_len - 4) {
379 return 0;
380 }
381 }
382
383 *out_len = bytes_out;
384 return 1;
385 }
386
387 int EVP_DecodeBlock(uint8_t *dst, const uint8_t *src, size_t src_len) {
388 // Trim spaces and tabs from the beginning of the input.
389 while (src_len > 0) {
390 if (src[0] != ' ' && src[0] != '\t') {
391 break;
392 }
393
394 src++;
395 src_len--;
396 }
397
398 // Trim newlines, spaces and tabs from the end of the line.
399 while (src_len > 0) {
400 switch (src[src_len-1]) {
401 case ' ':
402 case '\t':
403 case '\r':
404 case '\n':
405 src_len--;
406 continue;
407 }
408
409 break;
410 }
411
412 size_t dst_len;
413 if (!EVP_DecodedLength(&dst_len, src_len) ||
414 dst_len > INT_MAX ||
415 !EVP_DecodeBase64(dst, &dst_len, dst_len, src, src_len)) {
416 return -1;
417 }
418
419 // EVP_DecodeBlock does not take padding into account, so put the
420 // NULs back in... so the caller can strip them back out.
421 while (dst_len % 3 != 0) {
422 dst[dst_len++] = '\0';
423 }
424 assert(dst_len <= INT_MAX);
425
426 return (int)dst_len;
427 }