| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <stdio.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <limits.h> |
| 22 |
#include "encoding.h" |
| 23 |
#include "core.h" |
| 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 |
#define EQ(x, y) ((((0U - ((unsigned)(x) ^ (unsigned)(y))) >> 8) & 0xFF) ^ 0xFF) |
| 75 |
#define GT(x, y) ((((unsigned)(y) - (unsigned)(x)) >> 8) & 0xFF) |
| 76 |
#define GE(x, y) (GT(y, x) ^ 0xFF) |
| 77 |
#define LT(x, y) GT(y, x) |
| 78 |
#define LE(x, y) GE(y, x) |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
static int b64_byte_to_char(unsigned x) { |
| 84 |
return (LT(x, 26) & (x + 'A')) | |
| 85 |
(GE(x, 26) & LT(x, 52) & (x + ('a' - 26))) | |
| 86 |
(GE(x, 52) & LT(x, 62) & (x + ('0' - 52))) | (EQ(x, 62) & '+') | |
| 87 |
(EQ(x, 63) & '/'); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
static unsigned b64_char_to_byte(int c) { |
| 95 |
unsigned x; |
| 96 |
|
| 97 |
x = (GE(c, 'A') & LE(c, 'Z') & (c - 'A')) | |
| 98 |
(GE(c, 'a') & LE(c, 'z') & (c - ('a' - 26))) | |
| 99 |
(GE(c, '0') & LE(c, '9') & (c - ('0' - 52))) | (EQ(c, '+') & 62) | |
| 100 |
(EQ(c, '/') & 63); |
| 101 |
return x | (EQ(x, 0) & (EQ(c, 'A') ^ 0xFF)); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
static size_t to_base64(char *dst, size_t dst_len, const void *src, |
| 113 |
size_t src_len) { |
| 114 |
size_t olen; |
| 115 |
const unsigned char *buf; |
| 116 |
unsigned acc, acc_len; |
| 117 |
|
| 118 |
olen = (src_len / 3) << 2; |
| 119 |
switch (src_len % 3) { |
| 120 |
case 2: |
| 121 |
olen++; |
| 122 |
|
| 123 |
case 1: |
| 124 |
olen += 2; |
| 125 |
break; |
| 126 |
} |
| 127 |
if (dst_len <= olen) { |
| 128 |
return (size_t)-1; |
| 129 |
} |
| 130 |
acc = 0; |
| 131 |
acc_len = 0; |
| 132 |
buf = (const unsigned char *)src; |
| 133 |
while (src_len-- > 0) { |
| 134 |
acc = (acc << 8) + (*buf++); |
| 135 |
acc_len += 8; |
| 136 |
while (acc_len >= 6) { |
| 137 |
acc_len -= 6; |
| 138 |
*dst++ = (char)b64_byte_to_char((acc >> acc_len) & 0x3F); |
| 139 |
} |
| 140 |
} |
| 141 |
if (acc_len > 0) { |
| 142 |
*dst++ = (char)b64_byte_to_char((acc << (6 - acc_len)) & 0x3F); |
| 143 |
} |
| 144 |
*dst++ = 0; |
| 145 |
return olen; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
static const char *from_base64(void *dst, size_t *dst_len, const char *src) { |
| 162 |
size_t len; |
| 163 |
unsigned char *buf; |
| 164 |
unsigned acc, acc_len; |
| 165 |
|
| 166 |
buf = (unsigned char *)dst; |
| 167 |
len = 0; |
| 168 |
acc = 0; |
| 169 |
acc_len = 0; |
| 170 |
for (;;) { |
| 171 |
unsigned d; |
| 172 |
|
| 173 |
d = b64_char_to_byte(*src); |
| 174 |
if (d == 0xFF) { |
| 175 |
break; |
| 176 |
} |
| 177 |
src++; |
| 178 |
acc = (acc << 6) + d; |
| 179 |
acc_len += 6; |
| 180 |
if (acc_len >= 8) { |
| 181 |
acc_len -= 8; |
| 182 |
if ((len++) >= *dst_len) { |
| 183 |
return NULL; |
| 184 |
} |
| 185 |
*buf++ = (acc >> acc_len) & 0xFF; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
if (acc_len > 4 || (acc & (((unsigned)1 << acc_len) - 1)) != 0) { |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
*dst_len = len; |
| 199 |
return src; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
static const char *decode_decimal(const char *str, unsigned long *v) { |
| 210 |
const char *orig; |
| 211 |
unsigned long acc; |
| 212 |
|
| 213 |
acc = 0; |
| 214 |
for (orig = str;; str++) { |
| 215 |
int c; |
| 216 |
|
| 217 |
c = *str; |
| 218 |
if (c < '0' || c > '9') { |
| 219 |
break; |
| 220 |
} |
| 221 |
c -= '0'; |
| 222 |
if (acc > (ULONG_MAX / 10)) { |
| 223 |
return NULL; |
| 224 |
} |
| 225 |
acc *= 10; |
| 226 |
if ((unsigned long)c > (ULONG_MAX - acc)) { |
| 227 |
return NULL; |
| 228 |
} |
| 229 |
acc += (unsigned long)c; |
| 230 |
} |
| 231 |
if (str == orig || (*orig == '0' && str != (orig + 1))) { |
| 232 |
return NULL; |
| 233 |
} |
| 234 |
*v = acc; |
| 235 |
return str; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
int decode_string(argon2_context *ctx, const char *str, argon2_type type) { |
| 259 |
|
| 260 |
|
| 261 |
#define CC(prefix) \ |
| 262 |
do { \ |
| 263 |
size_t cc_len = strlen(prefix); \ |
| 264 |
if (strncmp(str, prefix, cc_len) != 0) { \ |
| 265 |
return ARGON2_DECODING_FAIL; \ |
| 266 |
} \ |
| 267 |
str += cc_len; \ |
| 268 |
} while ((void)0, 0) |
| 269 |
|
| 270 |
|
| 271 |
#define CC_opt(prefix, code) \ |
| 272 |
do { \ |
| 273 |
size_t cc_len = strlen(prefix); \ |
| 274 |
if (strncmp(str, prefix, cc_len) == 0) { \ |
| 275 |
str += cc_len; \ |
| 276 |
{ code; } \ |
| 277 |
} \ |
| 278 |
} while ((void)0, 0) |
| 279 |
|
| 280 |
|
| 281 |
#define DECIMAL(x) \ |
| 282 |
do { \ |
| 283 |
unsigned long dec_x; \ |
| 284 |
str = decode_decimal(str, &dec_x); \ |
| 285 |
if (str == NULL) { \ |
| 286 |
return ARGON2_DECODING_FAIL; \ |
| 287 |
} \ |
| 288 |
(x) = dec_x; \ |
| 289 |
} while ((void)0, 0) |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
#define DECIMAL_U32(x) \ |
| 294 |
do { \ |
| 295 |
unsigned long dec_x; \ |
| 296 |
str = decode_decimal(str, &dec_x); \ |
| 297 |
if (str == NULL || dec_x > UINT32_MAX) { \ |
| 298 |
return ARGON2_DECODING_FAIL; \ |
| 299 |
} \ |
| 300 |
(x) = (uint32_t)dec_x; \ |
| 301 |
} while ((void)0, 0) |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
#define BIN(buf, max_len, len) \ |
| 306 |
do { \ |
| 307 |
size_t bin_len = (max_len); \ |
| 308 |
str = from_base64(buf, &bin_len, str); \ |
| 309 |
if (str == NULL || bin_len > UINT32_MAX) { \ |
| 310 |
return ARGON2_DECODING_FAIL; \ |
| 311 |
} \ |
| 312 |
(len) = (uint32_t)bin_len; \ |
| 313 |
} while ((void)0, 0) |
| 314 |
|
| 315 |
size_t maxsaltlen = ctx->saltlen; |
| 316 |
size_t maxoutlen = ctx->outlen; |
| 317 |
int validation_result; |
| 318 |
const char* type_string; |
| 319 |
|
| 320 |
|
| 321 |
type_string = argon2_type2string(type, 0); |
| 322 |
if (!type_string) { |
| 323 |
return ARGON2_INCORRECT_TYPE; |
| 324 |
} |
| 325 |
|
| 326 |
CC("$"); |
| 327 |
CC(type_string); |
| 328 |
|
| 329 |
|
| 330 |
ctx->version = ARGON2_VERSION_10; |
| 331 |
CC_opt("$v=", DECIMAL_U32(ctx->version)); |
| 332 |
|
| 333 |
CC("$m="); |
| 334 |
DECIMAL_U32(ctx->m_cost); |
| 335 |
CC(",t="); |
| 336 |
DECIMAL_U32(ctx->t_cost); |
| 337 |
CC(",p="); |
| 338 |
DECIMAL_U32(ctx->lanes); |
| 339 |
ctx->threads = ctx->lanes; |
| 340 |
|
| 341 |
CC("$"); |
| 342 |
BIN(ctx->salt, maxsaltlen, ctx->saltlen); |
| 343 |
CC("$"); |
| 344 |
BIN(ctx->out, maxoutlen, ctx->outlen); |
| 345 |
|
| 346 |
|
| 347 |
ctx->secret = NULL; |
| 348 |
ctx->secretlen = 0; |
| 349 |
ctx->ad = NULL; |
| 350 |
ctx->adlen = 0; |
| 351 |
ctx->allocate_cbk = NULL; |
| 352 |
ctx->free_cbk = NULL; |
| 353 |
ctx->flags = ARGON2_DEFAULT_FLAGS; |
| 354 |
|
| 355 |
|
| 356 |
validation_result = validate_inputs(ctx); |
| 357 |
if (validation_result != ARGON2_OK) { |
| 358 |
return validation_result; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
if (*str == 0) { |
| 363 |
return ARGON2_OK; |
| 364 |
} else { |
| 365 |
return ARGON2_DECODING_FAIL; |
| 366 |
} |
| 367 |
#undef CC |
| 368 |
#undef CC_opt |
| 369 |
#undef DECIMAL |
| 370 |
#undef BIN |
| 371 |
} |
| 372 |
|
| 373 |
int encode_string(char *dst, size_t dst_len, argon2_context *ctx, |
| 374 |
argon2_type type) { |
| 375 |
#define SS(str) \ |
| 376 |
do { \ |
| 377 |
size_t pp_len = strlen(str); \ |
| 378 |
if (pp_len >= dst_len) { \ |
| 379 |
return ARGON2_ENCODING_FAIL; \ |
| 380 |
} \ |
| 381 |
memcpy(dst, str, pp_len + 1); \ |
| 382 |
dst += pp_len; \ |
| 383 |
dst_len -= pp_len; \ |
| 384 |
} while ((void)0, 0) |
| 385 |
|
| 386 |
#define SX(x) \ |
| 387 |
do { \ |
| 388 |
char tmp[30]; \ |
| 389 |
sprintf(tmp, "%lu", (unsigned long)(x)); \ |
| 390 |
SS(tmp); \ |
| 391 |
} while ((void)0, 0) |
| 392 |
|
| 393 |
#define SB(buf, len) \ |
| 394 |
do { \ |
| 395 |
size_t sb_len = to_base64(dst, dst_len, buf, len); \ |
| 396 |
if (sb_len == (size_t)-1) { \ |
| 397 |
return ARGON2_ENCODING_FAIL; \ |
| 398 |
} \ |
| 399 |
dst += sb_len; \ |
| 400 |
dst_len -= sb_len; \ |
| 401 |
} while ((void)0, 0) |
| 402 |
|
| 403 |
const char* type_string = argon2_type2string(type, 0); |
| 404 |
int validation_result = validate_inputs(ctx); |
| 405 |
|
| 406 |
if (!type_string) { |
| 407 |
return ARGON2_ENCODING_FAIL; |
| 408 |
} |
| 409 |
|
| 410 |
if (validation_result != ARGON2_OK) { |
| 411 |
return validation_result; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
SS("$"); |
| 416 |
SS(type_string); |
| 417 |
|
| 418 |
SS("$v="); |
| 419 |
SX(ctx->version); |
| 420 |
|
| 421 |
SS("$m="); |
| 422 |
SX(ctx->m_cost); |
| 423 |
SS(",t="); |
| 424 |
SX(ctx->t_cost); |
| 425 |
SS(",p="); |
| 426 |
SX(ctx->lanes); |
| 427 |
|
| 428 |
SS("$"); |
| 429 |
SB(ctx->salt, ctx->saltlen); |
| 430 |
|
| 431 |
SS("$"); |
| 432 |
SB(ctx->out, ctx->outlen); |
| 433 |
return ARGON2_OK; |
| 434 |
|
| 435 |
#undef SS |
| 436 |
#undef SX |
| 437 |
#undef SB |
| 438 |
} |
| 439 |
|
| 440 |
size_t b64len(uint32_t len) { |
| 441 |
size_t olen = ((size_t)len / 3) << 2; |
| 442 |
|
| 443 |
switch (len % 3) { |
| 444 |
case 2: |
| 445 |
olen++; |
| 446 |
|
| 447 |
case 1: |
| 448 |
olen += 2; |
| 449 |
break; |
| 450 |
} |
| 451 |
|
| 452 |
return olen; |
| 453 |
} |
| 454 |
|
| 455 |
size_t numlen(uint32_t num) { |
| 456 |
size_t len = 1; |
| 457 |
while (num >= 10) { |
| 458 |
++len; |
| 459 |
num = num / 10; |
| 460 |
} |
| 461 |
return len; |
| 462 |
} |
| 463 |
|