Argon2: add specialized macro to decode uint32_t values

This commit is contained in:
Frank Denis 2017-03-30 10:15:06 +02:00
parent 4dec1da7c5
commit c229663acf

View File

@ -288,6 +288,17 @@ decode_string(argon2_context *ctx, const char *str, argon2_type type)
(x) = dec_x; \
} while ((void) 0, 0)
/* Decoding prefix into uint32_t decimal */
#define DECIMAL_U32(x) \
do { \
unsigned long dec_x; \
str = decode_decimal(str, &dec_x); \
if (str == NULL || dec_x > UINT32_MAX) { \
return ARGON2_DECODING_FAIL; \
} \
(x) = (uint32_t)dec_x; \
} while ((void)0, 0)
/* Decoding base64 into a binary buffer */
#define BIN(buf, max_len, len) \
do { \
@ -301,9 +312,8 @@ decode_string(argon2_context *ctx, const char *str, argon2_type type)
size_t maxsaltlen = ctx->saltlen;
size_t maxoutlen = ctx->outlen;
unsigned long val;
unsigned long version = 0;
int validation_result;
uint32_t version = 0;
ctx->saltlen = 0;
ctx->outlen = 0;
@ -314,28 +324,25 @@ decode_string(argon2_context *ctx, const char *str, argon2_type type)
return ARGON2_INCORRECT_TYPE;
}
CC("$v=");
DECIMAL(version);
DECIMAL_U32(version);
if (version != ARGON2_VERSION_NUMBER) {
return ARGON2_INCORRECT_TYPE;
}
CC("$m=");
DECIMAL(val);
if (val > UINT32_MAX) {
DECIMAL_U32(ctx->m_cost);
if (ctx->m_cost > UINT32_MAX) {
return ARGON2_INCORRECT_TYPE;
}
ctx->m_cost = (uint32_t) val;
CC(",t=");
DECIMAL(val);
if (val > UINT32_MAX) {
DECIMAL_U32(ctx->t_cost);
if (ctx->t_cost > UINT32_MAX) {
return ARGON2_INCORRECT_TYPE;
}
ctx->t_cost = (uint32_t) val;
CC(",p=");
DECIMAL(val);
if (val > UINT32_MAX) {
DECIMAL_U32(ctx->lanes);
if (ctx->lanes > UINT32_MAX) {
return ARGON2_INCORRECT_TYPE;
}
ctx->lanes = (uint32_t) val;
ctx->threads = ctx->lanes;
CC("$");