From c12a702caf3549a489b4589ab89816139a73778f Mon Sep 17 00:00:00 2001 From: technion Date: Thu, 30 May 2013 15:38:53 -0700 Subject: Initial commit --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..4e93f3e --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +libscrypt +========= -- cgit v1.2.3 From 92493cfa15840d857997385c07c1845de9cf8a40 Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 30 May 2013 18:40:38 -0400 Subject: Initial commit. Currently works but produces an archive format library. --- Makefile | 15 ++ crypto_scrypt-nosse.c | 337 +++++++++++++++++++++++++++++++++++++++++ crypto_scrypt-nosse.o | Bin 0 -> 3168 bytes crypto_scrypt.h | 54 +++++++ libscrypt.a | Bin 0 -> 14606 bytes main.c | 46 ++++++ sha256.c | 411 ++++++++++++++++++++++++++++++++++++++++++++++++++ sha256.h | 62 ++++++++ sha256.o | Bin 0 -> 11008 bytes sysendian.h | 139 +++++++++++++++++ 10 files changed, 1064 insertions(+) create mode 100644 Makefile create mode 100644 crypto_scrypt-nosse.c create mode 100644 crypto_scrypt-nosse.o create mode 100644 crypto_scrypt.h create mode 100644 libscrypt.a create mode 100644 main.c create mode 100644 sha256.c create mode 100644 sha256.h create mode 100644 sha256.o create mode 100644 sysendian.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..83a6942 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +CC=gcc +CFLAGS=-O2 -Wall +all: reference + +OBJS= crypto_scrypt-nosse.o sha256.o + + +library: $(OBJS) + ar rcs libscrypt.a $(OBJS) + +reference: library main.o + gcc -o reference main.o libscrypt.a +clean: + rm -f *.o reference libscrypt.a + diff --git a/crypto_scrypt-nosse.c b/crypto_scrypt-nosse.c new file mode 100644 index 0000000..9389029 --- /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 +#include + +#include +#include +#include +#include + +#include "sha256.h" +#include "sysendian.h" + +#include "crypto_scrypt.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 +crypto_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/crypto_scrypt-nosse.o b/crypto_scrypt-nosse.o new file mode 100644 index 0000000..092d8b7 Binary files /dev/null and b/crypto_scrypt-nosse.o differ diff --git a/crypto_scrypt.h b/crypto_scrypt.h new file mode 100644 index 0000000..7c16a16 --- /dev/null +++ b/crypto_scrypt.h @@ -0,0 +1,54 @@ +/*- + * 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. + */ +#ifndef _CRYPTO_SCRYPT_H_ +#define _CRYPTO_SCRYPT_H_ + + +#include + +/** + * 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. + * + * crypto_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 or r = 8, p = 1 are + * standard unless you want to modify the CPU/RAM ratio. + * Return 0 on success; or -1 on error. + */ +int crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, + uint32_t, uint32_t, uint8_t *, size_t); + +#endif /* !_CRYPTO_SCRYPT_H_ */ diff --git a/libscrypt.a b/libscrypt.a new file mode 100644 index 0000000..1458a94 Binary files /dev/null and b/libscrypt.a differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..3b1cc18 --- /dev/null +++ b/main.c @@ -0,0 +1,46 @@ +#include +#include +#include + +#include "crypto_scrypt.h" + + +void scrypt_hexprint(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, + uint32_t, uint32_t); + +int main() +{ +/** + * crypto_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 or r = 8, p = 1 are + * standard unless you want to modify the CPU/RAM ratio. +int crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, + uint32_t, uint32_t, uint8_t *, size_t); +*/ + + scrypt_hexprint((uint8_t*)"password",strlen("password"), (uint8_t*)"NaCl", strlen("NaCl"), 1024, 8, 16); + + return 0; +} + +void scrypt_hexprint(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[64]; + + crypto_scrypt(passwd, passwdlen, salt, saltlen,N,r,p,buf,(size_t)64); + + printf("Hex out for password password with salt NaCL is:\n"); + int i; + for(i=0; i<63; i++) + { + printf("%x ", (unsigned char) buf[i]); + } + + printf("\n"); +} 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 + +#include +#include + +#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..289a523 --- /dev/null +++ b/sha256.h @@ -0,0 +1,62 @@ +/*- + * 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 + +#include + +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(SHA256_CTX *); +void SHA256_Update(SHA256_CTX *, const void *, size_t); +void SHA256_Final(unsigned char [32], SHA256_CTX *); +void HMAC_SHA256_Init(HMAC_SHA256_CTX *, const void *, size_t); +void HMAC_SHA256_Update(HMAC_SHA256_CTX *, const void *, size_t); +void HMAC_SHA256_Final(unsigned char [32], 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/sha256.o b/sha256.o new file mode 100644 index 0000000..09e81c6 Binary files /dev/null and b/sha256.o differ 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 we have isn't usable. */ +#if !HAVE_DECL_BE64ENC +#undef HAVE_SYS_ENDIAN_H +#endif + +#ifdef HAVE_SYS_ENDIAN_H + +#include + +#else + +#include + +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_ */ -- cgit v1.2.3 From ac00a89e1febfe1601d9191604198ef952e01049 Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 30 May 2013 18:48:34 -0400 Subject: Don't commit object files --- crypto_scrypt-nosse.o | Bin 3168 -> 0 bytes libscrypt.a | Bin 14606 -> 0 bytes sha256.o | Bin 11008 -> 0 bytes 3 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 crypto_scrypt-nosse.o delete mode 100644 libscrypt.a delete mode 100644 sha256.o diff --git a/crypto_scrypt-nosse.o b/crypto_scrypt-nosse.o deleted file mode 100644 index 092d8b7..0000000 Binary files a/crypto_scrypt-nosse.o and /dev/null differ diff --git a/libscrypt.a b/libscrypt.a deleted file mode 100644 index 1458a94..0000000 Binary files a/libscrypt.a and /dev/null differ diff --git a/sha256.o b/sha256.o deleted file mode 100644 index 09e81c6..0000000 Binary files a/sha256.o and /dev/null differ -- cgit v1.2.3 From c276a9797af2f983493b87de66a442fb5f1a109f Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 30 May 2013 21:48:32 -0400 Subject: Implementation of first proper test vector First parts of better separation from library and test reference --- .gitignore | 3 +++ Makefile | 2 +- crypto_scrypt-nosse.c | 2 +- crypto_scrypt.h | 54 ---------------------------------------- libscrypt.h | 34 ++++++++++++++++++++++++++ main.c | 68 ++++++++++++++++++++++++++++++++++----------------- 6 files changed, 85 insertions(+), 78 deletions(-) create mode 100644 .gitignore delete mode 100644 crypto_scrypt.h create mode 100644 libscrypt.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..720aacc --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +*.a + diff --git a/Makefile b/Makefile index 83a6942..8a730b0 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ library: $(OBJS) ar rcs libscrypt.a $(OBJS) reference: library main.o - gcc -o reference main.o libscrypt.a + gcc -Wall -o reference main.o libscrypt.a clean: rm -f *.o reference libscrypt.a diff --git a/crypto_scrypt-nosse.c b/crypto_scrypt-nosse.c index 9389029..8c013be 100644 --- a/crypto_scrypt-nosse.c +++ b/crypto_scrypt-nosse.c @@ -38,7 +38,7 @@ #include "sha256.h" #include "sysendian.h" -#include "crypto_scrypt.h" +#include "libscrypt.h" static void blkcpy(void *, void *, size_t); static void blkxor(void *, void *, size_t); diff --git a/crypto_scrypt.h b/crypto_scrypt.h deleted file mode 100644 index 7c16a16..0000000 --- a/crypto_scrypt.h +++ /dev/null @@ -1,54 +0,0 @@ -/*- - * 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. - */ -#ifndef _CRYPTO_SCRYPT_H_ -#define _CRYPTO_SCRYPT_H_ - - -#include - -/** - * 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. - * - * crypto_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 or r = 8, p = 1 are - * standard unless you want to modify the CPU/RAM ratio. - * Return 0 on success; or -1 on error. - */ -int crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, - uint32_t, uint32_t, uint8_t *, size_t); - -#endif /* !_CRYPTO_SCRYPT_H_ */ diff --git a/libscrypt.h b/libscrypt.h new file mode 100644 index 0000000..f582267 --- /dev/null +++ b/libscrypt.h @@ -0,0 +1,34 @@ +/*- + */ +#ifndef _CRYPTO_SCRYPT_H_ +#define _CRYPTO_SCRYPT_H_ + + +#include + +/** + * 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. + * + * crypto_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 crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, + uint32_t, uint32_t, 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. + */ +void crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs); + +#endif /* !_CRYPTO_SCRYPT_H_ */ diff --git a/main.c b/main.c index 3b1cc18..95d5ed9 100644 --- a/main.c +++ b/main.c @@ -2,45 +2,69 @@ #include #include -#include "crypto_scrypt.h" +#include "libscrypt.h" +#define REF1 "fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640" -void scrypt_hexprint(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, - uint32_t, uint32_t); int main() { -/** - * crypto_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 or r = 8, p = 1 are - * standard unless you want to modify the CPU/RAM ratio. -int crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, + uint8_t hashbuf[64]; + char outbuf[132]; + int retval; + /** + * crypto_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 crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, uint32_t, uint32_t, uint8_t *, size_t); */ - scrypt_hexprint((uint8_t*)"password",strlen("password"), (uint8_t*)"NaCl", strlen("NaCl"), 1024, 8, 16); + printf("Hashing with password password with salt NaCL\n"); + + retval = crypto_scrypt((uint8_t*)"password",strlen("password"), (uint8_t*)"NaCl", strlen("NaCl"), 1024, 8, 16, hashbuf, sizeof(hashbuf)); + + if(retval != 0) + { + printf("Failed to create hash of \"password\"\\n"); + exit(EXIT_FAILURE); + } + + /* Convert the binary string to hex representation. Outbuf must be + * at least sizeof(hashbuf) * 2 + 1 + */ + crypto_scrypt_hexconvert(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); + printf("Hex output is:\n%s\n", outbuf); + + if(strcmp(outbuf, REF1) != 0) + { + printf("Failed to match reference on hash\n"); + exit(EXIT_FAILURE); + } + else + { + printf("Test vector matched!\n"); + } return 0; } -void scrypt_hexprint(const uint8_t *passwd, size_t passwdlen, - const uint8_t *salt, size_t saltlen, uint64_t N, uint32_t r, - uint32_t p) +void crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs) { - uint8_t buf[64]; - crypto_scrypt(passwd, passwdlen, salt, saltlen,N,r,p,buf,(size_t)64); + if (!buf || s < 1 || obs < (s * 2 + 1)) + return; + + memset(outbuf, 0, obs); - printf("Hex out for password password with salt NaCL is:\n"); int i; - for(i=0; i<63; i++) + for(i=0; i<=(s-1); i++) { - printf("%x ", (unsigned char) buf[i]); + sprintf(outbuf, "%s%02x", outbuf, (unsigned char) buf[i]); } - printf("\n"); } -- cgit v1.2.3 From c9ee9ce150cbd38e7a566a0cd708cafba9aab6ea Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 30 May 2013 21:49:17 -0400 Subject: Updated README --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 4e93f3e..dca861c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ libscrypt ========= +Linux scrypt shared library. + +Full credit to algorithm designer and example code from Colin Percival here: +http://www.tarsnap.com/scrypt.html + +Full documentation found here: +http://www.lolware.net/libscrypt.html + -- cgit v1.2.3 From d090f614a48e603a796e24e2fc88e03141ea03ec Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 31 May 2013 01:48:07 -0400 Subject: * Imported base64 library * Created MCF format function --- Makefile | 6 +- README.md | 3 + crypto-mcf.c | 24 +++ crypto_scrypt-hexconvert.c | 22 +++ libscrypt.h | 3 + main.c | 43 ++-- modp_b64.c | 264 +++++++++++++++++++++++++ modp_b64.h | 234 ++++++++++++++++++++++ modp_b64_data.h | 480 +++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 1066 insertions(+), 13 deletions(-) create mode 100644 crypto-mcf.c create mode 100644 crypto_scrypt-hexconvert.c create mode 100644 modp_b64.c create mode 100644 modp_b64.h create mode 100644 modp_b64_data.h diff --git a/Makefile b/Makefile index 8a730b0..5c24b4a 100644 --- a/Makefile +++ b/Makefile @@ -2,14 +2,16 @@ CC=gcc CFLAGS=-O2 -Wall all: reference -OBJS= crypto_scrypt-nosse.o sha256.o +OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o modp_b64.o +crypto-mcf.o: crypto-mcf.c + gcc -Wall -std=gnu99 -c -o crypto-mcf.o crypto-mcf.c library: $(OBJS) ar rcs libscrypt.a $(OBJS) reference: library main.o - gcc -Wall -o reference main.o libscrypt.a + gcc -Wall -o reference main.o libscrypt.a -lm clean: rm -f *.o reference libscrypt.a diff --git a/README.md b/README.md index dca861c..8e0b99c 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,9 @@ 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/ + Full documentation found here: http://www.lolware.net/libscrypt.html diff --git a/crypto-mcf.c b/crypto-mcf.c new file mode 100644 index 0000000..e49f3d7 --- /dev/null +++ b/crypto-mcf.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +#include + +void crypto_scrypt_mcf(uint32_t N, uint8_t r, uint8_t p, char *salt, char *hash, char *mcf) +{ + + + uint32_t params; + double t; + + t = log2(N); + + if (t != (int)t) + return; /* Not a valid state */ + + params = (r << 8) + p; + params += (uint32_t)t << 16; + + sprintf(mcf, "$s0$%06x$%s$%s\n", params, salt, hash); +} diff --git a/crypto_scrypt-hexconvert.c b/crypto_scrypt-hexconvert.c new file mode 100644 index 0000000..cd94488 --- /dev/null +++ b/crypto_scrypt-hexconvert.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include + +void crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs) +{ + + int i; + + if (!buf || s < 1 || obs < (s * 2 + 1)) + return; + + memset(outbuf, 0, obs); + + for(i=0; i<=(s-1); i++) + { + sprintf(outbuf, "%s%02x", outbuf, (unsigned char) buf[i]); + } + +} + diff --git a/libscrypt.h b/libscrypt.h index f582267..8a7f41f 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -31,4 +31,7 @@ int crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, */ void crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs); +/* Converts a series of input parameters to a MCF form for storage */ +void crypto_scrypt_mcf(uint32_t N, uint8_t r, uint8_t p, char *salt, char *hash, char *mcf); + #endif /* !_CRYPTO_SCRYPT_H_ */ diff --git a/main.c b/main.c index 95d5ed9..6877965 100644 --- a/main.c +++ b/main.c @@ -3,14 +3,18 @@ #include #include "libscrypt.h" +#include "modp_b64.h" #define REF1 "fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640" +#define REF2 "7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887" int main() { uint8_t hashbuf[64]; char outbuf[132]; + char mcf[256]; + char saltbuf[64]; int retval; /** * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen): @@ -50,21 +54,38 @@ int main() printf("Test vector matched!\n"); } - return 0; -} - -void crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs) -{ + printf("Second test vector: pleaseletmein with SodiumChloride as salt\n"); + retval = crypto_scrypt((uint8_t*)"pleaseletmein",strlen("pleaseletmein"), (uint8_t*)"SodiumChloride", strlen("SodiumChloride"), 16384, 8, 1, hashbuf, sizeof(hashbuf)); - if (!buf || s < 1 || obs < (s * 2 + 1)) - return; + if(retval != 0) + { + printf("Failed to create hash of \"pleaseletmein\"\\n"); + exit(EXIT_FAILURE); + } - memset(outbuf, 0, obs); + /* Convert the binary string to hex representation. Outbuf must be + * at least sizeof(hashbuf) * 2 + 1 + */ + crypto_scrypt_hexconvert(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); + printf("Hex output is:\n%s\n", outbuf); - int i; - for(i=0; i<=(s-1); i++) + if(strcmp(outbuf, REF2) != 0) + { + printf("Failed to match reference on hash\n"); + exit(EXIT_FAILURE); + } + else { - sprintf(outbuf, "%s%02x", outbuf, (unsigned char) buf[i]); + printf("Test vector matched!\n"); } + modp_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); + modp_b64_encode(saltbuf, "SodiumChloride", strlen("SodiumChloride")); + + crypto_scrypt_mcf(16384, 8, 1, saltbuf, outbuf, mcf); + + printf("The MCF for this string is:\n%s", mcf); + + return 0; } + diff --git a/modp_b64.c b/modp_b64.c new file mode 100644 index 0000000..32129e3 --- /dev/null +++ b/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 + *
+ * MODP_B64 - High performance base64 encoder/decoder
+ * http://code.google.com/p/stringencoders/
+ *
+ * Copyright © 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
+ * 
+ */ + +/* 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 modp_b64_encode(char* dest, const char* str, int 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 modp_b64_decode(char* dest, const char* src, int 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 modp_b64_decode(char* dest, const char* src, int 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..3256af7 --- /dev/null +++ b/modp_b64.h @@ -0,0 +1,234 @@ +/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */ +/* vi: set expandtab shiftwidth=4 tabstop=4: */ + +/** + * \file + *
+ * High performance base64 encoder / decoder
+ *
+ * Copyright © 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.
+ * 
+ * + * 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 + * + */ +int modp_b64_encode(char* dest, const char* str, int 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 modp_b64_decode(char* dest, const char* src, int 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 +#include + +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(x.data()), s, + static_cast(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(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(x.data()), src, + static_cast(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 +#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 -- cgit v1.2.3 From 46783953d8cd8256e366faf2fb486b55ea29b252 Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 31 May 2013 02:30:17 -0400 Subject: Added a salt generator --- Makefile | 2 +- crypto-scrypt-saltgen.c | 25 +++++++++++++++++++++++++ libscrypt.h | 4 ++++ main.c | 6 ++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 crypto-scrypt-saltgen.c diff --git a/Makefile b/Makefile index 5c24b4a..314b4a3 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC=gcc CFLAGS=-O2 -Wall all: reference -OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o modp_b64.o +OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o modp_b64.o crypto-scrypt-saltgen.o crypto-mcf.o: crypto-mcf.c gcc -Wall -std=gnu99 -c -o crypto-mcf.o crypto-mcf.c diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c new file mode 100644 index 0000000..3b73a22 --- /dev/null +++ b/crypto-scrypt-saltgen.c @@ -0,0 +1,25 @@ +#include +#include +#include + +#include "sha256.h" + + +void scrypt_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(¤t_time); + SHA256_Update(&ctx, c_time_string, strlen(c_time_string)); + SHA256_Final(buf, &ctx); + + memcpy(rand, buf, len); + +} diff --git a/libscrypt.h b/libscrypt.h index 8a7f41f..2af0b1c 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -34,4 +34,8 @@ void crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs); /* Converts a series of input parameters to a MCF form for storage */ void crypto_scrypt_mcf(uint32_t N, uint8_t r, uint8_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 scrypt_salt_gen(char *rand, size_t len); #endif /* !_CRYPTO_SCRYPT_H_ */ diff --git a/main.c b/main.c index 6877965..628f3c1 100644 --- a/main.c +++ b/main.c @@ -86,6 +86,12 @@ int main() printf("The MCF for this string is:\n%s", mcf); + printf("Testing salt generator\n"); + scrypt_salt_gen(saltbuf, 16); + modp_b64_encode(outbuf, (char*)saltbuf, 16); + printf("Generated %s, I guess it's random?\n", outbuf); + + return 0; } -- cgit v1.2.3 From 71b24bc7e862693e5134ba774ffb0c4fed1e11f5 Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 31 May 2013 08:37:29 -0400 Subject: Created a function for testing hashes --- Makefile | 2 +- crypto-mcf.c | 2 +- crypto_scrypt-check.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ libscrypt.h | 3 +++ main.c | 35 +++++++++++++++++++++++++++++- 5 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 crypto_scrypt-check.c diff --git a/Makefile b/Makefile index 314b4a3..36951bb 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC=gcc CFLAGS=-O2 -Wall all: reference -OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o modp_b64.o crypto-scrypt-saltgen.o +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-mcf.o: crypto-mcf.c gcc -Wall -std=gnu99 -c -o crypto-mcf.o crypto-mcf.c diff --git a/crypto-mcf.c b/crypto-mcf.c index e49f3d7..5c58294 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -20,5 +20,5 @@ void crypto_scrypt_mcf(uint32_t N, uint8_t r, uint8_t p, char *salt, char *hash, params = (r << 8) + p; params += (uint32_t)t << 16; - sprintf(mcf, "$s0$%06x$%s$%s\n", params, salt, hash); + sprintf(mcf, "$s0$%06x$%s$%s", params, salt, hash); } diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c new file mode 100644 index 0000000..5d1b091 --- /dev/null +++ b/crypto_scrypt-check.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +#include "libscrypt.h" +#include "modp_b64.h" + +int scrypt_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, "$s0", 3) != 0) + { + /* Only version 0 supported */ + return -1; + } + + tok = strtok(mcf, "$"); + tok = strtok(NULL, "$"); + sscanf(tok, "%x", ¶ms); + + tok = strtok(NULL, "$"); + + p = params & 0xff; + r = (params >> 8) & 0xff; + N = params >> 16; + N= pow(2, N); + + /* Useful debugging: + printf("We've obtained salt 'N' r p of '%s' %d %d %d\n", tok, N,r,p); + */ + + retval = modp_b64_decode(salt, tok, strlen(tok)); + retval = crypto_scrypt((uint8_t*)password,strlen(password), (uint8_t*)salt, retval, N, r, p, hashbuf, sizeof(hashbuf)); + + if (retval != 0) + return retval; + + modp_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); + + tok = strtok(NULL, "$"); + + if(strcmp(tok, outbuf) == 0) + { + return 1; + } + + return 0; +} + diff --git a/libscrypt.h b/libscrypt.h index 2af0b1c..d0a0d32 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -38,4 +38,7 @@ void crypto_scrypt_mcf(uint32_t N, uint8_t r, uint8_t p, char *salt, char *hash, * but should produce appropriately randomised output for this purpose */ void scrypt_salt_gen(char *rand, size_t len); + +/* Checks a given MCF against a password */ +int scrypt_check(char *mcf, char *password); #endif /* !_CRYPTO_SCRYPT_H_ */ diff --git a/main.c b/main.c index 628f3c1..ea847d7 100644 --- a/main.c +++ b/main.c @@ -9,11 +9,13 @@ #define REF2 "7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2d5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887" + int main() { uint8_t hashbuf[64]; char outbuf[132]; char mcf[256]; + char mcf2[256]; char saltbuf[64]; int retval; /** @@ -84,13 +86,44 @@ int main() crypto_scrypt_mcf(16384, 8, 1, saltbuf, outbuf, mcf); - printf("The MCF for this string is:\n%s", mcf); + /* Since later calls to scrypt_check() butcher mcf, make a second */ + strcpy(mcf2, mcf); printf("Testing salt generator\n"); scrypt_salt_gen(saltbuf, 16); modp_b64_encode(outbuf, (char*)saltbuf, 16); printf("Generated %s, I guess it's random?\n", outbuf); + /* Since scrypt)check butchers mcf - make a copy */ + retval = scrypt_check(mcf, "pleaseletmein"); + + if(retval < 0) + { + printf("pleaseletmein hash failed to calculate\n"); + exit(EXIT_FAILURE); + } + if(retval == 0) + { + printf("pleaseletmein hash claimed did not verify\n"); + exit(EXIT_FAILURE); + } + /* retval >0 is a success */ + printf("Successfully tested pleaseletmein\n"); + + retval = scrypt_check(mcf2, "pleasefailme"); + + if(retval < 0) + { + printf("deliberate failhash failed to calculate\n"); + exit(EXIT_FAILURE); + } + if(retval > 0) + { + printf("pleaseletmein deliberate fail hash has passed\n"); + exit(EXIT_FAILURE); + } + + printf("deliberate failhash failed\n"); return 0; } -- cgit v1.2.3 From 99519f7089751e3bcce868d5c8c23c3e84ad0ecb Mon Sep 17 00:00:00 2001 From: Technion Date: Sat, 1 Jun 2013 04:11:06 -0400 Subject: Created the easy hash function --- Makefile | 6 +++--- crypto-mcf.c | 6 +++--- crypto_scrypt-hash.c | 28 ++++++++++++++++++++++++++++ libscrypt.h | 5 ++++- main.c | 2 ++ 5 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 crypto_scrypt-hash.c diff --git a/Makefile b/Makefile index 36951bb..30b04db 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ CC=gcc -CFLAGS=-O2 -Wall +CFLAGS=-O2 -Wall -g -std=gnu99 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 +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 crypto-mcf.o: crypto-mcf.c - gcc -Wall -std=gnu99 -c -o crypto-mcf.o crypto-mcf.c + gcc -g -Wall -std=gnu99 -c -o crypto-mcf.o crypto-mcf.c library: $(OBJS) ar rcs libscrypt.a $(OBJS) diff --git a/crypto-mcf.c b/crypto-mcf.c index 5c58294..ca559ed 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -5,7 +5,7 @@ #include -void crypto_scrypt_mcf(uint32_t N, uint8_t r, uint8_t p, char *salt, char *hash, char *mcf) +void crypto_scrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, char *mcf) { @@ -13,9 +13,9 @@ void crypto_scrypt_mcf(uint32_t N, uint8_t r, uint8_t p, char *salt, char *hash, double t; t = log2(N); - + if (t != (int)t) - return; /* Not a valid state */ + printf("Failed"); /* Not a valid state */ params = (r << 8) + p; params += (uint32_t)t << 16; diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c new file mode 100644 index 0000000..df24a55 --- /dev/null +++ b/crypto_scrypt-hash.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +#include "libscrypt.h" +#include "modp_b64.h" + +int crypto_scrypt_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]; + + scrypt_salt_gen(salt, 16); + + retval = crypto_scrypt((uint8_t*)passphrase,strlen(passphrase), (uint8_t*)salt, sizeof(salt), N, r, p, hashbuf, sizeof(hashbuf)); + + modp_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); + modp_b64_encode(saltbuf, salt, sizeof(salt)); + + crypto_scrypt_mcf(N, r, p, saltbuf, outbuf, dst); + + return 1; +} diff --git a/libscrypt.h b/libscrypt.h index d0a0d32..5d4ed89 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -32,7 +32,7 @@ int crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, void crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs); /* Converts a series of input parameters to a MCF form for storage */ -void crypto_scrypt_mcf(uint32_t N, uint8_t r, uint8_t p, char *salt, char *hash, char *mcf); +void crypto_scrypt_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 @@ -41,4 +41,7 @@ void scrypt_salt_gen(char *rand, size_t len); /* Checks a given MCF against a password */ int scrypt_check(char *mcf, char *password); + +/* Creates a hash of a passphrase using a randomly generated salt */ +int crypto_scrypt_hash(char *dst, char* passphrase, uint32_t N, uint8_t r, uint8_t p); #endif /* !_CRYPTO_SCRYPT_H_ */ diff --git a/main.c b/main.c index ea847d7..a1d01b0 100644 --- a/main.c +++ b/main.c @@ -124,6 +124,8 @@ int main() } printf("deliberate failhash failed\n"); + crypto_scrypt_hash(outbuf, "My cats's breath smells like cat food", 16384, 8, 1); + printf("Received the following from simple hash:\n%s\n", outbuf); return 0; } -- cgit v1.2.3 From a631f852310cc1b0fb723b6ce273e60d52793745 Mon Sep 17 00:00:00 2001 From: Technion Date: Sat, 1 Jun 2013 04:19:06 -0400 Subject: Created early API documentation --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 8e0b99c..a830dad 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,20 @@ http://code.google.com/p/stringencoders/ Full documentation found here: http://www.lolware.net/libscrypt.html +Simple hashing interface + +A hash can be generated using the following function: +int crypto_scrypt_hash(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: +crypto_scrypt_hash(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 + +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. -- cgit v1.2.3 From 93a84d99c4bd686f598cd4cbe0a32bf0fa3c1754 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 3 Jun 2013 01:07:14 -0400 Subject: Significantly improved error testing and function documentation --- crypto-mcf.c | 9 ++- crypto_scrypt-hash.c | 19 +++++-- crypto_scrypt-hexconvert.c | 8 ++- libscrypt.h | 4 +- main.c | 138 ++++++++++++++++++++++++++++++++++++--------- 5 files changed, 138 insertions(+), 40 deletions(-) diff --git a/crypto-mcf.c b/crypto-mcf.c index ca559ed..955e413 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -5,20 +5,25 @@ #include -void crypto_scrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, char *mcf) +int crypto_scrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, char *mcf) { uint32_t params; double t; + if(!mcf || !hash) + return 0; + t = log2(N); if (t != (int)t) - printf("Failed"); /* Not a valid state */ + return 0; /* Not a valid state */ params = (r << 8) + p; params += (uint32_t)t << 16; sprintf(mcf, "$s0$%06x$%s$%s", params, salt, hash); + + return 1; } diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c index df24a55..fa8c146 100644 --- a/crypto_scrypt-hash.c +++ b/crypto_scrypt-hash.c @@ -18,11 +18,20 @@ int crypto_scrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8 scrypt_salt_gen(salt, 16); retval = crypto_scrypt((uint8_t*)passphrase,strlen(passphrase), (uint8_t*)salt, sizeof(salt), N, r, p, hashbuf, sizeof(hashbuf)); - - modp_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); - modp_b64_encode(saltbuf, salt, sizeof(salt)); - - crypto_scrypt_mcf(N, r, p, saltbuf, outbuf, dst); + if(retval == -1) + return 0; + + retval = modp_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); + if(retval == -1) + return 0; + + retval = modp_b64_encode(saltbuf, salt, sizeof(salt)); + if(retval == -1) + return 0; + + retval = crypto_scrypt_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 index cd94488..60c89db 100644 --- a/crypto_scrypt-hexconvert.c +++ b/crypto_scrypt-hexconvert.c @@ -3,20 +3,22 @@ #include #include -void crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs) +int crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs) { int i; if (!buf || s < 1 || obs < (s * 2 + 1)) - return; + return 0; - memset(outbuf, 0, obs); + memset(outbuf, 0, obs); + for(i=0; i<=(s-1); i++) { sprintf(outbuf, "%s%02x", outbuf, (unsigned char) buf[i]); } + return 1; } diff --git a/libscrypt.h b/libscrypt.h index 5d4ed89..3c9eaba 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -29,10 +29,10 @@ int crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, * Converts a binary string to a hex representation of that string * outbuf must have size of at least buf * 2 + 1. */ -void crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs); +int crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs); /* Converts a series of input parameters to a MCF form for storage */ -void crypto_scrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, char *mcf); +int crypto_scrypt_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 diff --git a/main.c b/main.c index a1d01b0..83597d2 100644 --- a/main.c +++ b/main.c @@ -30,102 +30,184 @@ int main() uint32_t, uint32_t, uint8_t *, size_t); */ - printf("Hashing with password password with salt NaCL\n"); + printf("TEST ONE: Direct call to reference function with password 'password' and salt 'NaCL'\n"); retval = crypto_scrypt((uint8_t*)"password",strlen("password"), (uint8_t*)"NaCl", strlen("NaCl"), 1024, 8, 16, hashbuf, sizeof(hashbuf)); if(retval != 0) { - printf("Failed to create hash of \"password\"\\n"); + 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 */ - crypto_scrypt_hexconvert(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); - printf("Hex output is:\n%s\n", outbuf); + printf("TEST TWO: Convert binary output to hex\n"); + retval = crypto_scrypt_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("Failed to match reference on hash\n"); + printf("TEST THREE: FAILED to match reference on hash\n"); exit(EXIT_FAILURE); } else { - printf("Test vector matched!\n"); + printf("TEST THREE: SUCCESSUL, Test vector matched!\n"); } - printf("Second test vector: pleaseletmein with SodiumChloride as salt\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 = crypto_scrypt((uint8_t*)"pleaseletmein",strlen("pleaseletmein"), (uint8_t*)"SodiumChloride", strlen("SodiumChloride"), 16384, 8, 1, hashbuf, sizeof(hashbuf)); if(retval != 0) { - printf("Failed to create hash of \"pleaseletmein\"\\n"); + 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 */ - crypto_scrypt_hexconvert(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); - printf("Hex output is:\n%s\n", outbuf); + printf("TEST FIVE: Convert binary output to hex\n"); + retval = crypto_scrypt_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("Failed to match reference on hash\n"); + printf("TEST SIX: FAILED to match reference on hash\n"); exit(EXIT_FAILURE); } else { - printf("Test vector matched!\n"); + printf("TEST SIX: SUCCESSUL, Test vector matched!\n"); } - modp_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); - modp_b64_encode(saltbuf, "SodiumChloride", strlen("SodiumChloride")); + /* 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 = modp_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); + if(retval == -1) + { + printf("TEST SEVEN FAILED\n"); + exit(EXIT_FAILURE); + } + retval = modp_b64_encode(saltbuf, "SodiumChloride", strlen("SodiumChloride")); + if(retval == -1) + { + printf("TEST SEVEN FAILED\n"); + exit(EXIT_FAILURE); + } - crypto_scrypt_mcf(16384, 8, 1, saltbuf, outbuf, mcf); + 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 = crypto_scrypt_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); - printf("Testing salt generator\n"); - scrypt_salt_gen(saltbuf, 16); - modp_b64_encode(outbuf, (char*)saltbuf, 16); - printf("Generated %s, I guess it's random?\n", outbuf); + /* 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 + */ - /* Since scrypt)check butchers mcf - make a copy */ + printf("TEST NINE: Password verify on given MCF\n"); retval = scrypt_check(mcf, "pleaseletmein"); if(retval < 0) { - printf("pleaseletmein hash failed to calculate\n"); + printf("TEST NINE: FAILED, hash failed to calculate\n"); exit(EXIT_FAILURE); } if(retval == 0) { - printf("pleaseletmein hash claimed did not verify\n"); + printf("TEST NINE: FAILED, claimed pleaseletmein hash claimed did not verify\n"); exit(EXIT_FAILURE); } /* retval >0 is a success */ - printf("Successfully tested pleaseletmein\n"); + printf("TEST NINE: SUCCESSFUL, tested pleaseletmein password\n"); + printf("TEST TEN: Password verify on same MCF, incorrect password\n"); retval = scrypt_check(mcf2, "pleasefailme"); if(retval < 0) { - printf("deliberate failhash failed to calculate\n"); + printf("TEST TEN: FAILED, hash failed to calculate\n"); exit(EXIT_FAILURE); } if(retval > 0) { - printf("pleaseletmein deliberate fail hash has passed\n"); + 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 */ + scrypt_salt_gen(saltbuf, 16); + + retval = modp_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"); - printf("deliberate failhash failed\n"); - crypto_scrypt_hash(outbuf, "My cats's breath smells like cat food", 16384, 8, 1); - printf("Received the following from simple hash:\n%s\n", outbuf); + retval = crypto_scrypt_hash(outbuf, "My cats's breath smells like cat food", 16384, 8, 16); + 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; } -- cgit v1.2.3 From 2eee64e79ebc8085740a76089a30f72607b2966a Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 3 Jun 2013 01:20:51 -0400 Subject: Rendering of README --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a830dad..4c4402a 100644 --- a/README.md +++ b/README.md @@ -14,16 +14,16 @@ http://www.lolware.net/libscrypt.html Simple hashing interface A hash can be generated using the following function: -int crypto_scrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p) +> int crypto_scrypt_hash(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: -crypto_scrypt_hash(outbuf, "My cats's breath smells like cat food", SCRYPT_N, SCRYPT_r, SCRYPT_p); +> crypto_scrypt_hash(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 +> retval = scrypt_check(mcf, "pleasefailme"); +> retval < 0 error +> retval = 0 password incorrect +> retval > 0 pass 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. -- cgit v1.2.3 From 1ef9ad22ec8c5ac6754090df42e312aa3e04f2e1 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 3 Jun 2013 01:27:53 -0400 Subject: Created #define's for N, r, p --- libscrypt.h | 5 +++++ main.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/libscrypt.h b/libscrypt.h index 3c9eaba..39ded86 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -44,4 +44,9 @@ int scrypt_check(char *mcf, char *password); /* Creates a hash of a passphrase using a randomly generated salt */ int crypto_scrypt_hash(char *dst, char* passphrase, uint32_t N, uint8_t r, uint8_t p); + +/* Sane default values */ +#define SCRYPT_N 16384 +#define SCRYPT_r 8 +#define SCRYPT_p 16 #endif /* !_CRYPTO_SCRYPT_H_ */ diff --git a/main.c b/main.c index 83597d2..2ded0d4 100644 --- a/main.c +++ b/main.c @@ -201,7 +201,7 @@ int main() printf("TEST TWELVE: Simple hash creation\n"); - retval = crypto_scrypt_hash(outbuf, "My cats's breath smells like cat food", 16384, 8, 16); + retval = crypto_scrypt_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"); -- cgit v1.2.3 From 53c91bd1039650692aed62215c927475d247d9cb Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 3 Jun 2013 20:41:21 -0400 Subject: Implemented log2() in order to avoid GNU99 extensions. Increased portability. --- Makefile | 4 +--- crypto-mcf.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 30b04db..7318b6c 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,9 @@ CC=gcc -CFLAGS=-O2 -Wall -g -std=gnu99 +CFLAGS=-O2 -Wall -g 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 -crypto-mcf.o: crypto-mcf.c - gcc -g -Wall -std=gnu99 -c -o crypto-mcf.o crypto-mcf.c library: $(OBJS) ar rcs libscrypt.a $(OBJS) diff --git a/crypto-mcf.c b/crypto-mcf.c index 955e413..ac6ea70 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -5,6 +5,16 @@ #include +/* 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 float scrypt_log2( uint32_t n ) +{ + // log(n)/log(2) is log2. + return (float)(log( n ) / log( 2 )); +} + int crypto_scrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, char *mcf) { @@ -15,7 +25,7 @@ int crypto_scrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash if(!mcf || !hash) return 0; - t = log2(N); + t = scrypt_log2(N); if (t != (int)t) return 0; /* Not a valid state */ -- cgit v1.2.3 From f715c50d45fe76e4e01dd26b5c8f8855a876d11c Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 4 Jun 2013 00:30:06 -0400 Subject: Implemented secure compile flags. Implemented workaround for issues when D_FORITFY_SOURCE=2 --- Makefile | 2 +- crypto_scrypt-hexconvert.c | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 7318b6c..c60fc20 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS=-O2 -Wall -g +CFLAGS=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector 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 diff --git a/crypto_scrypt-hexconvert.c b/crypto_scrypt-hexconvert.c index 60c89db..806fd15 100644 --- a/crypto_scrypt-hexconvert.c +++ b/crypto_scrypt-hexconvert.c @@ -7,16 +7,17 @@ int crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs) { int i; + int len = 0; if (!buf || s < 1 || obs < (s * 2 + 1)) return 0; - memset(outbuf, 0, obs); + memset(outbuf, 0, obs); for(i=0; i<=(s-1); i++) { - sprintf(outbuf, "%s%02x", outbuf, (unsigned char) buf[i]); + len += sprintf(outbuf+len, "%02x", (unsigned char) buf[i]); } return 1; -- cgit v1.2.3 From efd4fafdd5942a695dd97c7871c5a4d4868e5df2 Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 6 Jun 2013 05:33:18 -0400 Subject: Further #defines, less magic numbers --- libscrypt.h | 4 ++++ main.c | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/libscrypt.h b/libscrypt.h index 39ded86..e1ccb28 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -46,6 +46,10 @@ int scrypt_check(char *mcf, char *password); int crypto_scrypt_hash(char *dst, char* passphrase, uint32_t N, uint8_t r, uint8_t p); /* 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_N 16384 #define SCRYPT_r 8 #define SCRYPT_p 16 diff --git a/main.c b/main.c index 2ded0d4..7d25db5 100644 --- a/main.c +++ b/main.c @@ -12,10 +12,10 @@ int main() { - uint8_t hashbuf[64]; + uint8_t hashbuf[SCRYPT_HASH_LEN]; char outbuf[132]; - char mcf[256]; - char mcf2[256]; + char mcf[SCRYPT_MCF_LEN]; + char mcf2[SCRYPT_MCF_LEN]; char saltbuf[64]; int retval; /** -- cgit v1.2.3 From 8921653714fa99857613b5308144b4122bdba94a Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 6 Jun 2013 05:39:00 -0400 Subject: Update documentation relevant to last commit --- .gitignore | 2 +- README.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 720aacc..ce6ab24 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.o *.a - +reference diff --git a/README.md b/README.md index 4c4402a..5898bd2 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ Output stored in "outbuf" is stored in a standardised MCF form, which means incl > 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. -- cgit v1.2.3 From f1dab123746babe78370e652bc8e52e2acadd6a3 Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 6 Jun 2013 06:04:08 -0400 Subject: Standardised naming. API now locked in. --- README.md | 4 ++-- crypto-mcf.c | 2 +- crypto-scrypt-saltgen.c | 2 +- crypto_scrypt-check.c | 4 ++-- crypto_scrypt-hash.c | 8 ++++---- crypto_scrypt-hexconvert.c | 2 +- crypto_scrypt-nosse.c | 2 +- libscrypt.h | 14 +++++++------- main.c | 22 +++++++++++----------- 9 files changed, 30 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 5898bd2..e29d3d7 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,10 @@ http://www.lolware.net/libscrypt.html Simple hashing interface A hash can be generated using the following function: -> int crypto_scrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p) +> 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: -> crypto_scrypt_hash(outbuf, "My cats's breath smells like cat food", SCRYPT_N, SCRYPT_r, SCRYPT_p); +> 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"); diff --git a/crypto-mcf.c b/crypto-mcf.c index ac6ea70..b98b3e2 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -15,7 +15,7 @@ static float scrypt_log2( uint32_t n ) return (float)(log( n ) / log( 2 )); } -int crypto_scrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, char *mcf) +int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, char *mcf) { diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index 3b73a22..82af842 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -5,7 +5,7 @@ #include "sha256.h" -void scrypt_salt_gen(char *rand, size_t len) +void libscrypt_salt_gen(char *rand, size_t len) { unsigned char buf[32]; diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index 5d1b091..b2aebdf 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -6,7 +6,7 @@ #include "libscrypt.h" #include "modp_b64.h" -int scrypt_check(char *mcf, char *password) +int libscrypt_check(char *mcf, char *password) { uint32_t params; @@ -40,7 +40,7 @@ int scrypt_check(char *mcf, char *password) */ retval = modp_b64_decode(salt, tok, strlen(tok)); - retval = crypto_scrypt((uint8_t*)password,strlen(password), (uint8_t*)salt, retval, N, r, p, hashbuf, sizeof(hashbuf)); + retval = libscrypt_scrypt((uint8_t*)password,strlen(password), (uint8_t*)salt, retval, N, r, p, hashbuf, sizeof(hashbuf)); if (retval != 0) return retval; diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c index fa8c146..70ff800 100644 --- a/crypto_scrypt-hash.c +++ b/crypto_scrypt-hash.c @@ -6,7 +6,7 @@ #include "libscrypt.h" #include "modp_b64.h" -int crypto_scrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p) +int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p) { int retval; @@ -15,9 +15,9 @@ int crypto_scrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8 char outbuf[256]; char saltbuf[256]; - scrypt_salt_gen(salt, 16); + libscrypt_salt_gen(salt, 16); - retval = crypto_scrypt((uint8_t*)passphrase,strlen(passphrase), (uint8_t*)salt, sizeof(salt), N, r, p, hashbuf, sizeof(hashbuf)); + retval = libscrypt_scrypt((uint8_t*)passphrase,strlen(passphrase), (uint8_t*)salt, sizeof(salt), N, r, p, hashbuf, sizeof(hashbuf)); if(retval == -1) return 0; @@ -29,7 +29,7 @@ int crypto_scrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8 if(retval == -1) return 0; - retval = crypto_scrypt_mcf(N, r, p, saltbuf, outbuf, dst); + retval = libscrypt_mcf(N, r, p, saltbuf, outbuf, dst); if(retval == -1) return 0; diff --git a/crypto_scrypt-hexconvert.c b/crypto_scrypt-hexconvert.c index 806fd15..85d16f0 100644 --- a/crypto_scrypt-hexconvert.c +++ b/crypto_scrypt-hexconvert.c @@ -3,7 +3,7 @@ #include #include -int crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs) +int libscrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs) { int i; diff --git a/crypto_scrypt-nosse.c b/crypto_scrypt-nosse.c index 8c013be..845f014 100644 --- a/crypto_scrypt-nosse.c +++ b/crypto_scrypt-nosse.c @@ -230,7 +230,7 @@ smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY) * Return 0 on success; or -1 on error. */ int -crypto_scrypt(const uint8_t * passwd, size_t passwdlen, +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) { diff --git a/libscrypt.h b/libscrypt.h index e1ccb28..fbbd802 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -13,7 +13,7 @@ * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N * must be a power of 2 greater than 1. * - * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen): + * libscrypt_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen): * password; duh * N: CPU AND RAM cost (first modifier) * r: RAM Cost @@ -22,28 +22,28 @@ * standard unless you want to modify the CPU/RAM ratio. * Return 0 on success; or -1 on error. */ -int crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, +int libscrypt_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, uint32_t, uint32_t, 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 crypto_scrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs); +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 crypto_scrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, char *mcf); +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 scrypt_salt_gen(char *rand, size_t len); +void libscrypt_salt_gen(char *rand, size_t len); /* Checks a given MCF against a password */ -int scrypt_check(char *mcf, char *password); +int libscrypt_check(char *mcf, char *password); /* Creates a hash of a passphrase using a randomly generated salt */ -int crypto_scrypt_hash(char *dst, char* passphrase, uint32_t N, uint8_t r, uint8_t p); +int libscrypt_hash(char *dst, char* passphrase, uint32_t N, uint8_t r, uint8_t p); /* Sane default values */ #define SCRYPT_HASH_LEN 64 /* This can be user defined - diff --git a/main.c b/main.c index 7d25db5..84922f6 100644 --- a/main.c +++ b/main.c @@ -19,20 +19,20 @@ int main() char saltbuf[64]; int retval; /** - * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen): + * 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 crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, + 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 = crypto_scrypt((uint8_t*)"password",strlen("password"), (uint8_t*)"NaCl", strlen("NaCl"), 1024, 8, 16, hashbuf, sizeof(hashbuf)); + retval = libscrypt_scrypt((uint8_t*)"password",strlen("password"), (uint8_t*)"NaCl", strlen("NaCl"), 1024, 8, 16, hashbuf, sizeof(hashbuf)); if(retval != 0) { @@ -47,7 +47,7 @@ int main() * Returns 0 on fail, 1 on success */ printf("TEST TWO: Convert binary output to hex\n"); - retval = crypto_scrypt_hexconvert(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); + retval = libscrypt_hexconvert(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); if(!retval) { printf("TEST TWO: FAILED\n"); @@ -72,7 +72,7 @@ int main() /* Tests 4-6 repeat tests 1-3 with a different reference vector */ - retval = crypto_scrypt((uint8_t*)"pleaseletmein",strlen("pleaseletmein"), (uint8_t*)"SodiumChloride", strlen("SodiumChloride"), 16384, 8, 1, hashbuf, sizeof(hashbuf)); + retval = libscrypt_scrypt((uint8_t*)"pleaseletmein",strlen("pleaseletmein"), (uint8_t*)"SodiumChloride", strlen("SodiumChloride"), 16384, 8, 1, hashbuf, sizeof(hashbuf)); if(retval != 0) { @@ -86,7 +86,7 @@ int main() * at least sizeof(hashbuf) * 2 + 1 */ printf("TEST FIVE: Convert binary output to hex\n"); - retval = crypto_scrypt_hexconvert(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); + retval = libscrypt_hexconvert(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); if(!retval) { printf("TEST FIVE: FAILED\n"); @@ -137,7 +137,7 @@ int main() * 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 = crypto_scrypt_mcf(16384, 8, 1, saltbuf, outbuf, mcf); + retval = libscrypt_mcf(16384, 8, 1, saltbuf, outbuf, mcf); if(!retval) { printf("TEST EIGHT FAILED\n"); @@ -156,7 +156,7 @@ int main() */ printf("TEST NINE: Password verify on given MCF\n"); - retval = scrypt_check(mcf, "pleaseletmein"); + retval = libscrypt_check(mcf, "pleaseletmein"); if(retval < 0) { @@ -172,7 +172,7 @@ int main() printf("TEST NINE: SUCCESSFUL, tested pleaseletmein password\n"); printf("TEST TEN: Password verify on same MCF, incorrect password\n"); - retval = scrypt_check(mcf2, "pleasefailme"); + retval = libscrypt_check(mcf2, "pleasefailme"); if(retval < 0) { @@ -189,7 +189,7 @@ int main() printf("TEST ELEVEN: Testing salt generator\n"); /* TODO: I'm not presently sure how this function could fail */ - scrypt_salt_gen(saltbuf, 16); + libscrypt_salt_gen(saltbuf, 16); retval = modp_b64_encode(saltbuf, (char*)saltbuf, 16); if(retval == -1) @@ -201,7 +201,7 @@ int main() printf("TEST TWELVE: Simple hash creation\n"); - retval = crypto_scrypt_hash(outbuf, "My cats's breath smells like cat food", SCRYPT_N, SCRYPT_r, SCRYPT_p); + 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"); -- cgit v1.2.3 From 300ad52ec76905dff7aa8b20a28c0673cb311982 Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 6 Jun 2013 06:07:48 -0400 Subject: Documentation --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index e29d3d7..29a8a0e 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,11 @@ Sane constants have been created for N, r and p so you can create a has like thi 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. @@ -30,3 +33,9 @@ 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. + +Notes on Code Development +------------------------ + +Once declared "stable", the master branch will always be "stable" and development will be done on branches. +The reference machines are CentOS, Raspbian and FreeBSD, and the code is expected to compile and run on all of these before being moved to stable branch. -- cgit v1.2.3 From f2c9514b9a9d49a67842d9b3d339cc09de295bdb Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 6 Jun 2013 07:33:38 -0400 Subject: Moved to a proper shared library model. --- .gitignore | 2 +- Makefile | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index ce6ab24..5876345 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.o -*.a +*.so reference diff --git a/Makefile b/Makefile index c60fc20..f0b1ce7 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,18 @@ CC=gcc -CFLAGS=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector +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) - ar rcs libscrypt.a $(OBJS) + gcc -shared -Wl,-soname,libscrypt.so -o libscrypt.so -lc $(OBJS) reference: library main.o - gcc -Wall -o reference main.o libscrypt.a -lm + gcc -Wall -o reference main.o -Wl,-rpath=. -L. -lm -lscrypt + clean: - rm -f *.o reference libscrypt.a + rm -f *.o reference libscrypt.so +check: all + ./reference -- cgit v1.2.3 From c4aa4649bb862d5bbe0fc067e35e98e065331456 Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 6 Jun 2013 07:52:43 -0400 Subject: Created version file, explicitly list exports --- Makefile | 2 +- crypto_scrypt-check.c | 5 ++--- crypto_scrypt-hash.c | 5 ++--- libscrypt.h | 4 ++++ libscrypt.version | 10 ++++++++++ main.c | 7 +++---- modp_b64.c | 6 +++--- modp_b64.h | 4 ++-- 8 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 libscrypt.version diff --git a/Makefile b/Makefile index f0b1ce7..10df905 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o mod library: $(OBJS) - gcc -shared -Wl,-soname,libscrypt.so -o libscrypt.so -lc $(OBJS) + gcc -shared -Wl,-soname,libscrypt.so -Wl,--version-script=libscrypt.version -o libscrypt.so -lc $(OBJS) reference: library main.o gcc -Wall -o reference main.o -Wl,-rpath=. -L. -lm -lscrypt diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index b2aebdf..ea8b083 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -4,7 +4,6 @@ #include #include "libscrypt.h" -#include "modp_b64.h" int libscrypt_check(char *mcf, char *password) { @@ -39,13 +38,13 @@ int libscrypt_check(char *mcf, char *password) printf("We've obtained salt 'N' r p of '%s' %d %d %d\n", tok, N,r,p); */ - retval = modp_b64_decode(salt, tok, strlen(tok)); + retval = libscrypt_b64_decode(salt, tok, strlen(tok)); retval = libscrypt_scrypt((uint8_t*)password,strlen(password), (uint8_t*)salt, retval, N, r, p, hashbuf, sizeof(hashbuf)); if (retval != 0) return retval; - modp_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); + libscrypt_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); tok = strtok(NULL, "$"); diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c index 70ff800..4d4e7aa 100644 --- a/crypto_scrypt-hash.c +++ b/crypto_scrypt-hash.c @@ -4,7 +4,6 @@ #include #include "libscrypt.h" -#include "modp_b64.h" int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p) { @@ -21,11 +20,11 @@ int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p if(retval == -1) return 0; - retval = modp_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); + retval = libscrypt_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); if(retval == -1) return 0; - retval = modp_b64_encode(saltbuf, salt, sizeof(salt)); + retval = libscrypt_b64_encode(saltbuf, salt, sizeof(salt)); if(retval == -1) return 0; diff --git a/libscrypt.h b/libscrypt.h index fbbd802..a8d3f19 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -45,6 +45,10 @@ 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(char* dest, const char* str, int len); +int libscrypt_b64_decode(char* dest, const char* src, int len); + + /* Sane default values */ #define SCRYPT_HASH_LEN 64 /* This can be user defined - *but 64 is the reference size 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 index 84922f6..48c6024 100644 --- a/main.c +++ b/main.c @@ -3,7 +3,6 @@ #include #include "libscrypt.h" -#include "modp_b64.h" #define REF1 "fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640" @@ -116,13 +115,13 @@ int main() printf("TEST SEVEN: BASE64 encoding the salt and hash output\n"); - retval = modp_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); + retval = libscrypt_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); if(retval == -1) { printf("TEST SEVEN FAILED\n"); exit(EXIT_FAILURE); } - retval = modp_b64_encode(saltbuf, "SodiumChloride", strlen("SodiumChloride")); + retval = libscrypt_b64_encode(saltbuf, "SodiumChloride", strlen("SodiumChloride")); if(retval == -1) { printf("TEST SEVEN FAILED\n"); @@ -191,7 +190,7 @@ int main() /* TODO: I'm not presently sure how this function could fail */ libscrypt_salt_gen(saltbuf, 16); - retval = modp_b64_encode(saltbuf, (char*)saltbuf, 16); + retval = libscrypt_b64_encode(saltbuf, (char*)saltbuf, 16); if(retval == -1) { printf("TEST ELEVEN FAILED\n"); diff --git a/modp_b64.c b/modp_b64.c index 32129e3..2d515c9 100644 --- a/modp_b64.c +++ b/modp_b64.c @@ -71,7 +71,7 @@ #define CHARPAD '\0' #endif -int modp_b64_encode(char* dest, const char* str, int len) +int libscrypt_b64_encode(char* dest, const char* str, int len) { int i; const uint8_t* s = (const uint8_t*) str; @@ -113,7 +113,7 @@ int modp_b64_encode(char* dest, const char* str, int len) } #ifdef WORDS_BIGENDIAN /* BIG ENDIAN -- SUN / IBM / MOTOROLA */ -int modp_b64_decode(char* dest, const char* src, int len) +int libscrypt_b64_decode(char* dest, const char* src, int len) { int i; if (len == 0) return 0; @@ -183,7 +183,7 @@ int modp_b64_decode(char* dest, const char* src, int len) #else /* LITTLE ENDIAN -- INTEL AND FRIENDS */ -int modp_b64_decode(char* dest, const char* src, int len) +int libscrypt_b64_decode(char* dest, const char* src, int len) { int i; if (len == 0) return 0; diff --git a/modp_b64.h b/modp_b64.h index 3256af7..fb831dd 100644 --- a/modp_b64.h +++ b/modp_b64.h @@ -57,7 +57,7 @@ BEGIN_C * \endcode * */ -int modp_b64_encode(char* dest, const char* str, int len); +int libscrypt_b64_encode(char* dest, const char* str, int len); /** * Decode a base64 encoded string @@ -81,7 +81,7 @@ int modp_b64_encode(char* dest, const char* str, int len); * if (len == -1) { error } * \endcode */ -int modp_b64_decode(char* dest, const char* src, int len); +int libscrypt_b64_decode(char* dest, const char* src, int len); /** * Given a source string of length len, this returns the amount of -- cgit v1.2.3 From 9c5dd6256fd52c6ef37e17f442f8b113f24ea013 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 10 Jun 2013 00:40:53 -0400 Subject: -fpic -> -fPIC --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 10df905..466d08a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fpic +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 -- cgit v1.2.3 From 6e4c4ba0a586219f4fed8ca0f4e68ce9cae12275 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 10 Jun 2013 00:44:03 -0400 Subject: Better soname, CC is variable --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 466d08a..5319ea4 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,10 @@ OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o mod library: $(OBJS) - gcc -shared -Wl,-soname,libscrypt.so -Wl,--version-script=libscrypt.version -o libscrypt.so -lc $(OBJS) + $(CC) -shared -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version -o libscrypt.so -lc $(OBJS) reference: library main.o - gcc -Wall -o reference main.o -Wl,-rpath=. -L. -lm -lscrypt + $(CC) -Wall -o reference main.o -Wl,-rpath=. -L. -lm -lscrypt clean: rm -f *.o reference libscrypt.so -- cgit v1.2.3 From 28cd9c22cd238928945d83e75e693951861ef032 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 10 Jun 2013 00:55:44 -0400 Subject: Better input checking in mcf format, handle overflows --- Makefile | 5 +++-- crypto-mcf.c | 14 +++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 5319ea4..036546f 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,14 @@ OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o mod library: $(OBJS) - $(CC) -shared -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version -o libscrypt.so -lc $(OBJS) + $(CC) -shared -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version -o libscrypt.so.0 -lc $(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 + rm -f *.o reference libscrypt.so* check: all ./reference diff --git a/crypto-mcf.c b/crypto-mcf.c index b98b3e2..271e7ed 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -5,6 +5,8 @@ #include +#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 @@ -24,6 +26,12 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, ch 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); @@ -33,7 +41,11 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, ch params = (r << 8) + p; params += (uint32_t)t << 16; - sprintf(mcf, "$s0$%06x$%s$%s", params, salt, hash); + /* Using snprintf - not checking for overflows. We've already + * determined that mcf should be defined as at least SCRYPT_MCF_LEN + * in length + */ + snprintf(mcf, SCRYPT_MCF_LEN, "$s0$%06x$%s$%s", params, salt, hash); return 1; } -- cgit v1.2.3 From 56790d9f2d15dfcf95d30abbd15885ca9e081b91 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 10 Jun 2013 02:16:12 -0400 Subject: Strict integer checking, including dealing with precision, results of splint checking. --- crypto-mcf.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/crypto-mcf.c b/crypto-mcf.c index 271e7ed..2e674b2 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -2,6 +2,7 @@ #include #include #include +#include #include @@ -11,10 +12,13 @@ * Note that this function returns a float and hence is not compatible with the * GNU prototype */ -static float scrypt_log2( uint32_t n ) +static double scrypt_log2( uint32_t n ) { - // log(n)/log(2) is log2. - return (float)(log( n ) / log( 2 )); + // 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) @@ -22,7 +26,8 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, ch uint32_t params; - double t; + int s; + double t, t2, fracpart; if(!mcf || !hash) return 0; @@ -33,10 +38,16 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, ch if(r > (uint8_t)(-1) || p > (uint8_t)(-1)) return 0; + t = scrypt_log2(N); - - if (t != (int)t) - return 0; /* Not a valid state */ + + /* 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; @@ -45,7 +56,9 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, ch * determined that mcf should be defined as at least SCRYPT_MCF_LEN * in length */ - snprintf(mcf, SCRYPT_MCF_LEN, "$s0$%06x$%s$%s", params, salt, hash); + s = snprintf(mcf, SCRYPT_MCF_LEN, "$s0$%06x$%s$%s", (unsigned int)params, salt, hash); + if (s > SCRYPT_MCF_LEN) + return 0; return 1; } -- cgit v1.2.3 From c1c060ce3bce88e7ccffca8bf5d732ac0c5962cf Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 11 Jun 2013 22:57:36 -0400 Subject: Aggressive testing with splint, many integer comparisons improved, better error checking. --- Makefile | 5 +++++ crypto_scrypt-check.c | 41 +++++++++++++++++++++++++++++++++++++---- crypto_scrypt-hexconvert.c | 9 +++++++-- libscrypt.h | 6 +++--- modp_b64.c | 6 +++--- modp_b64.h | 5 +++-- sha256.h | 14 +++++++++++--- 7 files changed, 69 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 036546f..e4c7343 100644 --- a/Makefile +++ b/Makefile @@ -17,3 +17,8 @@ clean: check: all ./reference + +devtest: + splint crypto_scrypt-hexconvert.c + splint crypto-mcf.c crypto_scrypt-check.c + splint crypto-scrypt-saltgen.c +posixlib diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index ea8b083..d7ea4a5 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -5,6 +5,23 @@ #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) { @@ -25,29 +42,45 @@ int libscrypt_check(char *mcf, char *password) tok = strtok(mcf, "$"); tok = strtok(NULL, "$"); - sscanf(tok, "%x", ¶ms); + + 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= pow(2, N); + 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)); - retval = libscrypt_scrypt((uint8_t*)password,strlen(password), (uint8_t*)salt, retval, N, r, p, hashbuf, sizeof(hashbuf)); + 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; - libscrypt_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); + 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; diff --git a/crypto_scrypt-hexconvert.c b/crypto_scrypt-hexconvert.c index 85d16f0..ececbd9 100644 --- a/crypto_scrypt-hexconvert.c +++ b/crypto_scrypt-hexconvert.c @@ -6,7 +6,7 @@ int libscrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs) { - int i; + size_t i; int len = 0; if (!buf || s < 1 || obs < (s * 2 + 1)) @@ -17,7 +17,12 @@ int libscrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs) for(i=0; i<=(s-1); i++) { - len += sprintf(outbuf+len, "%02x", (unsigned char) buf[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/libscrypt.h b/libscrypt.h index a8d3f19..929efce 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -23,7 +23,7 @@ * 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, uint8_t *, size_t); + uint32_t, uint32_t, /*@out@*/ uint8_t *, size_t); /** * Converts a binary string to a hex representation of that string @@ -45,8 +45,8 @@ 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(char* dest, const char* str, int len); -int libscrypt_b64_decode(char* dest, const char* src, int len); +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 */ diff --git a/modp_b64.c b/modp_b64.c index 2d515c9..9215b45 100644 --- a/modp_b64.c +++ b/modp_b64.c @@ -71,7 +71,7 @@ #define CHARPAD '\0' #endif -int libscrypt_b64_encode(char* dest, const char* str, int len) +int libscrypt_b64_encode(char* dest, const char* str, size_t len) { int i; const uint8_t* s = (const uint8_t*) str; @@ -113,7 +113,7 @@ int libscrypt_b64_encode(char* dest, const char* str, int len) } #ifdef WORDS_BIGENDIAN /* BIG ENDIAN -- SUN / IBM / MOTOROLA */ -int libscrypt_b64_decode(char* dest, const char* src, int len) +int libscrypt_b64_decode(char* dest, const char* src, size_t len) { int i; if (len == 0) return 0; @@ -183,7 +183,7 @@ int libscrypt_b64_decode(char* dest, const char* src, int len) #else /* LITTLE ENDIAN -- INTEL AND FRIENDS */ -int libscrypt_b64_decode(char* dest, const char* src, int len) +int libscrypt_b64_decode(char* dest, const char* src, size_t len) { int i; if (len == 0) return 0; diff --git a/modp_b64.h b/modp_b64.h index fb831dd..720049e 100644 --- a/modp_b64.h +++ b/modp_b64.h @@ -57,7 +57,8 @@ BEGIN_C * \endcode * */ -int libscrypt_b64_encode(char* dest, const char* str, int len); +#include +int libscrypt_b64_encode(char* dest, const char* str, size_t len); /** * Decode a base64 encoded string @@ -81,7 +82,7 @@ int libscrypt_b64_encode(char* dest, const char* str, int len); * if (len == -1) { error } * \endcode */ -int libscrypt_b64_decode(char* dest, const char* src, int len); +int libscrypt_b64_decode(char* dest, const char* src, size_t len); /** * Given a source string of length len, this returns the amount of diff --git a/sha256.h b/sha256.h index 289a523..580183a 100644 --- a/sha256.h +++ b/sha256.h @@ -44,12 +44,20 @@ typedef struct HMAC_SHA256Context { SHA256_CTX octx; } HMAC_SHA256_CTX; -void SHA256_Init(SHA256_CTX *); +void SHA256_Init(/*@out@*/ SHA256_CTX *); void SHA256_Update(SHA256_CTX *, const void *, size_t); -void SHA256_Final(unsigned char [32], SHA256_CTX *); + +/* 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); -void HMAC_SHA256_Final(unsigned char [32], HMAC_SHA256_CTX *); + +/* 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): -- cgit v1.2.3 From d912415ce9a5b5b817164056556e82036adf46a9 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 11 Jun 2013 22:58:20 -0400 Subject: Added .so.0 --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5876345..b74f2ce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.o *.so +*.so.0 reference -- cgit v1.2.3 From 6ab8de6f9e622343c09a817d0de4f62f52dde342 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 11 Jun 2013 23:03:54 -0400 Subject: Additional error checks and balances --- Makefile | 3 ++- libscrypt.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index e4c7343..9f12b47 100644 --- a/Makefile +++ b/Makefile @@ -20,5 +20,6 @@ check: all devtest: splint crypto_scrypt-hexconvert.c - splint crypto-mcf.c crypto_scrypt-check.c + splint crypto-mcf.c crypto_scrypt-check.c crypto_scrypt-hash.c splint crypto-scrypt-saltgen.c +posixlib + valgrind ./reference diff --git a/libscrypt.h b/libscrypt.h index 929efce..43347f2 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -37,7 +37,7 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, ch /* Generates a salt. This is not a cryptographically unpredictable function, * but should produce appropriately randomised output for this purpose */ -void libscrypt_salt_gen(char *rand, size_t len); +void libscrypt_salt_gen(/*@out@*/ char *rand, size_t len); /* Checks a given MCF against a password */ int libscrypt_check(char *mcf, char *password); -- cgit v1.2.3 From e435fc082cbbcb8fdbd81a37e36f4ff5ffa5fbe3 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 11 Jun 2013 23:06:48 -0400 Subject: Static library placed back in --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 9f12b47..eb08e5b 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o mod 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 -- cgit v1.2.3 From 88562497cb06b7fc4106212e6a94df70c20a2623 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 11 Jun 2013 23:08:13 -0400 Subject: Documentation --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 29a8a0e..c7844c7 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,13 @@ 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 Notes on Code Development ------------------------ -Once declared "stable", the master branch will always be "stable" and development will be done on branches. +Code is now declared "stable", the master branch will always be "stable" and development will be done on branches. The reference machines are CentOS, Raspbian and FreeBSD, and the code is expected to compile and run on all of these before being moved to stable branch. -- cgit v1.2.3 From edb16a5e83a58b1af9bc963dd45776bee4a54051 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 11 Jun 2013 23:16:01 -0400 Subject: Markdown formatting --- README.md | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index c7844c7..3929223 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,10 @@ Sane constants have been created for N, r and p so you can create a has like thi > 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 + 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. @@ -35,8 +32,8 @@ A number of internal functions are exposed, and users wishing to create more com The test reference is also aimed at providing a well documented use case. Building -------- -> make -> make check + make + make check Notes on Code Development ------------------------ -- cgit v1.2.3 From 206e75f3295328c23094e435f495a9b57f538b27 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 11 Jun 2013 23:17:10 -0400 Subject: More formatting --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3929223..b2271c9 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Sane constants have been created for N, r and p so you can create a has like thi > 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 -- cgit v1.2.3 From 23b750f7951fdcb1d1075dc007f4d3c188da5070 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 11 Jun 2013 23:19:41 -0400 Subject: BUGS documentation --- README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b2271c9..ee9ae28 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,12 @@ 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) + + 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); + + 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: @@ -35,6 +37,11 @@ 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 ------------------------ -- cgit v1.2.3 From 7b85170b7bc469ce0b35783f5435f1092023d886 Mon Sep 17 00:00:00 2001 From: Technion Date: Wed, 12 Jun 2013 22:31:31 -0400 Subject: SCRYPT_MCF_ID definition. --- crypto-mcf.c | 2 +- crypto_scrypt-check.c | 2 +- libscrypt.h | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/crypto-mcf.c b/crypto-mcf.c index 2e674b2..66e92f0 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -56,7 +56,7 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, ch * determined that mcf should be defined as at least SCRYPT_MCF_LEN * in length */ - s = snprintf(mcf, SCRYPT_MCF_LEN, "$s0$%06x$%s$%s", (unsigned int)params, salt, hash); + s = snprintf(mcf, SCRYPT_MCF_LEN, SCRYPT_MCF_ID "$%06x$%s$%s", (unsigned int)params, salt, hash); if (s > SCRYPT_MCF_LEN) return 0; diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index d7ea4a5..1aaa88a 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -34,7 +34,7 @@ int libscrypt_check(char *mcf, char *password) char salt[32]; char *tok; - if(memcmp(mcf, "$s0", 3) != 0) + if(memcmp(mcf, SCRYPT_MCF_ID, 3) != 0) { /* Only version 0 supported */ return -1; diff --git a/libscrypt.h b/libscrypt.h index 43347f2..b244d8d 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -54,6 +54,7 @@ int libscrypt_b64_decode(/*@out@*/ char* dest, const char* src, size_t len); *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 -- cgit v1.2.3 From 7680b9ebf0983f6e42af8753d5a4453a6feed1f2 Mon Sep 17 00:00:00 2001 From: Technion Date: Wed, 12 Jun 2013 22:44:33 -0400 Subject: Clean up .a file on make clean --- .gitignore | 1 + Makefile | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b74f2ce..6becbe2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.o *.so *.so.0 +*.a reference diff --git a/Makefile b/Makefile index eb08e5b..40e1d83 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ reference: library main.o $(CC) -Wall -o reference main.o -Wl,-rpath=. -L. -lm -lscrypt clean: - rm -f *.o reference libscrypt.so* + rm -f *.o reference libscrypt.so* libscrypt.a check: all ./reference -- cgit v1.2.3 From ba32d44665da8ac5685881624508f2893c2ae48a Mon Sep 17 00:00:00 2001 From: Technion Date: Wed, 12 Jun 2013 22:54:40 -0400 Subject: Documentation update --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ee9ae28..92c2fdc 100644 --- a/README.md +++ b/README.md @@ -47,4 +47,7 @@ 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, Raspbian and FreeBSD, and the code is expected to compile and run on all of these before being moved to stable branch. +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 -- cgit v1.2.3 From 155c86ff760ca16563328e62cf38d18fa7778632 Mon Sep 17 00:00:00 2001 From: Technion Date: Wed, 12 Jun 2013 22:55:56 -0400 Subject: Further documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 92c2fdc..4fbfaad 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ http://www.tarsnap.com/scrypt.html Utilises BSD licensed BASE64 encoder here: http://code.google.com/p/stringencoders/ -Full documentation found here: +Official project page, including stable tarballs found here: http://www.lolware.net/libscrypt.html Simple hashing interface -- cgit v1.2.3 From 7dc48af97eb7e4e0699a8dfc602bdc0d556c3908 Mon Sep 17 00:00:00 2001 From: Technion Date: Sat, 29 Jun 2013 23:43:34 -0400 Subject: Add BSD license --- LICENSE | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 LICENSE 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. + -- cgit v1.2.3 From 04c0b44ee4c0521d94589495cee4e9633d9be0c5 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 29 Jun 2013 23:54:15 -0400 Subject: Added a make install target. --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Makefile b/Makefile index 40e1d83..69ee494 100644 --- a/Makefile +++ b/Makefile @@ -24,3 +24,9 @@ devtest: 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 -- cgit v1.2.3 From aaf95228044f5062ecd4a998354520bddc36b4fc Mon Sep 17 00:00:00 2001 From: Technion Date: Sun, 21 Jul 2013 20:52:27 -0400 Subject: Added note to confirm FreeBSD testing. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4fbfaad..c14770f 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ 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. +The reference machines are CentOS, FreeBSD 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 -- cgit v1.2.3 From 49f91360842f4f909b5614bba211707aefb7838d Mon Sep 17 00:00:00 2001 From: Technion Date: Sun, 21 Jul 2013 21:00:04 -0400 Subject: Patch from Micah to provide a more generic Makefile. --- Makefile | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 69ee494..567290f 100644 --- 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 @@ devtest: 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 $(DESTDIR)$(LIBDIR)/libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so + $(INSTALL_DATA) libscrypt.h $(DESTDIR)$(INCLUDEDIR) -- cgit v1.2.3 From 79360a47c10b8e62d0c73b5f84c1834017211e9e Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 2 Aug 2013 15:19:39 +1000 Subject: Improved Makefile - assists with packaging --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 567290f..21a56c2 100644 --- a/Makefile +++ b/Makefile @@ -35,5 +35,5 @@ 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 $(DESTDIR)$(LIBDIR)/libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so + cd $(DESTDIR)$(LIBDIR) && ln -s -f libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so $(INSTALL_DATA) libscrypt.h $(DESTDIR)$(INCLUDEDIR) -- cgit v1.2.3 From 37ce9c66d50635fe0d94320f3609ae92f3ddd10e Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 2 Aug 2013 15:38:37 +1000 Subject: README Update for v1.1a --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c14770f..2d84dfc 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,11 @@ 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, FreeBSD and Raspbian, and the code is expected to compile and run on all of these before being moved to stable branch. +The reference machines are Fedora, CentOS, FreeBSD 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 + +Changenotes +----------- +v1.1a: Single Makefile line change. I wouldn't ordinarily tag this as a new "release", but the purpose here is to assist with packaging in distributions. -- cgit v1.2.3 From 600187d4e1af5153fbcb14cf6ae7cca45845fb8c Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 2 Aug 2013 01:27:46 -0400 Subject: No longer bundle .a --- Makefile | 5 ++++- README.md | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 21a56c2..09947eb 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,10 @@ devtest: install: library $(MAKE_DIR) $(DESTDIR) $(DESTDIR)$(PREFIX) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR) - $(INSTALL_DATA) libscrypt.a $(DESTDIR)$(LIBDIR) $(INSTALL_DATA) libscrypt.so.0 $(DESTDIR)$(LIBDIR) cd $(DESTDIR)$(LIBDIR) && ln -s -f libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so $(INSTALL_DATA) libscrypt.h $(DESTDIR)$(INCLUDEDIR) + +install-static: libscrypt.a + $(INSTALL_DATA) libscrypt.a $(DESTDIR)$(LIBDIR) + diff --git a/README.md b/README.md index 2d84dfc..5049fb5 100644 --- a/README.md +++ b/README.md @@ -55,3 +55,4 @@ http://www.lolware.net/libscrypttesting.txt Changenotes ----------- v1.1a: Single Makefile line change. I wouldn't ordinarily tag this as a new "release", but the purpose here is to assist with packaging in distributions. +v1.12: The static library is built, but no longer installed by default. You can install it with "make install-static". This is because static libraries are not typically bundled in packages. -- cgit v1.2.3 From e2cfa89078454d11314e487289cefb64f4663f06 Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 2 Aug 2013 17:59:40 -0400 Subject: Fixed permissions on install command --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 09947eb..8e644a3 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ PREFIX = /usr/local LIBDIR = $(PREFIX)/lib INCLUDEDIR = $(PREFIX)/include MAKE_DIR = install -d -INSTALL_DATA = install -m 0644 +INSTALL_DATA = install CC=gcc CFLAGS=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC @@ -33,10 +33,10 @@ devtest: install: library $(MAKE_DIR) $(DESTDIR) $(DESTDIR)$(PREFIX) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR) - $(INSTALL_DATA) libscrypt.so.0 $(DESTDIR)$(LIBDIR) + $(INSTALL_DATA) -m 0755 libscrypt.so.0 $(DESTDIR)$(LIBDIR) cd $(DESTDIR)$(LIBDIR) && ln -s -f libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so - $(INSTALL_DATA) libscrypt.h $(DESTDIR)$(INCLUDEDIR) + $(INSTALL_DATA) -m 0644 libscrypt.h $(DESTDIR)$(INCLUDEDIR) install-static: libscrypt.a - $(INSTALL_DATA) libscrypt.a $(DESTDIR)$(LIBDIR) + $(INSTALL_DATA) -m 0644 libscrypt.a $(DESTDIR)$(LIBDIR) -- cgit v1.2.3 From 0ba71136b6b4cff3c2e5b8e082eb925639499593 Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 2 Aug 2013 19:05:07 -0400 Subject: Allowed for LDFLAGS to be set --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8e644a3..87d8c6d 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,14 @@ INSTALL_DATA = install CC=gcc CFLAGS=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC +LDFLAGS=-z now 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) + $(CC) $(LDFLAGS) -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 -- cgit v1.2.3 From 2c6c33bf3161d0b95b50b9747a60bd78b6950d97 Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 2 Aug 2013 21:28:33 -0400 Subject: Change where mathlib links --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 87d8c6d..3689cca 100644 --- a/Makefile +++ b/Makefile @@ -13,12 +13,12 @@ OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o mod library: $(OBJS) - $(CC) $(LDFLAGS) -shared -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version -o libscrypt.so.0 -lc $(OBJS) + $(CC) $(LDFLAGS) -shared -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version -o libscrypt.so.0 -lc -lm $(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 + $(CC) -Wall -o reference main.o -Wl,-rpath=. -L. -lscrypt clean: rm -f *.o reference libscrypt.so* libscrypt.a -- cgit v1.2.3 From a217e5b202a270ec13d903893ee725b40ca3e267 Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 2 Aug 2013 23:19:53 -0400 Subject: Tag v1.13 --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 5049fb5..cd70dc5 100644 --- a/README.md +++ b/README.md @@ -55,4 +55,7 @@ http://www.lolware.net/libscrypttesting.txt Changenotes ----------- v1.1a: Single Makefile line change. I wouldn't ordinarily tag this as a new "release", but the purpose here is to assist with packaging in distributions. + v1.12: The static library is built, but no longer installed by default. You can install it with "make install-static". This is because static libraries are not typically bundled in packages. + +v1.13: Minor packaging related update -- cgit v1.2.3 From 839c4246d34b919f30088c563435e541f7df3fb5 Mon Sep 17 00:00:00 2001 From: Christopher Meng Date: Sat, 3 Aug 2013 15:20:45 +0800 Subject: Preserve the timestamp. --- Makefile | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 3689cca..4a1f13c 100644 --- a/Makefile +++ b/Makefile @@ -34,10 +34,9 @@ devtest: install: library $(MAKE_DIR) $(DESTDIR) $(DESTDIR)$(PREFIX) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR) - $(INSTALL_DATA) -m 0755 libscrypt.so.0 $(DESTDIR)$(LIBDIR) + $(INSTALL_DATA) -pm 0755 libscrypt.so.0 $(DESTDIR)$(LIBDIR) cd $(DESTDIR)$(LIBDIR) && ln -s -f libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so - $(INSTALL_DATA) -m 0644 libscrypt.h $(DESTDIR)$(INCLUDEDIR) + $(INSTALL_DATA) -pm 0644 libscrypt.h $(DESTDIR)$(INCLUDEDIR) install-static: libscrypt.a - $(INSTALL_DATA) -m 0644 libscrypt.a $(DESTDIR)$(LIBDIR) - + $(INSTALL_DATA) -pm 0644 libscrypt.a $(DESTDIR)$(LIBDIR) -- cgit v1.2.3 From 4360fb69d032cf43191985335caaead68c4b7a19 Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 15 Aug 2013 22:45:34 -0400 Subject: Changed default LDFLAGS --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3689cca..c4abd65 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ INSTALL_DATA = install CC=gcc CFLAGS=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC -LDFLAGS=-z now +LDFLAGS=-Wl,-z,now -Wl,-z,relro 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 -- cgit v1.2.3 From 71547549771b24d48c92ae1c4ff9d00b7077ee40 Mon Sep 17 00:00:00 2001 From: Jeff Shaw Date: Tue, 10 Sep 2013 17:43:24 -0400 Subject: Added a test that fails. --- main.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 48c6024..281cf7b 100644 --- a/main.c +++ b/main.c @@ -206,7 +206,18 @@ int main() 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); + printf("TEST TWELVE: SUCCESSFUL. Received the following from simple hash:\n%s\n", outbuf); + + printf("TEST THIRTEEN: VERIFY TEST TWELVE'S HASH\n"); + + retval = libscrypt_check(outbuf, "My cats's breath smells like cat food"); + + if (retval != 1) { + printf("TEST THIRTEEN: FAILED TO VERIFY TEST TWELVE'S HASH\n"); + exit(EXIT_FAILURE); + } + + printf("TEST THIRTEEN SUCCESSFUL\n"); return 0; } -- cgit v1.2.3 From d5420f1682e7b1b082ff4bf1f9a231fcc2faae83 Mon Sep 17 00:00:00 2001 From: Jeff Shaw Date: Tue, 10 Sep 2013 17:47:53 -0400 Subject: Fix the failing test. --- libscrypt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libscrypt.h b/libscrypt.h index b244d8d..9e22a43 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -53,7 +53,7 @@ int libscrypt_b64_decode(/*@out@*/ char* dest, const char* src, size_t len); #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_LEN 125 /* mcf is 120 byte + nul */ #define SCRYPT_MCF_ID "$s1" #define SCRYPT_N 16384 #define SCRYPT_r 8 -- cgit v1.2.3 From e39cd011d29b013a1b8383149940df1626384638 Mon Sep 17 00:00:00 2001 From: technion Date: Tue, 24 Sep 2013 22:17:42 +0000 Subject: Patch from sharkz: Include the header and set the WORDS_BIGENDIAN define according to BYTE_ORDER --- modp_b64.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/modp_b64.c b/modp_b64.c index 9215b45..dd1164c 100644 --- a/modp_b64.c +++ b/modp_b64.c @@ -41,14 +41,19 @@ * */ +#include + /* 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 */ +#if BYTE_ORDER == BIG_ENDIAN +/* if on motoral, sun, ibm */ +#define WORDS_BIGENDIAN 1 +#else +/* else for Intel, Amd */ +#undef WORDS_BIGENDIAN +#endif #include "modp_b64_data.h" -- cgit v1.2.3 From 3a875625bf4be1a0064d182c19335ab366b4f5fd Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 24 Sep 2013 22:43:56 +0000 Subject: Address uninitialized variable warning on newer GCC. --- crypto_scrypt-nosse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto_scrypt-nosse.c b/crypto_scrypt-nosse.c index 845f014..5a07009 100644 --- a/crypto_scrypt-nosse.c +++ b/crypto_scrypt-nosse.c @@ -78,7 +78,7 @@ blkxor(void * dest, void * src, size_t len) static void salsa20_8(uint32_t B[16]) { - uint32_t x[16]; + uint32_t x[16] = {0}; size_t i; blkcpy(x, B, 64); -- cgit v1.2.3 From 80d434ca20d3d80b5346f5b6cbec739409ccaa63 Mon Sep 17 00:00:00 2001 From: Technion Date: Wed, 25 Sep 2013 00:00:12 +0000 Subject: More portable endian tests. --- .gitignore | 2 ++ Makefile | 7 +++++-- byteorder.c | 27 +++++++++++++++++++++++++++ modp_b64.c | 2 +- 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 byteorder.c diff --git a/.gitignore b/.gitignore index 6becbe2..5dbf7c6 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ *.so.0 *.a reference +endian.h +byteorder diff --git a/Makefile b/Makefile index d800b71..71dff6d 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,11 @@ 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 +endian.h: byteorder.c + $(CC) byteorder.c $(CFLAGS) -o byteorder + ./byteorder > endian.h -library: $(OBJS) +library: endian.h $(OBJS) $(CC) $(LDFLAGS) -shared -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version -o libscrypt.so.0 -lc -lm $(OBJS) ar rcs libscrypt.a $(OBJS) @@ -21,7 +24,7 @@ reference: library main.o $(CC) -Wall -o reference main.o -Wl,-rpath=. -L. -lscrypt clean: - rm -f *.o reference libscrypt.so* libscrypt.a + rm -f *.o reference libscrypt.so* libscrypt.a endian.h check: all ./reference diff --git a/byteorder.c b/byteorder.c new file mode 100644 index 0000000..6cb2ea5 --- /dev/null +++ b/byteorder.c @@ -0,0 +1,27 @@ +/* Creates the endianness.h file */ +#include + +int littleendian(); + +int main() +{ + printf("#define LITTLE_ENDIAN 1234\n#define BIG_ENDIAN 4321\n"); + + if(littleendian()) + { + printf("#define BYTE_ORDER LITTLE_ENDIAN\n"); + } + else + { + printf("#define BYTE_ORDER BIG_ENDIAN\n"); + } + return 0; +} + + +int littleendian() +{ + short int word = 0x0001; + char *byte = (char *) &word; + return(byte[0] ? 1 : 0); +} diff --git a/modp_b64.c b/modp_b64.c index dd1164c..8a60ce2 100644 --- a/modp_b64.c +++ b/modp_b64.c @@ -41,7 +41,7 @@ * */ -#include +#include "endian.h" /* public header */ #include "modp_b64.h" -- cgit v1.2.3 From 5de7eac718adb0ed27c3d26e2ea408e191d0b8ef Mon Sep 17 00:00:00 2001 From: Technion Date: Sun, 19 Jan 2014 22:54:59 +0000 Subject: New base64 library. --- Makefile | 8 +- byteorder.c | 27 --- crypto_scrypt-check.c | 4 +- crypto_scrypt-hash.c | 4 +- libscrypt.h | 4 +- main.c | 6 +- modp_b64.c | 269 ---------------------------- modp_b64.h | 235 ------------------------ modp_b64_data.h | 480 -------------------------------------------------- 9 files changed, 11 insertions(+), 1026 deletions(-) delete mode 100644 byteorder.c delete mode 100644 modp_b64.c delete mode 100644 modp_b64.h delete mode 100644 modp_b64_data.h diff --git a/Makefile b/Makefile index 71dff6d..46efd74 100644 --- a/Makefile +++ b/Makefile @@ -9,13 +9,9 @@ CFLAGS=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC LDFLAGS=-Wl,-z,now -Wl,-z,relro 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 +OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o -endian.h: byteorder.c - $(CC) byteorder.c $(CFLAGS) -o byteorder - ./byteorder > endian.h - -library: endian.h $(OBJS) +library: $(OBJS) $(CC) $(LDFLAGS) -shared -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version -o libscrypt.so.0 -lc -lm $(OBJS) ar rcs libscrypt.a $(OBJS) diff --git a/byteorder.c b/byteorder.c deleted file mode 100644 index 6cb2ea5..0000000 --- a/byteorder.c +++ /dev/null @@ -1,27 +0,0 @@ -/* Creates the endianness.h file */ -#include - -int littleendian(); - -int main() -{ - printf("#define LITTLE_ENDIAN 1234\n#define BIG_ENDIAN 4321\n"); - - if(littleendian()) - { - printf("#define BYTE_ORDER LITTLE_ENDIAN\n"); - } - else - { - printf("#define BYTE_ORDER BIG_ENDIAN\n"); - } - return 0; -} - - -int littleendian() -{ - short int word = 0x0001; - char *byte = (char *) &word; - return(byte[0] ? 1 : 0); -} diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index 1aaa88a..546e655 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -64,7 +64,7 @@ int libscrypt_check(char *mcf, char *password) 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)); + retval = libscrypt_b64_decode(tok, salt, sizeof(salt)); 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)); @@ -72,7 +72,7 @@ int libscrypt_check(char *mcf, char *password) if (retval != 0) return retval; - retval = libscrypt_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); + retval = libscrypt_b64_encode((char*)hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); if (retval == 0) return -1; diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c index 4d4e7aa..1fcf49b 100644 --- a/crypto_scrypt-hash.c +++ b/crypto_scrypt-hash.c @@ -20,11 +20,11 @@ int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p if(retval == -1) return 0; - retval = libscrypt_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); + retval = libscrypt_b64_encode((char*)hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); if(retval == -1) return 0; - retval = libscrypt_b64_encode(saltbuf, salt, sizeof(salt)); + retval = libscrypt_b64_encode(salt, sizeof(salt), saltbuf, sizeof(saltbuf)); if(retval == -1) return 0; diff --git a/libscrypt.h b/libscrypt.h index 9e22a43..383d131 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -45,8 +45,8 @@ 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); +int libscrypt_b64_encode(unsigned char const*, size_t, /*@out@*/ char*, size_t); +int libscrypt_b64_decode(char const*, /*@out@*/ unsigned char*, size_t); /* Sane default values */ diff --git a/main.c b/main.c index 281cf7b..1c75a17 100644 --- a/main.c +++ b/main.c @@ -115,13 +115,13 @@ int main() printf("TEST SEVEN: BASE64 encoding the salt and hash output\n"); - retval = libscrypt_b64_encode(outbuf, (char*)hashbuf, sizeof(hashbuf)); + retval = libscrypt_b64_encode((char*)hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); if(retval == -1) { printf("TEST SEVEN FAILED\n"); exit(EXIT_FAILURE); } - retval = libscrypt_b64_encode(saltbuf, "SodiumChloride", strlen("SodiumChloride")); + retval = libscrypt_b64_encode("SodiumChloride", strlen("SodiumChloride"), saltbuf, sizeof(saltbuf)); if(retval == -1) { printf("TEST SEVEN FAILED\n"); @@ -190,7 +190,7 @@ int main() /* TODO: I'm not presently sure how this function could fail */ libscrypt_salt_gen(saltbuf, 16); - retval = libscrypt_b64_encode(saltbuf, (char*)saltbuf, 16); + retval = libscrypt_b64_encode((char*)saltbuf, 16, saltbuf, sizeof(saltbuf)); if(retval == -1) { printf("TEST ELEVEN FAILED\n"); diff --git a/modp_b64.c b/modp_b64.c deleted file mode 100644 index 8a60ce2..0000000 --- a/modp_b64.c +++ /dev/null @@ -1,269 +0,0 @@ -/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */ -/* vi: set expandtab shiftwidth=4 tabstop=4: */ -/** - * \file modp_b64.c - *
- * MODP_B64 - High performance base64 encoder/decoder
- * http://code.google.com/p/stringencoders/
- *
- * Copyright © 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
- * 
- */ - -#include "endian.h" - -/* public header */ -#include "modp_b64.h" - - -#if BYTE_ORDER == BIG_ENDIAN -/* if on motoral, sun, ibm */ -#define WORDS_BIGENDIAN 1 -#else -/* else for Intel, Amd */ -#undef WORDS_BIGENDIAN -#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 deleted file mode 100644 index 720049e..0000000 --- a/modp_b64.h +++ /dev/null @@ -1,235 +0,0 @@ -/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */ -/* vi: set expandtab shiftwidth=4 tabstop=4: */ - -/** - * \file - *
- * High performance base64 encoder / decoder
- *
- * Copyright © 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.
- * 
- * - * 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 -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 -#include - -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(x.data()), s, - static_cast(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(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(x.data()), src, - static_cast(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 deleted file mode 100644 index 4fb321c..0000000 --- a/modp_b64_data.h +++ /dev/null @@ -1,480 +0,0 @@ -#include -#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 -- cgit v1.2.3 From 44c5312e6c1cf5f3eb3394c2b8c19f3ea66ea7d2 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 20 Jan 2014 00:06:02 +0000 Subject: Corrected signing for new prototypes. --- crypto-scrypt-saltgen.c | 2 +- crypto_scrypt-check.c | 5 +++-- crypto_scrypt-hash.c | 5 +++-- libscrypt.h | 5 +---- main.c | 9 +++++---- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index 82af842..8b9caf4 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -5,7 +5,7 @@ #include "sha256.h" -void libscrypt_salt_gen(char *rand, size_t len) +void libscrypt_salt_gen(uint8_t *rand, size_t len) { unsigned char buf[32]; diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index 546e655..1d954a1 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -3,6 +3,7 @@ #include #include +#include "b64.h" #include "libscrypt.h" /* pow() works with doubles. Sounds like it should cast to int correctly, @@ -64,7 +65,7 @@ int libscrypt_check(char *mcf, char *password) printf("We've obtained salt 'N' r p of '%s' %d %d %d\n", tok, N,r,p); */ - retval = libscrypt_b64_decode(tok, salt, sizeof(salt)); + retval = libscrypt_b64_decode(tok, (uint8_t*)salt, sizeof(salt)); 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)); @@ -72,7 +73,7 @@ int libscrypt_check(char *mcf, char *password) if (retval != 0) return retval; - retval = libscrypt_b64_encode((char*)hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); + retval = libscrypt_b64_encode(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); if (retval == 0) return -1; diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c index 1fcf49b..0816113 100644 --- a/crypto_scrypt-hash.c +++ b/crypto_scrypt-hash.c @@ -3,13 +3,14 @@ #include #include +#include "b64.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 salt[16]; uint8_t hashbuf[64]; char outbuf[256]; char saltbuf[256]; @@ -20,7 +21,7 @@ int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p if(retval == -1) return 0; - retval = libscrypt_b64_encode((char*)hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); + retval = libscrypt_b64_encode(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); if(retval == -1) return 0; diff --git a/libscrypt.h b/libscrypt.h index 383d131..6af01e9 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -37,7 +37,7 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, ch /* 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); +void libscrypt_salt_gen(/*@out@*/ uint8_t *rand, size_t len); /* Checks a given MCF against a password */ int libscrypt_check(char *mcf, char *password); @@ -45,9 +45,6 @@ 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(unsigned char const*, size_t, /*@out@*/ char*, size_t); -int libscrypt_b64_decode(char const*, /*@out@*/ unsigned char*, size_t); - /* Sane default values */ #define SCRYPT_HASH_LEN 64 /* This can be user defined - diff --git a/main.c b/main.c index 1c75a17..f9d2bfb 100644 --- a/main.c +++ b/main.c @@ -2,6 +2,7 @@ #include #include +#include "b64.h" #include "libscrypt.h" #define REF1 "fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640" @@ -115,13 +116,13 @@ int main() printf("TEST SEVEN: BASE64 encoding the salt and hash output\n"); - retval = libscrypt_b64_encode((char*)hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); + retval = libscrypt_b64_encode(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); if(retval == -1) { printf("TEST SEVEN FAILED\n"); exit(EXIT_FAILURE); } - retval = libscrypt_b64_encode("SodiumChloride", strlen("SodiumChloride"), saltbuf, sizeof(saltbuf)); + retval = libscrypt_b64_encode((unsigned char*)"SodiumChloride", strlen("SodiumChloride"), saltbuf, sizeof(saltbuf)); if(retval == -1) { printf("TEST SEVEN FAILED\n"); @@ -188,9 +189,9 @@ int main() printf("TEST ELEVEN: Testing salt generator\n"); /* TODO: I'm not presently sure how this function could fail */ - libscrypt_salt_gen(saltbuf, 16); + libscrypt_salt_gen((uint8_t*)saltbuf, 16); - retval = libscrypt_b64_encode((char*)saltbuf, 16, saltbuf, sizeof(saltbuf)); + retval = libscrypt_b64_encode((uint8_t*)saltbuf, 16, saltbuf, sizeof(saltbuf)); if(retval == -1) { printf("TEST ELEVEN FAILED\n"); -- cgit v1.2.3 From 932f7cdbb6510e2e35d6f4ab65049c9246bb8452 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 20 Jan 2014 00:06:48 +0000 Subject: Track new b64 files. --- b64.c | 312 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ b64.h | 8 ++ 2 files changed, 320 insertions(+) create mode 100644 b64.c create mode 100644 b64.h diff --git a/b64.c b/b64.c new file mode 100644 index 0000000..8453cdd --- /dev/null +++ b/b64.c @@ -0,0 +1,312 @@ +/* + * Copyright (c) 1996 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +/* + * Portions Copyright (c) 1995 by International Business Machines, Inc. + * + * International Business Machines, Inc. (hereinafter called IBM) grants + * permission under its copyrights to use, copy, modify, and distribute this + * Software with or without fee, provided that the above copyright notice and + * all paragraphs of this notice appear in all copies, and that the name of IBM + * not be used in connection with the marketing of any product incorporating + * the Software or modifications thereof, without specific, written prior + * permission. + * + * To the extent it has a right to do so, IBM grants an immunity from suit + * under its patents, if any, for the use, sale or manufacture of products to + * the extent that such products are used for performing Domain Name System + * dynamic updates in TCP/IP networks by means of the Software. No immunity is + * granted for any product per se or for any other function of any product. + * + * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, + * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN + * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +/* + * Base64 encode/decode functions from OpenBSD (src/lib/libc/net/base64.c). + */ +#include +#include +#include +#include +#include + +#include "b64.h" + + +static const char Base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char Pad64 = '='; + +/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt) + The following encoding technique is taken from RFC 1521 by Borenstein + and Freed. It is reproduced here in a slightly edited form for + convenience. + + A 65-character subset of US-ASCII is used, enabling 6 bits to be + represented per printable character. (The extra 65th character, "=", + is used to signify a special processing function.) + + The encoding process represents 24-bit groups of input bits as output + strings of 4 encoded characters. Proceeding from left to right, a + 24-bit input group is formed by concatenating 3 8-bit input groups. + These 24 bits are then treated as 4 concatenated 6-bit groups, each + of which is translated into a single digit in the base64 alphabet. + + Each 6-bit group is used as an index into an array of 64 printable + characters. The character referenced by the index is placed in the + output string. + + Table 1: The Base64 Alphabet + + Value Encoding Value Encoding Value Encoding Value Encoding + 0 A 17 R 34 i 51 z + 1 B 18 S 35 j 52 0 + 2 C 19 T 36 k 53 1 + 3 D 20 U 37 l 54 2 + 4 E 21 V 38 m 55 3 + 5 F 22 W 39 n 56 4 + 6 G 23 X 40 o 57 5 + 7 H 24 Y 41 p 58 6 + 8 I 25 Z 42 q 59 7 + 9 J 26 a 43 r 60 8 + 10 K 27 b 44 s 61 9 + 11 L 28 c 45 t 62 + + 12 M 29 d 46 u 63 / + 13 N 30 e 47 v + 14 O 31 f 48 w (pad) = + 15 P 32 g 49 x + 16 Q 33 h 50 y + + Special processing is performed if fewer than 24 bits are available + at the end of the data being encoded. A full encoding quantum is + always completed at the end of a quantity. When fewer than 24 input + bits are available in an input group, zero bits are added (on the + right) to form an integral number of 6-bit groups. Padding at the + end of the data is performed using the '=' character. + + Since all base64 input is an integral number of octets, only the + ------------------------------------------------- + following cases can arise: + + (1) the final quantum of encoding input is an integral + multiple of 24 bits; here, the final unit of encoded + output will be an integral multiple of 4 characters + with no "=" padding, + (2) the final quantum of encoding input is exactly 8 bits; + here, the final unit of encoded output will be two + characters followed by two "=" padding characters, or + (3) the final quantum of encoding input is exactly 16 bits; + here, the final unit of encoded output will be three + characters followed by one "=" padding character. +*/ + +int +libscrypt_b64_encode(src, srclength, target, targsize) + u_char const *src; + size_t srclength; + char *target; + size_t targsize; +{ + size_t datalength = 0; + u_char input[3]; + u_char output[4]; + int i; + + while (2 < srclength) { + input[0] = *src++; + input[1] = *src++; + input[2] = *src++; + srclength -= 3; + + output[0] = input[0] >> 2; + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); + output[3] = input[2] & 0x3f; + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + target[datalength++] = Base64[output[2]]; + target[datalength++] = Base64[output[3]]; + } + + /* Now we worry about padding. */ + if (0 != srclength) { + /* Get what's left. */ + input[0] = input[1] = input[2] = '\0'; + for (i = 0; i < srclength; i++) + input[i] = *src++; + + output[0] = input[0] >> 2; + output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); + output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + if (srclength == 1) + target[datalength++] = Pad64; + else + target[datalength++] = Base64[output[2]]; + target[datalength++] = Pad64; + } + if (datalength >= targsize) + return (-1); + target[datalength] = '\0'; /* Returned value doesn't count \0. */ + return (datalength); +} + +/* skips all whitespace anywhere. + converts characters, four at a time, starting at (or after) + src from base - 64 numbers into three 8 bit bytes in the target area. + it returns the number of data bytes stored at the target, or -1 on error. + */ + +int +libscrypt_b64_decode(src, target, targsize) + char const *src; + u_char *target; + size_t targsize; +{ + int tarindex, state, ch; + u_char nextbyte; + char *pos; + + state = 0; + tarindex = 0; + + while ((ch = (unsigned char)*src++) != '\0') { + if (isspace(ch)) /* Skip whitespace anywhere. */ + continue; + + if (ch == Pad64) + break; + + pos = strchr(Base64, ch); + if (pos == 0) /* A non-base64 character. */ + return (-1); + + switch (state) { + case 0: + if (target) { + if (tarindex >= targsize) + return (-1); + target[tarindex] = (pos - Base64) << 2; + } + state = 1; + break; + case 1: + if (target) { + if (tarindex >= targsize) + return (-1); + target[tarindex] |= (pos - Base64) >> 4; + nextbyte = ((pos - Base64) & 0x0f) << 4; + if (tarindex + 1 < targsize) + target[tarindex+1] = nextbyte; + else if (nextbyte) + return (-1); + } + tarindex++; + state = 2; + break; + case 2: + if (target) { + if (tarindex >= targsize) + return (-1); + target[tarindex] |= (pos - Base64) >> 2; + nextbyte = ((pos - Base64) & 0x03) << 6; + if (tarindex + 1 < targsize) + target[tarindex+1] = nextbyte; + else if (nextbyte) + return (-1); + } + tarindex++; + state = 3; + break; + case 3: + if (target) { + if (tarindex >= targsize) + return (-1); + target[tarindex] |= (pos - Base64); + } + tarindex++; + state = 0; + break; + } + } + + /* + * We are done decoding Base-64 chars. Let's see if we ended + * on a byte boundary, and/or with erroneous trailing characters. + */ + + if (ch == Pad64) { /* We got a pad char. */ + ch = (unsigned char)*src++; /* Skip it, get next. */ + switch (state) { + case 0: /* Invalid = in first position */ + case 1: /* Invalid = in second position */ + return (-1); + + case 2: /* Valid, means one byte of info */ + /* Skip any number of spaces. */ + for (; ch != '\0'; ch = (unsigned char)*src++) + if (!isspace(ch)) + break; + /* Make sure there is another trailing = sign. */ + if (ch != Pad64) + return (-1); + ch = (unsigned char)*src++; /* Skip the = */ + /* Fall through to "single trailing =" case. */ + /* FALLTHROUGH */ + + case 3: /* Valid, means two bytes of info */ + /* + * We know this char is an =. Is there anything but + * whitespace after it? + */ + for (; ch != '\0'; ch = (unsigned char)*src++) + if (!isspace(ch)) + return (-1); + + /* + * Now make sure for cases 2 and 3 that the "extra" + * bits that slopped past the last full byte were + * zeros. If we don't check them, they become a + * subliminal channel. + */ + if (target && tarindex < targsize && + target[tarindex] != 0) + return (-1); + } + } else { + /* + * We ended by seeing the end of the string. Make sure we + * have no partial bytes lying around. + */ + if (state != 0) + return (-1); + } + + return (tarindex); +} diff --git a/b64.h b/b64.h new file mode 100644 index 0000000..0f21f08 --- /dev/null +++ b/b64.h @@ -0,0 +1,8 @@ + +/* BASE64 libraries used internally - should not need to be packaged */ + +#define b64_encode_len(A) ((A+2)/3 * 4 + 1) +#define b64_decode_len(A) (A / 4 * 3 + 2) + +int libscrypt_b64_encode(unsigned char const *, size_t, char *, size_t); +int libscrypt_b64_decode(char const *, unsigned char *, size_t); -- cgit v1.2.3 From 0ea61330f3f900504072d2a8e713d2954141aa47 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 20 Jan 2014 03:06:49 +0000 Subject: Improved consistency. --- b64.h | 6 ++++-- crypto_scrypt-check.c | 13 +++++++++---- crypto_scrypt-hash.c | 6 ++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/b64.h b/b64.h index 0f21f08..2e271eb 100644 --- a/b64.h +++ b/b64.h @@ -4,5 +4,7 @@ #define b64_encode_len(A) ((A+2)/3 * 4 + 1) #define b64_decode_len(A) (A / 4 * 3 + 2) -int libscrypt_b64_encode(unsigned char const *, size_t, char *, size_t); -int libscrypt_b64_decode(char const *, unsigned char *, size_t); +int libscrypt_b64_encode(unsigned char const *src, size_t srclength, + /*@out@*/ char *target, size_t targetsize); +int libscrypt_b64_decode(char const *src, /*@out@*/ unsigned char *target, + size_t targetsize); diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index 1d954a1..b19ef12 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -32,7 +32,7 @@ int libscrypt_check(char *mcf, char *password) int retval; uint8_t hashbuf[64]; char outbuf[128]; - char salt[32]; + uint8_t salt[32]; char *tok; if(memcmp(mcf, SCRYPT_MCF_ID, 3) != 0) @@ -65,15 +65,20 @@ int libscrypt_check(char *mcf, char *password) printf("We've obtained salt 'N' r p of '%s' %d %d %d\n", tok, N,r,p); */ - retval = libscrypt_b64_decode(tok, (uint8_t*)salt, sizeof(salt)); + memset(salt, 0, sizeof(salt)); /* Keeps splint happy */ + retval = libscrypt_b64_decode(tok, (unsigned char*)salt, sizeof(salt)); 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)); + + retval = libscrypt_scrypt((uint8_t*)password, strlen(password), salt, + (uint32_t)retval, N, r, p, hashbuf, sizeof(hashbuf)); if (retval != 0) return retval; - retval = libscrypt_b64_encode(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); + retval = libscrypt_b64_encode((unsigned char*)hashbuf, sizeof(hashbuf), + outbuf, sizeof(outbuf)); + if (retval == 0) return -1; diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c index 0816113..e5384ca 100644 --- a/crypto_scrypt-hash.c +++ b/crypto_scrypt-hash.c @@ -21,11 +21,13 @@ int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p if(retval == -1) return 0; - retval = libscrypt_b64_encode(hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); + retval = libscrypt_b64_encode((unsigned char*)hashbuf, sizeof(hashbuf), + outbuf, sizeof(outbuf)); if(retval == -1) return 0; - retval = libscrypt_b64_encode(salt, sizeof(salt), saltbuf, sizeof(saltbuf)); + retval = libscrypt_b64_encode((unsigned char *)salt, sizeof(salt), + saltbuf, sizeof(saltbuf)); if(retval == -1) return 0; -- cgit v1.2.3 From 034f91902d530befcf35d1fa8c5cf8e8d7b1974c Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 20 Jan 2014 03:49:07 +0000 Subject: Case sensitivity cleanup. --- main.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index f9d2bfb..1c5b43b 100644 --- a/main.c +++ b/main.c @@ -209,16 +209,16 @@ int main() } printf("TEST TWELVE: SUCCESSFUL. Received the following from simple hash:\n%s\n", outbuf); - printf("TEST THIRTEEN: VERIFY TEST TWELVE'S HASH\n"); + printf("TEST THIRTEEN: Verify test twelve's hash\n"); retval = libscrypt_check(outbuf, "My cats's breath smells like cat food"); if (retval != 1) { - printf("TEST THIRTEEN: FAILED TO VERIFY TEST TWELVE'S HASH\n"); + printf("TEST THIRTEEN: FAILED, hash not verified\n"); exit(EXIT_FAILURE); } - printf("TEST THIRTEEN SUCCESSFUL\n"); + printf("TEST THIRTEEN: SUCCESSFUL\n"); return 0; } -- cgit v1.2.3 From 6032e0b36bba7ee1aa5214a431d764edbd7fa0ee Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 20 Jan 2014 08:30:20 +0000 Subject: Revert 3a875625b due to GCC 4.8.1 apparently breaking under it. --- README.md | 3 +-- crypto_scrypt-nosse.c | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cd70dc5..a0de779 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,7 @@ 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/ +Utilises BASE64 encoding library from ISC. Official project page, including stable tarballs found here: http://www.lolware.net/libscrypt.html diff --git a/crypto_scrypt-nosse.c b/crypto_scrypt-nosse.c index 5a07009..845f014 100644 --- a/crypto_scrypt-nosse.c +++ b/crypto_scrypt-nosse.c @@ -78,7 +78,7 @@ blkxor(void * dest, void * src, size_t len) static void salsa20_8(uint32_t B[16]) { - uint32_t x[16] = {0}; + uint32_t x[16]; size_t i; blkcpy(x, B, 64); -- cgit v1.2.3 From 186bc6bddf08db79b6accf3edcfc93239f426a02 Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 23 Jan 2014 01:24:55 +0000 Subject: Do not export b64 functions. --- Makefile | 4 ++-- libscrypt.version | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 46efd74..8460768 100644 --- a/Makefile +++ b/Makefile @@ -15,9 +15,9 @@ library: $(OBJS) $(CC) $(LDFLAGS) -shared -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version -o libscrypt.so.0 -lc -lm $(OBJS) ar rcs libscrypt.a $(OBJS) -reference: library main.o +reference: library main.o b64.o ln -s -f libscrypt.so.0 libscrypt.so - $(CC) -Wall -o reference main.o -Wl,-rpath=. -L. -lscrypt + $(CC) -Wall -o reference main.o b64.o -Wl,-rpath=. -L. -lscrypt clean: rm -f *.o reference libscrypt.so* libscrypt.a endian.h diff --git a/libscrypt.version b/libscrypt.version index 7f4cbc0..6ad45dc 100644 --- a/libscrypt.version +++ b/libscrypt.version @@ -5,6 +5,5 @@ libscrypt_hexconvert; libscrypt_mcf; libscrypt_salt_gen; libscrypt_scrypt; -libscrypt_b64_encode; local: *; }; -- cgit v1.2.3 From 20087c9b03a267b46a4f5483c6329fcb7342b8c2 Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 23 Jan 2014 01:32:17 +0000 Subject: Document v1.15 --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a0de779..6ba5f33 100644 --- a/README.md +++ b/README.md @@ -58,3 +58,5 @@ v1.1a: Single Makefile line change. I wouldn't ordinarily tag this as a new "rel v1.12: The static library is built, but no longer installed by default. You can install it with "make install-static". This is because static libraries are not typically bundled in packages. v1.13: Minor packaging related update + +v1.15: Replaced the b64 libraries with more portable one from ISC. Now tested and verified on a wider variety of architectures. Note, libscrypt_b64_encrypt was originally an exported function. This is no longer the case as it is considered an internal function only. -- cgit v1.2.3 From b690857f56363476e7cd2bba23f0df32e8460618 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 11 Feb 2014 10:30:43 +0000 Subject: Streamline "make test" --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8460768..e03737f 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ reference: library main.o b64.o clean: rm -f *.o reference libscrypt.so* libscrypt.a endian.h -check: all +check: library ./reference devtest: -- cgit v1.2.3 From 6d3f72a6d6533c6a38e11889283312e991999733 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 11 Feb 2014 10:32:53 +0000 Subject: Revert "Streamline "make test"" This commit did not have desired effect. This reverts commit b690857f56363476e7cd2bba23f0df32e8460618. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e03737f..8460768 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ reference: library main.o b64.o clean: rm -f *.o reference libscrypt.so* libscrypt.a endian.h -check: library +check: all ./reference devtest: -- cgit v1.2.3 From cf4fad307c0471adb40fd54cc0737ea9487a056d Mon Sep 17 00:00:00 2001 From: koobs Date: Wed, 12 Feb 2014 18:53:18 +1100 Subject: Allow overriding variables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allow overriding variables for user-customisabilityâ„¢ and platform portability. --- Makefile | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 8460768..772b88d 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,12 @@ -PREFIX = /usr/local -LIBDIR = $(PREFIX)/lib -INCLUDEDIR = $(PREFIX)/include -MAKE_DIR = install -d -INSTALL_DATA = install - -CC=gcc -CFLAGS=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC -LDFLAGS=-Wl,-z,now -Wl,-z,relro +PREFIX ?= /usr/local +LIBDIR ?= $(PREFIX)/lib +INCLUDEDIR ?= $(PREFIX)/include +MAKE_DIR ?= install -d +INSTALL_DATA ?= install + +CC?=gcc +CFLAGS?=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC +LDFLAGS?=-Wl,-z,now -Wl,-z,relro all: reference OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o -- cgit v1.2.3 From de183179d166e9454a707f55300f15578e6a6c24 Mon Sep 17 00:00:00 2001 From: Technion Date: Wed, 19 Feb 2014 08:20:49 +0000 Subject: Patch from acv for OSX compat. --- Makefile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 772b88d..480d66f 100644 --- a/Makefile +++ b/Makefile @@ -6,18 +6,20 @@ INSTALL_DATA ?= install CC?=gcc CFLAGS?=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC -LDFLAGS?=-Wl,-z,now -Wl,-z,relro +LDFLAGS?=-Wl,-z,now -Wl,-z,relro -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version +CFLAGS_EXTRA?=-Wl,-rpath=. + all: reference OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o library: $(OBJS) - $(CC) $(LDFLAGS) -shared -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version -o libscrypt.so.0 -lc -lm $(OBJS) + $(CC) $(LDFLAGS) -shared -o libscrypt.so.0 -lc -lm $(OBJS) ar rcs libscrypt.a $(OBJS) reference: library main.o b64.o ln -s -f libscrypt.so.0 libscrypt.so - $(CC) -Wall -o reference main.o b64.o -Wl,-rpath=. -L. -lscrypt + $(CC) -Wall -o reference main.o b64.o $(CFLAGS_EXTRA) -L. -lscrypt clean: rm -f *.o reference libscrypt.so* libscrypt.a endian.h -- cgit v1.2.3 From bf71717573bd7b514651e4cd4a2a68d92b547d5f Mon Sep 17 00:00:00 2001 From: Technion Date: Wed, 19 Feb 2014 08:25:07 +0000 Subject: Fix for issue #8. --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 480d66f..71aca0e 100644 --- a/Makefile +++ b/Makefile @@ -13,11 +13,11 @@ all: reference OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o -library: $(OBJS) +libscrypt.so.0: $(OBJS) $(CC) $(LDFLAGS) -shared -o libscrypt.so.0 -lc -lm $(OBJS) ar rcs libscrypt.a $(OBJS) -reference: library main.o b64.o +reference: libscrypt.so.0 main.o b64.o ln -s -f libscrypt.so.0 libscrypt.so $(CC) -Wall -o reference main.o b64.o $(CFLAGS_EXTRA) -L. -lscrypt -- cgit v1.2.3 From 13857d9d03e75145a0edb2f8dba5406f181dd6e5 Mon Sep 17 00:00:00 2001 From: Technion Date: Wed, 19 Feb 2014 23:34:02 +0000 Subject: Document OSX installation. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 6ba5f33..565c316 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,12 @@ Building make check Check the Makefile for advice on linking against your application. +OSX +----- +Please compile with: + make LDFLAGS= CFLAGS_EXTRA= + + 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. -- cgit v1.2.3 From 8deaaea13f0a43462ca8db6c0624c5ace1e20897 Mon Sep 17 00:00:00 2001 From: Technion Date: Wed, 19 Feb 2014 23:45:23 +0000 Subject: Documentation formatting. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 565c316..910b681 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ Check the Makefile for advice on linking against your application. OSX ----- Please compile with: + make LDFLAGS= CFLAGS_EXTRA= @@ -53,7 +54,6 @@ 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 Fedora, CentOS, FreeBSD 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 -- cgit v1.2.3 From 8820599fbfa66b422d437bc59e06bedc5c462ccc Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 26 Feb 2014 16:19:52 -0800 Subject: Fixed make install. Fixes #10 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 71aca0e..0d5281c 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ devtest: splint crypto-scrypt-saltgen.c +posixlib valgrind ./reference -install: library +install: libscrypt.so.0 $(MAKE_DIR) $(DESTDIR) $(DESTDIR)$(PREFIX) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR) $(INSTALL_DATA) -pm 0755 libscrypt.so.0 $(DESTDIR)$(LIBDIR) cd $(DESTDIR)$(LIBDIR) && ln -s -f libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so -- cgit v1.2.3 From 3c35dfe431117b4dd72ceb8df25cc2699033d685 Mon Sep 17 00:00:00 2001 From: Technion Date: Sat, 1 Mar 2014 00:58:13 +0000 Subject: Make soname user defined. --- Makefile | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 0d5281c..311d68e 100644 --- a/Makefile +++ b/Makefile @@ -5,20 +5,21 @@ MAKE_DIR ?= install -d INSTALL_DATA ?= install CC?=gcc +SONAME?=libscrypt.so.0 CFLAGS?=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC -LDFLAGS?=-Wl,-z,now -Wl,-z,relro -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version +LDFLAGS?=-Wl,-z,now -Wl,-z,relro -Wl,-soname,$(SONAME) -Wl,--version-script=libscrypt.version CFLAGS_EXTRA?=-Wl,-rpath=. all: reference OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o -libscrypt.so.0: $(OBJS) - $(CC) $(LDFLAGS) -shared -o libscrypt.so.0 -lc -lm $(OBJS) +$(SONAME): $(OBJS) + $(CC) $(LDFLAGS) -shared -o $(SONAME) -lc -lm $(OBJS) ar rcs libscrypt.a $(OBJS) -reference: libscrypt.so.0 main.o b64.o - ln -s -f libscrypt.so.0 libscrypt.so +reference: $(SONAME) main.o b64.o + ln -s -f $(SONAME) libscrypt.so $(CC) -Wall -o reference main.o b64.o $(CFLAGS_EXTRA) -L. -lscrypt clean: @@ -33,10 +34,10 @@ devtest: splint crypto-scrypt-saltgen.c +posixlib valgrind ./reference -install: libscrypt.so.0 +install: $(SONAME) $(MAKE_DIR) $(DESTDIR) $(DESTDIR)$(PREFIX) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR) - $(INSTALL_DATA) -pm 0755 libscrypt.so.0 $(DESTDIR)$(LIBDIR) - cd $(DESTDIR)$(LIBDIR) && ln -s -f libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so + $(INSTALL_DATA) -pm 0755 $(SONAME) $(DESTDIR)$(LIBDIR) + cd $(DESTDIR)$(LIBDIR) && ln -s -f $(SONAME) $(DESTDIR)$(LIBDIR)/libscrypt.so $(INSTALL_DATA) -pm 0644 libscrypt.h $(DESTDIR)$(INCLUDEDIR) install-static: libscrypt.a -- cgit v1.2.3 From 0be839fb2bce1e9c2b04d487d4ba7a93c7e79c3c Mon Sep 17 00:00:00 2001 From: Technion Date: Sat, 1 Mar 2014 01:44:49 +0000 Subject: Revert "Make soname user defined." This reverts commit 3c35dfe431117b4dd72ceb8df25cc2699033d685. --- Makefile | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 311d68e..0d5281c 100644 --- a/Makefile +++ b/Makefile @@ -5,21 +5,20 @@ MAKE_DIR ?= install -d INSTALL_DATA ?= install CC?=gcc -SONAME?=libscrypt.so.0 CFLAGS?=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC -LDFLAGS?=-Wl,-z,now -Wl,-z,relro -Wl,-soname,$(SONAME) -Wl,--version-script=libscrypt.version +LDFLAGS?=-Wl,-z,now -Wl,-z,relro -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version CFLAGS_EXTRA?=-Wl,-rpath=. all: reference OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o -$(SONAME): $(OBJS) - $(CC) $(LDFLAGS) -shared -o $(SONAME) -lc -lm $(OBJS) +libscrypt.so.0: $(OBJS) + $(CC) $(LDFLAGS) -shared -o libscrypt.so.0 -lc -lm $(OBJS) ar rcs libscrypt.a $(OBJS) -reference: $(SONAME) main.o b64.o - ln -s -f $(SONAME) libscrypt.so +reference: libscrypt.so.0 main.o b64.o + ln -s -f libscrypt.so.0 libscrypt.so $(CC) -Wall -o reference main.o b64.o $(CFLAGS_EXTRA) -L. -lscrypt clean: @@ -34,10 +33,10 @@ devtest: splint crypto-scrypt-saltgen.c +posixlib valgrind ./reference -install: $(SONAME) +install: libscrypt.so.0 $(MAKE_DIR) $(DESTDIR) $(DESTDIR)$(PREFIX) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR) - $(INSTALL_DATA) -pm 0755 $(SONAME) $(DESTDIR)$(LIBDIR) - cd $(DESTDIR)$(LIBDIR) && ln -s -f $(SONAME) $(DESTDIR)$(LIBDIR)/libscrypt.so + $(INSTALL_DATA) -pm 0755 libscrypt.so.0 $(DESTDIR)$(LIBDIR) + cd $(DESTDIR)$(LIBDIR) && ln -s -f libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so $(INSTALL_DATA) -pm 0644 libscrypt.h $(DESTDIR)$(INCLUDEDIR) install-static: libscrypt.a -- cgit v1.2.3 From cbc7da8acd2a08cae026427cf08943249e49c69e Mon Sep 17 00:00:00 2001 From: Technion Date: Sat, 1 Mar 2014 01:46:45 +0000 Subject: Another attempt at OSX dynamic libraries --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Makefile b/Makefile index 0d5281c..b460553 100644 --- a/Makefile +++ b/Makefile @@ -39,5 +39,11 @@ install: libscrypt.so.0 cd $(DESTDIR)$(LIBDIR) && ln -s -f libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so $(INSTALL_DATA) -pm 0644 libscrypt.h $(DESTDIR)$(INCLUDEDIR) +install-osx: libscrypt.so.0 + $(MAKE_DIR) $(DESTDIR) $(DESTDIR)$(PREFIX) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR) + $(INSTALL_DATA) -pm 0755 libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.0.dylib + cd $(DESTDIR)$(LIBDIR) && ln -s -f libscrypt.0.dylib $(DESTDIR)$(LIBDIR)/libscrypt.dylib + $(INSTALL_DATA) -pm 0644 libscrypt.h $(DESTDIR)$(INCLUDEDIR) + install-static: libscrypt.a $(INSTALL_DATA) -pm 0644 libscrypt.a $(DESTDIR)$(LIBDIR) -- cgit v1.2.3 From bcfa3d93faeab9b34788ffafe1f47c65cc166eb8 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 3 Mar 2014 02:18:24 +0000 Subject: Fix for #13 --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 910b681..1213327 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,9 @@ 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) + 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, /*@out@*/ uint8_t *buf, size_t buflen); Sane constants have been created for N, r and p so you can create a has like this: -- cgit v1.2.3 From 6ca43b25db582f038923e12432bba325318fe4d3 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 3 Mar 2014 02:21:44 +0000 Subject: Correction related to #13 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1213327..3ac6c4a 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ A hash can be generated using the following function: 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); + libscrypt_hash(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: -- cgit v1.2.3 From b802747d4d3106e10f8c97f84f577a7f0fe1b867 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 3 Mar 2014 02:25:35 +0000 Subject: Further README updates regarding API --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3ac6c4a..4bb11f9 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,16 @@ http://www.lolware.net/libscrypt.html Simple hashing interface -A hash can be generated using the following function: +The (reference) internal hashing function can be directly called as follows: 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, /*@out@*/ uint8_t *buf, size_t buflen); +Libscrypt's easier to use interface wraps this up to deal with the salt and produce BASE64 output as so: + + int libscrypt_hash(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_hash(outbuf, "My cats's breath smells like cat food", SCRYPT_N, SCRYPT_r, SCRYPT_p); -- cgit v1.2.3 From 54e2a61c8f75fb909d2c64c48a8e7cba854dfcda Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 6 Mar 2014 12:53:54 +1100 Subject: Fix for Ubuntuism, issue #14 --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b460553..7dbb3a1 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ all: reference OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o libscrypt.so.0: $(OBJS) - $(CC) $(LDFLAGS) -shared -o libscrypt.so.0 -lc -lm $(OBJS) + $(CC) $(LDFLAGS) -shared -o libscrypt.so.0 $(OBJS) -lm -lc ar rcs libscrypt.a $(OBJS) reference: libscrypt.so.0 main.o b64.o -- cgit v1.2.3 From dbd04dde129bb8a37c3990c88c8391c66c2a355b Mon Sep 17 00:00:00 2001 From: Ayrx Date: Sun, 9 Mar 2014 14:53:22 +0800 Subject: Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4bb11f9..823d4e4 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Sane constants have been created for N, r and p so you can create a has like thi 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 = libscrypt_check(mcf, "pleasefailme"); retval < 0 error retval = 0 password incorrect retval > 0 pass -- cgit v1.2.3 From 35b689457448ceb7781834942c0db2e6dc058359 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 11 Mar 2014 07:46:43 +0000 Subject: New version description --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 823d4e4..77c3454 100644 --- a/README.md +++ b/README.md @@ -72,3 +72,6 @@ v1.12: The static library is built, but no longer installed by default. You can v1.13: Minor packaging related update v1.15: Replaced the b64 libraries with more portable one from ISC. Now tested and verified on a wider variety of architectures. Note, libscrypt_b64_encrypt was originally an exported function. This is no longer the case as it is considered an internal function only. + +v1.18: God damnit Apple + -- cgit v1.2.3 From 202c76497ec1c8963125683fd4117535b6ad6ea0 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 11 Mar 2014 08:26:52 +0000 Subject: Doc update for OSX --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 77c3454..51ecee2 100644 --- a/README.md +++ b/README.md @@ -46,9 +46,10 @@ Check the Makefile for advice on linking against your application. OSX ----- -Please compile with: +Please compile and install with: make LDFLAGS= CFLAGS_EXTRA= + make install-osx BUGS -- cgit v1.2.3 From a15b65eea1f374e2a1576c784d33750908ae17a8 Mon Sep 17 00:00:00 2001 From: Ayrx Date: Wed, 9 Apr 2014 23:13:28 +0800 Subject: Fixed salt gen to use CSPRNG --- crypto-scrypt-saltgen.c | 81 +++++++++++++++++++++++++++++++++++-------------- crypto_scrypt-hash.c | 15 +++++---- libscrypt.h | 6 ++-- 3 files changed, 71 insertions(+), 31 deletions(-) diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index 8b9caf4..7a9583e 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -1,25 +1,62 @@ #include #include -#include - -#include "sha256.h" - - -void libscrypt_salt_gen(uint8_t *rand, size_t len) +#include +#include +#include + +#ifdef _WIN32 +#include +#include +#else +#include +#endif + +int libscrypt_salt_gen(uint8_t *salt, 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(¤t_time); - SHA256_Update(&ctx, c_time_string, strlen(c_time_string)); - SHA256_Final(buf, &ctx); - - memcpy(rand, buf, len); - -} + unsigned char buf[len]; +#ifdef _WIN32 + static HCRYPTPROV provider; + if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) + { + return -1; + } + + if (!CryptGenRandom(provider, len, buf)) + { + return -1; + } + + if(!CryptReleaseContext(provider, 0)) { + return -1; + } +#else + int data_read = 0; + int urandom = open("/dev/urandom", O_RDONLY); + + if (urandom < 0) + { + return -1; + } + + while (data_read < len) { + ssize_t result = read(urandom, buf, len); + + if (result < 0) + { + if (errno == EINTR) { + continue; + } + + else { + return -1; + } + } + + data_read += result; + } + + close(urandom); +#endif + memcpy(salt, buf, len); + return 0; +} \ No newline at end of file diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c index e5384ca..675de17 100644 --- a/crypto_scrypt-hash.c +++ b/crypto_scrypt-hash.c @@ -15,23 +15,26 @@ int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p char outbuf[256]; char saltbuf[256]; - libscrypt_salt_gen(salt, 16); + if(libscrypt_salt_gen(salt, 16) == -1) + { + return 0; + } 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((unsigned char*)hashbuf, sizeof(hashbuf), - outbuf, sizeof(outbuf)); + retval = libscrypt_b64_encode((unsigned char*)hashbuf, sizeof(hashbuf), + outbuf, sizeof(outbuf)); if(retval == -1) return 0; - retval = libscrypt_b64_encode((unsigned char *)salt, sizeof(salt), - saltbuf, sizeof(saltbuf)); + retval = libscrypt_b64_encode((unsigned char *)salt, sizeof(salt), + saltbuf, sizeof(saltbuf)); if(retval == -1) return 0; - retval = libscrypt_mcf(N, r, p, saltbuf, outbuf, dst); + retval = libscrypt_mcf(N, r, p, saltbuf, outbuf, dst); if(retval == -1) return 0; diff --git a/libscrypt.h b/libscrypt.h index 6af01e9..aebc604 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -34,10 +34,10 @@ 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 +/* Generates a salt. Uses CryptGenRandom on Windows and /dev/urandom on + * Unix-based systems. */ -void libscrypt_salt_gen(/*@out@*/ uint8_t *rand, size_t len); +int libscrypt_salt_gen(/*@out@*/ uint8_t *rand, size_t len); /* Checks a given MCF against a password */ int libscrypt_check(char *mcf, char *password); -- cgit v1.2.3 From 3bf4a159e1a2bb7cac01f49e92f54fb483c5cf17 Mon Sep 17 00:00:00 2001 From: Ayrx Date: Thu, 10 Apr 2014 15:21:22 +0800 Subject: Removed Windows code --- crypto-scrypt-saltgen.c | 23 ----------------------- libscrypt.h | 3 +-- 2 files changed, 1 insertion(+), 25 deletions(-) diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index 7a9583e..08435f8 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -3,33 +3,11 @@ #include #include #include - -#ifdef _WIN32 -#include -#include -#else #include -#endif int libscrypt_salt_gen(uint8_t *salt, size_t len) { unsigned char buf[len]; -#ifdef _WIN32 - static HCRYPTPROV provider; - if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) - { - return -1; - } - - if (!CryptGenRandom(provider, len, buf)) - { - return -1; - } - - if(!CryptReleaseContext(provider, 0)) { - return -1; - } -#else int data_read = 0; int urandom = open("/dev/urandom", O_RDONLY); @@ -56,7 +34,6 @@ int libscrypt_salt_gen(uint8_t *salt, size_t len) } close(urandom); -#endif memcpy(salt, buf, len); return 0; } \ No newline at end of file diff --git a/libscrypt.h b/libscrypt.h index aebc604..86c5f7b 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -34,8 +34,7 @@ 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. Uses CryptGenRandom on Windows and /dev/urandom on - * Unix-based systems. +/* Generates a salt. Uses /dev/urandom/ */ int libscrypt_salt_gen(/*@out@*/ uint8_t *rand, size_t len); -- cgit v1.2.3 From c5a66e4c54f44ab4bdb6a84c25b047221de87e0a Mon Sep 17 00:00:00 2001 From: Ayrx Date: Thu, 10 Apr 2014 18:34:05 +0800 Subject: Changed data_read from int to size_t --- crypto-scrypt-saltgen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index 08435f8..3db0eb3 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -8,7 +8,7 @@ int libscrypt_salt_gen(uint8_t *salt, size_t len) { unsigned char buf[len]; - int data_read = 0; + size_t data_read = 0; int urandom = open("/dev/urandom", O_RDONLY); if (urandom < 0) -- cgit v1.2.3 From 5e0d669a4d94fbc4eb50aefb220a1272937cb619 Mon Sep 17 00:00:00 2001 From: Ayrx Date: Thu, 10 Apr 2014 20:00:10 +0800 Subject: Fixed error with read() --- crypto-scrypt-saltgen.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index 3db0eb3..bea6221 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -17,11 +17,11 @@ int libscrypt_salt_gen(uint8_t *salt, size_t len) } while (data_read < len) { - ssize_t result = read(urandom, buf, len); + ssize_t result = read(urandom, (unsigned char*)buf + data_read, len - data_read); if (result < 0) { - if (errno == EINTR) { + if (errno == EINTR || errno == EAGAIN) { continue; } -- cgit v1.2.3 From ca8fa2e6e8c6a88d3d777f390623a394c0e9e4b3 Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 10 Apr 2014 12:18:13 +0000 Subject: Fixed test hardness bug identified during #16 --- main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.c b/main.c index 1c5b43b..f16569a 100644 --- a/main.c +++ b/main.c @@ -191,7 +191,7 @@ int main() /* TODO: I'm not presently sure how this function could fail */ libscrypt_salt_gen((uint8_t*)saltbuf, 16); - retval = libscrypt_b64_encode((uint8_t*)saltbuf, 16, saltbuf, sizeof(saltbuf)); + retval = libscrypt_b64_encode((uint8_t*)saltbuf, 16, outbuf, sizeof(outbuf)); if(retval == -1) { printf("TEST ELEVEN FAILED\n"); -- cgit v1.2.3 From 8ce09010450a00d7e227455a2a8b610ff1f2f9dc Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 10 Apr 2014 12:21:11 +0000 Subject: Better use of #define --- crypto-scrypt-saltgen.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index bea6221..039faa6 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -5,11 +5,13 @@ #include #include +#define RNGDEV "/dev/urandom" + int libscrypt_salt_gen(uint8_t *salt, size_t len) { unsigned char buf[len]; size_t data_read = 0; - int urandom = open("/dev/urandom", O_RDONLY); + int urandom = open(RNGDEV, O_RDONLY); if (urandom < 0) { @@ -36,4 +38,4 @@ int libscrypt_salt_gen(uint8_t *salt, size_t len) close(urandom); memcpy(salt, buf, len); return 0; -} \ No newline at end of file +} -- cgit v1.2.3 From e2bd96966e24e90ae42e49bbc37a7986dee57ed4 Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 10 Apr 2014 12:21:58 +0000 Subject: I don't know why I thought this cast was necessary --- crypto-scrypt-saltgen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index 039faa6..c3f2978 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -19,7 +19,7 @@ int libscrypt_salt_gen(uint8_t *salt, size_t len) } while (data_read < len) { - ssize_t result = read(urandom, (unsigned char*)buf + data_read, len - data_read); + ssize_t result = read(urandom, buf + data_read, len - data_read); if (result < 0) { -- cgit v1.2.3 From f2b63dedfa40e37d5c02ddb4441a5e5ab474a55b Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 15 Apr 2014 00:53:09 +0000 Subject: Address splint crash under new code --- Makefile | 2 +- crypto-scrypt-saltgen.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 7dbb3a1..fdc0347 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ check: all devtest: splint crypto_scrypt-hexconvert.c splint crypto-mcf.c crypto_scrypt-check.c crypto_scrypt-hash.c - splint crypto-scrypt-saltgen.c +posixlib + splint crypto-scrypt-saltgen.c +posixlib -compdef valgrind ./reference install: libscrypt.so.0 diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index c3f2978..5c747c1 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -1,10 +1,13 @@ #include #include -#include #include #include #include +#ifndef S_SPLINT_S /* Including this here triggers a known bug in splint */ +#include +#endif + #define RNGDEV "/dev/urandom" int libscrypt_salt_gen(uint8_t *salt, size_t len) @@ -35,7 +38,10 @@ int libscrypt_salt_gen(uint8_t *salt, size_t len) data_read += result; } - close(urandom); + /* Failures on close() shouldn't occur with O_RDONLY */ + (void)close(urandom); + memcpy(salt, buf, len); + return 0; } -- cgit v1.2.3 From ed9199a3b65b787def6fb4a956a863a5bb8f88a7 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 15 Apr 2014 00:58:46 +0000 Subject: Remove int -> unsigned int comparisons. --- b64.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/b64.c b/b64.c index 8453cdd..edce1b8 100644 --- a/b64.c +++ b/b64.c @@ -129,7 +129,7 @@ libscrypt_b64_encode(src, srclength, target, targsize) size_t datalength = 0; u_char input[3]; u_char output[4]; - int i; + unsigned int i; while (2 < srclength) { input[0] = *src++; @@ -189,7 +189,8 @@ libscrypt_b64_decode(src, target, targsize) u_char *target; size_t targsize; { - int tarindex, state, ch; + int state, ch; + unsigned int tarindex; u_char nextbyte; char *pos; -- cgit v1.2.3 From eeaf35fa036f487999d50f2b4141dc09b7862ff7 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 15 Apr 2014 03:59:34 +0000 Subject: Implement constant-time comparison. --- Makefile | 4 ++-- README.md | 1 + crypto_scrypt-check.c | 12 +++++++++--- slowequals.c | 26 ++++++++++++++++++++++++++ slowequals.h | 5 +++++ 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 slowequals.c create mode 100644 slowequals.h diff --git a/Makefile b/Makefile index fdc0347..a5af9ac 100644 --- a/Makefile +++ b/Makefile @@ -11,13 +11,13 @@ CFLAGS_EXTRA?=-Wl,-rpath=. all: reference -OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o +OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o slowequals.o libscrypt.so.0: $(OBJS) $(CC) $(LDFLAGS) -shared -o libscrypt.so.0 $(OBJS) -lm -lc ar rcs libscrypt.a $(OBJS) -reference: libscrypt.so.0 main.o b64.o +reference: libscrypt.so.0 main.o b64.o slowequals.o ln -s -f libscrypt.so.0 libscrypt.so $(CC) -Wall -o reference main.o b64.o $(CFLAGS_EXTRA) -L. -lscrypt diff --git a/README.md b/README.md index 51ecee2..89c8b43 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ Please compile and install with: 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. +Experiments were performed with using memset() to zero out passwords as they were checked. This often caused issues with calling applications where the password based have been passed as a const*. We highly recommend implementing your own zeroing function the moment this library is called. Notes on Code Development ------------------------ diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index b19ef12..5ecc949 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -4,6 +4,7 @@ #include #include "b64.h" +#include "slowequals.h" #include "libscrypt.h" /* pow() works with doubles. Sounds like it should cast to int correctly, @@ -25,6 +26,11 @@ static uint16_t ipow(uint16_t base, uint32_t exp) int libscrypt_check(char *mcf, char *password) { + /* Return values: + * <0 error + * == 0 password incorrect + * >0 correct password + */ uint32_t params; uint16_t N; @@ -87,11 +93,11 @@ int libscrypt_check(char *mcf, char *password) if ( !tok ) return -1; - if(strcmp(tok, outbuf) == 0) + if(slow_equals(tok, outbuf) == 0) { - return 1; + return 0; } - return 0; + return 1; /* This is the "else" condition */ } diff --git a/slowequals.c b/slowequals.c new file mode 100644 index 0000000..48e488e --- /dev/null +++ b/slowequals.c @@ -0,0 +1,26 @@ +#include + +/* Implements a constant time version of strcmp() + * Will return 1 if a and b are equal, 0 if they are not */ +int slow_equals(const char* a, const char* b) +{ + size_t lena, lenb, diff, i; + lena = strlen(a); + lenb = strlen(b); + diff = strlen(a) ^ strlen(b); + + for(i=0; i Date: Sat, 26 Apr 2014 22:46:58 +0300 Subject: libscrypt_mcf returns 0 on failure, not -1 --- crypto_scrypt-hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c index 675de17..25130c5 100644 --- a/crypto_scrypt-hash.c +++ b/crypto_scrypt-hash.c @@ -35,7 +35,7 @@ int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p return 0; retval = libscrypt_mcf(N, r, p, saltbuf, outbuf, dst); - if(retval == -1) + if(!retval) return 0; return 1; -- cgit v1.2.3 From c8278a9022f1e447f2f1a6c905bc8c96f329b9a6 Mon Sep 17 00:00:00 2001 From: Jan Varho Date: Sat, 26 Apr 2014 23:23:37 +0300 Subject: Sanity check N > 1 in libscrypt_scrypt --- crypto_scrypt-nosse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto_scrypt-nosse.c b/crypto_scrypt-nosse.c index 845f014..0502302 100644 --- a/crypto_scrypt-nosse.c +++ b/crypto_scrypt-nosse.c @@ -251,7 +251,7 @@ libscrypt_scrypt(const uint8_t * passwd, size_t passwdlen, errno = EFBIG; goto err0; } - if (((N & (N - 1)) != 0) || (N == 0)) { + if (((N & (N - 1)) != 0) || (N < 2)) { errno = EINVAL; goto err0; } -- cgit v1.2.3 From 7fdb7eba40b911b006002dcdda8d6e198fe402f9 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 28 Apr 2014 04:54:07 +0000 Subject: More explicit fix for #18 as per splint advice. --- crypto_scrypt-hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c index 25130c5..a927e24 100644 --- a/crypto_scrypt-hash.c +++ b/crypto_scrypt-hash.c @@ -35,7 +35,7 @@ int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p return 0; retval = libscrypt_mcf(N, r, p, saltbuf, outbuf, dst); - if(!retval) + if(retval != 1) return 0; return 1; -- cgit v1.2.3 From 3ad94144a0d98a82757ba64d8f5ead3ba543b3cc Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 28 Apr 2014 04:54:40 +0000 Subject: Following #16, it's now plausible for this function to fail. --- main.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index f16569a..41f1aba 100644 --- a/main.c +++ b/main.c @@ -188,13 +188,18 @@ int main() 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((uint8_t*)saltbuf, 16); + + retval = libscrypt_salt_gen((uint8_t*)saltbuf, 16); + if(retval == -1) + { + printf("TEST ELEVEN (salt generate) FAILED\n"); + exit(EXIT_FAILURE); + } retval = libscrypt_b64_encode((uint8_t*)saltbuf, 16, outbuf, sizeof(outbuf)); if(retval == -1) { - printf("TEST ELEVEN FAILED\n"); + printf("TEST ELEVEN (b64 encode) FAILED\n"); exit(EXIT_FAILURE); } printf("TEST ELEVEN: SUCCESSFUL, Generated %s\n", outbuf); -- cgit v1.2.3 From 8f8d9c5de12e9ca9c32e424cafec3c85d0c272b7 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 28 Apr 2014 06:40:33 +0000 Subject: Use existing define instead of magic number --- crypto_scrypt-hash.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c index a927e24..7c3b59e 100644 --- a/crypto_scrypt-hash.c +++ b/crypto_scrypt-hash.c @@ -11,7 +11,7 @@ int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p int retval; uint8_t salt[16]; - uint8_t hashbuf[64]; + uint8_t hashbuf[SCRYPT_HASH_LEN]; char outbuf[256]; char saltbuf[256]; -- cgit v1.2.3 From c6b516efbfde7fd30d14cdfe5ea6c267f413bcad Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 28 Apr 2014 06:45:12 +0000 Subject: Define and use SCRYPT_SALT_LEN to 16 --- crypto_scrypt-hash.c | 6 +++--- libscrypt.h | 1 + main.c | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c index 7c3b59e..4493f02 100644 --- a/crypto_scrypt-hash.c +++ b/crypto_scrypt-hash.c @@ -10,17 +10,17 @@ int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p { int retval; - uint8_t salt[16]; + uint8_t salt[SCRYPT_SALT_LEN]; uint8_t hashbuf[SCRYPT_HASH_LEN]; char outbuf[256]; char saltbuf[256]; - if(libscrypt_salt_gen(salt, 16) == -1) + if(libscrypt_salt_gen(salt, SCRYPT_SALT_LEN) == -1) { return 0; } - retval = libscrypt_scrypt((uint8_t*)passphrase,strlen(passphrase), (uint8_t*)salt, sizeof(salt), N, r, p, hashbuf, sizeof(hashbuf)); + retval = libscrypt_scrypt((uint8_t*)passphrase,strlen(passphrase), (uint8_t*)salt, SCRYPT_SALT_LEN, N, r, p, hashbuf, sizeof(hashbuf)); if(retval == -1) return 0; diff --git a/libscrypt.h b/libscrypt.h index 86c5f7b..7906791 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -49,6 +49,7 @@ int libscrypt_hash(char *dst, char* passphrase, uint32_t N, uint8_t r, uint8_t p #define SCRYPT_HASH_LEN 64 /* This can be user defined - *but 64 is the reference size */ +#define SCRYPT_SALT_LEN 16 /* This is just a recommended size */ #define SCRYPT_MCF_LEN 125 /* mcf is 120 byte + nul */ #define SCRYPT_MCF_ID "$s1" #define SCRYPT_N 16384 diff --git a/main.c b/main.c index 41f1aba..91b802b 100644 --- a/main.c +++ b/main.c @@ -189,14 +189,14 @@ int main() printf("TEST ELEVEN: Testing salt generator\n"); - retval = libscrypt_salt_gen((uint8_t*)saltbuf, 16); + retval = libscrypt_salt_gen((uint8_t*)saltbuf, SCRYPT_SALT_LEN); if(retval == -1) { printf("TEST ELEVEN (salt generate) FAILED\n"); exit(EXIT_FAILURE); } - retval = libscrypt_b64_encode((uint8_t*)saltbuf, 16, outbuf, sizeof(outbuf)); + retval = libscrypt_b64_encode((uint8_t*)saltbuf, SCRYPT_SALT_LEN, outbuf, sizeof(outbuf)); if(retval == -1) { printf("TEST ELEVEN (b64 encode) FAILED\n"); -- cgit v1.2.3 From b7ab038150c16605cab826a68a279b0dcf6bb46d Mon Sep 17 00:00:00 2001 From: Jan Varho Date: Tue, 29 Apr 2014 13:33:06 +0300 Subject: Check that r and p are not zero before dividing by them These parameters are required to be positive by scrypt.pdf and upstream checks them before handing them to the scrypt function. --- crypto_scrypt-nosse.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crypto_scrypt-nosse.c b/crypto_scrypt-nosse.c index 0502302..31e695d 100644 --- a/crypto_scrypt-nosse.c +++ b/crypto_scrypt-nosse.c @@ -251,6 +251,10 @@ libscrypt_scrypt(const uint8_t * passwd, size_t passwdlen, errno = EFBIG; goto err0; } + if (r == 0 || p == 0) { + errno = EINVAL; + goto err0; + } if (((N & (N - 1)) != 0) || (N < 2)) { errno = EINVAL; goto err0; -- cgit v1.2.3 From bd2feddee8fe7407d404c517bccc8068e3dfb3f8 Mon Sep 17 00:00:00 2001 From: Technion Date: Sun, 4 May 2014 23:48:34 +0000 Subject: hexconvert only exists for the test harness. Library functions all use BASE64. Don't export it. --- Makefile | 4 ++-- crypto_scrypt-hexconvert.c | 5 +++++ libscrypt.version | 1 - main.c | 1 + 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index a5af9ac..7474901 100644 --- a/Makefile +++ b/Makefile @@ -17,9 +17,9 @@ libscrypt.so.0: $(OBJS) $(CC) $(LDFLAGS) -shared -o libscrypt.so.0 $(OBJS) -lm -lc ar rcs libscrypt.a $(OBJS) -reference: libscrypt.so.0 main.o b64.o slowequals.o +reference: libscrypt.so.0 main.o b64.o crypto_scrypt-hexconvert.o ln -s -f libscrypt.so.0 libscrypt.so - $(CC) -Wall -o reference main.o b64.o $(CFLAGS_EXTRA) -L. -lscrypt + $(CC) -Wall -o reference main.o b64.o crypto_scrypt-hexconvert.o $(CFLAGS_EXTRA) -L. -lscrypt clean: rm -f *.o reference libscrypt.so* libscrypt.a endian.h diff --git a/crypto_scrypt-hexconvert.c b/crypto_scrypt-hexconvert.c index ececbd9..3df12a0 100644 --- a/crypto_scrypt-hexconvert.c +++ b/crypto_scrypt-hexconvert.c @@ -3,6 +3,11 @@ #include #include +/* The hexconvert function is only used to test reference vectors against + * known answers. The contents of this file are therefore a component + * to assist with test harnesses only + */ + int libscrypt_hexconvert(uint8_t *buf, size_t s, char *outbuf, size_t obs) { diff --git a/libscrypt.version b/libscrypt.version index 6ad45dc..9cc574d 100644 --- a/libscrypt.version +++ b/libscrypt.version @@ -1,7 +1,6 @@ libscrypt { global: libscrypt_check; libscrypt_hash; -libscrypt_hexconvert; libscrypt_mcf; libscrypt_salt_gen; libscrypt_scrypt; diff --git a/main.c b/main.c index 91b802b..4b165e5 100644 --- a/main.c +++ b/main.c @@ -112,6 +112,7 @@ int main() * Correct buffer length can be determined using the below function if retuired. * char* dest = (char*) malloc(modp_b64_encode_len); + * Note that this is not an exported function */ printf("TEST SEVEN: BASE64 encoding the salt and hash output\n"); -- cgit v1.2.3 From 77ff7cd4fb2cc3994981576f8d6054d7f9664bc9 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 5 May 2014 00:11:42 +0000 Subject: Add details for GPG key and Coverity scan. --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 89c8b43..deb88b2 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,14 @@ The reference machines are Fedora, CentOS, FreeBSD and Raspbian, and the code is 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 +Contact +------- +I can be contacted at: technion@lolware.net +If required, my GPG key can be found at: +https://lolware.net/technion-GPG-KEY +Future releases will have the Git tag signed. + + Changenotes ----------- v1.1a: Single Makefile line change. I wouldn't ordinarily tag this as a new "release", but the purpose here is to assist with packaging in distributions. @@ -77,3 +85,7 @@ v1.15: Replaced the b64 libraries with more portable one from ISC. Now tested an v1.18: God damnit Apple + + Coverity Scan Build Status + -- cgit v1.2.3 From 618dc1570bb7f14835a02f1018452ff6083a1f54 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 5 May 2014 01:10:44 +0000 Subject: README formatting --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index deb88b2..6aaf223 100644 --- a/README.md +++ b/README.md @@ -68,8 +68,9 @@ http://www.lolware.net/libscrypttesting.txt Contact ------- I can be contacted at: technion@lolware.net -If required, my GPG key can be found at: -https://lolware.net/technion-GPG-KEY + +If required, my GPG key can be found at: https://lolware.net/technion-GPG-KEY + Future releases will have the Git tag signed. -- cgit v1.2.3 From 535d2d6ec6487301fd6e39f6a4070390c08b4c25 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 5 May 2014 23:28:09 +0000 Subject: Filehandle leak - uncovered by Coverity --- crypto-scrypt-saltgen.c | 1 + 1 file changed, 1 insertion(+) diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index 5c747c1..2ef1ab2 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -31,6 +31,7 @@ int libscrypt_salt_gen(uint8_t *salt, size_t len) } else { + (void)close(urandom); return -1; } } -- cgit v1.2.3 From 645f974aa44fa00042d20743ab8ec8e8110b7125 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 5 May 2014 23:29:05 +0000 Subject: Paranoid checking - uncovered by Coverity --- crypto_scrypt-check.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index 5ecc949..85ea451 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -48,6 +48,9 @@ int libscrypt_check(char *mcf, char *password) } tok = strtok(mcf, "$"); + if ( !tok ) + return -1; + tok = strtok(NULL, "$"); if ( !tok ) -- cgit v1.2.3 From 526149d4bad9ed5891cd8d304e143abc60f4e504 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 6 May 2014 05:44:59 +0000 Subject: Whitespace fixes --- crypto-scrypt-saltgen.c | 2 +- crypto_scrypt-check.c | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index 2ef1ab2..7bb27a3 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -39,7 +39,7 @@ int libscrypt_salt_gen(uint8_t *salt, size_t len) data_read += result; } - /* Failures on close() shouldn't occur with O_RDONLY */ + /* Failures on close() shouldn't occur with O_RDONLY */ (void)close(urandom); memcpy(salt, buf, len); diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index 85ea451..73eb541 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -26,11 +26,11 @@ static uint16_t ipow(uint16_t base, uint32_t exp) int libscrypt_check(char *mcf, char *password) { - /* Return values: - * <0 error - * == 0 password incorrect - * >0 correct password - */ + /* Return values: + * <0 error + * == 0 password incorrect + * >0 correct password + */ uint32_t params; uint16_t N; @@ -74,7 +74,7 @@ int libscrypt_check(char *mcf, char *password) printf("We've obtained salt 'N' r p of '%s' %d %d %d\n", tok, N,r,p); */ - memset(salt, 0, sizeof(salt)); /* Keeps splint happy */ + memset(salt, 0, sizeof(salt)); /* Keeps splint happy */ retval = libscrypt_b64_decode(tok, (unsigned char*)salt, sizeof(salt)); if (retval < 1) return -1; -- cgit v1.2.3 From 0f053eca64ce1e11e54d7e7af03be99c785c8cf0 Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 6 May 2014 05:57:23 +0000 Subject: More whitespace --- crypto-scrypt-saltgen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index 7bb27a3..a0e2998 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -31,7 +31,7 @@ int libscrypt_salt_gen(uint8_t *salt, size_t len) } else { - (void)close(urandom); + (void)close(urandom); return -1; } } -- cgit v1.2.3 From 457ff867eee5fe8535503d5af0b39b78875f2d7f Mon Sep 17 00:00:00 2001 From: Technion Date: Tue, 6 May 2014 09:42:07 +0000 Subject: Document v1.19 --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 6aaf223..802b51f 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,9 @@ v1.15: Replaced the b64 libraries with more portable one from ISC. Now tested an v1.18: God damnit Apple +v1.19: Code safety cleanups. Now running Coverity. + + Coverity Scan Build Status -- cgit v1.2.3 From f850d6b3af1914361ae6ecbe5decb9474ae1443e Mon Sep 17 00:00:00 2001 From: Jan Varho Date: Tue, 29 Apr 2014 08:25:01 +0300 Subject: Add const to immutable function arguments --- crypto-mcf.c | 3 ++- crypto_scrypt-check.c | 2 +- crypto_scrypt-hash.c | 6 ++++-- libscrypt.h | 11 +++++++---- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/crypto-mcf.c b/crypto-mcf.c index 66e92f0..6011aaf 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -21,7 +21,8 @@ static double scrypt_log2( uint32_t n ) return (log((double)n) / temp); } -int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, char *salt, char *hash, char *mcf) +int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt, + const char *hash, char *mcf) { diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index 73eb541..5c54753 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -24,7 +24,7 @@ static uint16_t ipow(uint16_t base, uint32_t exp) return result; } -int libscrypt_check(char *mcf, char *password) +int libscrypt_check(char *mcf, const char *password) { /* Return values: * <0 error diff --git a/crypto_scrypt-hash.c b/crypto_scrypt-hash.c index 4493f02..4b41007 100644 --- a/crypto_scrypt-hash.c +++ b/crypto_scrypt-hash.c @@ -6,7 +6,8 @@ #include "b64.h" #include "libscrypt.h" -int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p) +int libscrypt_hash(char *dst, const char *passphrase, uint32_t N, uint8_t r, + uint8_t p) { int retval; @@ -20,7 +21,8 @@ int libscrypt_hash(char *dst, char *passphrase, uint32_t N, uint8_t r, uint8_t p return 0; } - retval = libscrypt_scrypt((uint8_t*)passphrase,strlen(passphrase), (uint8_t*)salt, SCRYPT_SALT_LEN, N, r, p, hashbuf, sizeof(hashbuf)); + retval = libscrypt_scrypt((const uint8_t*)passphrase, strlen(passphrase), + (uint8_t*)salt, SCRYPT_SALT_LEN, N, r, p, hashbuf, sizeof(hashbuf)); if(retval == -1) return 0; diff --git a/libscrypt.h b/libscrypt.h index 7906791..59cda83 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -29,20 +29,23 @@ int libscrypt_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_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); +int libscrypt_hexconvert(const 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); +int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt, + const char *hash, char *mcf); /* Generates a salt. Uses /dev/urandom/ */ int libscrypt_salt_gen(/*@out@*/ uint8_t *rand, size_t len); /* Checks a given MCF against a password */ -int libscrypt_check(char *mcf, char *password); +int libscrypt_check(char *mcf, const 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_hash(char *dst, const char* passphrase, uint32_t N, uint8_t r, + uint8_t p); /* Sane default values */ -- cgit v1.2.3 From 287236d6bf79702b3d4dca2907b34e2056a84afb Mon Sep 17 00:00:00 2001 From: Jan Varho Date: Sat, 3 May 2014 12:41:20 +0300 Subject: Document that libscrypt_check modifies mcf --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 802b51f..494c8f3 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Libscrypt's easier to use interface wraps this up to deal with the salt and prod int libscrypt_hash(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: +Sane constants have been created for N, r and p so you can create a hash like this: libscrypt_hash(outbuf, "My cats's breath smells like cat food", SCRYPT_N, SCRYPT_r, SCRYPT_p); @@ -35,6 +35,9 @@ Output stored in "outbuf" is stored in a standardised MCF form, which means incl mcf should be defined as at least SCRYPT_MCF_LEN in size. +Note that libscrypt_check needs to modify the mcf string and will not return it +to the original state. Pass it a copy if you need to keep the original mcf. + 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. -- cgit v1.2.3 From 6abf9d1ec935d5a1f5b22b1f0088fd17242affb6 Mon Sep 17 00:00:00 2001 From: Jan Varho Date: Thu, 8 May 2014 13:25:44 +0300 Subject: Remove unexported libscrypt_hexconvert from libscrypt.h --- crypto_scrypt-hexconvert.h | 9 +++++++++ libscrypt.h | 7 ------- main.c | 1 + 3 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 crypto_scrypt-hexconvert.h diff --git a/crypto_scrypt-hexconvert.h b/crypto_scrypt-hexconvert.h new file mode 100644 index 0000000..8175b24 --- /dev/null +++ b/crypto_scrypt-hexconvert.h @@ -0,0 +1,9 @@ + +#include + +/** + * Converts a binary string to a hex representation of that string + * outbuf must have size of at least buf * 2 + 1. + */ +int libscrypt_hexconvert(const uint8_t *buf, size_t s, char *outbuf, + size_t obs); diff --git a/libscrypt.h b/libscrypt.h index 59cda83..b301457 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -25,13 +25,6 @@ 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(const 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, const char *salt, const char *hash, char *mcf); diff --git a/main.c b/main.c index 4b165e5..cf60c05 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,7 @@ #include #include "b64.h" +#include "crypto_scrypt-hexconvert.h" #include "libscrypt.h" #define REF1 "fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b3731622eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640" -- cgit v1.2.3 From 8372a423175eba9cbd671bbe00c37db8cc01409d Mon Sep 17 00:00:00 2001 From: Jan Varho Date: Thu, 8 May 2014 13:29:27 +0300 Subject: Don't link crypto_scrypt-hexconvert either --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 7474901..5691188 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ CFLAGS_EXTRA?=-Wl,-rpath=. all: reference -OBJS= crypto_scrypt-nosse.o sha256.o crypto_scrypt-hexconvert.o crypto-mcf.o b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o slowequals.o +OBJS= crypto_scrypt-nosse.o sha256.o crypto-mcf.o b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o slowequals.o libscrypt.so.0: $(OBJS) $(CC) $(LDFLAGS) -shared -o libscrypt.so.0 $(OBJS) -lm -lc -- cgit v1.2.3 From 00de3aec01e8c8fb9940f048c50e2c61849ddd5e Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 9 May 2014 05:23:18 +0000 Subject: N should be 64 bit, but define a sane maximum. --- crypto_scrypt-check.c | 6 +++++- libscrypt.h | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index 5c54753..81bc12e 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -33,7 +33,7 @@ int libscrypt_check(char *mcf, const char *password) */ uint32_t params; - uint16_t N; + uint64_t N; uint8_t r, p; int retval; uint8_t hashbuf[64]; @@ -68,6 +68,10 @@ int libscrypt_check(char *mcf, const char *password) p = params & 0xff; r = (params >> 8) & 0xff; N = params >> 16; + + if (N > SCRYPT_SAFE_N) + return -1; + N = ipow(2, N); /* Useful debugging: diff --git a/libscrypt.h b/libscrypt.h index b301457..7c009fb 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -45,6 +45,9 @@ int libscrypt_hash(char *dst, const char* passphrase, uint32_t N, uint8_t r, #define SCRYPT_HASH_LEN 64 /* This can be user defined - *but 64 is the reference size */ +#define SCRYPT_SAFE_N 32 /* This is much higher than you want. It's just + * a blocker for insane defines + */ #define SCRYPT_SALT_LEN 16 /* This is just a recommended size */ #define SCRYPT_MCF_LEN 125 /* mcf is 120 byte + nul */ #define SCRYPT_MCF_ID "$s1" -- cgit v1.2.3 From d14aa0ef229f574e2607ea8aaca8cb8d1d2446b7 Mon Sep 17 00:00:00 2001 From: Jan Varho Date: Sat, 3 May 2014 23:25:34 +0300 Subject: Integer based scrypt_ilog2 --- crypto-mcf.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/crypto-mcf.c b/crypto-mcf.c index 6011aaf..8ea1844 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -6,19 +6,22 @@ #include +#ifndef S_SPLINT_S /* Including this here triggers a known bug in splint */ +#include +#endif + #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); +/* ilog2 for powers of two */ +static int scrypt_ilog2(uint32_t n) +{ + /* Check for a valid power of two */ + if (n < 2 || (n & (n - 1))) + return -1; + int t = 1; + while ((1 << t) < n) + t++; + return t; } int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt, @@ -27,8 +30,7 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt, uint32_t params; - int s; - double t, t2, fracpart; + int s, t; if(!mcf || !hash) return 0; @@ -39,15 +41,8 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt, 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) + t = scrypt_ilog2(N); + if (t < 1) return 0; params = (r << 8) + p; -- cgit v1.2.3 From cda230377d9954e7df6ef278a60cd60f3ca18eae Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 9 May 2014 05:28:08 +0000 Subject: Applied sanity check of N --- crypto-mcf.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crypto-mcf.c b/crypto-mcf.c index 8ea1844..2d0cff3 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -20,7 +20,12 @@ static int scrypt_ilog2(uint32_t n) return -1; int t = 1; while ((1 << t) < n) + { + if(t > SCRYPT_SAFE_N) + return -1; /* Check for insanity */ t++; + } + return t; } -- cgit v1.2.3 From a86d8b6aa142496e8370d46b921287468ea9b71c Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 9 May 2014 05:48:03 +0000 Subject: Fix for #21 --- libscrypt.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libscrypt.h b/libscrypt.h index 7c009fb..1c559c4 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -45,7 +45,7 @@ int libscrypt_hash(char *dst, const char* passphrase, uint32_t N, uint8_t r, #define SCRYPT_HASH_LEN 64 /* This can be user defined - *but 64 is the reference size */ -#define SCRYPT_SAFE_N 32 /* This is much higher than you want. It's just +#define SCRYPT_SAFE_N 30 /* This is much higher than you want. It's just * a blocker for insane defines */ #define SCRYPT_SALT_LEN 16 /* This is just a recommended size */ -- cgit v1.2.3 From 9a769694658625ba2ee5c9dce7ccfd0e42223a96 Mon Sep 17 00:00:00 2001 From: Technion Date: Fri, 20 Jun 2014 09:22:15 +0000 Subject: Fix #22 --- libscrypt.h | 1 + 1 file changed, 1 insertion(+) diff --git a/libscrypt.h b/libscrypt.h index 1c559c4..e25b86c 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -37,6 +37,7 @@ int libscrypt_salt_gen(/*@out@*/ uint8_t *rand, size_t len); int libscrypt_check(char *mcf, const char *password); /* Creates a hash of a passphrase using a randomly generated salt */ +/* Returns >0 on success, or 0 for fail */ int libscrypt_hash(char *dst, const char* passphrase, uint32_t N, uint8_t r, uint8_t p); -- cgit v1.2.3 From 7cd8a4fa3d062c5bf38d0d709468a414a6d32773 Mon Sep 17 00:00:00 2001 From: Nick Parker Date: Mon, 7 Jul 2014 10:13:24 -0500 Subject: Return actual error code in failure state --- crypto_scrypt-nosse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crypto_scrypt-nosse.c b/crypto_scrypt-nosse.c index 31e695d..6d183ae 100644 --- a/crypto_scrypt-nosse.c +++ b/crypto_scrypt-nosse.c @@ -227,7 +227,7 @@ smix(uint8_t * B, size_t r, uint64_t N, uint32_t * V, uint32_t * XY) * 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. + * Return 0 on success; or error code defined by errno.h. */ int libscrypt_scrypt(const uint8_t * passwd, size_t passwdlen, @@ -337,5 +337,5 @@ err1: free(B0); err0: /* Failure! */ - return (-1); + return (errno); } -- cgit v1.2.3 From 22813d5cc85f7c4b522ea1d660367862fb0c603f Mon Sep 17 00:00:00 2001 From: Markus Kolb Date: Thu, 17 Jul 2014 10:16:20 +0200 Subject: Renamed SHA256 functions to support additional linking with OpenSSL --- crypto_scrypt-nosse.c | 4 ++-- sha256.c | 54 +++++++++++++++++++++++++-------------------------- sha256.h | 18 ++++++++--------- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/crypto_scrypt-nosse.c b/crypto_scrypt-nosse.c index 6d183ae..f54e42e 100644 --- a/crypto_scrypt-nosse.c +++ b/crypto_scrypt-nosse.c @@ -307,7 +307,7 @@ libscrypt_scrypt(const uint8_t * passwd, size_t passwdlen, #endif /* 1: (B_0 ... B_{p-1}) <-- PBKDF2(P, S, 1, p * MFLen) */ - PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, 1, B, p * 128 * r); + libscrypt_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++) { @@ -316,7 +316,7 @@ libscrypt_scrypt(const uint8_t * passwd, size_t passwdlen, } /* 5: DK <-- PBKDF2(P, B, 1, dkLen) */ - PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen); + libscrypt_PBKDF2_SHA256(passwd, passwdlen, B, p * 128 * r, 1, buf, buflen); /* Free memory. */ #ifdef MAP_ANON diff --git a/sha256.c b/sha256.c index d2f915f..279e3cf 100644 --- a/sha256.c +++ b/sha256.c @@ -203,15 +203,15 @@ SHA256_Pad(SHA256_CTX * ctx) /* 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); + libscrypt_SHA256_Update(ctx, PAD, (size_t)plen); /* Add the terminating bit-count */ - SHA256_Update(ctx, len, 8); + libscrypt_SHA256_Update(ctx, len, 8); } /* SHA-256 initialization. Begins a SHA-256 operation. */ void -SHA256_Init(SHA256_CTX * ctx) +libscrypt_SHA256_Init(SHA256_CTX * ctx) { /* Zero bits processed so far */ @@ -230,7 +230,7 @@ SHA256_Init(SHA256_CTX * ctx) /* Add bytes into the hash */ void -SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len) +libscrypt_SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len) { uint32_t bitlen[2]; uint32_t r; @@ -276,7 +276,7 @@ SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len) * and clears the context state. */ void -SHA256_Final(unsigned char digest[32], SHA256_CTX * ctx) +libscrypt_SHA256_Final(unsigned char digest[32], SHA256_CTX * ctx) { /* Add padding */ @@ -291,7 +291,7 @@ SHA256_Final(unsigned char digest[32], SHA256_CTX * ctx) /* Initialize an HMAC-SHA256 operation with the given key. */ void -HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen) +libscrypt_HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen) { unsigned char pad[64]; unsigned char khash[32]; @@ -300,26 +300,26 @@ HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen) /* 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); + libscrypt_SHA256_Init(&ctx->ictx); + libscrypt_SHA256_Update(&ctx->ictx, K, Klen); + libscrypt_SHA256_Final(khash, &ctx->ictx); K = khash; Klen = 32; } /* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */ - SHA256_Init(&ctx->ictx); + libscrypt_SHA256_Init(&ctx->ictx); memset(pad, 0x36, 64); for (i = 0; i < Klen; i++) pad[i] ^= K[i]; - SHA256_Update(&ctx->ictx, pad, 64); + libscrypt_SHA256_Update(&ctx->ictx, pad, 64); /* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */ - SHA256_Init(&ctx->octx); + libscrypt_SHA256_Init(&ctx->octx); memset(pad, 0x5c, 64); for (i = 0; i < Klen; i++) pad[i] ^= K[i]; - SHA256_Update(&ctx->octx, pad, 64); + libscrypt_SHA256_Update(&ctx->octx, pad, 64); /* Clean the stack. */ memset(khash, 0, 32); @@ -327,27 +327,27 @@ HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen) /* Add bytes to the HMAC-SHA256 operation. */ void -HMAC_SHA256_Update(HMAC_SHA256_CTX * ctx, const void *in, size_t len) +libscrypt_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); + libscrypt_SHA256_Update(&ctx->ictx, in, len); } /* Finish an HMAC-SHA256 operation. */ void -HMAC_SHA256_Final(unsigned char digest[32], HMAC_SHA256_CTX * ctx) +libscrypt_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); + libscrypt_SHA256_Final(ihash, &ctx->ictx); /* Feed the inner hash to the outer SHA256 operation. */ - SHA256_Update(&ctx->octx, ihash, 32); + libscrypt_SHA256_Update(&ctx->octx, ihash, 32); /* Finish the outer SHA256 operation. */ - SHA256_Final(digest, &ctx->octx); + libscrypt_SHA256_Final(digest, &ctx->octx); /* Clean the stack. */ memset(ihash, 0, 32); @@ -359,7 +359,7 @@ HMAC_SHA256_Final(unsigned char digest[32], HMAC_SHA256_CTX * ctx) * 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, +libscrypt_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; @@ -372,8 +372,8 @@ PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt, size_t clen; /* Compute HMAC state after processing P and S. */ - HMAC_SHA256_Init(&PShctx, passwd, passwdlen); - HMAC_SHA256_Update(&PShctx, salt, saltlen); + libscrypt_HMAC_SHA256_Init(&PShctx, passwd, passwdlen); + libscrypt_HMAC_SHA256_Update(&PShctx, salt, saltlen); /* Iterate through the blocks. */ for (i = 0; i * 32 < dkLen; i++) { @@ -382,17 +382,17 @@ PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt, /* 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); + libscrypt_HMAC_SHA256_Update(&hctx, ivec, 4); + libscrypt_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); + libscrypt_HMAC_SHA256_Init(&hctx, passwd, passwdlen); + libscrypt_HMAC_SHA256_Update(&hctx, U, 32); + libscrypt_HMAC_SHA256_Final(U, &hctx); /* ... xor U_j ... */ for (k = 0; k < 32; k++) diff --git a/sha256.h b/sha256.h index 580183a..f7138b4 100644 --- a/sha256.h +++ b/sha256.h @@ -33,38 +33,38 @@ #include -typedef struct SHA256Context { +typedef struct libscrypt_SHA256Context { uint32_t state[8]; uint32_t count[2]; unsigned char buf[64]; } SHA256_CTX; -typedef struct HMAC_SHA256Context { +typedef struct libscrypt_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); +void libscrypt_SHA256_Init(/*@out@*/ SHA256_CTX *); +void libscrypt_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); +void libscrypt_SHA256_Final(/*@out@*/ unsigned char [], SHA256_CTX *); +void libscrypt_HMAC_SHA256_Init(HMAC_SHA256_CTX *, const void *, size_t); +void libscrypt_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 *); +void libscrypt_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, +void libscrypt_PBKDF2_SHA256(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, uint8_t *, size_t); #endif /* !_SHA256_H_ */ -- cgit v1.2.3 From 7c0ecdae2856ffe33647c5a6b54b73da6c55a9f6 Mon Sep 17 00:00:00 2001 From: jayesbee Date: Tue, 29 Jul 2014 06:02:02 -0400 Subject: allow the library to be used in C++ applications. I don't know if I could have done this external to modifying libscrypt.h --- libscrypt.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libscrypt.h b/libscrypt.h index e25b86c..318576e 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -6,6 +6,10 @@ #include +#ifdef __cplusplus +extern "C"{ +#endif + /** * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen): * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r, @@ -42,6 +46,10 @@ int libscrypt_hash(char *dst, const char* passphrase, uint32_t N, uint8_t r, uint8_t p); +#ifdef __cplusplus +} +#endif + /* Sane default values */ #define SCRYPT_HASH_LEN 64 /* This can be user defined - *but 64 is the reference size -- cgit v1.2.3 From 8d009cde90c00656c3e5b22cc03c696313d88ddf Mon Sep 17 00:00:00 2001 From: Technion Date: Wed, 30 Jul 2014 04:24:30 +0000 Subject: More paranoid type checks. --- crypto-mcf.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/crypto-mcf.c b/crypto-mcf.c index 2d0cff3..65a6a97 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -3,7 +3,7 @@ #include #include #include - +#include #include #ifndef S_SPLINT_S /* Including this here triggers a known bug in splint */ @@ -13,13 +13,16 @@ #include "libscrypt.h" /* ilog2 for powers of two */ -static int scrypt_ilog2(uint32_t n) +static uint32_t scrypt_ilog2(uint32_t n) { +#ifndef S_SPLINT_S + /* Check for a valid power of two */ if (n < 2 || (n & (n - 1))) return -1; - int t = 1; - while ((1 << t) < n) +#endif + uint32_t t = 1; + while (((uint32_t)1 << t) < n) { if(t > SCRYPT_SAFE_N) return -1; /* Check for insanity */ @@ -34,8 +37,8 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt, { - uint32_t params; - int s, t; + uint32_t t, params; + int s; if(!mcf || !hash) return 0; -- cgit v1.2.3 From 0f3098e0c82c5faf10236c8b0cfff48ba5c07bff Mon Sep 17 00:00:00 2001 From: Technion Date: Wed, 30 Jul 2014 04:41:12 +0000 Subject: Use reentrant functions so that code is thread safe. --- Makefile | 2 +- crypto-mcf.c | 1 - crypto_scrypt-check.c | 9 +++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 5691188..0073e2e 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ check: all devtest: splint crypto_scrypt-hexconvert.c - splint crypto-mcf.c crypto_scrypt-check.c crypto_scrypt-hash.c + splint crypto-mcf.c crypto_scrypt-check.c crypto_scrypt-hash.c -unrecog splint crypto-scrypt-saltgen.c +posixlib -compdef valgrind ./reference diff --git a/crypto-mcf.c b/crypto-mcf.c index 65a6a97..74e7be0 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -36,7 +36,6 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt, const char *hash, char *mcf) { - uint32_t t, params; int s; diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index 81bc12e..c889a7c 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -32,6 +32,7 @@ int libscrypt_check(char *mcf, const char *password) * >0 correct password */ + char *saveptr = NULL; uint32_t params; uint64_t N; uint8_t r, p; @@ -47,11 +48,11 @@ int libscrypt_check(char *mcf, const char *password) return -1; } - tok = strtok(mcf, "$"); + tok = strtok_r(mcf, "$", &saveptr); if ( !tok ) return -1; - tok = strtok(NULL, "$"); + tok = strtok_r(NULL, "$", &saveptr); if ( !tok ) return -1; @@ -60,7 +61,7 @@ int libscrypt_check(char *mcf, const char *password) if ( params == 0 ) return -1; - tok = strtok(NULL, "$"); + tok = strtok_r(NULL, "$", &saveptr); if ( !tok ) return -1; @@ -95,7 +96,7 @@ int libscrypt_check(char *mcf, const char *password) if (retval == 0) return -1; - tok = strtok(NULL, "$"); + tok = strtok_r(NULL, "$", &saveptr); if ( !tok ) return -1; -- cgit v1.2.3 From d45175b21ffd9e96fa7b80c7eadcc87df24fea88 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 28 Aug 2014 09:33:11 -0400 Subject: Use 'unsigned char', not 'u_char' Fixes mingw compilation for b64.c --- b64.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/b64.c b/b64.c index edce1b8..0e8c221 100644 --- a/b64.c +++ b/b64.c @@ -121,14 +121,14 @@ static const char Pad64 = '='; int libscrypt_b64_encode(src, srclength, target, targsize) - u_char const *src; + unsigned char const *src; size_t srclength; char *target; size_t targsize; { size_t datalength = 0; - u_char input[3]; - u_char output[4]; + unsigned char input[3]; + unsigned char output[4]; unsigned int i; while (2 < srclength) { @@ -186,12 +186,12 @@ libscrypt_b64_encode(src, srclength, target, targsize) int libscrypt_b64_decode(src, target, targsize) char const *src; - u_char *target; + unsigned char *target; size_t targsize; { int state, ch; unsigned int tarindex; - u_char nextbyte; + unsigned char nextbyte; char *pos; state = 0; -- cgit v1.2.3 From 23225508d2d82ac805e4cc820a800ed6eabbac6b Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 28 Aug 2014 09:35:04 -0400 Subject: Don't include sys/mman.h on Windows The header doesn't exist; instead, just fall back to malloc. --- crypto_scrypt-nosse.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crypto_scrypt-nosse.c b/crypto_scrypt-nosse.c index f54e42e..311e84a 100644 --- a/crypto_scrypt-nosse.c +++ b/crypto_scrypt-nosse.c @@ -28,8 +28,9 @@ */ #include +#ifndef _WIN32 #include - +#endif #include #include #include -- cgit v1.2.3 From c88ddb27de082085a2d72e5b2eefc3e5d1557294 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 28 Aug 2014 09:42:43 -0400 Subject: Use strtok() on Windows. See the MSDN documentation for strtok(): They don't provide a strtok_r, but they do have a thread-safe strtok(). --- crypto_scrypt-check.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index c889a7c..5ed7ab0 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -7,6 +7,12 @@ #include "slowequals.h" #include "libscrypt.h" +#ifdef _WIN32 +/* On windows, strtok uses a thread-local static variable in strtok to + * make strtok thread-safe. It also neglects to provide a strtok_r. */ +#define strtok_r(str, val, saveptr) strtok((str), (val)) +#endif + /* pow() works with doubles. Sounds like it should cast to int correctly, * but doesn't always. This is faster anyway */ @@ -32,7 +38,9 @@ int libscrypt_check(char *mcf, const char *password) * >0 correct password */ +#ifndef _WIN32 char *saveptr = NULL; +#endif uint32_t params; uint64_t N; uint8_t r, p; -- cgit v1.2.3 From 7894d848087e84d4011f392c3e91a603800ab36b Mon Sep 17 00:00:00 2001 From: Jose Sebastian Battig Date: Tue, 16 Sep 2014 17:47:26 -0500 Subject: B-0 modifications made to allow compilation in Visual Studio --- b64.c | 2 +- crypto-mcf.c | 10 ++++++++-- crypto-scrypt-saltgen.c | 4 ++++ libscrypt.h | 9 +++++---- sysendian.h | 21 +++++++++++++-------- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/b64.c b/b64.c index 0e8c221..b797dd0 100644 --- a/b64.c +++ b/b64.c @@ -174,7 +174,7 @@ libscrypt_b64_encode(src, srclength, target, targsize) if (datalength >= targsize) return (-1); target[datalength] = '\0'; /* Returned value doesn't count \0. */ - return (datalength); + return (int)(datalength); } /* skips all whitespace anywhere. diff --git a/crypto-mcf.c b/crypto-mcf.c index 74e7be0..8ad3eb8 100644 --- a/crypto-mcf.c +++ b/crypto-mcf.c @@ -25,13 +25,19 @@ static uint32_t scrypt_ilog2(uint32_t n) while (((uint32_t)1 << t) < n) { if(t > SCRYPT_SAFE_N) - return -1; /* Check for insanity */ + return (uint32_t) -1; /* Check for insanity */ t++; } return t; } +#ifdef _MSC_VER + #define SNPRINTF _snprintf +#else + #define SNPRINTF snprintf +#endif + int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt, const char *hash, char *mcf) { @@ -59,7 +65,7 @@ int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt, * 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); + s = SNPRINTF(mcf, SCRYPT_MCF_LEN, SCRYPT_MCF_ID "$%06x$%s$%s", (unsigned int)params, salt, hash); if (s > SCRYPT_MCF_LEN) return 0; diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index a0e2998..beded9c 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -8,6 +8,8 @@ #include #endif +#ifndef _MSC_VER + #define RNGDEV "/dev/urandom" int libscrypt_salt_gen(uint8_t *salt, size_t len) @@ -46,3 +48,5 @@ int libscrypt_salt_gen(uint8_t *salt, size_t len) return 0; } + +#endif diff --git a/libscrypt.h b/libscrypt.h index 318576e..b7141f5 100644 --- a/libscrypt.h +++ b/libscrypt.h @@ -33,18 +33,19 @@ int libscrypt_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, int libscrypt_mcf(uint32_t N, uint32_t r, uint32_t p, const char *salt, const char *hash, char *mcf); +#ifndef _MSC_VER /* Generates a salt. Uses /dev/urandom/ */ int libscrypt_salt_gen(/*@out@*/ uint8_t *rand, size_t len); -/* Checks a given MCF against a password */ -int libscrypt_check(char *mcf, const char *password); - /* Creates a hash of a passphrase using a randomly generated salt */ /* Returns >0 on success, or 0 for fail */ int libscrypt_hash(char *dst, const char* passphrase, uint32_t N, uint8_t r, - uint8_t p); + uint8_t p); +#endif +/* Checks a given MCF against a password */ +int libscrypt_check(char *mcf, const char *password); #ifdef __cplusplus } diff --git a/sysendian.h b/sysendian.h index 5ecb505..af1ecdc 100644 --- a/sysendian.h +++ b/sysendian.h @@ -42,8 +42,13 @@ #else #include +#ifdef _MSC_VER + #define INLINE __inline +#else + #define INLINE inline +#endif -static inline uint32_t +static INLINE uint32_t be32dec(const void *pp) { const uint8_t *p = (uint8_t const *)pp; @@ -52,7 +57,7 @@ be32dec(const void *pp) ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24)); } -static inline void +static INLINE void be32enc(void *pp, uint32_t x) { uint8_t * p = (uint8_t *)pp; @@ -63,7 +68,7 @@ be32enc(void *pp, uint32_t x) p[0] = (x >> 24) & 0xff; } -static inline uint64_t +static INLINE uint64_t be64dec(const void *pp) { const uint8_t *p = (uint8_t const *)pp; @@ -74,7 +79,7 @@ be64dec(const void *pp) ((uint64_t)(p[1]) << 48) + ((uint64_t)(p[0]) << 56)); } -static inline void +static INLINE void be64enc(void *pp, uint64_t x) { uint8_t * p = (uint8_t *)pp; @@ -89,7 +94,7 @@ be64enc(void *pp, uint64_t x) p[0] = (x >> 56) & 0xff; } -static inline uint32_t +static INLINE uint32_t le32dec(const void *pp) { const uint8_t *p = (uint8_t const *)pp; @@ -98,7 +103,7 @@ le32dec(const void *pp) ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24)); } -static inline void +static INLINE void le32enc(void *pp, uint32_t x) { uint8_t * p = (uint8_t *)pp; @@ -109,7 +114,7 @@ le32enc(void *pp, uint32_t x) p[3] = (x >> 24) & 0xff; } -static inline uint64_t +static INLINE uint64_t le64dec(const void *pp) { const uint8_t *p = (uint8_t const *)pp; @@ -120,7 +125,7 @@ le64dec(const void *pp) ((uint64_t)(p[6]) << 48) + ((uint64_t)(p[7]) << 56)); } -static inline void +static INLINE void le64enc(void *pp, uint64_t x) { uint8_t * p = (uint8_t *)pp; -- cgit v1.2.3 From 1023e18857560035d8eb2f25e1321abbba770714 Mon Sep 17 00:00:00 2001 From: Jose Sebastian Battig Date: Tue, 16 Sep 2014 17:53:51 -0500 Subject: B-0 restored back to original state --- crypto-scrypt-saltgen.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/crypto-scrypt-saltgen.c b/crypto-scrypt-saltgen.c index beded9c..a0e2998 100644 --- a/crypto-scrypt-saltgen.c +++ b/crypto-scrypt-saltgen.c @@ -8,8 +8,6 @@ #include #endif -#ifndef _MSC_VER - #define RNGDEV "/dev/urandom" int libscrypt_salt_gen(uint8_t *salt, size_t len) @@ -48,5 +46,3 @@ int libscrypt_salt_gen(uint8_t *salt, size_t len) return 0; } - -#endif -- cgit v1.2.3 From 3f4ed127b9b62f376060125e4b827452ba441f52 Mon Sep 17 00:00:00 2001 From: Technion Date: Thu, 6 Nov 2014 01:41:11 +0000 Subject: Fixed #28. --- crypto_scrypt-check.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index 5ed7ab0..055717f 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -16,7 +16,7 @@ /* 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) +static uint16_t ipow(uint16_t base, uint64_t exp) { uint16_t result = 1; while (exp != 0) -- cgit v1.2.3 From a098d03c3d17a7252cafd6c2d42e988c1e9bbc94 Mon Sep 17 00:00:00 2001 From: Glyph Date: Wed, 26 Nov 2014 17:55:58 +0100 Subject: Use install_name_tool to name the installed dylib. --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 0073e2e..05eeea6 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,7 @@ install: libscrypt.so.0 install-osx: libscrypt.so.0 $(MAKE_DIR) $(DESTDIR) $(DESTDIR)$(PREFIX) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR) $(INSTALL_DATA) -pm 0755 libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.0.dylib + cd $(DESTDIR)$(LIBDIR) && install_name_tool -id $(DESTDIR)$(LIBDIR)/libscrypt.0.dylib $(DESTDIR)$(LIBDIR)/libscrypt.0.dylib cd $(DESTDIR)$(LIBDIR) && ln -s -f libscrypt.0.dylib $(DESTDIR)$(LIBDIR)/libscrypt.dylib $(INSTALL_DATA) -pm 0644 libscrypt.h $(DESTDIR)$(INCLUDEDIR) -- cgit v1.2.3 From bd161172424af4627952d03412ee67142766d71c Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 1 Dec 2014 09:17:48 +0000 Subject: Because of commit 7cd8a4fa3, libscrypt_scrypt can return EINVAL, which is 22. Passing this from libscrypt_check incorrectly returns a failure as a success. Reported by Achim Stahlberger. --- crypto_scrypt-check.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index 055717f..f198ef0 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -96,7 +96,7 @@ int libscrypt_check(char *mcf, const char *password) (uint32_t)retval, N, r, p, hashbuf, sizeof(hashbuf)); if (retval != 0) - return retval; + return -1; retval = libscrypt_b64_encode((unsigned char*)hashbuf, sizeof(hashbuf), outbuf, sizeof(outbuf)); -- cgit v1.2.3 From 7c949b891f7d5b964a5a233edec0a7feda402163 Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 1 Dec 2014 09:24:16 +0000 Subject: Much more effecient N calculation, and now handles N > 15 (althought you wouldn't want to with today's computers). Credit to Achim Stahlberger . --- crypto_scrypt-check.c | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/crypto_scrypt-check.c b/crypto_scrypt-check.c index f198ef0..7ac0e0c 100644 --- a/crypto_scrypt-check.c +++ b/crypto_scrypt-check.c @@ -13,23 +13,6 @@ #define strtok_r(str, val, saveptr) strtok((str), (val)) #endif -/* 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, uint64_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, const char *password) { /* Return values: @@ -81,7 +64,7 @@ int libscrypt_check(char *mcf, const char *password) if (N > SCRYPT_SAFE_N) return -1; - N = ipow(2, N); + N = (uint64_t)1 << N; /* Useful debugging: printf("We've obtained salt 'N' r p of '%s' %d %d %d\n", tok, N,r,p); -- cgit v1.2.3 From c107c413ea127c652fe798d6b50441f34e6ffe9c Mon Sep 17 00:00:00 2001 From: Technion Date: Mon, 1 Dec 2014 10:39:02 +0000 Subject: Version 1.20 bump --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 494c8f3..a6894a3 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ v1.18: God damnit Apple v1.19: Code safety cleanups. Now running Coverity. +v1.20: Bigfixes involving large N values, return values on error Coverity Scan Build Status Date: Sun, 14 Dec 2014 02:36:40 +0200 Subject: Remove debian patches in preparation for merge with upstream. All of the necessary changes are included upstream, so we're not losing anything. --- .pc/.quilt_patches | 1 - .pc/.quilt_series | 1 - .pc/.version | 1 - .pc/applied-patches | 2 - .pc/big-endian.patch/modp_b64.c | 264 ---------------------------------------- .pc/fix_makefile/Makefile | 32 ----- Makefile | 15 +-- debian/patches/big-endian.patch | 53 -------- debian/patches/fix_makefile | 25 ---- debian/patches/series | 2 - modp_b64.c | 12 +- 11 files changed, 8 insertions(+), 400 deletions(-) delete mode 100644 .pc/.quilt_patches delete mode 100644 .pc/.quilt_series delete mode 100644 .pc/.version delete mode 100644 .pc/applied-patches delete mode 100644 .pc/big-endian.patch/modp_b64.c delete mode 100644 .pc/fix_makefile/Makefile delete mode 100644 debian/patches/big-endian.patch delete mode 100644 debian/patches/fix_makefile delete mode 100644 debian/patches/series diff --git a/.pc/.quilt_patches b/.pc/.quilt_patches deleted file mode 100644 index 6857a8d..0000000 --- a/.pc/.quilt_patches +++ /dev/null @@ -1 +0,0 @@ -debian/patches diff --git a/.pc/.quilt_series b/.pc/.quilt_series deleted file mode 100644 index c206706..0000000 --- a/.pc/.quilt_series +++ /dev/null @@ -1 +0,0 @@ -series diff --git a/.pc/.version b/.pc/.version deleted file mode 100644 index 0cfbf08..0000000 --- a/.pc/.version +++ /dev/null @@ -1 +0,0 @@ -2 diff --git a/.pc/applied-patches b/.pc/applied-patches deleted file mode 100644 index 5d020e2..0000000 --- a/.pc/applied-patches +++ /dev/null @@ -1,2 +0,0 @@ -fix_makefile -big-endian.patch diff --git a/.pc/big-endian.patch/modp_b64.c b/.pc/big-endian.patch/modp_b64.c deleted file mode 100644 index 9215b45..0000000 --- a/.pc/big-endian.patch/modp_b64.c +++ /dev/null @@ -1,264 +0,0 @@ -/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */ -/* vi: set expandtab shiftwidth=4 tabstop=4: */ -/** - * \file modp_b64.c - *
- * MODP_B64 - High performance base64 encoder/decoder
- * http://code.google.com/p/stringencoders/
- *
- * Copyright © 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
- * 
- */ - -/* 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 deleted file mode 100644 index 69ee494..0000000 --- a/.pc/fix_makefile/Makefile +++ /dev/null @@ -1,32 +0,0 @@ -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/Makefile b/Makefile index 6a0a659..69ee494 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,3 @@ -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 @@ -32,8 +26,7 @@ devtest: 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) + 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/debian/patches/big-endian.patch b/debian/patches/big-endian.patch deleted file mode 100644 index 60c9868..0000000 --- a/debian/patches/big-endian.patch +++ /dev/null @@ -1,53 +0,0 @@ -From: Aurelien Jarno -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 -. - -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 -+#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 deleted file mode 100644 index 872015b..0000000 --- a/debian/patches/fix_makefile +++ /dev/null @@ -1,25 +0,0 @@ ---- 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 deleted file mode 100644 index 5d020e2..0000000 --- a/debian/patches/series +++ /dev/null @@ -1,2 +0,0 @@ -fix_makefile -big-endian.patch diff --git a/modp_b64.c b/modp_b64.c index 5cbf41a..9215b45 100644 --- a/modp_b64.c +++ b/modp_b64.c @@ -45,14 +45,10 @@ #include "modp_b64.h" -#include -#if __BYTE_ORDER == __BIG_ENDIAN -# define WORDS_BIGENDIAN 1 -#elif __BYTE_ORDER == __LITTLE_ENDIAN -# undef WORDS_BIGENDIAN -#else -#error "Unknown endianess" -#endif +/* 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" -- cgit v1.2.3 From 88680bcc4591ec16a3aa4a5defcf0ef488f45470 Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 02:44:37 +0200 Subject: New upstream release. - Add myself as co-maintainer. --- debian/changelog | 7 +++++++ debian/control | 1 + debian/copyright | 3 ++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/debian/changelog b/debian/changelog index 758090e..d1c8e05 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +libscrypt (1.20-1) experimental; urgency=medium + + * New upstream release. + * Add myself as co-maintainer. + + -- Tristan Seligmann Sun, 14 Dec 2014 02:42:29 +0200 + libscrypt (1-2.2) unstable; urgency=medium * Non-maintainer upload. diff --git a/debian/control b/debian/control index 981b11e..64cc6dc 100644 --- a/debian/control +++ b/debian/control @@ -1,6 +1,7 @@ Source: libscrypt Priority: extra Maintainer: Micah Anderson +Uploaders: Tristan Seligmann Build-Depends: debhelper (>= 9) Standards-Version: 3.9.4 Section: libs diff --git a/debian/copyright b/debian/copyright index 3ac7de8..cfc1ece 100644 --- a/debian/copyright +++ b/debian/copyright @@ -61,7 +61,8 @@ License: BSD-3-Clause OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Files: debian/* -Copyright: 2013 Micah Anderson +Copyright: 2013 Micah Anderson , + 2014 Tristan Seligmann 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 -- cgit v1.2.3 From 5dcc64370a17bc551e7bf2b4085468e62b36aa1e Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 04:23:07 +0200 Subject: Low priority, not that it matters. --- debian/changelog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/changelog b/debian/changelog index d1c8e05..8c69680 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,4 +1,4 @@ -libscrypt (1.20-1) experimental; urgency=medium +libscrypt (1.20-1) experimental; urgency=low * New upstream release. * Add myself as co-maintainer. -- cgit v1.2.3 From 50c2fd628ac252ce982686274e711a2ab570c011 Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 04:23:25 +0200 Subject: Work around changes in upstream Makefile. --- debian/rules | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/debian/rules b/debian/rules index 2dfe8b7..ba1ff64 100755 --- a/debian/rules +++ b/debian/rules @@ -4,8 +4,11 @@ # Uncomment this to turn on verbose mode. export DH_VERBOSE=1 +export DEB_CFLAGS_MAINT_APPEND=-fPIC +export DEB_LDFLAGS_MAINT_APPEND=-Wl,-z,now -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version + %: - dh $@ + dh $@ override_dh_auto_install: - $(MAKE) DESTDIR=debian/tmp PREFIX=/usr install + $(MAKE) DESTDIR=$(CURDIR)/debian/tmp PREFIX=/usr install install-static -- cgit v1.2.3 From f3a21f54b2e1e9e41c9d0ac6843bea18add9c71b Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 04:23:40 +0200 Subject: Remove some cruft. --- debian/libscrypt1.dirs | 1 - debian/shlibs.local | 1 - 2 files changed, 2 deletions(-) delete mode 100644 debian/libscrypt1.dirs delete mode 100644 debian/shlibs.local diff --git a/debian/libscrypt1.dirs b/debian/libscrypt1.dirs deleted file mode 100644 index 6845771..0000000 --- a/debian/libscrypt1.dirs +++ /dev/null @@ -1 +0,0 @@ -usr/lib diff --git a/debian/shlibs.local b/debian/shlibs.local deleted file mode 100644 index 637ff02..0000000 --- a/debian/shlibs.local +++ /dev/null @@ -1 +0,0 @@ -liblibscrypt 0.1 libscrypt (>> 0.1-0), libscrypt (<< 0.1-99) -- cgit v1.2.3 From 8c155e5750b1de950be093cbc46ff1b43c56b00b Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 04:35:38 +0200 Subject: Bump Standards-Version. --- debian/changelog | 3 +++ debian/control | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/debian/changelog b/debian/changelog index 8c69680..530cbee 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,7 +1,10 @@ libscrypt (1.20-1) experimental; urgency=low + * ACK NMUs, thanks for the fixes. * New upstream release. + - Drop patches from NMUs due to inclusion of equivalent changes upstream. * Add myself as co-maintainer. + * Bump Standards-Version. -- Tristan Seligmann Sun, 14 Dec 2014 02:42:29 +0200 diff --git a/debian/control b/debian/control index 64cc6dc..751808d 100644 --- a/debian/control +++ b/debian/control @@ -3,7 +3,7 @@ Priority: extra Maintainer: Micah Anderson Uploaders: Tristan Seligmann Build-Depends: debhelper (>= 9) -Standards-Version: 3.9.4 +Standards-Version: 3.9.6 Section: libs Homepage: http://www.lolware.net/libscrypt.html Vcs-Git: git://git.debian.org/collab-maint/libscrypt.git -- cgit v1.2.3 From da16c203967f9ef9f336af43c1aaa5a7d1363825 Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 04:52:30 +0200 Subject: Add a symbols file. --- debian/changelog | 1 + debian/libscrypt0.symbols | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 debian/libscrypt0.symbols diff --git a/debian/changelog b/debian/changelog index 530cbee..09d3c41 100644 --- a/debian/changelog +++ b/debian/changelog @@ -5,6 +5,7 @@ libscrypt (1.20-1) experimental; urgency=low - Drop patches from NMUs due to inclusion of equivalent changes upstream. * Add myself as co-maintainer. * Bump Standards-Version. + * Add a symbols file. -- Tristan Seligmann Sun, 14 Dec 2014 02:42:29 +0200 diff --git a/debian/libscrypt0.symbols b/debian/libscrypt0.symbols new file mode 100644 index 0000000..89f4a5b --- /dev/null +++ b/debian/libscrypt0.symbols @@ -0,0 +1,7 @@ +libscrypt.so.0 libscrypt0 #MINVER# + libscrypt@libscrypt 1 + libscrypt_check@libscrypt 1 + libscrypt_hash@libscrypt 1 + libscrypt_mcf@libscrypt 1 + libscrypt_salt_gen@libscrypt 1 + libscrypt_scrypt@libscrypt 1 -- cgit v1.2.3 From 6faf699a4009e193db53e9fddd687df171f45b96 Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 05:00:01 +0200 Subject: Tweak -dev package description. --- debian/changelog | 1 + debian/control | 5 +---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/debian/changelog b/debian/changelog index 09d3c41..4dabf6f 100644 --- a/debian/changelog +++ b/debian/changelog @@ -6,6 +6,7 @@ libscrypt (1.20-1) experimental; urgency=low * Add myself as co-maintainer. * Bump Standards-Version. * Add a symbols file. + * Tweak -dev package description. -- Tristan Seligmann Sun, 14 Dec 2014 02:42:29 +0200 diff --git a/debian/control b/debian/control index 751808d..703b6bb 100644 --- a/debian/control +++ b/debian/control @@ -19,12 +19,9 @@ Description: scrypt shared library - development files 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. + This package contains files for development with libscrypt. Package: libscrypt0 -Section: libs Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends} Description: scrypt shared library -- cgit v1.2.3 From 5052167d54dc0d7ea620fbd9ed7d44089ce794c8 Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 05:01:36 +0200 Subject: Update Vcs-* fields. --- debian/changelog | 1 + debian/control | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/debian/changelog b/debian/changelog index 4dabf6f..ad4f25f 100644 --- a/debian/changelog +++ b/debian/changelog @@ -7,6 +7,7 @@ libscrypt (1.20-1) experimental; urgency=low * Bump Standards-Version. * Add a symbols file. * Tweak -dev package description. + * Update Vcs-* fields. -- Tristan Seligmann Sun, 14 Dec 2014 02:42:29 +0200 diff --git a/debian/control b/debian/control index 703b6bb..128a018 100644 --- a/debian/control +++ b/debian/control @@ -6,8 +6,8 @@ Build-Depends: debhelper (>= 9) Standards-Version: 3.9.6 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 +Vcs-Git: git://anonscm.debian.org/collab-maint/libscrypt.git +Vcs-Browser: https://anonscm.debian.org/cgit/collab-maint/libscrypt.git;a=summary Package: libscrypt-dev Section: libdevel -- cgit v1.2.3 From dc475639ff1a060e79ad5d824ab32df715fef374 Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 05:18:43 +0200 Subject: Update copyright file. --- debian/changelog | 1 + debian/copyright | 113 ++++++++++++++++++++++++++++++------------------------- 2 files changed, 62 insertions(+), 52 deletions(-) diff --git a/debian/changelog b/debian/changelog index ad4f25f..6f1a07c 100644 --- a/debian/changelog +++ b/debian/changelog @@ -8,6 +8,7 @@ libscrypt (1.20-1) experimental; urgency=low * Add a symbols file. * Tweak -dev package description. * Update Vcs-* fields. + * Update copyright file. -- Tristan Seligmann Sun, 14 Dec 2014 02:42:29 +0200 diff --git a/debian/copyright b/debian/copyright index cfc1ece..df0e411 100644 --- a/debian/copyright +++ b/debian/copyright @@ -4,66 +4,54 @@ Source: https://github.com/technion/libscrypt Files: * Copyright: 2013 Joshua Small -License: BSD-2-Clause +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: +Files: b64.* +Copyright: 1995 International Business Machines, Inc. + 1996 Internet Software Consortium +License: ISC and IBM + Permission to use, copy, modify, and distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. . - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. + THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + SOFTWARE. . - 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. + International Business Machines, Inc. (hereinafter called IBM) grants + permission under its copyrights to use, copy, modify, and distribute this + Software with or without fee, provided that the above copyright notice and + all paragraphs of this notice appear in all copies, and that the name of IBM + not be used in connection with the marketing of any product incorporating + the Software or modifications thereof, without specific, written prior + permission. . - 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. + To the extent it has a right to do so, IBM grants an immunity from suit + under its patents, if any, for the use, sale or manufacture of products to + the extent that such products are used for performing Domain Name System + dynamic updates in TCP/IP networks by means of the Software. No immunity is + granted for any product per se or for any other function of any product. . - 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. + THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, + INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, + DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING + OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN + IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. + +Files: crypto_scrypt-nosse.c sha256.* sysendian.h +Copyright: 2005, 2007-2009 Colin Percival +License: BSD-2-clause Files: debian/* -Copyright: 2013 Micah Anderson , +Copyright: 2013 Micah Anderson 2014 Tristan Seligmann -License: GPL-3+ +License: GPL-2+ 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 @@ -80,3 +68,24 @@ License: GPL-3+ On Debian systems, the complete text of the GNU General Public License version 2 can be found in "/usr/share/common-licenses/GPL-2". +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. -- cgit v1.2.3 From aab9a36595610a50a279da0b0cafabaa6ed3777e Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 05:29:11 +0200 Subject: Update changelog for release. --- debian/changelog | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debian/changelog b/debian/changelog index 6f1a07c..03220df 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,7 +1,7 @@ libscrypt (1.20-1) experimental; urgency=low * ACK NMUs, thanks for the fixes. - * New upstream release. + * New upstream release (Closes: #746041). - Drop patches from NMUs due to inclusion of equivalent changes upstream. * Add myself as co-maintainer. * Bump Standards-Version. @@ -10,7 +10,7 @@ libscrypt (1.20-1) experimental; urgency=low * Update Vcs-* fields. * Update copyright file. - -- Tristan Seligmann Sun, 14 Dec 2014 02:42:29 +0200 + -- Tristan Seligmann Sun, 14 Dec 2014 05:28:49 +0200 libscrypt (1-2.2) unstable; urgency=medium -- cgit v1.2.3 From b52519aa74464e561c2fa883c39685a85547d88a Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 05:55:25 +0200 Subject: Handle Makefile differently. --- debian/patches/fix-makefile | 21 +++++++++++++++++++++ debian/patches/series | 1 + debian/rules | 3 --- 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 debian/patches/fix-makefile create mode 100644 debian/patches/series diff --git a/debian/patches/fix-makefile b/debian/patches/fix-makefile new file mode 100644 index 0000000..3a19cf8 --- /dev/null +++ b/debian/patches/fix-makefile @@ -0,0 +1,21 @@ +Description: Fix makefile flag handling +Author: Tristan Seligmann +Origin: vendor +Forwarded: no +--- +This patch header follows DEP-3: http://dep.debian.net/deps/dep3/ +Index: libscrypt/Makefile +=================================================================== +--- libscrypt.orig/Makefile 2014-12-14 02:38:36.667099740 +0200 ++++ libscrypt/Makefile 2014-12-14 05:53:34.488194255 +0200 +@@ -5,8 +5,8 @@ + INSTALL_DATA ?= install + + CC?=gcc +-CFLAGS?=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC +-LDFLAGS?=-Wl,-z,now -Wl,-z,relro -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version ++CFLAGS+=-fPIC ++LDFLAGS+=-Wl,-z,now -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version + CFLAGS_EXTRA?=-Wl,-rpath=. + + all: reference diff --git a/debian/patches/series b/debian/patches/series new file mode 100644 index 0000000..4f20ff2 --- /dev/null +++ b/debian/patches/series @@ -0,0 +1 @@ +fix-makefile diff --git a/debian/rules b/debian/rules index ba1ff64..cf18250 100755 --- a/debian/rules +++ b/debian/rules @@ -4,9 +4,6 @@ # Uncomment this to turn on verbose mode. export DH_VERBOSE=1 -export DEB_CFLAGS_MAINT_APPEND=-fPIC -export DEB_LDFLAGS_MAINT_APPEND=-Wl,-z,now -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version - %: dh $@ -- cgit v1.2.3 From 5eaeaf5a6d989d89c76c1a135698c69f1ed16f43 Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 05:55:32 +0200 Subject: Commit Debian 3.0 (quilt) metadata --- .pc/.dpkg-source-unapply | 0 .pc/.quilt_patches | 1 + .pc/.quilt_series | 1 + .pc/.version | 1 + .pc/applied-patches | 1 + .pc/fix-makefile/Makefile | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 54 insertions(+) create mode 100644 .pc/.dpkg-source-unapply create mode 100644 .pc/.quilt_patches create mode 100644 .pc/.quilt_series create mode 100644 .pc/.version create mode 100644 .pc/applied-patches create mode 100644 .pc/fix-makefile/Makefile diff --git a/.pc/.dpkg-source-unapply b/.pc/.dpkg-source-unapply new file mode 100644 index 0000000..e69de29 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..4f20ff2 --- /dev/null +++ b/.pc/applied-patches @@ -0,0 +1 @@ +fix-makefile diff --git a/.pc/fix-makefile/Makefile b/.pc/fix-makefile/Makefile new file mode 100644 index 0000000..05eeea6 --- /dev/null +++ b/.pc/fix-makefile/Makefile @@ -0,0 +1,50 @@ +PREFIX ?= /usr/local +LIBDIR ?= $(PREFIX)/lib +INCLUDEDIR ?= $(PREFIX)/include +MAKE_DIR ?= install -d +INSTALL_DATA ?= install + +CC?=gcc +CFLAGS?=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC +LDFLAGS?=-Wl,-z,now -Wl,-z,relro -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version +CFLAGS_EXTRA?=-Wl,-rpath=. + +all: reference + +OBJS= crypto_scrypt-nosse.o sha256.o crypto-mcf.o b64.o crypto-scrypt-saltgen.o crypto_scrypt-check.o crypto_scrypt-hash.o slowequals.o + +libscrypt.so.0: $(OBJS) + $(CC) $(LDFLAGS) -shared -o libscrypt.so.0 $(OBJS) -lm -lc + ar rcs libscrypt.a $(OBJS) + +reference: libscrypt.so.0 main.o b64.o crypto_scrypt-hexconvert.o + ln -s -f libscrypt.so.0 libscrypt.so + $(CC) -Wall -o reference main.o b64.o crypto_scrypt-hexconvert.o $(CFLAGS_EXTRA) -L. -lscrypt + +clean: + rm -f *.o reference libscrypt.so* libscrypt.a endian.h + +check: all + ./reference + +devtest: + splint crypto_scrypt-hexconvert.c + splint crypto-mcf.c crypto_scrypt-check.c crypto_scrypt-hash.c -unrecog + splint crypto-scrypt-saltgen.c +posixlib -compdef + valgrind ./reference + +install: libscrypt.so.0 + $(MAKE_DIR) $(DESTDIR) $(DESTDIR)$(PREFIX) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR) + $(INSTALL_DATA) -pm 0755 libscrypt.so.0 $(DESTDIR)$(LIBDIR) + cd $(DESTDIR)$(LIBDIR) && ln -s -f libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.so + $(INSTALL_DATA) -pm 0644 libscrypt.h $(DESTDIR)$(INCLUDEDIR) + +install-osx: libscrypt.so.0 + $(MAKE_DIR) $(DESTDIR) $(DESTDIR)$(PREFIX) $(DESTDIR)$(LIBDIR) $(DESTDIR)$(INCLUDEDIR) + $(INSTALL_DATA) -pm 0755 libscrypt.so.0 $(DESTDIR)$(LIBDIR)/libscrypt.0.dylib + cd $(DESTDIR)$(LIBDIR) && install_name_tool -id $(DESTDIR)$(LIBDIR)/libscrypt.0.dylib $(DESTDIR)$(LIBDIR)/libscrypt.0.dylib + cd $(DESTDIR)$(LIBDIR) && ln -s -f libscrypt.0.dylib $(DESTDIR)$(LIBDIR)/libscrypt.dylib + $(INSTALL_DATA) -pm 0644 libscrypt.h $(DESTDIR)$(INCLUDEDIR) + +install-static: libscrypt.a + $(INSTALL_DATA) -pm 0644 libscrypt.a $(DESTDIR)$(LIBDIR) -- cgit v1.2.3 From e8e6fcc1a7d6d2cba74d11157cd0ec7797484cff Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 05:57:35 +0200 Subject: Fix warning flag. --- Makefile | 4 ++-- debian/patches/fix-makefile | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 05eeea6..4708a9d 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,8 @@ MAKE_DIR ?= install -d INSTALL_DATA ?= install CC?=gcc -CFLAGS?=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC -LDFLAGS?=-Wl,-z,now -Wl,-z,relro -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version +CFLAGS += -Wall -fPIC +LDFLAGS += -Wl,-z,now -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version CFLAGS_EXTRA?=-Wl,-rpath=. all: reference diff --git a/debian/patches/fix-makefile b/debian/patches/fix-makefile index 3a19cf8..23b7868 100644 --- a/debian/patches/fix-makefile +++ b/debian/patches/fix-makefile @@ -14,8 +14,8 @@ Index: libscrypt/Makefile CC?=gcc -CFLAGS?=-O2 -Wall -g -D_FORTIFY_SOURCE=2 -fstack-protector -fPIC -LDFLAGS?=-Wl,-z,now -Wl,-z,relro -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version -+CFLAGS+=-fPIC -+LDFLAGS+=-Wl,-z,now -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version ++CFLAGS += -Wall -fPIC ++LDFLAGS += -Wl,-z,now -Wl,-soname,libscrypt.so.0 -Wl,--version-script=libscrypt.version CFLAGS_EXTRA?=-Wl,-rpath=. all: reference -- cgit v1.2.3 From 35b2e3e08bc71de8bb97a039b3e4aade6c715da0 Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Sun, 14 Dec 2014 06:00:33 +0200 Subject: Huh. --- .pc/.dpkg-source-unapply | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .pc/.dpkg-source-unapply diff --git a/.pc/.dpkg-source-unapply b/.pc/.dpkg-source-unapply deleted file mode 100644 index e69de29..0000000 -- cgit v1.2.3 From b5881b2ff269c5459fad2f82840672a204fdb251 Mon Sep 17 00:00:00 2001 From: Tristan Seligmann Date: Wed, 24 Dec 2014 19:49:04 +0200 Subject: Fix Vcs-Browse URI. --- debian/control | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debian/control b/debian/control index 128a018..7667bba 100644 --- a/debian/control +++ b/debian/control @@ -7,7 +7,7 @@ Standards-Version: 3.9.6 Section: libs Homepage: http://www.lolware.net/libscrypt.html Vcs-Git: git://anonscm.debian.org/collab-maint/libscrypt.git -Vcs-Browser: https://anonscm.debian.org/cgit/collab-maint/libscrypt.git;a=summary +Vcs-Browser: https://anonscm.debian.org/cgit/collab-maint/libscrypt.git Package: libscrypt-dev Section: libdevel -- cgit v1.2.3