summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2015-11-06 16:19:57 +0000
committerDaniel Silverstone <dsilvers@digital-scurf.org>2015-11-06 16:19:57 +0000
commit52e578cc2643cf3a804b2cfddfb6e983cd7cf804 (patch)
tree563ffd006c104d57b00fd72d17c08de5b9297352
parent72abd9a73b13e15e467ad96ec15d8217837b21e4 (diff)
downloadlua-scrypt-git-52e578cc2643cf3a804b2cfddfb6e983cd7cf804.tar.gz
Inline a base64 implementation so that we don't rely on the libscrypt one which is hidden on some platforms
-rw-r--r--Makefile4
-rw-r--r--base64.c140
-rw-r--r--base64.h45
-rw-r--r--luascrypt.c13
4 files changed, 194 insertions, 8 deletions
diff --git a/Makefile b/Makefile
index c9e6543..8e756c1 100644
--- a/Makefile
+++ b/Makefile
@@ -123,7 +123,7 @@ lua-5.1-install: lua-5.1
$(INSTALL) -d $(DESTDIR)$(LUA51_CMOD_INST)
$(INSTALL) -m 755 scrypt-5.1.so $(DESTDIR)$(LUA51_CMOD_INST)/scrypt.so
-scrypt-5.1.so: luascrypt.c
+scrypt-5.1.so: luascrypt.c base64.c
$(CC) $(CFLAGS) $(LUA51_INC) -shared -o $@ $^ $(LUA51_LIB) $(SCRYPT_LIBS)
lua-5.2: scrypt-5.2.so
@@ -133,7 +133,7 @@ lua-5.2-install: lua-5.2
$(INSTALL) -d $(DESTDIR)$(LUA52_CMOD_INST)
$(INSTALL) -m 755 scrypt-5.2.so $(DESTDIR)$(LUA52_CMOD_INST)/scrypt.so
-scrypt-5.2.so: luascrypt.c
+scrypt-5.2.so: luascrypt.c base64.c
$(CC) $(CFLAGS) $(LUA52_INC) -shared -o $@ $^ $(LUA52_LIB) $(SCRYPT_LIBS)
diff --git a/base64.c b/base64.c
new file mode 100644
index 0000000..fcc7f5c
--- /dev/null
+++ b/base64.c
@@ -0,0 +1,140 @@
+/* Part of publib.
+
+ Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+/*
+ * File: base64.c
+ * Purpose: Implementation of MIME's Base64 encoding and decoding.
+ * Author: Lars Wirzenius
+ * Version: $Id: base64.c,v 1.2 2003/11/15 18:24:10 liw Exp $
+ */
+
+#include <limits.h>
+#include "base64.h"
+
+static const unsigned char sixtet_to_base64[] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+
+/*
+ * Function: base64_length
+ * Purpose: Compute minimum length of encoded output.
+ */
+size_t base64_length(size_t n) {
+ return 4*(n+3)/3 + 1;
+}
+
+
+/*
+ * Function: base64_encode
+ * Purpose: Convert octets to base64
+ * Note: Output buffer must be at least base64_length(n) chars.
+ */
+size_t base64_encode(char *to, const char *from, size_t n) {
+ unsigned w;
+ char *to_start;
+ unsigned char *fromp;
+
+ to_start = to;
+
+ fromp = (unsigned char *) from;
+ for (; n >= 3; n -= 3, fromp += 3) {
+ w = (fromp[0] << 16) | (fromp[1] << 8) | fromp[2];
+ *to++ = sixtet_to_base64[(w >> 18) & 0x3f];
+ *to++ = sixtet_to_base64[(w >> 12) & 0x3f];
+ *to++ = sixtet_to_base64[(w >> 6) & 0x3f];
+ *to++ = sixtet_to_base64[w & 0x3f];
+ }
+
+ switch (n) {
+ case 0:
+ /* Nothing to do */
+ break;
+
+ case 1:
+ w = fromp[0];
+ *to++ = sixtet_to_base64[(w >> 2) & 0x3f];
+ *to++ = sixtet_to_base64[(w << 4) & 0x3f];
+ *to++ = '=';
+ *to++ = '=';
+ break;
+
+ case 2:
+ w = (fromp[0] << 8) | fromp[1];
+ *to++ = sixtet_to_base64[(w >> 10) & 0x3f];
+ *to++ = sixtet_to_base64[(w >> 4) & 0x3f];
+ *to++ = sixtet_to_base64[(w << 2) & 0x3f];
+ *to++ = '=';
+ break;
+ }
+
+ return to - to_start;
+}
+
+
+/*
+ * Function: base64_decode
+ * Purpose: Convert base64 to octets.
+ * Note: Output buffer must be at least 3*n/4 chars.
+ * The output buffer does not get a '\0' appended.
+ */
+size_t base64_decode(char *to, const char *from, size_t len) {
+ static int base64_to_sixtet[UCHAR_MAX + 2];
+ static int tab_init = 0;
+ int i;
+ unsigned bitbuf;
+ int nbits;
+ unsigned char *fromp;
+ char *to_start;
+
+ if (!tab_init) {
+ tab_init = 1;
+ for (i = 0; i <= UCHAR_MAX + 1; ++i)
+ base64_to_sixtet[i] = -1;
+ for (i = 0; sixtet_to_base64[i] != '\0'; ++i)
+ base64_to_sixtet[sixtet_to_base64[i]] = i;
+ }
+
+ to_start = to;
+
+ bitbuf = 0;
+ nbits = 0;
+ fromp = (unsigned char *) from;
+ for (i = 0; i < len && fromp[i] != '='; ++i) {
+ if (base64_to_sixtet[fromp[i]] != -1) {
+ bitbuf = (bitbuf << 6) | base64_to_sixtet[fromp[i]];
+ nbits += 6;
+ if (nbits >= 8) {
+ *to++ = (bitbuf >> (nbits - 8)) & 0xff;
+ bitbuf >>= 8;
+ nbits -= 8;
+ }
+ }
+ }
+
+ return to - to_start;
+}
diff --git a/base64.h b/base64.h
new file mode 100644
index 0000000..b71d9c5
--- /dev/null
+++ b/base64.h
@@ -0,0 +1,45 @@
+/* Part of publib.
+
+ Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+/*
+ * File: base64.h
+ * Purpose: Declarations for MIME's Base64 encoding and decoding.
+ * Author: Lars Wirzenius
+ * Version: $Id: base64.h,v 1.1.1.1 1996/11/05 21:01:42 liw Exp $
+ */
+
+#ifndef __publib_base64_h_included
+#define __publib_base64_h_included
+
+#include <stddef.h>
+
+size_t base64_length(size_t);
+size_t base64_encode(char *, const char *, size_t);
+size_t base64_decode(char *, const char *, size_t);
+
+#endif
diff --git a/luascrypt.c b/luascrypt.c
index f9dce0a..181f1e8 100644
--- a/luascrypt.c
+++ b/luascrypt.c
@@ -20,6 +20,8 @@
#include "libscrypt.h"
+#include "base64.h"
+
static void
luascrypt_salt_gen(char *salt, int saltlen)
{
@@ -51,6 +53,7 @@ luascrypt_hash_password(lua_State *L)
uint8_t hashbuf[64]; /* numbers, these are taken from the */
char saltbuf[256]; /* libscrypt_hash() source. */
char outbuf[256]; /* Icky, I know, but what can I do? */
+ size_t bufused;
uint32_t N = SCRYPT_N;
uint32_t r = SCRYPT_r;
uint32_t p = SCRYPT_p;
@@ -81,13 +84,11 @@ luascrypt_hash_password(lua_State *L)
"Unknown error");
}
- if (libscrypt_b64_encode(outbuf, (char *)hashbuf, sizeof(hashbuf)) < 0) {
- return luaL_error(L, "Unable to encode password hash.");
- }
+ bufused = base64_encode(outbuf, (char *)hashbuf, sizeof(hashbuf));
+ outbuf[bufused] = '\0';
- if (libscrypt_b64_encode(saltbuf, salt, sizeof(salt)) < 0) {
- return luaL_error(L, "Unable to encode salt.");
- }
+ bufused = base64_encode(saltbuf, salt, sizeof(salt));
+ saltbuf[bufused] = '\0';
if (libscrypt_mcf(N, r, p, saltbuf, outbuf, buffer) < 1) {
return luaL_error(L, "Unable to mcf encode password.");