summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnibal Monsalve Salazar <anibal@debian.org>2014-07-21 07:32:03 +0200
committerAnibal Monsalve Salazar <anibal@debian.org>2014-07-21 07:32:03 +0200
commit001fa2d8cd00491ff4fbad554f66f6031dff9ccf (patch)
tree2d97a40c7848d8c40fd83b9433c29b83c59f27e5
libscrypt (1-2.2) unstable; urgency=medium
* Non-maintainer upload. * Fix FTBFS on big endian architecture. Patch by Aurelien Jarno. Add big-endian.patch. Closes: #728254. # imported from the archive
-rw-r--r--.pc/.quilt_patches1
-rw-r--r--.pc/.quilt_series1
-rw-r--r--.pc/.version1
-rw-r--r--.pc/applied-patches2
-rw-r--r--.pc/big-endian.patch/modp_b64.c264
-rw-r--r--.pc/fix_makefile/Makefile32
-rw-r--r--LICENSE9
-rw-r--r--Makefile39
-rw-r--r--README.md53
-rw-r--r--crypto-mcf.c64
-rw-r--r--crypto-scrypt-saltgen.c25
-rw-r--r--crypto_scrypt-check.c91
-rw-r--r--crypto_scrypt-hash.c36
-rw-r--r--crypto_scrypt-hexconvert.c30
-rw-r--r--crypto_scrypt-nosse.c337
-rw-r--r--debian/changelog28
-rw-r--r--debian/compat1
-rw-r--r--debian/control38
-rw-r--r--debian/copyright81
-rw-r--r--debian/docs1
-rw-r--r--debian/libscrypt-dev.dirs2
-rw-r--r--debian/libscrypt-dev.install3
-rw-r--r--debian/libscrypt0.install1
-rw-r--r--debian/libscrypt1.dirs1
-rw-r--r--debian/patches/big-endian.patch53
-rw-r--r--debian/patches/fix_makefile25
-rw-r--r--debian/patches/series2
-rwxr-xr-xdebian/rules11
-rw-r--r--debian/shlibs.local1
-rw-r--r--debian/source/format1
-rw-r--r--debian/watch8
-rw-r--r--libscrypt.h61
-rw-r--r--libscrypt.version10
-rw-r--r--main.c213
-rw-r--r--modp_b64.c268
-rw-r--r--modp_b64.h235
-rw-r--r--modp_b64_data.h480
-rw-r--r--sha256.c411
-rw-r--r--sha256.h70
-rw-r--r--sysendian.h139
40 files changed, 3129 insertions, 0 deletions
diff --git a/.pc/.quilt_patches b/.pc/.quilt_patches
new file mode 100644
index 0000000..6857a8d
--- /dev/null
+++ b/.pc/.quilt_patches
@@ -0,0 +1 @@
+debian/patches
diff --git a/.pc/.quilt_series b/.pc/.quilt_series
new file mode 100644
index 0000000..c206706
--- /dev/null
+++ b/.pc/.quilt_series
@@ -0,0 +1 @@
+series
diff --git a/.pc/.version b/.pc/.version
new file mode 100644
index 0000000..0cfbf08
--- /dev/null
+++ b/.pc/.version
@@ -0,0 +1 @@
+2
diff --git a/.pc/applied-patches b/.pc/applied-patches
new file mode 100644
index 0000000..5d020e2
--- /dev/null
+++ b/.pc/applied-patches
@@ -0,0 +1,2 @@
+fix_makefile
+big-endian.patch
diff --git a/.pc/big-endian.patch/modp_b64.c b/.pc/big-endian.patch/modp_b64.c
new file mode 100644
index 0000000..9215b45
--- /dev/null
+++ b/.pc/big-endian.patch/modp_b64.c
@@ -0,0 +1,264 @@
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
+/* vi: set expandtab shiftwidth=4 tabstop=4: */
+/**
+ * \file modp_b64.c
+ * <PRE>
+ * MODP_B64 - High performance base64 encoder/decoder
+ * http://code.google.com/p/stringencoders/
+ *
+ * Copyright &copy; 2005, 2006, 2007 Nick Galbreath -- nickg [at] modp [dot] com
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 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.
+ *
+ * Neither the name of the modp.com nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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 COPYRIGHT
+ * OWNER OR CONTRIBUTORS 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.
+ *
+ * This is the standard "new" BSD license:
+ * http://www.opensource.org/licenses/bsd-license.php
+ * </PRE>
+ */
+
+/* public header */
+#include "modp_b64.h"
+
+
+/* if on motoral, sun, ibm; uncomment this */
+/* #define WORDS_BIGENDIAN 1 */
+/* else for Intel, Amd; uncomment this */
+/* #undef WORDS_BIGENDIAN */
+
+#include "modp_b64_data.h"
+
+#define BADCHAR 0x01FFFFFF
+
+/**
+ * you can control if we use padding by commenting out this
+ * next line. However, I highly recommend you use padding and not
+ * using it should only be for compatability with a 3rd party.
+ * Also, 'no padding' is not tested!
+ */
+#define DOPAD 1
+
+/*
+ * if we aren't doing padding
+ * set the pad character to NULL
+ */
+#ifndef DOPAD
+#undef CHARPAD
+#define CHARPAD '\0'
+#endif
+
+int libscrypt_b64_encode(char* dest, const char* str, size_t len)
+{
+ int i;
+ const uint8_t* s = (const uint8_t*) str;
+ uint8_t* p = (uint8_t*) dest;
+
+ /* unsigned here is important! */
+ /* uint8_t is fastest on G4, amd */
+ /* uint32_t is fastest on Intel */
+ uint32_t t1, t2, t3;
+
+ for (i = 0; i < len - 2; i += 3) {
+ t1 = s[i]; t2 = s[i+1]; t3 = s[i+2];
+ *p++ = e0[t1];
+ *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
+ *p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
+ *p++ = e2[t3];
+ }
+
+ switch (len - i) {
+ case 0:
+ break;
+ case 1:
+ t1 = s[i];
+ *p++ = e0[t1];
+ *p++ = e1[(t1 & 0x03) << 4];
+ *p++ = CHARPAD;
+ *p++ = CHARPAD;
+ break;
+ default: /* case 2 */
+ t1 = s[i]; t2 = s[i+1];
+ *p++ = e0[t1];
+ *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
+ *p++ = e2[(t2 & 0x0F) << 2];
+ *p++ = CHARPAD;
+ }
+
+ *p = '\0';
+ return (int)(p - (uint8_t*)dest);
+}
+
+#ifdef WORDS_BIGENDIAN /* BIG ENDIAN -- SUN / IBM / MOTOROLA */
+int libscrypt_b64_decode(char* dest, const char* src, size_t len)
+{
+ int i;
+ if (len == 0) return 0;
+
+#ifdef DOPAD
+ /* if padding is used, then the message must be at least
+ 4 chars and be a multiple of 4.
+ there can be at most 2 pad chars at the end */
+ if (len < 4 || (len % 4 != 0)) return -1;
+ if (src[len-1] == CHARPAD) {
+ len--;
+ if (src[len -1] == CHARPAD) {
+ len--;
+ }
+ }
+#endif /* DOPAD */
+
+ int leftover = len % 4;
+ int chunks = (leftover == 0) ? len / 4 - 1 : len /4;
+
+ uint8_t* p = (uint8_t*) dest;
+ uint32_t x = 0;
+ uint32_t* destInt = (uint32_t*) p;
+ uint32_t* srcInt = (uint32_t*) src;
+ uint32_t y = *srcInt++;
+ for (i = 0; i < chunks; ++i) {
+ x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] |
+ d2[y >> 8 & 0xff] | d3[y & 0xff];
+
+ if (x >= BADCHAR) return -1;
+ *destInt = x << 8;
+ p += 3;
+ destInt = (uint32_t*)p;
+ y = *srcInt++;
+ }
+
+ switch (leftover) {
+ case 0:
+ x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] |
+ d2[y >> 8 & 0xff] | d3[y & 0xff];
+ if (x >= BADCHAR) return -1;
+ *p++ = ((uint8_t*)&x)[1];
+ *p++ = ((uint8_t*)&x)[2];
+ *p = ((uint8_t*)&x)[3];
+ return (chunks+1)*3;
+#ifndef DOPAD
+ case 1: /* with padding this is an impossible case */
+ x = d3[y >> 24];
+ *p = (uint8_t)x;
+ break;
+#endif
+ case 2:
+ x = d3[y >> 24] *64 + d3[(y >> 16) & 0xff];
+ *p = (uint8_t)(x >> 4);
+ break;
+ default: /* case 3 */
+ x = (d3[y >> 24] *64 + d3[(y >> 16) & 0xff])*64 +
+ d3[(y >> 8) & 0xff];
+ *p++ = (uint8_t) (x >> 10);
+ *p = (uint8_t) (x >> 2);
+ break;
+ }
+
+ if (x >= BADCHAR) return -1;
+ return 3*chunks + (6*leftover)/8;
+}
+
+#else /* LITTLE ENDIAN -- INTEL AND FRIENDS */
+
+int libscrypt_b64_decode(char* dest, const char* src, size_t len)
+{
+ int i;
+ if (len == 0) return 0;
+
+#ifdef DOPAD
+ /*
+ * if padding is used, then the message must be at least
+ * 4 chars and be a multiple of 4
+ */
+ if (len < 4 || (len % 4 != 0)) return -1; /* error */
+ /* there can be at most 2 pad chars at the end */
+ if (src[len-1] == CHARPAD) {
+ len--;
+ if (src[len -1] == CHARPAD) {
+ len--;
+ }
+ }
+#endif
+
+ int leftover = len % 4;
+ int chunks = (leftover == 0) ? len / 4 - 1 : len /4;
+
+ uint8_t* p = (uint8_t*) dest;
+ uint32_t x = 0;
+ uint32_t* destInt = (uint32_t*) p;
+ uint32_t* srcInt = (uint32_t*) src;
+ uint32_t y = *srcInt++;
+ for (i = 0; i < chunks; ++i) {
+ x = d0[y & 0xff] |
+ d1[(y >> 8) & 0xff] |
+ d2[(y >> 16) & 0xff] |
+ d3[(y >> 24) & 0xff];
+
+ if (x >= BADCHAR) return -1;
+ *destInt = x ;
+ p += 3;
+ destInt = (uint32_t*)p;
+ y = *srcInt++;}
+
+
+ switch (leftover) {
+ case 0:
+ x = d0[y & 0xff] |
+ d1[(y >> 8) & 0xff] |
+ d2[(y >> 16) & 0xff] |
+ d3[(y >> 24) & 0xff];
+
+ if (x >= BADCHAR) return -1;
+ *p++ = ((uint8_t*)(&x))[0];
+ *p++ = ((uint8_t*)(&x))[1];
+ *p = ((uint8_t*)(&x))[2];
+ return (chunks+1)*3;
+ break;
+#ifndef DOPAD
+ case 1: /* with padding this is an impossible case */
+ x = d0[y & 0xff];
+ *p = *((uint8_t*)(&x)); // i.e. first char/byte in int
+ break;
+#endif
+ case 2: // * case 2, 1 output byte */
+ x = d0[y & 0xff] | d1[y >> 8 & 0xff];
+ *p = *((uint8_t*)(&x)); // i.e. first char
+ break;
+ default: /* case 3, 2 output bytes */
+ x = d0[y & 0xff] |
+ d1[y >> 8 & 0xff ] |
+ d2[y >> 16 & 0xff]; /* 0x3c */
+ *p++ = ((uint8_t*)(&x))[0];
+ *p = ((uint8_t*)(&x))[1];
+ break;
+ }
+
+ if (x >= BADCHAR) return -1;
+
+ return 3*chunks + (6*leftover)/8;
+}
+
+#endif /* if bigendian / else / endif */
diff --git a/.pc/fix_makefile/Makefile b/.pc/fix_makefile/Makefile
new file mode 100644
index 0000000..69ee494
--- /dev/null
+++ b/.pc/fix_makefile/Makefile
@@ -0,0 +1,32 @@
+CC=gcc
+CFLAGS=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC
+all: reference
+
+OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o modp_b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o
+
+
+library: $(OBJS)
+ $(CC) -shared -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version -o libscrypt.so.0 -lc $(OBJS)
+ ar rcs libscrypt.a $(OBJS)
+
+reference: library main.o
+ ln -s -f libscrypt.so.0 libscrypt.so
+ $(CC) -Wall -o reference main.o -Wl,-rpath=. -L. -lm -lscrypt
+
+clean:
+ rm -f *.o reference libscrypt.so* libscrypt.a
+
+check: all
+ ./reference
+
+devtest:
+ splint crypto_scrypt-hexconvert.c
+ splint crypto-mcf.c crypto_scrypt-check.c crypto_scrypt-hash.c
+ splint crypto-scrypt-saltgen.c +posixlib
+ valgrind ./reference
+
+install: library
+ install -m 0644 libscrypt.a $(DESTDIR)/usr/local/lib
+ install -m 0644 libscrypt.so.0 $(DESTDIR)/usr/local/lib
+ ln -s -f $(DESTDIR)/usr/local/lib/libscrypt.so.0 $(DESTDIR)/usr/local/lib/libscrypt.so
+ install -m 0644 libscrypt.h $(DESTDIR)/usr/local/include
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..46a7431
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,9 @@
+Copyright (c) 2013, Joshua Small
+ All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
+
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..6a0a659
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,39 @@
+PREFIX = /usr/local
+LIBDIR = $(PREFIX)/lib
+INCLUDEDIR = $(PREFIX)/include
+MAKE_DIR = install -d
+INSTALL_DATA = install -m 0644
+
+CC=gcc
+CFLAGS=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC
+all: reference
+
+OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o modp_b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o
+
+
+library: $(OBJS)
+ $(CC) -shared -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version -o libscrypt.so.0 -lc $(OBJS)
+ ar rcs libscrypt.a $(OBJS)
+
+reference: library main.o
+ ln -s -f libscrypt.so.0 libscrypt.so
+ $(CC) -Wall -o reference main.o -Wl,-rpath=. -L. -lm -lscrypt
+
+clean:
+ rm -f *.o reference libscrypt.so* libscrypt.a
+
+check: all
+ ./reference
+
+devtest:
+ splint crypto_scrypt-hexconvert.c
+ splint crypto-mcf.c crypto_scrypt-check.c crypto_scrypt-hash.c
+ splint crypto-scrypt-saltgen.c +posixlib
+ valgrind ./reference
+
+install: library
+ $(MAKE_DIR) $(DESTDIR) $(DESTDIR)$(PREFIX) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR)
+ $(INSTALL_DATA) libscrypt.a $(DESTDIR)$(LIBDIR)
+ $(INSTALL_DATA) libscrypt.so.0 $(DESTDIR)$(LIBDIR)
+ ln -s -f libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so
+ $(INSTALL_DATA) libscrypt.h $(DESTDIR)$(INCLUDEDIR)
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..4fbfaad
--- /dev/null
+++ b/README.md
@@ -0,0 +1,53 @@
+libscrypt
+=========
+Linux scrypt shared library.
+
+Full credit to algorithm designer and example code from Colin Percival here:
+http://www.tarsnap.com/scrypt.html
+
+Utilises BSD licensed BASE64 encoder here:
+http://code.google.com/p/stringencoders/
+
+Official project page, including stable tarballs found here:
+http://www.lolware.net/libscrypt.html
+
+Simple hashing interface
+
+A hash can be generated using the following function:
+
+ int libscrypt_scrypt(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p)
+
+Sane constants have been created for N, r and p so you can create a has like this:
+
+ libscrypt_scrypt(outbuf, "My cats's breath smells like cat food", SCRYPT_N, SCRYPT_r, SCRYPT_p);
+
+Output stored in "outbuf" is stored in a standardised MCF form, which means includes the randomly created, 128 bit salt, all N, r and p values, and a BASE64 encoded version of the hash. The entire MCF can be stored in a database, and compared for use as below:
+
+ retval = scrypt_check(mcf, "pleasefailme");
+ retval < 0 error
+ retval = 0 password incorrect
+ retval > 0 pass
+
+mcf should be defined as at least SCRYPT_MCF_LEN in size.
+
+A number of internal functions are exposed, and users wishing to create more complex use cases should consult the header file, which is aimed at documenting the API fully.
+
+The test reference is also aimed at providing a well documented use case.
+Building
+--------
+ make
+ make check
+Check the Makefile for advice on linking against your application.
+
+BUGS
+----
+SCRYPT_* constants are probably a little high for something like a Raspberry pi. Using '1' as SCRYPT_p is acceptable from a security and performance standpoint if needed.
+
+Notes on Code Development
+------------------------
+
+Code is now declared "stable", the master branch will always be "stable" and development will be done on branches.
+The reference machines are CentOS and Raspbian, and the code is expected to compile and run on all of these before being moved to stable branch.
+Testing has also confirmed that libscrypt does compile and run on MacOS with minor Makefile edits.
+Full transparancy on the regular application of thorough testing can be found by reviewing recent test harness results here:
+http://www.lolware.net/libscrypttesting.txt
diff --git a/crypto-mcf.c b/crypto-mcf.c
new file mode 100644
index 0000000..66e92f0
--- /dev/null
+++ b/crypto-mcf.c
@@ -0,0 +1,64 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <float.h>
+
+#include <math.h>
+
+#include "libscrypt.h"
+
+/* Although log2 exists in GNU99 C, more portable code shouldn't use it
+* Note that this function returns a float and hence is not compatible with the
+* GNU prototype
+*/
+static double scrypt_log2( uint32_t n )
+{
+ // log(n)/log(2) is log2.
+ double temp;
+ /* Using the temp variable keeps splint happy */
+ temp = log(2);
+ return (log((double)n) / temp);
+}
+
+int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, char *mcf)
+{
+
+
+ uint32_t params;
+ int s;
+ double t, t2, fracpart;
+
+ if(!mcf || !hash)
+ return 0;
+ /* Although larger values of r, p are valid in scrypt, this mcf format
+ * limits to 8 bits. If your number is larger, current computers will
+ * struggle
+ */
+ if(r > (uint8_t)(-1) || p > (uint8_t)(-1))
+ return 0;
+
+
+ t = scrypt_log2(N);
+
+ /* The "whole numebr" check below is non-trivial due to precision
+ * issues, where you could printf("%d", (int)t) and find yourself
+ * looking at (expected value) -1
+ */
+ fracpart = modf(t, &t2);
+ if(fracpart > DBL_EPSILON)
+ return 0;
+
+ params = (r << 8) + p;
+ params += (uint32_t)t << 16;
+
+ /* Using snprintf - not checking for overflows. We've already
+ * determined that mcf should be defined as at least SCRYPT_MCF_LEN
+ * in length
+ */
+ s = snprintf(mcf, SCRYPT_MCF_LEN, SCRYPT_MCF_ID "$%06x$%s$%s", (unsigned int)params, salt, hash);
+ if (s > SCRYPT_MCF_LEN)
+ return 0;
+
+ return 1;
+}
diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c
new file mode 100644
index 0000000..82af842
--- /dev/null
+++ b/crypto-scrypt-saltgen.c
@@ -0,0 +1,25 @@
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "sha256.h"
+
+
+void libscrypt_salt_gen(char *rand, size_t len)
+{
+
+ unsigned char buf[32];
+ time_t current_time;
+ char *c_time_string;
+
+ SHA256_CTX ctx;
+
+ SHA256_Init(&ctx );
+ current_time = time(NULL);
+ c_time_string = ctime(&current_time);
+ SHA256_Update(&ctx, c_time_string, strlen(c_time_string));
+ SHA256_Final(buf, &ctx);
+
+ memcpy(rand, buf, len);
+
+}
diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c
new file mode 100644
index 0000000..1aaa88a
--- /dev/null
+++ b/crypto_scrypt-check.c
@@ -0,0 +1,91 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <math.h>
+
+#include "libscrypt.h"
+
+/* pow() works with doubles. Sounds like it should cast to int correctly,
+* but doesn't always. This is faster anyway
+*/
+static uint16_t ipow(uint16_t base, uint32_t exp)
+{
+ uint16_t result = 1;
+ while (exp != 0)
+ {
+ if ((exp & 1) != 0)
+ result *= base;
+ exp >>= 1;
+ base *= base;
+ }
+
+ return result;
+}
+
+int libscrypt_check(char *mcf, char *password)
+{
+
+ uint32_t params;
+ uint16_t N;
+ uint8_t r, p;
+ int retval;
+ uint8_t hashbuf[64];
+ char outbuf[128];
+ char salt[32];
+ char *tok;
+
+ if(memcmp(mcf, SCRYPT_MCF_ID, 3) != 0)
+ {
+ /* Only version 0 supported */
+ return -1;
+ }
+
+ tok = strtok(mcf, "$");
+ tok = strtok(NULL, "$");
+
+ if ( !tok )
+ return -1;
+
+ params = (uint32_t)strtoul(tok, NULL, 16);
+ if ( params == 0 )
+ return -1;
+
+ tok = strtok(NULL, "$");
+
+ if ( !tok )
+ return -1;
+
+ p = params & 0xff;
+ r = (params >> 8) & 0xff;
+ N = params >> 16;
+ N = ipow(2, N);
+
+ /* Useful debugging:
+ printf("We've obtained salt 'N' r p of '%s' %d %d %d\n", tok, N,r,p);
+ */
+
+ retval = libscrypt_b64_decode(salt, tok, strlen(tok));
+ if (retval < 1)
+ return -1;
+ retval = libscrypt_scrypt((uint8_t*)password,strlen(password), (uint8_t*)salt, (uint32_t)retval, N, r, p, hashbuf, sizeof(hashbuf));
+
+ if (retval != 0)
+ return retval;
+
+ retval = libscrypt_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf));
+ if (retval == 0)
+ return -1;
+
+ tok = strtok(NULL, "$");
+
+ if ( !tok )
+ return -1;
+
+ if(strcmp(tok, outbuf) == 0)
+ {
+ return 1;
+ }
+
+ return 0;
+}
+
diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c
new file mode 100644
index 0000000..4d4e7aa
--- /dev/null
+++ b/crypto_scrypt-hash.c
@@ -0,0 +1,36 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdint.h>
+
+#include "libscrypt.h"
+
+int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p)
+{
+
+ int retval;
+ char salt[16];
+ uint8_t hashbuf[64];
+ char outbuf[256];
+ char saltbuf[256];
+
+ libscrypt_salt_gen(salt, 16);
+
+ retval = libscrypt_scrypt((uint8_t*)passphrase,strlen(passphrase), (uint8_t*)salt, sizeof(salt), N, r, p, hashbuf, sizeof(hashbuf));
+ if(retval == -1)
+ return 0;
+
+ retval = libscrypt_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf));
+ if(retval == -1)
+ return 0;
+
+ retval = libscrypt_b64_encode(saltbuf, salt, sizeof(salt));
+ if(retval == -1)
+ return 0;
+
+ retval = libscrypt_mcf(N, r, p, saltbuf, outbuf, dst);
+ if(retval == -1)
+ return 0;
+
+ return 1;
+}
diff --git a/crypto_scrypt-hexconvert.c b/crypto_scrypt-hexconvert.c
new file mode 100644
index 0000000..ececbd9
--- /dev/null
+++ b/crypto_scrypt-hexconvert.c
@@ -0,0 +1,30 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdint.h>
+
+int libscrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs)
+{
+
+ size_t i;
+ int len = 0;
+
+ if (!buf || s < 1 || obs < (s * 2 + 1))
+ return 0;
+
+ memset(outbuf, 0, obs);
+
+
+ for(i=0; i<=(s-1); i++)
+ {
+ /* snprintf(outbuf, s,"%s...", outbuf....) has undefined results
+ * and can't be used. Using offests like this makes snprintf
+ * nontrivial. we therefore have use inescure sprintf() and
+ * lengths checked elsewhere (start of function) */
+ /*@ -bufferoverflowhigh @*/
+ len += sprintf(outbuf+len, "%02x", (unsigned int) buf[i]);
+ }
+
+ return 1;
+}
+
diff --git a/crypto_scrypt-nosse.c b/crypto_scrypt-nosse.c
new file mode 100644
index 0000000..845f014
--- /dev/null
+++ b/crypto_scrypt-nosse.c
@@ -0,0 +1,337 @@
+/*-
+ * Copyright 2009 Colin Percival
+ * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
+ *
+ * This file was originally written by Colin Percival as part of the Tarsnap
+ * online backup system.
+ */
+
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "sha256.h"
+#include "sysendian.h"
+
+#include "libscrypt.h"
+
+static void blkcpy(void *, void *, size_t);
+static void blkxor(void *, void *, size_t);
+static void salsa20_8(uint32_t[16]);
+static void blockmix_salsa8(uint32_t *, uint32_t *, uint32_t *, size_t);
+static uint64_t integerify(void *, size_t);
+static void smix(uint8_t *, size_t, uint64_t, uint32_t *, uint32_t *);
+
+static void
+blkcpy(void * dest, void * src, size_t len)
+{
+ size_t * D = dest;
+ size_t * S = src;
+ size_t L = len / sizeof(size_t);
+ size_t i;
+
+ for (i = 0; i < L; i++)
+ D[i] = S[i];
+}
+
+static void
+blkxor(void * dest, void * src, size_t len)
+{
+ size_t * D = dest;
+ size_t * S = src;
+ size_t L = len / sizeof(size_t);
+ size_t i;
+
+ for (i = 0; i < L; i++)
+ D[i] ^= S[i];
+}
+
+/**
+ * salsa20_8(B):
+ * Apply the salsa20/8 core to the provided block.
+ */
+static void
+salsa20_8(uint32_t B[16])
+{
+ uint32_t x[16];
+ size_t i;
+
+ blkcpy(x, B, 64);
+ for (i = 0; i < 8; i += 2) {
+#define R(a,b) (((a) << (b)) | ((a) >> (32 - (b))))
+ /* Operate on columns. */
+ x[ 4] ^= R(x[ 0]+x[12], 7); x[ 8] ^= R(x[ 4]+x[ 0], 9);
+ x[12] ^= R(x[ 8]+x[ 4],13); x[ 0] ^= R(x[12]+x[ 8],18);
+
+ x[ 9] ^= R(x[ 5]+x[ 1], 7); x[13] ^= R(x[ 9]+x[ 5], 9);
+ x[ 1] ^= R(x[13]+x[ 9],13); x[ 5] ^= R(x[ 1]+x[13],18);
+
+ x[14] ^= R(x[10]+x[ 6], 7); x[ 2] ^= R(x[14]+x[10], 9);
+ x[ 6] ^= R(x[ 2]+x[14],13); x[10] ^= R(x[ 6]+x[ 2],18);
+
+ x[ 3] ^= R(x[15]+x[11], 7); x[ 7] ^= R(x[ 3]+x[15], 9);
+ x[11] ^= R(x[ 7]+x[ 3],13); x[15] ^= R(x[11]+x[ 7],18);
+
+ /* Operate on rows. */
+ x[ 1] ^= R(x[ 0]+x[ 3], 7); x[ 2] ^= R(x[ 1]+x[ 0], 9);
+ x[ 3] ^= R(x[ 2]+x[ 1],13); x[ 0] ^= R(x[ 3]+x[ 2],18);
+
+ x[ 6] ^= R(x[ 5]+x[ 4], 7); x[ 7] ^= R(x[ 6]+x[ 5], 9);
+ x[ 4] ^= R(x[ 7]+x[ 6],13); x[ 5] ^= R(x[ 4]+x[ 7],18);
+
+ x[11] ^= R(x[10]+x[ 9], 7); x[ 8] ^= R(x[11]+x[10], 9);
+ x[ 9] ^= R(x[ 8]+x[11],13); x[10] ^= R(x[ 9]+x[ 8],18);
+
+ x[12] ^= R(x[15]+x[14], 7); x[13] ^= R(x[12]+x[15], 9);
+ x[14] ^= R(x[13]+x[12],13); x[15] ^= R(x[14]+x[13],18);
+#undef R
+ }
+ for (i = 0; i < 16; i++)
+ B[i] += x[i];
+}
+
+/**
+ * blockmix_salsa8(Bin, Bout, X, r):
+ * Compute Bout = BlockMix_{salsa20/8, r}(Bin). The input Bin must be 128r
+ * bytes in length; the output Bout must also be the same size. The
+ * temporary space X must be 64 bytes.
+ */
+static void
+blockmix_salsa8(uint32_t * Bin, uint32_t * Bout, uint32_t * X, size_t r)
+{
+ size_t i;
+
+ /* 1: X <-- B_{2r - 1} */
+ blkcpy(X, &Bin[(2 * r - 1) * 16], 64);
+
+ /* 2: for i = 0 to 2r - 1 do */
+ for (i = 0; i < 2 * r; i += 2) {
+ /* 3: X <-- H(X \xor B_i) */
+ blkxor(X, &Bin[i * 16], 64);
+ salsa20_8(X);
+
+ /* 4: Y_i <-- X */
+ /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
+ blkcpy(&Bout[i * 8], X, 64);
+
+ /* 3: X <-- H(X \xor B_i) */
+ blkxor(X, &Bin[i * 16 + 16], 64);
+ salsa20_8(X);
+
+ /* 4: Y_i <-- X */
+ /* 6: B' <-- (Y_0, Y_2 ... Y_{2r-2}, Y_1, Y_3 ... Y_{2r-1}) */
+ blkcpy(&Bout[i * 8 + r * 16], X, 64);
+ }
+}
+
+/**
+ * integerify(B, r):
+ * Return the result of parsing B_{2r-1} as a little-endian integer.
+ */
+static uint64_t
+integerify(void * B, size_t r)
+{
+ uint32_t * X = (void *)((uintptr_t)(B) + (2 * r - 1) * 64);
+
+ return (((uint64_t)(X[1]) << 32) + X[0]);
+}
+
+/**
+ * smix(B, r, N, V, XY):
+ * Compute B = SMix_r(B, N). The input B must be 128r bytes in length;
+ * the temporary storage V must be 128rN bytes in length; the temporary
+ * storage XY must be 256r + 64 bytes in length. The value N must be a
+ * power of 2 greater than 1. The arrays B, V, and XY must be aligned to a
+ * multiple of 64 bytes.
+ */
+static void
+smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY)
+{
+ uint32_t * X = XY;
+ uint32_t * Y = &XY[32 * r];
+ uint32_t * Z = &XY[64 * r];
+ uint64_t i;
+ uint64_t j;
+ size_t k;
+
+ /* 1: X <-- B */
+ for (k = 0; k < 32 * r; k++)
+ X[k] = le32dec(&B[4 * k]);
+
+ /* 2: for i = 0 to N - 1 do */
+ for (i = 0; i < N; i += 2) {
+ /* 3: V_i <-- X */
+ blkcpy(&V[i * (32 * r)], X, 128 * r);
+
+ /* 4: X <-- H(X) */
+ blockmix_salsa8(X, Y, Z, r);
+
+ /* 3: V_i <-- X */
+ blkcpy(&V[(i + 1) * (32 * r)], Y, 128 * r);
+
+ /* 4: X <-- H(X) */
+ blockmix_salsa8(Y, X, Z, r);
+ }
+
+ /* 6: for i = 0 to N - 1 do */
+ for (i = 0; i < N; i += 2) {
+ /* 7: j <-- Integerify(X) mod N */
+ j = integerify(X, r) & (N - 1);
+
+ /* 8: X <-- H(X \xor V_j) */
+ blkxor(X, &V[j * (32 * r)], 128 * r);
+ blockmix_salsa8(X, Y, Z, r);
+
+ /* 7: j <-- Integerify(X) mod N */
+ j = integerify(Y, r) & (N - 1);
+
+ /* 8: X <-- H(X \xor V_j) */
+ blkxor(Y, &V[j * (32 * r)], 128 * r);
+ blockmix_salsa8(Y, X, Z, r);
+ }
+
+ /* 10: B' <-- X */
+ for (k = 0; k < 32 * r; k++)
+ le32enc(&B[4 * k], X[k]);
+}
+
+/**
+ * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
+ * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
+ * p, buflen) and write the result into buf. The parameters r, p, and buflen
+ * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
+ * must be a power of 2 greater than 1.
+ *
+ * Return 0 on success; or -1 on error.
+ */
+int
+libscrypt_scrypt(const uint8_t * passwd, size_t passwdlen,
+ const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p,
+ uint8_t * buf, size_t buflen)
+{
+ void * B0, * V0, * XY0;
+ uint8_t * B;
+ uint32_t * V;
+ uint32_t * XY;
+ uint32_t i;
+
+ /* Sanity-check parameters. */
+#if SIZE_MAX > UINT32_MAX
+ if (buflen > (((uint64_t)(1) << 32) - 1) * 32) {
+ errno = EFBIG;
+ goto err0;
+ }
+#endif
+ if ((uint64_t)(r) * (uint64_t)(p) >= (1 << 30)) {
+ errno = EFBIG;
+ goto err0;
+ }
+ if (((N & (N - 1)) != 0) || (N == 0)) {
+ errno = EINVAL;
+ goto err0;
+ }
+ if ((r > SIZE_MAX / 128 / p) ||
+#if SIZE_MAX / 256 <= UINT32_MAX
+ (r > SIZE_MAX / 256) ||
+#endif
+ (N > SIZE_MAX / 128 / r)) {
+ errno = ENOMEM;
+ goto err0;
+ }
+
+ /* Allocate memory. */
+#ifdef HAVE_POSIX_MEMALIGN
+ if ((errno = posix_memalign(&B0, 64, 128 * r * p)) != 0)
+ goto err0;
+ B = (uint8_t *)(B0);
+ if ((errno = posix_memalign(&XY0, 64, 256 * r + 64)) != 0)
+ goto err1;
+ XY = (uint32_t *)(XY0);
+#ifndef MAP_ANON
+ if ((errno = posix_memalign(&V0, 64, 128 * r * N)) != 0)
+ goto err2;
+ V = (uint32_t *)(V0);
+#endif
+#else
+ if ((B0 = malloc(128 * r * p + 63)) == NULL)
+ goto err0;
+ B = (uint8_t *)(((uintptr_t)(B0) + 63) & ~ (uintptr_t)(63));
+ if ((XY0 = malloc(256 * r + 64 + 63)) == NULL)
+ goto err1;
+ XY = (uint32_t *)(((uintptr_t)(XY0) + 63) & ~ (uintptr_t)(63));
+#ifndef MAP_ANON
+ if ((V0 = malloc(128 * r * N + 63)) == NULL)
+ goto err2;
+ V = (uint32_t *)(((uintptr_t)(V0) + 63) & ~ (uintptr_t)(63));
+#endif
+#endif
+#ifdef MAP_ANON
+ if ((V0 = mmap(NULL, 128 * r * N, PROT_READ | PROT_WRITE,
+#ifdef MAP_NOCORE
+ MAP_ANON | MAP_PRIVATE | MAP_NOCORE,
+#else
+ MAP_ANON | MAP_PRIVATE,
+#endif
+ -1, 0)) == MAP_FAILED)
+ goto err2;
+ V = (uint32_t *)(V0);
+#endif
+
+ /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */
+ PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r);
+
+ /* 2: for i = 0 to p - 1 do */
+ for (i = 0; i < p; i++) {
+ /* 3: B_i <-- MF(B_i, N) */
+ smix(&B[i * 128 * r], r, N, V, XY);
+ }
+
+ /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */
+ PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen);
+
+ /* Free memory. */
+#ifdef MAP_ANON
+ if (munmap(V0, 128 * r * N))
+ goto err2;
+#else
+ free(V0);
+#endif
+ free(XY0);
+ free(B0);
+
+ /* Success! */
+ return (0);
+
+err2:
+ free(XY0);
+err1:
+ free(B0);
+err0:
+ /* Failure! */
+ return (-1);
+}
diff --git a/debian/changelog b/debian/changelog
new file mode 100644
index 0000000..758090e
--- /dev/null
+++ b/debian/changelog
@@ -0,0 +1,28 @@
+libscrypt (1-2.2) unstable; urgency=medium
+
+ * Non-maintainer upload.
+ * Fix FTBFS on big endian architecture.
+ Patch by Aurelien Jarno.
+ Add big-endian.patch.
+ Closes: #728254.
+
+ -- Anibal Monsalve Salazar <anibal@debian.org> Mon, 21 Jul 2014 06:32:03 +0100
+
+libscrypt (1-2.1) unstable; urgency=medium
+
+ * Non-maintainer upload
+ * Make symlink relative (Closes: #731174)
+
+ -- David Prévot <taffit@debian.org> Wed, 25 Dec 2013 20:48:11 -0400
+
+libscrypt (1-2) unstable; urgency=low
+
+ * Add missing BSD-3-clause license to debian/copyright
+
+ -- Micah Anderson <micah@debian.org> Tue, 13 Aug 2013 09:42:28 -0400
+
+libscrypt (1-1) unstable; urgency=low
+
+ * Initial release (Closes: #713913)
+
+ -- Micah Anderson <micah@debian.org> Sun, 23 Jun 2013 15:59:17 -0400
diff --git a/debian/compat b/debian/compat
new file mode 100644
index 0000000..ec63514
--- /dev/null
+++ b/debian/compat
@@ -0,0 +1 @@
+9
diff --git a/debian/control b/debian/control
new file mode 100644
index 0000000..981b11e
--- /dev/null
+++ b/debian/control
@@ -0,0 +1,38 @@
+Source: libscrypt
+Priority: extra
+Maintainer: Micah Anderson <micah@debian.org>
+Build-Depends: debhelper (>= 9)
+Standards-Version: 3.9.4
+Section: libs
+Homepage: http://www.lolware.net/libscrypt.html
+Vcs-Git: git://git.debian.org/collab-maint/libscrypt.git
+Vcs-Browser: http://git.debian.org/?p=collab-maint/libscrypt.git;a=summary
+
+Package: libscrypt-dev
+Section: libdevel
+Architecture: any
+Depends: libscrypt0 (= ${binary:Version}), ${misc:Depends}
+Description: scrypt shared library - development files
+ The scrypt algorithm is a password-based key derivation function
+ designed to require large amounts of memory. This proof-of-work scheme
+ is intended to make it costly to perform large-scale hardware
+ attacks.
+ .
+ This package contains a shared library implementing the scrypt
+ algorithm, based on the original implementation with a number of
+ harnesses and simplified interfaces.
+
+Package: libscrypt0
+Section: libs
+Architecture: any
+Depends: ${shlibs:Depends}, ${misc:Depends}
+Description: scrypt shared library
+ The scrypt algorithm is a password-based key derivation function
+ designed to require large amounts of memory. This proof-of-work scheme
+ is intended to make it costly to perform large-scale hardware
+ attacks.
+ .
+ This package contains a shared library implementing the scrypt
+ algorithm, based on the original implementation with a number of
+ harnesses and simplified interfaces.
+
diff --git a/debian/copyright b/debian/copyright
new file mode 100644
index 0000000..3ac7de8
--- /dev/null
+++ b/debian/copyright
@@ -0,0 +1,81 @@
+Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: libscrypt
+Source: https://github.com/technion/libscrypt
+
+Files: *
+Copyright: 2013 Joshua Small <technion@lolware.net>
+License: BSD-2-Clause
+
+License: BSD-2-Clause
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+ .
+ Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+ .
+ 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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
+
+Files: modp_b64.*
+Copyright: 2005, 2006, 2007 Nick Galbreath -- nickg [at] modp [dot] com
+License: BSD-3-Clause
+
+License: BSD-3-Clause
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are
+ met:
+ .
+ Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ .
+ 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.
+ .
+ Neither the name of the modp.com nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+ .
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "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 COPYRIGHT
+ OWNER OR CONTRIBUTORS 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.
+
+Files: debian/*
+Copyright: 2013 Micah Anderson <micah@debian.org>
+License: GPL-3+
+ This package is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+ .
+ This package is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+ .
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>
+ .
+ On Debian systems, the complete text of the GNU General
+ Public License version 2 can be found in "/usr/share/common-licenses/GPL-2".
+
diff --git a/debian/docs b/debian/docs
new file mode 100644
index 0000000..b43bf86
--- /dev/null
+++ b/debian/docs
@@ -0,0 +1 @@
+README.md
diff --git a/debian/libscrypt-dev.dirs b/debian/libscrypt-dev.dirs
new file mode 100644
index 0000000..4418816
--- /dev/null
+++ b/debian/libscrypt-dev.dirs
@@ -0,0 +1,2 @@
+usr/lib
+usr/include
diff --git a/debian/libscrypt-dev.install b/debian/libscrypt-dev.install
new file mode 100644
index 0000000..d1bffed
--- /dev/null
+++ b/debian/libscrypt-dev.install
@@ -0,0 +1,3 @@
+usr/include/*
+usr/lib/lib*.a
+usr/lib/lib*.so
diff --git a/debian/libscrypt0.install b/debian/libscrypt0.install
new file mode 100644
index 0000000..d0dbfd1
--- /dev/null
+++ b/debian/libscrypt0.install
@@ -0,0 +1 @@
+usr/lib/lib*.so.*
diff --git a/debian/libscrypt1.dirs b/debian/libscrypt1.dirs
new file mode 100644
index 0000000..6845771
--- /dev/null
+++ b/debian/libscrypt1.dirs
@@ -0,0 +1 @@
+usr/lib
diff --git a/debian/patches/big-endian.patch b/debian/patches/big-endian.patch
new file mode 100644
index 0000000..60c9868
--- /dev/null
+++ b/debian/patches/big-endian.patch
@@ -0,0 +1,53 @@
+From: Aurelien Jarno <aurel32@debian.org>
+Subject: libscrypt: FTBFS on big endian architecture
+Date: Wed, 30 Oct 2013 00:09:08 +0100
+
+Version: 1-2
+Severity: important
+Tags: upstream patch
+Justification: fails to build from source
+
+libscrypt fails to build from source on big endian architectures, due
+to testsuite errors:
+
+| TEST EIGHT: Create an MCF format output
+| TEST EIGHT: SUCCESSFUL, calculated mcf
+| $s1$0e0801$U29kaXVtQ2hsb3JpZGU=$cCO9yzr9c0hGHAbNgf046/2o+7qQT44+qbVD9lRdofLVQylVYT8Pz2LUlwUkKpr55h6F3A1lHkDfzwF7RVdYhw==
+| TEST NINE: Password verify on given MCF
+| TEST NINE: FAILED, hash failed to calculate
+| make[1]: *** [check] Error 1
+
+This is due to code in modp_b64.c which is endianness dependent. A big
+and a little endian version of the code are provided, but the selection
+mechanism is supposed to be done by modifying the source code, which is
+not really compatible with a Debian source package. This leads to the
+little endian code to be always used.
+
+The patch below fixes the problem by getting the endianness from
+<endian.h>.
+
+It has been tested on mips, powerpc and s390x.
+
+Index: libscrypt-1/modp_b64.c
+===================================================================
+--- libscrypt-1.orig/modp_b64.c
++++ libscrypt-1/modp_b64.c
+@@ -45,10 +45,14 @@
+ #include "modp_b64.h"
+
+
+-/* if on motoral, sun, ibm; uncomment this */
+-/* #define WORDS_BIGENDIAN 1 */
+-/* else for Intel, Amd; uncomment this */
+-/* #undef WORDS_BIGENDIAN */
++#include <endian.h>
++#if __BYTE_ORDER == __BIG_ENDIAN
++# define WORDS_BIGENDIAN 1
++#elif __BYTE_ORDER == __LITTLE_ENDIAN
++# undef WORDS_BIGENDIAN
++#else
++#error "Unknown endianess"
++#endif
+
+ #include "modp_b64_data.h"
+
diff --git a/debian/patches/fix_makefile b/debian/patches/fix_makefile
new file mode 100644
index 0000000..872015b
--- /dev/null
+++ b/debian/patches/fix_makefile
@@ -0,0 +1,25 @@
+--- a/Makefile
++++ b/Makefile
+@@ -1,3 +1,9 @@
++PREFIX = /usr/local
++LIBDIR = $(PREFIX)/lib
++INCLUDEDIR = $(PREFIX)/include
++MAKE_DIR = install -d
++INSTALL_DATA = install -m 0644
++
+ CC=gcc
+ CFLAGS=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC
+ all: reference
+@@ -26,7 +32,8 @@
+ valgrind ./reference
+
+ install: library
+- install -m 0644 libscrypt.a $(DESTDIR)/usr/local/lib
+- install -m 0644 libscrypt.so.0 $(DESTDIR)/usr/local/lib
+- ln -s -f $(DESTDIR)/usr/local/lib/libscrypt.so.0 $(DESTDIR)/usr/local/lib/libscrypt.so
+- install -m 0644 libscrypt.h $(DESTDIR)/usr/local/include
++ $(MAKE_DIR) $(DESTDIR) $(DESTDIR)$(PREFIX) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR)
++ $(INSTALL_DATA) libscrypt.a $(DESTDIR)$(LIBDIR)
++ $(INSTALL_DATA) libscrypt.so.0 $(DESTDIR)$(LIBDIR)
++ ln -s -f libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so
++ $(INSTALL_DATA) libscrypt.h $(DESTDIR)$(INCLUDEDIR)
diff --git a/debian/patches/series b/debian/patches/series
new file mode 100644
index 0000000..5d020e2
--- /dev/null
+++ b/debian/patches/series
@@ -0,0 +1,2 @@
+fix_makefile
+big-endian.patch
diff --git a/debian/rules b/debian/rules
new file mode 100755
index 0000000..2dfe8b7
--- /dev/null
+++ b/debian/rules
@@ -0,0 +1,11 @@
+#!/usr/bin/make -f
+# -*- makefile -*-
+
+# Uncomment this to turn on verbose mode.
+export DH_VERBOSE=1
+
+%:
+ dh $@
+
+override_dh_auto_install:
+ $(MAKE) DESTDIR=debian/tmp PREFIX=/usr install
diff --git a/debian/shlibs.local b/debian/shlibs.local
new file mode 100644
index 0000000..637ff02
--- /dev/null
+++ b/debian/shlibs.local
@@ -0,0 +1 @@
+liblibscrypt 0.1 libscrypt (>> 0.1-0), libscrypt (<< 0.1-99)
diff --git a/debian/source/format b/debian/source/format
new file mode 100644
index 0000000..163aaf8
--- /dev/null
+++ b/debian/source/format
@@ -0,0 +1 @@
+3.0 (quilt)
diff --git a/debian/watch b/debian/watch
new file mode 100644
index 0000000..ea0c3a5
--- /dev/null
+++ b/debian/watch
@@ -0,0 +1,8 @@
+# Compulsory line, this is a version 3 file
+version=3
+
+# Uncomment to examine a Webpage
+# <Webpage URL> <string match>
+http://www.lolware.net/libscrypt.html http://www.lolware.net/libscrypt-stable(.*?).tar.gz
+
+
diff --git a/libscrypt.h b/libscrypt.h
new file mode 100644
index 0000000..b244d8d
--- /dev/null
+++ b/libscrypt.h
@@ -0,0 +1,61 @@
+/*-
+ */
+#ifndef _CRYPTO_SCRYPT_H_
+#define _CRYPTO_SCRYPT_H_
+
+
+#include <stdint.h>
+
+/**
+ * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
+ * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r,
+ * p, buflen) and write the result into buf. The parameters r, p, and buflen
+ * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N
+ * must be a power of 2 greater than 1.
+ *
+ * libscrypt_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
+ * password; duh
+ * N: CPU AND RAM cost (first modifier)
+ * r: RAM Cost
+ * p: CPU cost (parallelisation)
+ * In short, N is your main performance modifier. Values of r = 8, p = 1 are
+ * standard unless you want to modify the CPU/RAM ratio.
+ * Return 0 on success; or -1 on error.
+ */
+int libscrypt_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t,
+ uint32_t, uint32_t, /*@out@*/ uint8_t *, size_t);
+
+/**
+ * Converts a binary string to a hex representation of that string
+ * outbuf must have size of at least buf * 2 + 1.
+ */
+int libscrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs);
+
+/* Converts a series of input parameters to a MCF form for storage */
+int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, char *mcf);
+
+/* Generates a salt. This is not a cryptographically unpredictable function,
+ * but should produce appropriately randomised output for this purpose
+ */
+void libscrypt_salt_gen(/*@out@*/ char *rand, size_t len);
+
+/* Checks a given MCF against a password */
+int libscrypt_check(char *mcf, char *password);
+
+/* Creates a hash of a passphrase using a randomly generated salt */
+int libscrypt_hash(char *dst, char* passphrase, uint32_t N, uint8_t r, uint8_t p);
+
+int libscrypt_b64_encode(/*@out@*/ char* dest, const char* str, size_t len);
+int libscrypt_b64_decode(/*@out@*/ char* dest, const char* src, size_t len);
+
+
+/* Sane default values */
+#define SCRYPT_HASH_LEN 64 /* This can be user defined -
+ *but 64 is the reference size
+ */
+#define SCRYPT_MCF_LEN 124 /* mcf is 120 byte + nul */
+#define SCRYPT_MCF_ID "$s1"
+#define SCRYPT_N 16384
+#define SCRYPT_r 8
+#define SCRYPT_p 16
+#endif /* !_CRYPTO_SCRYPT_H_ */
diff --git a/libscrypt.version b/libscrypt.version
new file mode 100644
index 0000000..7f4cbc0
--- /dev/null
+++ b/libscrypt.version
@@ -0,0 +1,10 @@
+libscrypt {
+ global: libscrypt_check;
+libscrypt_hash;
+libscrypt_hexconvert;
+libscrypt_mcf;
+libscrypt_salt_gen;
+libscrypt_scrypt;
+libscrypt_b64_encode;
+ local: *;
+};
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..48c6024
--- /dev/null
+++ b/main.c
@@ -0,0 +1,213 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "libscrypt.h"
+
+#define REF1 "fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640"
+
+#define REF2 "7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887"
+
+
+int main()
+{
+ uint8_t hashbuf[SCRYPT_HASH_LEN];
+ char outbuf[132];
+ char mcf[SCRYPT_MCF_LEN];
+ char mcf2[SCRYPT_MCF_LEN];
+ char saltbuf[64];
+ int retval;
+ /**
+ * libscrypt_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen):
+ * password; duh
+ * N: CPU AND RAM cost (first modifier)
+ * r: RAM Cost
+ * p: CPU cost (parallelisation)
+ * In short, N is your main performance modifier. Values of r = 8, p = 1 are
+ * standard unless you want to modify the CPU/RAM ratio.
+ int libscrypt_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t,
+ uint32_t, uint32_t, uint8_t *, size_t);
+*/
+
+ printf("TEST ONE: Direct call to reference function with password 'password' and salt 'NaCL'\n");
+
+ retval = libscrypt_scrypt((uint8_t*)"password",strlen("password"), (uint8_t*)"NaCl", strlen("NaCl"), 1024, 8, 16, hashbuf, sizeof(hashbuf));
+
+ if(retval != 0)
+ {
+ printf("TEST ONE FAILED: Failed to create hash of \"password\"\\n");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("TEST ONE: SUCCESSFUL\n");
+
+ /* Convert the binary string to hex representation. Outbuf must be
+ * at least sizeof(hashbuf) * 2 + 1
+ * Returns 0 on fail, 1 on success
+ */
+ printf("TEST TWO: Convert binary output to hex\n");
+ retval = libscrypt_hexconvert(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf));
+ if(!retval)
+ {
+ printf("TEST TWO: FAILED\n");
+ exit(EXIT_FAILURE);
+ }
+ printf("TEST TWO: SUCCESSFUL, Hex output is:\n%s\n", outbuf);
+
+ printf("TEST THREE: Compare hex output to reference hash output\n");
+
+ /* REF1 is a reference vector from Colin's implementation. */
+ if(strcmp(outbuf, REF1) != 0)
+ {
+ printf("TEST THREE: FAILED to match reference on hash\n");
+ exit(EXIT_FAILURE);
+ }
+ else
+ {
+ printf("TEST THREE: SUCCESSUL, Test vector matched!\n");
+ }
+
+ printf("TEST FOUR: Direct call to reference function with pleaseletmein password and SodiumChloride as salt\n");
+
+ /* Tests 4-6 repeat tests 1-3 with a different reference vector */
+
+ retval = libscrypt_scrypt((uint8_t*)"pleaseletmein",strlen("pleaseletmein"), (uint8_t*)"SodiumChloride", strlen("SodiumChloride"), 16384, 8, 1, hashbuf, sizeof(hashbuf));
+
+ if(retval != 0)
+ {
+ printf("TEST FOUR FAILED: Failed to create hash of 'pleaseletmein'\n");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("TEST FOUR: SUCCESSFUL\n");
+
+ /* Convert the binary string to hex representation. Outbuf must be
+ * at least sizeof(hashbuf) * 2 + 1
+ */
+ printf("TEST FIVE: Convert binary output to hex\n");
+ retval = libscrypt_hexconvert(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf));
+ if(!retval)
+ {
+ printf("TEST FIVE: FAILED\n");
+ exit(EXIT_FAILURE);
+ }
+ printf("TEST FIVE: SUCCESSFUL, Hex output is:\n%s\n", outbuf);
+
+ printf("TEST SIX: Compare hex output to reference hash output\n");
+
+ if(strcmp(outbuf, REF2) != 0)
+ {
+ printf("TEST SIX: FAILED to match reference on hash\n");
+ exit(EXIT_FAILURE);
+ }
+ else
+ {
+ printf("TEST SIX: SUCCESSUL, Test vector matched!\n");
+ }
+
+ /* This function will convert the binary output to BASE64. Although
+ * we converted to hex for the reference vectors, BASE64 is more useful.
+ * Returns -1 on error, else returns length.
+ * Correct buffer length can be determined using the below function if
+ retuired.
+ * char* dest = (char*) malloc(modp_b64_encode_len);
+ */
+
+ printf("TEST SEVEN: BASE64 encoding the salt and hash output\n");
+
+ retval = libscrypt_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf));
+ if(retval == -1)
+ {
+ printf("TEST SEVEN FAILED\n");
+ exit(EXIT_FAILURE);
+ }
+ retval = libscrypt_b64_encode(saltbuf, "SodiumChloride", strlen("SodiumChloride"));
+ if(retval == -1)
+ {
+ printf("TEST SEVEN FAILED\n");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("TEST SEVEN: SUCCESSFUL\n");
+
+ printf("TEST EIGHT: Create an MCF format output\n");
+
+ /* Creates a standard format output
+ * int crypto_scrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, char *mcf);
+ * Returns 0 on error, most likely reason is log2(N) not an integer.
+ */
+ retval = libscrypt_mcf(16384, 8, 1, saltbuf, outbuf, mcf);
+ if(!retval)
+ {
+ printf("TEST EIGHT FAILED\n");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("TEST EIGHT: SUCCESSFUL, calculated mcf\n%s\n", mcf);
+
+ /* Since later calls to scrypt_check() butcher mcf, make a second */
+ strcpy(mcf2, mcf);
+
+ /* Couldn't be simpler - for a given mcf, check is the password is valid
+ * Returns < 0 on failure to calculate hash
+ * 0 if password incorrect
+ * >1 if password correct
+ */
+
+ printf("TEST NINE: Password verify on given MCF\n");
+ retval = libscrypt_check(mcf, "pleaseletmein");
+
+ if(retval < 0)
+ {
+ printf("TEST NINE: FAILED, hash failed to calculate\n");
+ exit(EXIT_FAILURE);
+ }
+ if(retval == 0)
+ {
+ printf("TEST NINE: FAILED, claimed pleaseletmein hash claimed did not verify\n");
+ exit(EXIT_FAILURE);
+ }
+ /* retval >0 is a success */
+ printf("TEST NINE: SUCCESSFUL, tested pleaseletmein password\n");
+
+ printf("TEST TEN: Password verify on same MCF, incorrect password\n");
+ retval = libscrypt_check(mcf2, "pleasefailme");
+
+ if(retval < 0)
+ {
+ printf("TEST TEN: FAILED, hash failed to calculate\n");
+ exit(EXIT_FAILURE);
+ }
+ if(retval > 0)
+ {
+ printf("TEST TEN: FAILED, fail hash has passed\n");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("TEST TEN: SUCCESSFUL, refused incorrect password\n");
+
+ printf("TEST ELEVEN: Testing salt generator\n");
+ /* TODO: I'm not presently sure how this function could fail */
+ libscrypt_salt_gen(saltbuf, 16);
+
+ retval = libscrypt_b64_encode(saltbuf, (char*)saltbuf, 16);
+ if(retval == -1)
+ {
+ printf("TEST ELEVEN FAILED\n");
+ exit(EXIT_FAILURE);
+ }
+ printf("TEST ELEVEN: SUCCESSFUL, Generated %s\n", outbuf);
+
+ printf("TEST TWELVE: Simple hash creation\n");
+
+ retval = libscrypt_hash(outbuf, "My cats's breath smells like cat food", SCRYPT_N, SCRYPT_r, SCRYPT_p);
+ if(!retval)
+ {
+ printf("TEST TWELVE: FAILED, Failed to create simple hash\n");
+ exit(EXIT_FAILURE);
+ }
+ printf("TEST TWELVE: SUCCESSSFUL. Received the following from simple hash:\n%s\n", outbuf);
+
+ return 0;
+}
+
diff --git a/modp_b64.c b/modp_b64.c
new file mode 100644
index 0000000..5cbf41a
--- /dev/null
+++ b/modp_b64.c
@@ -0,0 +1,268 @@
+/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
+/* vi: set expandtab shiftwidth=4 tabstop=4: */
+/**
+ * \file modp_b64.c
+ * <PRE>
+ * MODP_B64 - High performance base64 encoder/decoder
+ * http://code.google.com/p/stringencoders/
+ *
+ * Copyright &copy; 2005, 2006, 2007 Nick Galbreath -- nickg [at] modp [dot] com
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 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.
+ *
+ * Neither the name of the modp.com nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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 COPYRIGHT
+ * OWNER OR CONTRIBUTORS 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.
+ *
+ * This is the standard "new" BSD license:
+ * http://www.opensource.org/licenses/bsd-license.php
+ * </PRE>
+ */
+
+/* public header */
+#include "modp_b64.h"
+
+
+#include <endian.h>
+#if __BYTE_ORDER == __BIG_ENDIAN
+# define WORDS_BIGENDIAN 1
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+# undef WORDS_BIGENDIAN
+#else
+#error "Unknown endianess"
+#endif
+
+#include "modp_b64_data.h"
+
+#define BADCHAR 0x01FFFFFF
+
+/**
+ * you can control if we use padding by commenting out this
+ * next line. However, I highly recommend you use padding and not
+ * using it should only be for compatability with a 3rd party.
+ * Also, 'no padding' is not tested!
+ */
+#define DOPAD 1
+
+/*
+ * if we aren't doing padding
+ * set the pad character to NULL
+ */
+#ifndef DOPAD
+#undef CHARPAD
+#define CHARPAD '\0'
+#endif
+
+int libscrypt_b64_encode(char* dest, const char* str, size_t len)
+{
+ int i;
+ const uint8_t* s = (const uint8_t*) str;
+ uint8_t* p = (uint8_t*) dest;
+
+ /* unsigned here is important! */
+ /* uint8_t is fastest on G4, amd */
+ /* uint32_t is fastest on Intel */
+ uint32_t t1, t2, t3;
+
+ for (i = 0; i < len - 2; i += 3) {
+ t1 = s[i]; t2 = s[i+1]; t3 = s[i+2];
+ *p++ = e0[t1];
+ *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
+ *p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
+ *p++ = e2[t3];
+ }
+
+ switch (len - i) {
+ case 0:
+ break;
+ case 1:
+ t1 = s[i];
+ *p++ = e0[t1];
+ *p++ = e1[(t1 & 0x03) << 4];
+ *p++ = CHARPAD;
+ *p++ = CHARPAD;
+ break;
+ default: /* case 2 */
+ t1 = s[i]; t2 = s[i+1];
+ *p++ = e0[t1];
+ *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
+ *p++ = e2[(t2 & 0x0F) << 2];
+ *p++ = CHARPAD;
+ }
+
+ *p = '\0';
+ return (int)(p - (uint8_t*)dest);
+}
+
+#ifdef WORDS_BIGENDIAN /* BIG ENDIAN -- SUN / IBM / MOTOROLA */
+int libscrypt_b64_decode(char* dest, const char* src, size_t len)
+{
+ int i;
+ if (len == 0) return 0;
+
+#ifdef DOPAD
+ /* if padding is used, then the message must be at least
+ 4 chars and be a multiple of 4.
+ there can be at most 2 pad chars at the end */
+ if (len < 4 || (len % 4 != 0)) return -1;
+ if (src[len-1] == CHARPAD) {
+ len--;
+ if (src[len -1] == CHARPAD) {
+ len--;
+ }
+ }
+#endif /* DOPAD */
+
+ int leftover = len % 4;
+ int chunks = (leftover == 0) ? len / 4 - 1 : len /4;
+
+ uint8_t* p = (uint8_t*) dest;
+ uint32_t x = 0;
+ uint32_t* destInt = (uint32_t*) p;
+ uint32_t* srcInt = (uint32_t*) src;
+ uint32_t y = *srcInt++;
+ for (i = 0; i < chunks; ++i) {
+ x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] |
+ d2[y >> 8 & 0xff] | d3[y & 0xff];
+
+ if (x >= BADCHAR) return -1;
+ *destInt = x << 8;
+ p += 3;
+ destInt = (uint32_t*)p;
+ y = *srcInt++;
+ }
+
+ switch (leftover) {
+ case 0:
+ x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] |
+ d2[y >> 8 & 0xff] | d3[y & 0xff];
+ if (x >= BADCHAR) return -1;
+ *p++ = ((uint8_t*)&x)[1];
+ *p++ = ((uint8_t*)&x)[2];
+ *p = ((uint8_t*)&x)[3];
+ return (chunks+1)*3;
+#ifndef DOPAD
+ case 1: /* with padding this is an impossible case */
+ x = d3[y >> 24];
+ *p = (uint8_t)x;
+ break;
+#endif
+ case 2:
+ x = d3[y >> 24] *64 + d3[(y >> 16) & 0xff];
+ *p = (uint8_t)(x >> 4);
+ break;
+ default: /* case 3 */
+ x = (d3[y >> 24] *64 + d3[(y >> 16) & 0xff])*64 +
+ d3[(y >> 8) & 0xff];
+ *p++ = (uint8_t) (x >> 10);
+ *p = (uint8_t) (x >> 2);
+ break;
+ }
+
+ if (x >= BADCHAR) return -1;
+ return 3*chunks + (6*leftover)/8;
+}
+
+#else /* LITTLE ENDIAN -- INTEL AND FRIENDS */
+
+int libscrypt_b64_decode(char* dest, const char* src, size_t len)
+{
+ int i;
+ if (len == 0) return 0;
+
+#ifdef DOPAD
+ /*
+ * if padding is used, then the message must be at least
+ * 4 chars and be a multiple of 4
+ */
+ if (len < 4 || (len % 4 != 0)) return -1; /* error */
+ /* there can be at most 2 pad chars at the end */
+ if (src[len-1] == CHARPAD) {
+ len--;
+ if (src[len -1] == CHARPAD) {
+ len--;
+ }
+ }
+#endif
+
+ int leftover = len % 4;
+ int chunks = (leftover == 0) ? len / 4 - 1 : len /4;
+
+ uint8_t* p = (uint8_t*) dest;
+ uint32_t x = 0;
+ uint32_t* destInt = (uint32_t*) p;
+ uint32_t* srcInt = (uint32_t*) src;
+ uint32_t y = *srcInt++;
+ for (i = 0; i < chunks; ++i) {
+ x = d0[y & 0xff] |
+ d1[(y >> 8) & 0xff] |
+ d2[(y >> 16) & 0xff] |
+ d3[(y >> 24) & 0xff];
+
+ if (x >= BADCHAR) return -1;
+ *destInt = x ;
+ p += 3;
+ destInt = (uint32_t*)p;
+ y = *srcInt++;}
+
+
+ switch (leftover) {
+ case 0:
+ x = d0[y & 0xff] |
+ d1[(y >> 8) & 0xff] |
+ d2[(y >> 16) & 0xff] |
+ d3[(y >> 24) & 0xff];
+
+ if (x >= BADCHAR) return -1;
+ *p++ = ((uint8_t*)(&x))[0];
+ *p++ = ((uint8_t*)(&x))[1];
+ *p = ((uint8_t*)(&x))[2];
+ return (chunks+1)*3;
+ break;
+#ifndef DOPAD
+ case 1: /* with padding this is an impossible case */
+ x = d0[y & 0xff];
+ *p = *((uint8_t*)(&x)); // i.e. first char/byte in int
+ break;
+#endif
+ case 2: // * case 2, 1 output byte */
+ x = d0[y & 0xff] | d1[y >> 8 & 0xff];
+ *p = *((uint8_t*)(&x)); // i.e. first char
+ break;
+ default: /* case 3, 2 output bytes */
+ x = d0[y & 0xff] |
+ d1[y >> 8 & 0xff ] |
+ d2[y >> 16 & 0xff]; /* 0x3c */
+ *p++ = ((uint8_t*)(&x))[0];
+ *p = ((uint8_t*)(&x))[1];
+ break;
+ }
+
+ if (x >= BADCHAR) return -1;
+
+ return 3*chunks + (6*leftover)/8;
+}
+
+#endif /* if bigendian / else / endif */
diff --git a/modp_b64.h b/modp_b64.h
new file mode 100644
index 0000000..720049e
--- /dev/null
+++ b/modp_b64.h
@@ -0,0 +1,235 @@
+/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
+/* vi: set expandtab shiftwidth=4 tabstop=4: */
+
+/**
+ * \file
+ * <PRE>
+ * High performance base64 encoder / decoder
+ *
+ * Copyright &copy; 2005, 2006, 2007 Nick Galbreath -- nickg [at] modp [dot] com
+ * All rights reserved.
+ *
+ * http://code.google.com/p/stringencoders/
+ *
+ * Released under bsd license. See modp_b64.c for details.
+ * </pre>
+ *
+ * This uses the standard base 64 alphabet. If you are planning
+ * to embed a base 64 encoding inside a URL use modp_b64w instead.
+ *
+ */
+
+#ifndef COM_MODP_STRINGENCODERS_B64
+#define COM_MODP_STRINGENCODERS_B64
+
+#ifdef __cplusplus
+#define BEGIN_C extern "C" {
+#define END_C }
+#else
+#define BEGIN_C
+#define END_C
+#endif
+
+BEGIN_C
+
+/**
+ * Encode a raw binary string into base 64.
+ * \param[out] dest should be allocated by the caller to contain
+ * at least modp_b64_encode_len(len) bytes (see below)
+ * This will contain the null-terminated b64 encoded result
+ * \param[in] src contains the bytes
+ * \param[in] len contains the number of bytes in the src
+ * \return length of the destination string plus the ending null byte
+ * i.e. the result will be equal to strlen(dest) + 1
+ *
+ * Example
+ *
+ * \code
+ * char* src = ...;
+ * int srclen = ...; //the length of number of bytes in src
+ * char* dest = (char*) malloc(modp_b64_encode_len);
+ * int len = modp_b64_encode(dest, src, sourcelen);
+ * if (len == -1) {
+ * printf("Error\n");
+ * } else {
+ * printf("b64 = %s\n", dest);
+ * }
+ * \endcode
+ *
+ */
+#include <string.h>
+int libscrypt_b64_encode(char* dest, const char* str, size_t len);
+
+/**
+ * Decode a base64 encoded string
+ *
+ * \param[out] dest should be allocated by the caller to contain at least
+ * len * 3 / 4 bytes. The destination cannot be the same as the source
+ * They must be different buffers.
+ * \param[in] src should contain exactly len bytes of b64 characters.
+ * if src contains -any- non-base characters (such as white
+ * space, -1 is returned.
+ * \param[in] len is the length of src
+ *
+ * \return the length (strlen) of the output, or -1 if unable to
+ * decode
+ *
+ * \code
+ * char* src = ...;
+ * int srclen = ...; // or if you don't know use strlen(src)
+ * char* dest = (char*) malloc(modp_b64_decode_len(srclen));
+ * int len = modp_b64_decode(dest, src, sourcelen);
+ * if (len == -1) { error }
+ * \endcode
+ */
+int libscrypt_b64_decode(char* dest, const char* src, size_t len);
+
+/**
+ * Given a source string of length len, this returns the amount of
+ * memory the destination string should have.
+ *
+ * remember, this is integer math
+ * 3 bytes turn into 4 chars
+ * ceiling[len / 3] * 4 + 1
+ *
+ * +1 is for any extra null.
+ */
+#define modp_b64_encode_len(A) ((A+2)/3 * 4 + 1)
+
+/**
+ * Given a base64 string of length len,
+ * this returns the amount of memory required for output string
+ * It maybe be more than the actual number of bytes written.
+ * NOTE: remember this is integer math
+ * this allocates a bit more memory than traditional versions of b64
+ * decode 4 chars turn into 3 bytes
+ * floor[len * 3/4] + 2
+ */
+#define modp_b64_decode_len(A) (A / 4 * 3 + 2)
+
+/**
+ * Will return the strlen of the output from encoding.
+ * This may be less than the required number of bytes allocated.
+ *
+ * This allows you to 'deserialized' a struct
+ * \code
+ * char* b64encoded = "...";
+ * int len = strlen(b64encoded);
+ *
+ * struct datastuff foo;
+ * if (modp_b64_encode_strlen(sizeof(struct datastuff)) != len) {
+ * // wrong size
+ * return false;
+ * } else {
+ * // safe to do;
+ * if (modp_b64_decode((char*) &foo, b64encoded, len) == -1) {
+ * // bad characters
+ * return false;
+ * }
+ * }
+ * // foo is filled out now
+ * \endcode
+ */
+#define modp_b64_encode_strlen(A) ((A + 2)/ 3 * 4)
+
+END_C
+
+#ifdef __cplusplus
+#include <cstring>
+#include <string>
+
+namespace modp {
+ /** \brief b64 encode a cstr with len
+ *
+ * \param[in] s the input string to encode
+ * \param[in] len the length of the input string
+ * \return a newly allocated b64 string. Empty if failed.
+ */
+ inline std::string b64_encode(const char* s, size_t len)
+ {
+ std::string x(modp_b64_encode_len(len), '\0');
+ int d = modp_b64_encode(const_cast<char*>(x.data()), s,
+ static_cast<int>(len));
+ x.erase(d, std::string::npos);
+ return x;
+ }
+
+ /** \brief b64 encode a cstr
+ *
+ * \param[in] s the input string to encode
+ * \return a newly allocated b64 string. Empty if failed.
+ */
+ inline std::string b64_encode(const char* s)
+ {
+ return b64_encode(s, static_cast<int>(strlen(s)));
+ }
+
+ /** \brief b64 encode a const std::string
+ *
+ * \param[in] s the input string to encode
+ * \return a newly allocated b64 string. Empty if failed.
+ */
+ inline std::string b64_encode(const std::string& s)
+ {
+ return b64_encode(s.data(), s.size());
+ }
+
+ /**
+ * base 64 encode a string (self-modifing)
+ *
+ * This function is for C++ only (duh)
+ *
+ * \param[in,out] s the string to be decoded
+ * \return a reference to the input string
+ */
+ inline std::string& b64_encode(std::string& s)
+ {
+ std::string x(b64_encode(s.data(), s.size()));
+ s.swap(x);
+ return s;
+ }
+
+ inline std::string b64_decode(const char* src, size_t len)
+ {
+ std::string x(modp_b64_decode_len(len)+1, '\0');
+ int d = modp_b64_decode(const_cast<char*>(x.data()), src,
+ static_cast<int>(len));
+ if (d < 0) {
+ x.clear();
+ } else {
+ x.erase(d, std::string::npos);
+ }
+ return x;
+ }
+
+ inline std::string b64_decode(const char* src)
+ {
+ return b64_decode(src, strlen(src));
+ }
+
+ /**
+ * base 64 decode a string (self-modifing)
+ * On failure, the string is empty.
+ *
+ * This function is for C++ only (duh)
+ *
+ * \param[in,out] s the string to be decoded
+ * \return a reference to the input string
+ */
+ inline std::string& b64_decode(std::string& s)
+ {
+ std::string x(b64_decode(s.data(), s.size()));
+ s.swap(x);
+ return s;
+ }
+
+ inline std::string b64_decode(const std::string& s)
+ {
+ return b64_decode(s.data(), s.size());
+ }
+
+}
+
+#endif /* __cplusplus */
+
+#endif /* MODP_B64 */
diff --git a/modp_b64_data.h b/modp_b64_data.h
new file mode 100644
index 0000000..4fb321c
--- /dev/null
+++ b/modp_b64_data.h
@@ -0,0 +1,480 @@
+#include <stdint.h>
+#define CHAR62 '+'
+#define CHAR63 '/'
+#define CHARPAD '='
+static const unsigned char e0[256] = {
+ 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C',
+ 'C', 'C', 'D', 'D', 'D', 'D', 'E', 'E', 'E', 'E',
+ 'F', 'F', 'F', 'F', 'G', 'G', 'G', 'G', 'H', 'H',
+ 'H', 'H', 'I', 'I', 'I', 'I', 'J', 'J', 'J', 'J',
+ 'K', 'K', 'K', 'K', 'L', 'L', 'L', 'L', 'M', 'M',
+ 'M', 'M', 'N', 'N', 'N', 'N', 'O', 'O', 'O', 'O',
+ 'P', 'P', 'P', 'P', 'Q', 'Q', 'Q', 'Q', 'R', 'R',
+ 'R', 'R', 'S', 'S', 'S', 'S', 'T', 'T', 'T', 'T',
+ 'U', 'U', 'U', 'U', 'V', 'V', 'V', 'V', 'W', 'W',
+ 'W', 'W', 'X', 'X', 'X', 'X', 'Y', 'Y', 'Y', 'Y',
+ 'Z', 'Z', 'Z', 'Z', 'a', 'a', 'a', 'a', 'b', 'b',
+ 'b', 'b', 'c', 'c', 'c', 'c', 'd', 'd', 'd', 'd',
+ 'e', 'e', 'e', 'e', 'f', 'f', 'f', 'f', 'g', 'g',
+ 'g', 'g', 'h', 'h', 'h', 'h', 'i', 'i', 'i', 'i',
+ 'j', 'j', 'j', 'j', 'k', 'k', 'k', 'k', 'l', 'l',
+ 'l', 'l', 'm', 'm', 'm', 'm', 'n', 'n', 'n', 'n',
+ 'o', 'o', 'o', 'o', 'p', 'p', 'p', 'p', 'q', 'q',
+ 'q', 'q', 'r', 'r', 'r', 'r', 's', 's', 's', 's',
+ 't', 't', 't', 't', 'u', 'u', 'u', 'u', 'v', 'v',
+ 'v', 'v', 'w', 'w', 'w', 'w', 'x', 'x', 'x', 'x',
+ 'y', 'y', 'y', 'y', 'z', 'z', 'z', 'z', '0', '0',
+ '0', '0', '1', '1', '1', '1', '2', '2', '2', '2',
+ '3', '3', '3', '3', '4', '4', '4', '4', '5', '5',
+ '5', '5', '6', '6', '6', '6', '7', '7', '7', '7',
+ '8', '8', '8', '8', '9', '9', '9', '9', '+', '+',
+ '+', '+', '/', '/', '/', '/'
+};
+
+static const unsigned char e1[256] = {
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
+ 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
+ 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
+ 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+ 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', '+', '/', 'A', 'B', 'C', 'D', 'E', 'F',
+ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+ 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
+ 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
+ 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
+ '4', '5', '6', '7', '8', '9', '+', '/', 'A', 'B',
+ 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
+ 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
+ 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
+ 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ '+', '/', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+ 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
+ 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
+ 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
+ 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+ 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
+ '6', '7', '8', '9', '+', '/'
+};
+
+static const unsigned char e2[256] = {
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
+ 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
+ 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
+ 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+ 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', '+', '/', 'A', 'B', 'C', 'D', 'E', 'F',
+ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+ 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
+ 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
+ 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
+ '4', '5', '6', '7', '8', '9', '+', '/', 'A', 'B',
+ 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
+ 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
+ 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
+ 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ '+', '/', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+ 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
+ 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b',
+ 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
+ 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+ 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
+ '6', '7', '8', '9', '+', '/'
+};
+
+
+
+#ifdef WORDS_BIGENDIAN
+
+
+/* SPECIAL DECODE TABLES FOR BIG ENDIAN (IBM/MOTOROLA/SUN) CPUS */
+
+static const uint32_t d0[256] = {
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x00f80000, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00fc0000,
+0x00d00000, 0x00d40000, 0x00d80000, 0x00dc0000, 0x00e00000, 0x00e40000,
+0x00e80000, 0x00ec0000, 0x00f00000, 0x00f40000, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
+0x00040000, 0x00080000, 0x000c0000, 0x00100000, 0x00140000, 0x00180000,
+0x001c0000, 0x00200000, 0x00240000, 0x00280000, 0x002c0000, 0x00300000,
+0x00340000, 0x00380000, 0x003c0000, 0x00400000, 0x00440000, 0x00480000,
+0x004c0000, 0x00500000, 0x00540000, 0x00580000, 0x005c0000, 0x00600000,
+0x00640000, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x00680000, 0x006c0000, 0x00700000, 0x00740000, 0x00780000,
+0x007c0000, 0x00800000, 0x00840000, 0x00880000, 0x008c0000, 0x00900000,
+0x00940000, 0x00980000, 0x009c0000, 0x00a00000, 0x00a40000, 0x00a80000,
+0x00ac0000, 0x00b00000, 0x00b40000, 0x00b80000, 0x00bc0000, 0x00c00000,
+0x00c40000, 0x00c80000, 0x00cc0000, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff
+};
+
+
+static const uint32_t d1[256] = {
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x0003e000, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x0003f000,
+0x00034000, 0x00035000, 0x00036000, 0x00037000, 0x00038000, 0x00039000,
+0x0003a000, 0x0003b000, 0x0003c000, 0x0003d000, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
+0x00001000, 0x00002000, 0x00003000, 0x00004000, 0x00005000, 0x00006000,
+0x00007000, 0x00008000, 0x00009000, 0x0000a000, 0x0000b000, 0x0000c000,
+0x0000d000, 0x0000e000, 0x0000f000, 0x00010000, 0x00011000, 0x00012000,
+0x00013000, 0x00014000, 0x00015000, 0x00016000, 0x00017000, 0x00018000,
+0x00019000, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x0001a000, 0x0001b000, 0x0001c000, 0x0001d000, 0x0001e000,
+0x0001f000, 0x00020000, 0x00021000, 0x00022000, 0x00023000, 0x00024000,
+0x00025000, 0x00026000, 0x00027000, 0x00028000, 0x00029000, 0x0002a000,
+0x0002b000, 0x0002c000, 0x0002d000, 0x0002e000, 0x0002f000, 0x00030000,
+0x00031000, 0x00032000, 0x00033000, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff
+};
+
+
+static const uint32_t d2[256] = {
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x00000f80, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000fc0,
+0x00000d00, 0x00000d40, 0x00000d80, 0x00000dc0, 0x00000e00, 0x00000e40,
+0x00000e80, 0x00000ec0, 0x00000f00, 0x00000f40, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
+0x00000040, 0x00000080, 0x000000c0, 0x00000100, 0x00000140, 0x00000180,
+0x000001c0, 0x00000200, 0x00000240, 0x00000280, 0x000002c0, 0x00000300,
+0x00000340, 0x00000380, 0x000003c0, 0x00000400, 0x00000440, 0x00000480,
+0x000004c0, 0x00000500, 0x00000540, 0x00000580, 0x000005c0, 0x00000600,
+0x00000640, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x00000680, 0x000006c0, 0x00000700, 0x00000740, 0x00000780,
+0x000007c0, 0x00000800, 0x00000840, 0x00000880, 0x000008c0, 0x00000900,
+0x00000940, 0x00000980, 0x000009c0, 0x00000a00, 0x00000a40, 0x00000a80,
+0x00000ac0, 0x00000b00, 0x00000b40, 0x00000b80, 0x00000bc0, 0x00000c00,
+0x00000c40, 0x00000c80, 0x00000cc0, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff
+};
+
+
+static const uint32_t d3[256] = {
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x0000003e, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x0000003f,
+0x00000034, 0x00000035, 0x00000036, 0x00000037, 0x00000038, 0x00000039,
+0x0000003a, 0x0000003b, 0x0000003c, 0x0000003d, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
+0x00000001, 0x00000002, 0x00000003, 0x00000004, 0x00000005, 0x00000006,
+0x00000007, 0x00000008, 0x00000009, 0x0000000a, 0x0000000b, 0x0000000c,
+0x0000000d, 0x0000000e, 0x0000000f, 0x00000010, 0x00000011, 0x00000012,
+0x00000013, 0x00000014, 0x00000015, 0x00000016, 0x00000017, 0x00000018,
+0x00000019, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x0000001a, 0x0000001b, 0x0000001c, 0x0000001d, 0x0000001e,
+0x0000001f, 0x00000020, 0x00000021, 0x00000022, 0x00000023, 0x00000024,
+0x00000025, 0x00000026, 0x00000027, 0x00000028, 0x00000029, 0x0000002a,
+0x0000002b, 0x0000002c, 0x0000002d, 0x0000002e, 0x0000002f, 0x00000030,
+0x00000031, 0x00000032, 0x00000033, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff
+};
+
+
+#else
+
+
+/* SPECIAL DECODE TABLES FOR LITTLE ENDIAN (INTEL) CPUS */
+
+static const uint32_t d0[256] = {
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x000000f8, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x000000fc,
+0x000000d0, 0x000000d4, 0x000000d8, 0x000000dc, 0x000000e0, 0x000000e4,
+0x000000e8, 0x000000ec, 0x000000f0, 0x000000f4, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
+0x00000004, 0x00000008, 0x0000000c, 0x00000010, 0x00000014, 0x00000018,
+0x0000001c, 0x00000020, 0x00000024, 0x00000028, 0x0000002c, 0x00000030,
+0x00000034, 0x00000038, 0x0000003c, 0x00000040, 0x00000044, 0x00000048,
+0x0000004c, 0x00000050, 0x00000054, 0x00000058, 0x0000005c, 0x00000060,
+0x00000064, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x00000068, 0x0000006c, 0x00000070, 0x00000074, 0x00000078,
+0x0000007c, 0x00000080, 0x00000084, 0x00000088, 0x0000008c, 0x00000090,
+0x00000094, 0x00000098, 0x0000009c, 0x000000a0, 0x000000a4, 0x000000a8,
+0x000000ac, 0x000000b0, 0x000000b4, 0x000000b8, 0x000000bc, 0x000000c0,
+0x000000c4, 0x000000c8, 0x000000cc, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff
+};
+
+
+static const uint32_t d1[256] = {
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x0000e003, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x0000f003,
+0x00004003, 0x00005003, 0x00006003, 0x00007003, 0x00008003, 0x00009003,
+0x0000a003, 0x0000b003, 0x0000c003, 0x0000d003, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
+0x00001000, 0x00002000, 0x00003000, 0x00004000, 0x00005000, 0x00006000,
+0x00007000, 0x00008000, 0x00009000, 0x0000a000, 0x0000b000, 0x0000c000,
+0x0000d000, 0x0000e000, 0x0000f000, 0x00000001, 0x00001001, 0x00002001,
+0x00003001, 0x00004001, 0x00005001, 0x00006001, 0x00007001, 0x00008001,
+0x00009001, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x0000a001, 0x0000b001, 0x0000c001, 0x0000d001, 0x0000e001,
+0x0000f001, 0x00000002, 0x00001002, 0x00002002, 0x00003002, 0x00004002,
+0x00005002, 0x00006002, 0x00007002, 0x00008002, 0x00009002, 0x0000a002,
+0x0000b002, 0x0000c002, 0x0000d002, 0x0000e002, 0x0000f002, 0x00000003,
+0x00001003, 0x00002003, 0x00003003, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff
+};
+
+
+static const uint32_t d2[256] = {
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x00800f00, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00c00f00,
+0x00000d00, 0x00400d00, 0x00800d00, 0x00c00d00, 0x00000e00, 0x00400e00,
+0x00800e00, 0x00c00e00, 0x00000f00, 0x00400f00, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
+0x00400000, 0x00800000, 0x00c00000, 0x00000100, 0x00400100, 0x00800100,
+0x00c00100, 0x00000200, 0x00400200, 0x00800200, 0x00c00200, 0x00000300,
+0x00400300, 0x00800300, 0x00c00300, 0x00000400, 0x00400400, 0x00800400,
+0x00c00400, 0x00000500, 0x00400500, 0x00800500, 0x00c00500, 0x00000600,
+0x00400600, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x00800600, 0x00c00600, 0x00000700, 0x00400700, 0x00800700,
+0x00c00700, 0x00000800, 0x00400800, 0x00800800, 0x00c00800, 0x00000900,
+0x00400900, 0x00800900, 0x00c00900, 0x00000a00, 0x00400a00, 0x00800a00,
+0x00c00a00, 0x00000b00, 0x00400b00, 0x00800b00, 0x00c00b00, 0x00000c00,
+0x00400c00, 0x00800c00, 0x00c00c00, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff
+};
+
+
+static const uint32_t d3[256] = {
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x003e0000, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x003f0000,
+0x00340000, 0x00350000, 0x00360000, 0x00370000, 0x00380000, 0x00390000,
+0x003a0000, 0x003b0000, 0x003c0000, 0x003d0000, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x00000000,
+0x00010000, 0x00020000, 0x00030000, 0x00040000, 0x00050000, 0x00060000,
+0x00070000, 0x00080000, 0x00090000, 0x000a0000, 0x000b0000, 0x000c0000,
+0x000d0000, 0x000e0000, 0x000f0000, 0x00100000, 0x00110000, 0x00120000,
+0x00130000, 0x00140000, 0x00150000, 0x00160000, 0x00170000, 0x00180000,
+0x00190000, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x001a0000, 0x001b0000, 0x001c0000, 0x001d0000, 0x001e0000,
+0x001f0000, 0x00200000, 0x00210000, 0x00220000, 0x00230000, 0x00240000,
+0x00250000, 0x00260000, 0x00270000, 0x00280000, 0x00290000, 0x002a0000,
+0x002b0000, 0x002c0000, 0x002d0000, 0x002e0000, 0x002f0000, 0x00300000,
+0x00310000, 0x00320000, 0x00330000, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff,
+0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff
+};
+
+
+#endif
diff --git a/sha256.c b/sha256.c
new file mode 100644
index 0000000..d2f915f
--- /dev/null
+++ b/sha256.c
@@ -0,0 +1,411 @@
+/*-
+ * Copyright 2005,2007,2009 Colin Percival
+ * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
+ */
+
+#include <sys/types.h>
+
+#include <stdint.h>
+#include <string.h>
+
+#include "sysendian.h"
+
+#include "sha256.h"
+
+/*
+ * Encode a length len/4 vector of (uint32_t) into a length len vector of
+ * (unsigned char) in big-endian form. Assumes len is a multiple of 4.
+ */
+static void
+be32enc_vect(unsigned char *dst, const uint32_t *src, size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len / 4; i++)
+ be32enc(dst + i * 4, src[i]);
+}
+
+/*
+ * Decode a big-endian length len vector of (unsigned char) into a length
+ * len/4 vector of (uint32_t). Assumes len is a multiple of 4.
+ */
+static void
+be32dec_vect(uint32_t *dst, const unsigned char *src, size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len / 4; i++)
+ dst[i] = be32dec(src + i * 4);
+}
+
+/* Elementary functions used by SHA256 */
+#define Ch(x, y, z) ((x & (y ^ z)) ^ z)
+#define Maj(x, y, z) ((x & (y | z)) | (y & z))
+#define SHR(x, n) (x >> n)
+#define ROTR(x, n) ((x >> n) | (x << (32 - n)))
+#define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
+#define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
+#define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
+#define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
+
+/* SHA256 round function */
+#define RND(a, b, c, d, e, f, g, h, k) \
+ t0 = h + S1(e) + Ch(e, f, g) + k; \
+ t1 = S0(a) + Maj(a, b, c); \
+ d += t0; \
+ h = t0 + t1;
+
+/* Adjusted round function for rotating state */
+#define RNDr(S, W, i, k) \
+ RND(S[(64 - i) % 8], S[(65 - i) % 8], \
+ S[(66 - i) % 8], S[(67 - i) % 8], \
+ S[(68 - i) % 8], S[(69 - i) % 8], \
+ S[(70 - i) % 8], S[(71 - i) % 8], \
+ W[i] + k)
+
+/*
+ * SHA256 block compression function. The 256-bit state is transformed via
+ * the 512-bit input block to produce a new state.
+ */
+static void
+SHA256_Transform(uint32_t * state, const unsigned char block[64])
+{
+ uint32_t W[64];
+ uint32_t S[8];
+ uint32_t t0, t1;
+ int i;
+
+ /* 1. Prepare message schedule W. */
+ be32dec_vect(W, block, 64);
+ for (i = 16; i < 64; i++)
+ W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
+
+ /* 2. Initialize working variables. */
+ memcpy(S, state, 32);
+
+ /* 3. Mix. */
+ RNDr(S, W, 0, 0x428a2f98);
+ RNDr(S, W, 1, 0x71374491);
+ RNDr(S, W, 2, 0xb5c0fbcf);
+ RNDr(S, W, 3, 0xe9b5dba5);
+ RNDr(S, W, 4, 0x3956c25b);
+ RNDr(S, W, 5, 0x59f111f1);
+ RNDr(S, W, 6, 0x923f82a4);
+ RNDr(S, W, 7, 0xab1c5ed5);
+ RNDr(S, W, 8, 0xd807aa98);
+ RNDr(S, W, 9, 0x12835b01);
+ RNDr(S, W, 10, 0x243185be);
+ RNDr(S, W, 11, 0x550c7dc3);
+ RNDr(S, W, 12, 0x72be5d74);
+ RNDr(S, W, 13, 0x80deb1fe);
+ RNDr(S, W, 14, 0x9bdc06a7);
+ RNDr(S, W, 15, 0xc19bf174);
+ RNDr(S, W, 16, 0xe49b69c1);
+ RNDr(S, W, 17, 0xefbe4786);
+ RNDr(S, W, 18, 0x0fc19dc6);
+ RNDr(S, W, 19, 0x240ca1cc);
+ RNDr(S, W, 20, 0x2de92c6f);
+ RNDr(S, W, 21, 0x4a7484aa);
+ RNDr(S, W, 22, 0x5cb0a9dc);
+ RNDr(S, W, 23, 0x76f988da);
+ RNDr(S, W, 24, 0x983e5152);
+ RNDr(S, W, 25, 0xa831c66d);
+ RNDr(S, W, 26, 0xb00327c8);
+ RNDr(S, W, 27, 0xbf597fc7);
+ RNDr(S, W, 28, 0xc6e00bf3);
+ RNDr(S, W, 29, 0xd5a79147);
+ RNDr(S, W, 30, 0x06ca6351);
+ RNDr(S, W, 31, 0x14292967);
+ RNDr(S, W, 32, 0x27b70a85);
+ RNDr(S, W, 33, 0x2e1b2138);
+ RNDr(S, W, 34, 0x4d2c6dfc);
+ RNDr(S, W, 35, 0x53380d13);
+ RNDr(S, W, 36, 0x650a7354);
+ RNDr(S, W, 37, 0x766a0abb);
+ RNDr(S, W, 38, 0x81c2c92e);
+ RNDr(S, W, 39, 0x92722c85);
+ RNDr(S, W, 40, 0xa2bfe8a1);
+ RNDr(S, W, 41, 0xa81a664b);
+ RNDr(S, W, 42, 0xc24b8b70);
+ RNDr(S, W, 43, 0xc76c51a3);
+ RNDr(S, W, 44, 0xd192e819);
+ RNDr(S, W, 45, 0xd6990624);
+ RNDr(S, W, 46, 0xf40e3585);
+ RNDr(S, W, 47, 0x106aa070);
+ RNDr(S, W, 48, 0x19a4c116);
+ RNDr(S, W, 49, 0x1e376c08);
+ RNDr(S, W, 50, 0x2748774c);
+ RNDr(S, W, 51, 0x34b0bcb5);
+ RNDr(S, W, 52, 0x391c0cb3);
+ RNDr(S, W, 53, 0x4ed8aa4a);
+ RNDr(S, W, 54, 0x5b9cca4f);
+ RNDr(S, W, 55, 0x682e6ff3);
+ RNDr(S, W, 56, 0x748f82ee);
+ RNDr(S, W, 57, 0x78a5636f);
+ RNDr(S, W, 58, 0x84c87814);
+ RNDr(S, W, 59, 0x8cc70208);
+ RNDr(S, W, 60, 0x90befffa);
+ RNDr(S, W, 61, 0xa4506ceb);
+ RNDr(S, W, 62, 0xbef9a3f7);
+ RNDr(S, W, 63, 0xc67178f2);
+
+ /* 4. Mix local working variables into global state */
+ for (i = 0; i < 8; i++)
+ state[i] += S[i];
+
+ /* Clean the stack. */
+ memset(W, 0, 256);
+ memset(S, 0, 32);
+ t0 = t1 = 0;
+}
+
+static unsigned char PAD[64] = {
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+/* Add padding and terminating bit-count. */
+static void
+SHA256_Pad(SHA256_CTX * ctx)
+{
+ unsigned char len[8];
+ uint32_t r, plen;
+
+ /*
+ * Convert length to a vector of bytes -- we do this now rather
+ * than later because the length will change after we pad.
+ */
+ be32enc_vect(len, ctx->count, 8);
+
+ /* Add 1--64 bytes so that the resulting length is 56 mod 64 */
+ r = (ctx->count[1] >> 3) & 0x3f;
+ plen = (r < 56) ? (56 - r) : (120 - r);
+ SHA256_Update(ctx, PAD, (size_t)plen);
+
+ /* Add the terminating bit-count */
+ SHA256_Update(ctx, len, 8);
+}
+
+/* SHA-256 initialization. Begins a SHA-256 operation. */
+void
+SHA256_Init(SHA256_CTX * ctx)
+{
+
+ /* Zero bits processed so far */
+ ctx->count[0] = ctx->count[1] = 0;
+
+ /* Magic initialization constants */
+ ctx->state[0] = 0x6A09E667;
+ ctx->state[1] = 0xBB67AE85;
+ ctx->state[2] = 0x3C6EF372;
+ ctx->state[3] = 0xA54FF53A;
+ ctx->state[4] = 0x510E527F;
+ ctx->state[5] = 0x9B05688C;
+ ctx->state[6] = 0x1F83D9AB;
+ ctx->state[7] = 0x5BE0CD19;
+}
+
+/* Add bytes into the hash */
+void
+SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len)
+{
+ uint32_t bitlen[2];
+ uint32_t r;
+ const unsigned char *src = in;
+
+ /* Number of bytes left in the buffer from previous updates */
+ r = (ctx->count[1] >> 3) & 0x3f;
+
+ /* Convert the length into a number of bits */
+ bitlen[1] = ((uint32_t)len) << 3;
+ bitlen[0] = (uint32_t)(len >> 29);
+
+ /* Update number of bits */
+ if ((ctx->count[1] += bitlen[1]) < bitlen[1])
+ ctx->count[0]++;
+ ctx->count[0] += bitlen[0];
+
+ /* Handle the case where we don't need to perform any transforms */
+ if (len < 64 - r) {
+ memcpy(&ctx->buf[r], src, len);
+ return;
+ }
+
+ /* Finish the current block */
+ memcpy(&ctx->buf[r], src, 64 - r);
+ SHA256_Transform(ctx->state, ctx->buf);
+ src += 64 - r;
+ len -= 64 - r;
+
+ /* Perform complete blocks */
+ while (len >= 64) {
+ SHA256_Transform(ctx->state, src);
+ src += 64;
+ len -= 64;
+ }
+
+ /* Copy left over data into buffer */
+ memcpy(ctx->buf, src, len);
+}
+
+/*
+ * SHA-256 finalization. Pads the input data, exports the hash value,
+ * and clears the context state.
+ */
+void
+SHA256_Final(unsigned char digest[32], SHA256_CTX * ctx)
+{
+
+ /* Add padding */
+ SHA256_Pad(ctx);
+
+ /* Write the hash */
+ be32enc_vect(digest, ctx->state, 32);
+
+ /* Clear the context state */
+ memset((void *)ctx, 0, sizeof(*ctx));
+}
+
+/* Initialize an HMAC-SHA256 operation with the given key. */
+void
+HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen)
+{
+ unsigned char pad[64];
+ unsigned char khash[32];
+ const unsigned char * K = _K;
+ size_t i;
+
+ /* If Klen > 64, the key is really SHA256(K). */
+ if (Klen > 64) {
+ SHA256_Init(&ctx->ictx);
+ SHA256_Update(&ctx->ictx, K, Klen);
+ SHA256_Final(khash, &ctx->ictx);
+ K = khash;
+ Klen = 32;
+ }
+
+ /* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */
+ SHA256_Init(&ctx->ictx);
+ memset(pad, 0x36, 64);
+ for (i = 0; i < Klen; i++)
+ pad[i] ^= K[i];
+ SHA256_Update(&ctx->ictx, pad, 64);
+
+ /* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */
+ SHA256_Init(&ctx->octx);
+ memset(pad, 0x5c, 64);
+ for (i = 0; i < Klen; i++)
+ pad[i] ^= K[i];
+ SHA256_Update(&ctx->octx, pad, 64);
+
+ /* Clean the stack. */
+ memset(khash, 0, 32);
+}
+
+/* Add bytes to the HMAC-SHA256 operation. */
+void
+HMAC_SHA256_Update(HMAC_SHA256_CTX * ctx, const void *in, size_t len)
+{
+
+ /* Feed data to the inner SHA256 operation. */
+ SHA256_Update(&ctx->ictx, in, len);
+}
+
+/* Finish an HMAC-SHA256 operation. */
+void
+HMAC_SHA256_Final(unsigned char digest[32], HMAC_SHA256_CTX * ctx)
+{
+ unsigned char ihash[32];
+
+ /* Finish the inner SHA256 operation. */
+ SHA256_Final(ihash, &ctx->ictx);
+
+ /* Feed the inner hash to the outer SHA256 operation. */
+ SHA256_Update(&ctx->octx, ihash, 32);
+
+ /* Finish the outer SHA256 operation. */
+ SHA256_Final(digest, &ctx->octx);
+
+ /* Clean the stack. */
+ memset(ihash, 0, 32);
+}
+
+/**
+ * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
+ * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
+ * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
+ */
+void
+PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
+ size_t saltlen, uint64_t c, uint8_t * buf, size_t dkLen)
+{
+ HMAC_SHA256_CTX PShctx, hctx;
+ size_t i;
+ uint8_t ivec[4];
+ uint8_t U[32];
+ uint8_t T[32];
+ uint64_t j;
+ int k;
+ size_t clen;
+
+ /* Compute HMAC state after processing P and S. */
+ HMAC_SHA256_Init(&PShctx, passwd, passwdlen);
+ HMAC_SHA256_Update(&PShctx, salt, saltlen);
+
+ /* Iterate through the blocks. */
+ for (i = 0; i * 32 < dkLen; i++) {
+ /* Generate INT(i + 1). */
+ be32enc(ivec, (uint32_t)(i + 1));
+
+ /* Compute U_1 = PRF(P, S || INT(i)). */
+ memcpy(&hctx, &PShctx, sizeof(HMAC_SHA256_CTX));
+ HMAC_SHA256_Update(&hctx, ivec, 4);
+ HMAC_SHA256_Final(U, &hctx);
+
+ /* T_i = U_1 ... */
+ memcpy(T, U, 32);
+
+ for (j = 2; j <= c; j++) {
+ /* Compute U_j. */
+ HMAC_SHA256_Init(&hctx, passwd, passwdlen);
+ HMAC_SHA256_Update(&hctx, U, 32);
+ HMAC_SHA256_Final(U, &hctx);
+
+ /* ... xor U_j ... */
+ for (k = 0; k < 32; k++)
+ T[k] ^= U[k];
+ }
+
+ /* Copy as many bytes as necessary into buf. */
+ clen = dkLen - i * 32;
+ if (clen > 32)
+ clen = 32;
+ memcpy(&buf[i * 32], T, clen);
+ }
+
+ /* Clean PShctx, since we never called _Final on it. */
+ memset(&PShctx, 0, sizeof(HMAC_SHA256_CTX));
+}
diff --git a/sha256.h b/sha256.h
new file mode 100644
index 0000000..580183a
--- /dev/null
+++ b/sha256.h
@@ -0,0 +1,70 @@
+/*-
+ * Copyright 2005,2007,2009 Colin Percival
+ * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
+ *
+ * $FreeBSD: src/lib/libmd/sha256.h,v 1.2 2006/01/17 15:35:56 phk Exp $
+ */
+
+#ifndef _SHA256_H_
+#define _SHA256_H_
+
+#include <sys/types.h>
+
+#include <stdint.h>
+
+typedef struct SHA256Context {
+ uint32_t state[8];
+ uint32_t count[2];
+ unsigned char buf[64];
+} SHA256_CTX;
+
+typedef struct HMAC_SHA256Context {
+ SHA256_CTX ictx;
+ SHA256_CTX octx;
+} HMAC_SHA256_CTX;
+
+void SHA256_Init(/*@out@*/ SHA256_CTX *);
+void SHA256_Update(SHA256_CTX *, const void *, size_t);
+
+/* Original declaration:
+ * void SHA256_Final(unsigned char [32], SHA256_CTX *);
+*/
+void SHA256_Final(/*@out@*/ unsigned char [], SHA256_CTX *);
+void HMAC_SHA256_Init(HMAC_SHA256_CTX *, const void *, size_t);
+void HMAC_SHA256_Update(HMAC_SHA256_CTX *, const void *, size_t);
+
+/* Original declaration:
+ * void HMAC_SHA256_Final(unsigned char [32], HMAC_SHA256_CTX *);
+*/
+void HMAC_SHA256_Final(unsigned char [], HMAC_SHA256_CTX *);
+
+/**
+ * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
+ * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
+ * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
+ */
+void PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t,
+ uint64_t, uint8_t *, size_t);
+
+#endif /* !_SHA256_H_ */
diff --git a/sysendian.h b/sysendian.h
new file mode 100644
index 0000000..5ecb505
--- /dev/null
+++ b/sysendian.h
@@ -0,0 +1,139 @@
+/*-
+ * Copyright 2007-2009 Colin Percival
+ * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
+ *
+ * This file was originally written by Colin Percival as part of the Tarsnap
+ * online backup system.
+ */
+#ifndef _SYSENDIAN_H_
+#define _SYSENDIAN_H_
+
+
+/* If we don't have be64enc, the <sys/endian.h> we have isn't usable. */
+#if !HAVE_DECL_BE64ENC
+#undef HAVE_SYS_ENDIAN_H
+#endif
+
+#ifdef HAVE_SYS_ENDIAN_H
+
+#include <sys/endian.h>
+
+#else
+
+#include <stdint.h>
+
+static inline uint32_t
+be32dec(const void *pp)
+{
+ const uint8_t *p = (uint8_t const *)pp;
+
+ return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
+ ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
+}
+
+static inline void
+be32enc(void *pp, uint32_t x)
+{
+ uint8_t * p = (uint8_t *)pp;
+
+ p[3] = x & 0xff;
+ p[2] = (x >> 8) & 0xff;
+ p[1] = (x >> 16) & 0xff;
+ p[0] = (x >> 24) & 0xff;
+}
+
+static inline uint64_t
+be64dec(const void *pp)
+{
+ const uint8_t *p = (uint8_t const *)pp;
+
+ return ((uint64_t)(p[7]) + ((uint64_t)(p[6]) << 8) +
+ ((uint64_t)(p[5]) << 16) + ((uint64_t)(p[4]) << 24) +
+ ((uint64_t)(p[3]) << 32) + ((uint64_t)(p[2]) << 40) +
+ ((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56));
+}
+
+static inline void
+be64enc(void *pp, uint64_t x)
+{
+ uint8_t * p = (uint8_t *)pp;
+
+ p[7] = x & 0xff;
+ p[6] = (x >> 8) & 0xff;
+ p[5] = (x >> 16) & 0xff;
+ p[4] = (x >> 24) & 0xff;
+ p[3] = (x >> 32) & 0xff;
+ p[2] = (x >> 40) & 0xff;
+ p[1] = (x >> 48) & 0xff;
+ p[0] = (x >> 56) & 0xff;
+}
+
+static inline uint32_t
+le32dec(const void *pp)
+{
+ const uint8_t *p = (uint8_t const *)pp;
+
+ return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) +
+ ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24));
+}
+
+static inline void
+le32enc(void *pp, uint32_t x)
+{
+ uint8_t * p = (uint8_t *)pp;
+
+ p[0] = x & 0xff;
+ p[1] = (x >> 8) & 0xff;
+ p[2] = (x >> 16) & 0xff;
+ p[3] = (x >> 24) & 0xff;
+}
+
+static inline uint64_t
+le64dec(const void *pp)
+{
+ const uint8_t *p = (uint8_t const *)pp;
+
+ return ((uint64_t)(p[0]) + ((uint64_t)(p[1]) << 8) +
+ ((uint64_t)(p[2]) << 16) + ((uint64_t)(p[3]) << 24) +
+ ((uint64_t)(p[4]) << 32) + ((uint64_t)(p[5]) << 40) +
+ ((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56));
+}
+
+static inline void
+le64enc(void *pp, uint64_t x)
+{
+ uint8_t * p = (uint8_t *)pp;
+
+ p[0] = x & 0xff;
+ p[1] = (x >> 8) & 0xff;
+ p[2] = (x >> 16) & 0xff;
+ p[3] = (x >> 24) & 0xff;
+ p[4] = (x >> 32) & 0xff;
+ p[5] = (x >> 40) & 0xff;
+ p[6] = (x >> 48) & 0xff;
+ p[7] = (x >> 56) & 0xff;
+}
+#endif /* !HAVE_SYS_ENDIAN_H */
+
+#endif /* !_SYSENDIAN_H_ */