1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "core.h"
#include "utils.h"
/* Derived from original code by CodesInChaos */
char *
sodium_bin2hex(char *const hex, const size_t hex_maxlen,
const unsigned char *const bin, const size_t bin_len)
{
size_t i = (size_t) 0U;
unsigned int x;
int b;
int c;
if (bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U) {
sodium_misuse(); /* LCOV_EXCL_LINE */
}
while (i < bin_len) {
c = bin[i] & 0xf;
b = bin[i] >> 4;
x = (unsigned char) (87U + c + (((c - 10U) >> 8) & ~38U)) << 8 |
(unsigned char) (87U + b + (((b - 10U) >> 8) & ~38U));
hex[i * 2U] = (char) x;
x >>= 8;
hex[i * 2U + 1U] = (char) x;
i++;
}
hex[i * 2U] = 0U;
return hex;
}
int
sodium_hex2bin(unsigned char *const bin, const size_t bin_maxlen,
const char *const hex, const size_t hex_len,
const char *const ignore, size_t *const bin_len,
const char **const hex_end)
{
size_t bin_pos = (size_t) 0U;
size_t hex_pos = (size_t) 0U;
int ret = 0;
unsigned char c;
unsigned char c_acc = 0U;
unsigned char c_alpha0, c_alpha;
unsigned char c_num0, c_num;
unsigned char c_val;
unsigned char state = 0U;
while (hex_pos < hex_len) {
c = (unsigned char) hex[hex_pos];
c_num = c ^ 48U;
c_num0 = (c_num - 10U) >> 8;
c_alpha = (c & ~32U) - 55U;
c_alpha0 = ((c_alpha - 10U) ^ (c_alpha - 16U)) >> 8;
if ((c_num0 | c_alpha0) == 0U) {
if (ignore != NULL && state == 0U && strchr(ignore, c) != NULL) {
hex_pos++;
continue;
}
break;
}
c_val = (c_num0 & c_num) | (c_alpha0 & c_alpha);
if (bin_pos >= bin_maxlen) {
ret = -1;
errno = ERANGE;
break;
}
if (state == 0U) {
c_acc = c_val * 16U;
} else {
bin[bin_pos++] = c_acc | c_val;
}
state = ~state;
hex_pos++;
}
if (state != 0U) {
hex_pos--;
errno = EINVAL;
ret = -1;
}
if (ret != 0) {
bin_pos = (size_t) 0U;
}
if (hex_end != NULL) {
*hex_end = &hex[hex_pos];
} else if (hex_pos != hex_len) {
errno = EINVAL;
ret = -1;
}
if (bin_len != NULL) {
*bin_len = bin_pos;
}
return ret;
}
/*
* Some macros for constant-time comparisons. These work over values in
* the 0..255 range. Returned value is 0x00 on "false", 0xFF on "true".
*
* Original code by Thomas Pornin.
*/
#define EQ(x, y) \
((((0U - ((unsigned int) (x) ^ (unsigned int) (y))) >> 8) & 0xFF) ^ 0xFF)
#define GT(x, y) ((((unsigned int) (y) - (unsigned int) (x)) >> 8) & 0xFF)
#define GE(x, y) (GT(y, x) ^ 0xFF)
#define LT(x, y) GT(y, x)
#define LE(x, y) GE(y, x)
static int
b64_byte_to_char(unsigned int x)
{
return (LT(x, 26) & (x + 'A')) |
(GE(x, 26) & LT(x, 52) & (x + ('a' - 26))) |
(GE(x, 52) & LT(x, 62) & (x + ('0' - 52))) | (EQ(x, 62) & '+') |
(EQ(x, 63) & '/');
}
static unsigned int
b64_char_to_byte(int c)
{
const unsigned int x =
(GE(c, 'A') & LE(c, 'Z') & (c - 'A')) |
(GE(c, 'a') & LE(c, 'z') & (c - ('a' - 26))) |
(GE(c, '0') & LE(c, '9') & (c - ('0' - 52))) | (EQ(c, '+') & 62) |
(EQ(c, '/') & 63);
return x | (EQ(x, 0) & (EQ(c, 'A') ^ 0xFF));
}
static int
b64_byte_to_urlsafe_char(unsigned int x)
{
return (LT(x, 26) & (x + 'A')) |
(GE(x, 26) & LT(x, 52) & (x + ('a' - 26))) |
(GE(x, 52) & LT(x, 62) & (x + ('0' - 52))) | (EQ(x, 62) & '-') |
(EQ(x, 63) & '_');
}
static unsigned int
b64_urlsafe_char_to_byte(int c)
{
const unsigned x =
(GE(c, 'A') & LE(c, 'Z') & (c - 'A')) |
(GE(c, 'a') & LE(c, 'z') & (c - ('a' - 26))) |
(GE(c, '0') & LE(c, '9') & (c - ('0' - 52))) | (EQ(c, '-') & 62) |
(EQ(c, '_') & 63);
return x | (EQ(x, 0) & (EQ(c, 'A') ^ 0xFF));
}
#define VARIANT_NO_PADDING_MASK 0x2U
#define VARIANT_URLSAFE_MASK 0x4U
static void
sodium_base64_check_variant(const int variant)
{
if ((((unsigned int) variant) & ~ 0x6U) != 0x1U) {
sodium_misuse();
}
}
size_t
sodium_base64_encoded_len(const size_t bin_len, const int variant)
{
sodium_base64_check_variant(variant);
return sodium_base64_ENCODED_LEN(bin_len, variant);
}
char *
sodium_bin2base64(char * const b64, const size_t b64_maxlen,
const unsigned char * const bin, const size_t bin_len,
const int variant)
{
size_t acc_len = (size_t) 0;
size_t b64_len;
size_t b64_pos = (size_t) 0;
size_t bin_pos = (size_t) 0;
size_t nibbles;
size_t remainder;
unsigned int acc = 0U;
sodium_base64_check_variant(variant);
nibbles = bin_len / 3;
remainder = bin_len - 3 * nibbles;
b64_len = nibbles * 4;
if (remainder != 0) {
if ((((unsigned int) variant) & VARIANT_NO_PADDING_MASK) == 0U) {
b64_len += 4;
} else {
b64_len += 2 + (remainder >> 1);
}
}
if (b64_maxlen <= b64_len) {
sodium_misuse();
}
if ((((unsigned int) variant) & VARIANT_URLSAFE_MASK) != 0U) {
while (bin_pos < bin_len) {
acc = (acc << 8) + bin[bin_pos++];
acc_len += 8;
while (acc_len >= 6) {
acc_len -= 6;
b64[b64_pos++] = (char) b64_byte_to_urlsafe_char((acc >> acc_len) & 0x3F);
}
}
if (acc_len > 0) {
b64[b64_pos++] = (char) b64_byte_to_urlsafe_char((acc << (6 - acc_len)) & 0x3F);
}
} else {
while (bin_pos < bin_len) {
acc = (acc << 8) + bin[bin_pos++];
acc_len += 8;
while (acc_len >= 6) {
acc_len -= 6;
b64[b64_pos++] = (char) b64_byte_to_char((acc >> acc_len) & 0x3F);
}
}
if (acc_len > 0) {
b64[b64_pos++] = (char) b64_byte_to_char((acc << (6 - acc_len)) & 0x3F);
}
}
assert(b64_pos <= b64_len);
while (b64_pos < b64_len) {
b64[b64_pos++] = '=';
}
do {
b64[b64_pos++] = 0U;
} while (b64_pos < b64_maxlen);
return b64;
}
static int
_sodium_base642bin_skip_padding(const char * const b64, const size_t b64_len,
size_t * const b64_pos_p,
const char * const ignore, size_t padding_len)
{
int c;
while (padding_len > 0) {
if (*b64_pos_p >= b64_len) {
errno = ERANGE;
return -1;
}
c = b64[*b64_pos_p];
if (c == '=') {
padding_len--;
} else if (ignore == NULL || strchr(ignore, c) == NULL) {
errno = EINVAL;
return -1;
}
(*b64_pos_p)++;
}
return 0;
}
int
sodium_base642bin(unsigned char * const bin, const size_t bin_maxlen,
const char * const b64, const size_t b64_len,
const char * const ignore, size_t * const bin_len,
const char ** const b64_end, const int variant)
{
size_t acc_len = (size_t) 0;
size_t b64_pos = (size_t) 0;
size_t bin_pos = (size_t) 0;
int is_urlsafe;
int ret = 0;
unsigned int acc = 0U;
unsigned int d;
char c;
sodium_base64_check_variant(variant);
is_urlsafe = ((unsigned int) variant) & VARIANT_URLSAFE_MASK;
while (b64_pos < b64_len) {
c = b64[b64_pos];
if (is_urlsafe) {
d = b64_urlsafe_char_to_byte(c);
} else {
d = b64_char_to_byte(c);
}
if (d == 0xFF) {
if (ignore != NULL && strchr(ignore, c) != NULL) {
b64_pos++;
continue;
}
break;
}
acc = (acc << 6) + d;
acc_len += 6;
if (acc_len >= 8) {
acc_len -= 8;
if (bin_pos >= bin_maxlen) {
errno = ERANGE;
ret = -1;
break;
}
bin[bin_pos++] = (acc >> acc_len) & 0xFF;
}
b64_pos++;
}
if (acc_len > 4U || (acc & ((1U << acc_len) - 1U)) != 0U) {
ret = -1;
} else if (ret == 0 &&
(((unsigned int) variant) & VARIANT_NO_PADDING_MASK) == 0U) {
ret = _sodium_base642bin_skip_padding(b64, b64_len, &b64_pos, ignore,
acc_len / 2);
}
if (ret != 0) {
bin_pos = (size_t) 0U;
} else if (ignore != NULL) {
while (b64_pos < b64_len && strchr(ignore, b64[b64_pos]) != NULL) {
b64_pos++;
}
}
if (b64_end != NULL) {
*b64_end = &b64[b64_pos];
} else if (b64_pos != b64_len) {
errno = EINVAL;
ret = -1;
}
if (bin_len != NULL) {
*bin_len = bin_pos;
}
return ret;
}
|