summaryrefslogtreecommitdiff
path: root/luascrypt.c
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2017-06-13 18:58:37 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2017-06-13 18:58:37 +0100
commit9c319bdb4e2614878aa6392bee788cf597591c1d (patch)
tree6ed5df7009ab8edeb067cf86d4fbbf74cf9a3d44 /luascrypt.c
parent55e859083e94dc052791bb7329af2149eded470c (diff)
parent9271157304dbd707f87343df0106c3465b50d6a1 (diff)
downloadlua-scrypt-git-9c319bdb4e2614878aa6392bee788cf597591c1d.tar.gz
Merge remote-tracking branch 'ripsum/richardipsum/no-saltgen-override-v3'
Diffstat (limited to 'luascrypt.c')
-rw-r--r--luascrypt.c39
1 files changed, 34 insertions, 5 deletions
diff --git a/luascrypt.c b/luascrypt.c
index 181f1e8..11ae799 100644
--- a/luascrypt.c
+++ b/luascrypt.c
@@ -26,7 +26,9 @@ static void
luascrypt_salt_gen(char *salt, int saltlen)
{
int fd;
- /* We'd go with libscrypt's implementation, but since libscrypt's salt
+ /* Following comment applies to libscrypt prior to 1.21:
+ *
+ * We'd go with libscrypt's implementation, but since libscrypt's salt
* generation is time based, we cannot fully trust it to generate
* unique salts so to improve our chances we assume we have urandom
* and fall back to libscrypt's implementation if we don't. Since the
@@ -34,11 +36,31 @@ luascrypt_salt_gen(char *salt, int saltlen)
* if we can...
*/
libscrypt_salt_gen(salt, saltlen);
-
+
fd = open("/dev/urandom", O_RDONLY);
if (fd >= 0) {
- read(fd, salt, saltlen); /* Ignore errors in these two calls */
- close(fd); /* Since we have our fallback. */
+ size_t total = 0;
+ ssize_t n;
+
+ while (total < saltlen) {
+ n = read(fd, salt + total, saltlen - total);
+ if (n == 0) {
+ break;
+ }
+
+ if (n == -1) {
+ if (errno == EINTR) {
+ continue; /* just try again */
+ }
+
+ /* Ignore all other errors, since we have our fallback. */
+ break;
+ }
+
+ total += n;
+ }
+
+ close(fd);
}
}
@@ -70,8 +92,15 @@ luascrypt_hash_password(lua_State *L)
return luaL_error(L, "Unable to generate password hash: %s",
"N is too large (limited to 2^15)");
}
-
+
+#ifdef TRUST_LIBSCRYPT_SALT_GEN
+ /* Modern versions of libscrypt generate sufficiently random salts
+ * and take a uint8_t * instead of char *
+ */
+ libscrypt_salt_gen((uint8_t *) salt, sizeof(salt));
+#else
luascrypt_salt_gen(salt, sizeof(salt));
+#endif
if (libscrypt_scrypt((uint8_t*)passwd, passwd_len,
(uint8_t*)salt, sizeof(salt),