From c229663acf6083867b8df35aeb8647e6d4db1270 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 30 Mar 2017 10:15:06 +0200 Subject: [PATCH] Argon2: add specialized macro to decode uint32_t values --- .../crypto_pwhash/argon2/argon2-encoding.c | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/libsodium/crypto_pwhash/argon2/argon2-encoding.c b/src/libsodium/crypto_pwhash/argon2/argon2-encoding.c index 264bddd8..961e96b0 100644 --- a/src/libsodium/crypto_pwhash/argon2/argon2-encoding.c +++ b/src/libsodium/crypto_pwhash/argon2/argon2-encoding.c @@ -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("$");