mirror of
https://github.com/jedisct1/libsodium.git
synced 2026-08-25 08:37:13 +09:00
Make Argon2 encode/decode return codes consistent with other functions
This commit is contained in:
@@ -245,7 +245,7 @@ static const char *decode_decimal(const char *str, unsigned long *v) {
|
||||
|
||||
/*
|
||||
* Decode an Argon2i hash string into the provided structure 'ctx'.
|
||||
* Returned value is 1 on success, 0 on error.
|
||||
* Returned value is ARGON2_OK on success.
|
||||
*/
|
||||
int decode_string(argon2_context *ctx, const char *str, argon2_type type) {
|
||||
/* Prefix checking */
|
||||
@@ -253,7 +253,7 @@ int decode_string(argon2_context *ctx, const char *str, argon2_type type) {
|
||||
do { \
|
||||
size_t cc_len = strlen(prefix); \
|
||||
if (strncmp(str, prefix, cc_len) != 0) { \
|
||||
return 0; \
|
||||
return ARGON2_DECODING_FAIL; \
|
||||
} \
|
||||
str += cc_len; \
|
||||
} while ((void)0, 0)
|
||||
@@ -274,7 +274,7 @@ int decode_string(argon2_context *ctx, const char *str, argon2_type type) {
|
||||
unsigned long dec_x; \
|
||||
str = decode_decimal(str, &dec_x); \
|
||||
if (str == NULL) { \
|
||||
return 0; \
|
||||
return ARGON2_DECODING_FAIL; \
|
||||
} \
|
||||
(x) = dec_x; \
|
||||
} while ((void)0, 0)
|
||||
@@ -285,7 +285,7 @@ int decode_string(argon2_context *ctx, const char *str, argon2_type type) {
|
||||
size_t bin_len = (max_len); \
|
||||
str = from_base64(buf, &bin_len, str); \
|
||||
if (str == NULL || bin_len > UINT32_MAX) { \
|
||||
return 0; \
|
||||
return ARGON2_DECODING_FAIL; \
|
||||
} \
|
||||
(len) = (uint32_t)bin_len; \
|
||||
} while ((void)0, 0)
|
||||
@@ -294,6 +294,7 @@ int decode_string(argon2_context *ctx, const char *str, argon2_type type) {
|
||||
size_t maxsaltlen = ctx->saltlen;
|
||||
size_t maxoutlen = ctx->outlen;
|
||||
unsigned long version = 0;
|
||||
int validation_result;
|
||||
|
||||
ctx->adlen = 0;
|
||||
ctx->saltlen = 0;
|
||||
@@ -302,12 +303,12 @@ int decode_string(argon2_context *ctx, const char *str, argon2_type type) {
|
||||
if (type == Argon2_i) {
|
||||
CC("$argon2i");
|
||||
} else {
|
||||
return 0;
|
||||
return ARGON2_INCORRECT_TYPE;
|
||||
}
|
||||
CC("$v=");
|
||||
DECIMAL(version);
|
||||
if (version != ARGON2_VERSION_NUMBER) {
|
||||
return 1;
|
||||
return ARGON2_INCORRECT_TYPE;
|
||||
}
|
||||
CC("$m=");
|
||||
DECIMAL(ctx->m_cost);
|
||||
@@ -319,19 +320,23 @@ int decode_string(argon2_context *ctx, const char *str, argon2_type type) {
|
||||
|
||||
CC_opt(",data=", BIN(ctx->ad, maxadlen, ctx->adlen));
|
||||
if (*str == 0) {
|
||||
return 1;
|
||||
return ARGON2_OK;
|
||||
}
|
||||
CC("$");
|
||||
BIN(ctx->salt, maxsaltlen, ctx->saltlen);
|
||||
if (*str == 0) {
|
||||
return 1;
|
||||
return ARGON2_OK;
|
||||
}
|
||||
CC("$");
|
||||
BIN(ctx->out, maxoutlen, ctx->outlen);
|
||||
if (validate_inputs(ctx) != ARGON2_OK) {
|
||||
return 0;
|
||||
validation_result = validate_inputs(ctx);
|
||||
if (validation_result != ARGON2_OK) {
|
||||
return validation_result;
|
||||
}
|
||||
return *str == 0;
|
||||
if (*str == 0) {
|
||||
return ARGON2_OK;
|
||||
}
|
||||
return ARGON2_DECODING_FAIL;
|
||||
|
||||
#undef CC
|
||||
#undef CC_opt
|
||||
@@ -364,7 +369,7 @@ static void u32_to_string(char *str, uint32_t x) {
|
||||
* (no output). if pp->salt_len is also 0, then the string will be a
|
||||
* parameter-only string (no salt and no output).
|
||||
*
|
||||
* On success, 1 is returned.
|
||||
* On success, ARGON2_OK is returned.
|
||||
*/
|
||||
int encode_string(char *dst, size_t dst_len, argon2_context *ctx,
|
||||
argon2_type type) {
|
||||
@@ -372,7 +377,7 @@ int encode_string(char *dst, size_t dst_len, argon2_context *ctx,
|
||||
do { \
|
||||
size_t pp_len = strlen(str); \
|
||||
if (pp_len >= dst_len) { \
|
||||
return 0; \
|
||||
return ARGON2_ENCODING_FAIL; \
|
||||
} \
|
||||
memcpy(dst, str, pp_len + 1); \
|
||||
dst += pp_len; \
|
||||
@@ -390,19 +395,22 @@ int encode_string(char *dst, size_t dst_len, argon2_context *ctx,
|
||||
do { \
|
||||
size_t sb_len = to_base64(dst, dst_len, buf, len); \
|
||||
if (sb_len == (size_t)-1) { \
|
||||
return 0; \
|
||||
return ARGON2_ENCODING_FAIL; \
|
||||
} \
|
||||
dst += sb_len; \
|
||||
dst_len -= sb_len; \
|
||||
} while ((void)0, 0)
|
||||
|
||||
int validation_result;
|
||||
|
||||
if (type == Argon2_i) {
|
||||
SS("$argon2i$v=");
|
||||
} else {
|
||||
return 0;
|
||||
return ARGON2_ENCODING_FAIL;
|
||||
}
|
||||
if (validate_inputs(ctx) != ARGON2_OK) {
|
||||
return 0;
|
||||
validation_result = validate_inputs(ctx);
|
||||
if (validation_result != ARGON2_OK) {
|
||||
return validation_result;
|
||||
}
|
||||
SX(ARGON2_VERSION_NUMBER);
|
||||
SS("$m=");
|
||||
@@ -418,17 +426,17 @@ int encode_string(char *dst, size_t dst_len, argon2_context *ctx,
|
||||
}
|
||||
|
||||
if (ctx->saltlen == 0) {
|
||||
return 1;
|
||||
return ARGON2_OK;
|
||||
}
|
||||
SS("$");
|
||||
SB(ctx->salt, ctx->saltlen);
|
||||
|
||||
if (ctx->outlen == 0) {
|
||||
return 1;
|
||||
return ARGON2_OK;
|
||||
}
|
||||
SS("$");
|
||||
SB(ctx->out, ctx->outlen);
|
||||
return 1;
|
||||
return ARGON2_OK;
|
||||
|
||||
#undef SS
|
||||
#undef SX
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* (no output). if ctx->saltlen is also 0, then the string will be a
|
||||
* parameter-only string (no salt and no output).
|
||||
*
|
||||
* On success, 1 is returned.
|
||||
* On success, ARGON2_OK is returned.
|
||||
*
|
||||
* No other parameters are checked
|
||||
*/
|
||||
@@ -25,7 +25,7 @@ int encode_string(char *dst, size_t dst_len, argon2_context *ctx,
|
||||
* The fields ctx.saltlen, ctx.adlen, ctx.outlen set the maximal salt, ad, out length values
|
||||
* that are allowed; invalid input string causes an error
|
||||
*
|
||||
* Returned value is 1 on success, 0 on error.
|
||||
* Returned value is ARGON2_OK on success.
|
||||
*/
|
||||
int decode_string(argon2_context *ctx, const char *str, argon2_type type);
|
||||
|
||||
|
||||
@@ -139,7 +139,7 @@ int argon2_hash(const uint32_t t_cost, const uint32_t m_cost,
|
||||
|
||||
/* if encoding requested, write it */
|
||||
if (encoded && encodedlen) {
|
||||
if (!encode_string(encoded, encodedlen, &context, type)) {
|
||||
if (encode_string(encoded, encodedlen, &context, type) != ARGON2_OK) {
|
||||
sodium_memzero(out, hashlen);
|
||||
sodium_memzero(encoded, encodedlen);
|
||||
free(out);
|
||||
@@ -177,6 +177,7 @@ int argon2_verify(const char *encoded, const void *pwd, const size_t pwdlen,
|
||||
|
||||
argon2_context ctx;
|
||||
uint8_t *out;
|
||||
int decode_result;
|
||||
int ret;
|
||||
uint32_t encoded_len;
|
||||
|
||||
@@ -208,12 +209,13 @@ int argon2_verify(const char *encoded, const void *pwd, const size_t pwdlen,
|
||||
return ARGON2_MEMORY_ALLOCATION_ERROR;
|
||||
}
|
||||
|
||||
if(decode_string(&ctx, encoded, type) != 1) {
|
||||
decode_result = decode_string(&ctx, encoded, type);
|
||||
if (decode_result != ARGON2_OK) {
|
||||
free(ctx.ad);
|
||||
free(ctx.salt);
|
||||
free(ctx.out);
|
||||
free(out);
|
||||
return ARGON2_DECODING_FAIL;
|
||||
return decode_result;
|
||||
}
|
||||
|
||||
ret = argon2_hash(ctx.t_cost, ctx.m_cost, ctx.threads, pwd, pwdlen, ctx.salt,
|
||||
|
||||
Reference in New Issue
Block a user