-/* $OpenBSD: bcrypt.c,v 1.48 2015/01/05 13:10:10 tedu Exp $ */
+/* $OpenBSD: bcrypt.c,v 1.49 2015/01/07 15:46:23 tedu Exp $ */
/*
* Copyright (c) 2014 Ted Unangst <tedu@openbsd.org>
{
uint8_t csalt[BCRYPT_MAXSALT];
- if (saltbuflen < BCRYPT_SALTSPACE)
+ if (saltbuflen < BCRYPT_SALTSPACE) {
+ errno = EINVAL;
return -1;
+ }
arc4random_buf(csalt, sizeof(csalt));
u_int32_t cdata[BCRYPT_BLOCKS];
if (encryptedlen < BCRYPT_HASHSPACE)
- return -1;
+ goto inval;
/* Check and discard "$" identifier */
if (salt[0] != '$')
- return -1;
+ goto inval;
salt += 1;
if (salt[0] != BCRYPT_VERSION)
- return -1;
+ goto inval;
/* Check for minor versions */
switch ((minor = salt[1])) {
key_len++; /* include the NUL */
break;
default:
- return -1;
+ goto inval;
}
if (salt[2] != '$')
- return -1;
+ goto inval;
/* Discard version + "$" identifier */
salt += 3;
/* Check and parse num rounds */
if (!isdigit((unsigned char)salt[0]) ||
!isdigit((unsigned char)salt[1]) || salt[2] != '$')
- return -1;
+ goto inval;
logr = atoi(salt);
if (logr < BCRYPT_MINLOGROUNDS || logr > 31)
- return -1;
+ goto inval;
/* Computer power doesn't increase linearly, 2^x should be fine */
rounds = 1U << logr;
salt += 3;
if (strlen(salt) * 3 / 4 < BCRYPT_MAXSALT)
- return -1;
+ goto inval;
/* We dont want the base64 salt but the raw data */
if (decode_base64(csalt, BCRYPT_MAXSALT, salt))
- return -1;
+ goto inval;
salt_len = BCRYPT_MAXSALT;
/* Setting up S-Boxes and Subkeys */
explicit_bzero(csalt, sizeof(csalt));
explicit_bzero(cdata, sizeof(cdata));
return 0;
+
+inval:
+ errno = EINVAL;
+ return -1;
}
/*
if (bcrypt_hashpass(pass, goodhash, hash, sizeof(hash)) != 0)
return -1;
if (strlen(hash) != strlen(goodhash) ||
- timingsafe_bcmp(hash, goodhash, strlen(goodhash)) != 0)
+ timingsafe_bcmp(hash, goodhash, strlen(goodhash)) != 0) {
+ errno = EACCES;
return -1;
+ }
explicit_bzero(hash, sizeof(hash));
return 0;