2
0

Support for dynamically linking libscrypt & libsodium as alternative to statically linking Tarsnap's scrypt.

This commit is contained in:
Maarten Billemont 2017-04-08 14:25:54 -04:00
parent 98c5ee3425
commit 4058d33202
10 changed files with 170 additions and 40 deletions

View File

@ -1,4 +1,4 @@
home=http://www.tarsnap.com/scrypt.html
git=https://github.com/Tarsnap/scrypt.git
pkg=http://www.tarsnap.com/scrypt/scrypt-1.2.0.tgz
pkg_sha256=1754bc89405277c8ac14220377a4c240ddc34b1ce70882aa92cd01bfdc8569d4
pkg=https://www.tarsnap.com/scrypt/scrypt-1.2.1.tgz
pkg_sha256=4621f5e7da2f802e20850436219370092e9fcda93bd598f6d4236cce33f4c577

View File

@ -20,14 +20,23 @@
#include <stdlib.h>
#include <string.h>
#ifdef COLOR
#if COLOR
#include <unistd.h>
#include <curses.h>
#include <term.h>
#endif
#include <scrypt/sha256.h>
#if HAS_CPERCIVA
#include <scrypt/crypto_scrypt.h>
#include <scrypt/sha256.h>
#elif HAS_SCRYPT_SODIUM
#include <libscrypt.h>
#include <sodium.h>
#endif
#ifndef trc
int mpw_verbosity;
#endif
#include "mpw-util.h"
@ -85,30 +94,62 @@ uint8_t const *mpw_scrypt(const size_t keySize, const char *secret, const uint8_
if (!key)
return NULL;
#if HAS_CPERCIVA
if (crypto_scrypt( (const uint8_t *)secret, strlen( secret ), salt, saltSize, N, r, p, key, keySize ) < 0) {
mpw_free( key, keySize );
return NULL;
}
#elif HAS_SCRYPT_SODIUM
if (crypto_pwhash_scryptsalsa208sha256_ll( (const uint8_t *)secret, strlen( secret ), salt, saltSize, N, r, p, key, keySize) != 0 ) {
mpw_free( key, keySize );
return NULL;
}
#endif
return key;
}
uint8_t const *mpw_hmac_sha256(const uint8_t *key, const size_t keySize, const uint8_t *salt, const size_t saltSize) {
#if HAS_CPERCIVA
uint8_t *const buffer = malloc( 32 );
if (!buffer)
return NULL;
HMAC_SHA256_Buf( key, keySize, salt, saltSize, buffer );
return buffer;
#elif HAS_SCRYPT_SODIUM
uint8_t *const buffer = malloc( crypto_auth_hmacsha256_BYTES );
if (!buffer)
return NULL;
crypto_auth_hmacsha256_state state;
if (crypto_auth_hmacsha256_init( &state, key, keySize ) != 0 ||
crypto_auth_hmacsha256_update( &state, salt, saltSize ) != 0 ||
crypto_auth_hmacsha256_final( &state, buffer ) != 0) {
mpw_free( buffer, crypto_auth_hmacsha256_BYTES );
return NULL;
}
return buffer;
#endif
return NULL;
}
const char *mpw_id_buf(const void *buf, size_t length) {
#if HAS_CPERCIVA
uint8_t hash[32];
SHA256_Buf( buf, length, hash );
return mpw_hex( hash, 32 );
#elif HAS_SCRYPT_SODIUM
uint8_t hash[crypto_hash_sha256_BYTES];
crypto_hash_sha256( hash, buf, length );
return mpw_hex( hash, crypto_hash_sha256_BYTES );
#endif
}
static char **mpw_hex_buf = NULL;
@ -147,7 +188,7 @@ static int initputvar() {
free( putvarc );
if (!termsetup) {
int status;
if (! (termsetup = (setupterm(NULL, STDERR_FILENO, &status) == OK && status == 1))) {
if (! (termsetup = (setupterm( NULL, STDERR_FILENO, &status ) == 0 && status == 1))) {
wrn( "Terminal doesn't support color (setupterm errno %d).\n", status );
return 0;
}
@ -174,8 +215,9 @@ const char *mpw_identicon(const char *fullName, const char *masterPassword) {
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""
};
uint8_t identiconSeed[32];
HMAC_SHA256_Buf( masterPassword, strlen( masterPassword ), fullName, strlen( fullName ), identiconSeed );
const uint8_t *identiconSeed = mpw_hmac_sha256( (const uint8_t *)masterPassword, strlen( masterPassword ), (const uint8_t *)fullName, strlen( fullName ) );
if (!identiconSeed)
return NULL;
char *colorString, *resetString;
#ifdef COLOR
@ -203,6 +245,7 @@ const char *mpw_identicon(const char *fullName, const char *masterPassword) {
accessory[identiconSeed[3] % (sizeof( accessory ) / sizeof( accessory[0] ))],
resetString );
mpw_free( identiconSeed, 32 );
free( colorString );
free( resetString );
return identicon;

View File

@ -22,7 +22,7 @@
//// Logging.
#ifndef trc
int mpw_verbosity;
extern int mpw_verbosity;
#define trc_level 3
#define trc(...) \
if (mpw_verbosity >= 3) \

View File

@ -3916,6 +3916,7 @@
"\"$(SRCROOT)/External/Pearl/Pearl-Crypto/lib\"",
"$(inherited)",
);
OTHER_CFLAGS = "-DHAS_CPERCIVA=1";
OTHER_LDFLAGS = "$(inherited)";
"OTHER_LDFLAGS[sdk=iphoneos*]" = (
"$(inherited)",
@ -4192,6 +4193,7 @@
"\"$(SRCROOT)/External/Pearl/Pearl-Crypto/lib\"",
"$(inherited)",
);
OTHER_CFLAGS = "-DHAS_CPERCIVA=1";
OTHER_LDFLAGS = "$(inherited)";
"OTHER_LDFLAGS[sdk=iphoneos*]" = (
"$(inherited)",
@ -4230,6 +4232,7 @@
"\"$(SRCROOT)/External/Pearl/Pearl-Crypto/lib\"",
"$(inherited)",
);
OTHER_CFLAGS = "-DHAS_CPERCIVA=1";
OTHER_LDFLAGS = "$(inherited)";
"OTHER_LDFLAGS[sdk=iphoneos*]" = (
"$(inherited)",

View File

@ -23,6 +23,13 @@
93D39F833DEC1C89B2F795AC /* MPPasswordWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D39A57A7823DE98A0FF83C /* MPPasswordWindowController.m */; };
DA0933CC1747AD2D00DE1CEF /* shot-laptop-leaning-iphone.png in Resources */ = {isa = PBXBuildFile; fileRef = DA0933CB1747AD2D00DE1CEF /* shot-laptop-leaning-iphone.png */; };
DA0933D01747B91B00DE1CEF /* appstore.png in Resources */ = {isa = PBXBuildFile; fileRef = DA0933CF1747B91B00DE1CEF /* appstore.png */; };
DA0974541E9957F300F0BFE8 /* libopensslcrypto-osx.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DAE8E65319867ADA00416A0F /* libopensslcrypto-osx.a */; };
DA0974551E9957F300F0BFE8 /* libscryptenc-osx.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DA5E5C8717248AA1003798D8 /* libscryptenc-osx.a */; };
DA09745A1E99582900F0BFE8 /* mpw-tests-util.c in Sources */ = {isa = PBXBuildFile; fileRef = DA0974561E99582200F0BFE8 /* mpw-tests-util.c */; };
DA09745B1E99582900F0BFE8 /* mpw-tests.c in Sources */ = {isa = PBXBuildFile; fileRef = DA0974571E99582200F0BFE8 /* mpw-tests.c */; };
DA09745E1E99586600F0BFE8 /* libxml2.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = DA09745D1E99586600F0BFE8 /* libxml2.tbd */; };
DA0974621E9961F100F0BFE8 /* libsodium.18.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = DA0974611E9961F100F0BFE8 /* libsodium.18.dylib */; };
DA0974641E99620400F0BFE8 /* libscrypt.0.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = DA0974631E99620400F0BFE8 /* libscrypt.0.dylib */; };
DA10007F1998A4C6002B873F /* scrypt in Headers */ = {isa = PBXBuildFile; fileRef = DAE8E65619867AF500416A0F /* scrypt */; settings = {ATTRIBUTES = (Public, ); }; };
DA1000801998A4C6002B873F /* openssl in Headers */ = {isa = PBXBuildFile; fileRef = DAE8E65719867AF500416A0F /* openssl */; settings = {ATTRIBUTES = (Public, ); }; };
DA16B341170661DB000A0EAB /* Carbon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA16B340170661DB000A0EAB /* Carbon.framework */; };
@ -285,6 +292,13 @@
DA0933C91747A56A00DE1CEF /* MPInitialWindow.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MPInitialWindow.xib; sourceTree = "<group>"; };
DA0933CB1747AD2D00DE1CEF /* shot-laptop-leaning-iphone.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "shot-laptop-leaning-iphone.png"; sourceTree = "<group>"; };
DA0933CF1747B91B00DE1CEF /* appstore.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = appstore.png; sourceTree = "<group>"; };
DA0974561E99582200F0BFE8 /* mpw-tests-util.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = "mpw-tests-util.c"; path = "../../platform-independent/cli-c/cli/mpw-tests-util.c"; sourceTree = "<group>"; };
DA0974571E99582200F0BFE8 /* mpw-tests.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = "mpw-tests.c"; path = "../../platform-independent/cli-c/cli/mpw-tests.c"; sourceTree = "<group>"; };
DA09745C1E99583B00F0BFE8 /* mpw-tests-util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "mpw-tests-util.h"; path = "../../platform-independent/cli-c/cli/mpw-tests-util.h"; sourceTree = "<group>"; };
DA09745D1E99586600F0BFE8 /* libxml2.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libxml2.tbd; path = usr/lib/libxml2.tbd; sourceTree = SDKROOT; };
DA09745F1E995EB500F0BFE8 /* mpw_tests.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; name = mpw_tests.xml; path = ../../core/java/tests/src/main/resources/mpw_tests.xml; sourceTree = "<group>"; };
DA0974611E9961F100F0BFE8 /* libsodium.18.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsodium.18.dylib; path = ../../../../../../../usr/local/Cellar/libsodium/1.0.12/lib/libsodium.18.dylib; sourceTree = "<group>"; };
DA0974631E99620400F0BFE8 /* libscrypt.0.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libscrypt.0.dylib; path = ../../../../../../../usr/local/Cellar/libscrypt/1.21/lib/libscrypt.0.dylib; sourceTree = "<group>"; };
DA16B340170661DB000A0EAB /* Carbon.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Carbon.framework; path = System/Library/Frameworks/Carbon.framework; sourceTree = SDKROOT; };
DA16B343170661EE000A0EAB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
DA2508F019511D3600AC23F1 /* MPPasswordWindowController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MPPasswordWindowController.xib; sourceTree = "<group>"; };
@ -945,6 +959,11 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
DA0974641E99620400F0BFE8 /* libscrypt.0.dylib in Frameworks */,
DA0974621E9961F100F0BFE8 /* libsodium.18.dylib in Frameworks */,
DA09745E1E99586600F0BFE8 /* libxml2.tbd in Frameworks */,
DA0974541E9957F300F0BFE8 /* libopensslcrypto-osx.a in Frameworks */,
DA0974551E9957F300F0BFE8 /* libscryptenc-osx.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -1025,6 +1044,9 @@
DA5BFA47147E415C00F98B1E /* Frameworks */ = {
isa = PBXGroup;
children = (
DA0974631E99620400F0BFE8 /* libscrypt.0.dylib */,
DA0974611E9961F100F0BFE8 /* libsodium.18.dylib */,
DA09745D1E99586600F0BFE8 /* libxml2.tbd */,
DA6701B716406A4100B61001 /* Accounts.framework */,
DA16B340170661DB000A0EAB /* Carbon.framework */,
DA16B343170661EE000A0EAB /* Cocoa.framework */,
@ -1552,6 +1574,9 @@
DA831A2A1A6E1146000AC234 /* mpw-algorithm_v3.c */,
DA6773BB1A4746AF004F356A /* mpw-algorithm.c */,
DA6773BC1A4746AF004F356A /* mpw-algorithm.h */,
DA0974561E99582200F0BFE8 /* mpw-tests-util.c */,
DA09745C1E99583B00F0BFE8 /* mpw-tests-util.h */,
DA0974571E99582200F0BFE8 /* mpw-tests.c */,
DA6773C21A4746AF004F356A /* mpw-types.c */,
DA6773C31A4746AF004F356A /* mpw-types.h */,
DA6773C51A4746AF004F356A /* mpw-util.c */,
@ -1614,6 +1639,7 @@
DACA23B41705DF7D002C6C22 /* Resources */ = {
isa = PBXGroup;
children = (
DA09745F1E995EB500F0BFE8 /* mpw_tests.xml */,
DACA26991705DF81002C6C22 /* Crashlytics */,
DACA29701705E1A8002C6C22 /* Data */,
DACA23B51705DF7D002C6C22 /* Media */,
@ -2252,6 +2278,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
DA09745B1E99582900F0BFE8 /* mpw-tests.c in Sources */,
DA09745A1E99582900F0BFE8 /* mpw-tests-util.c in Sources */,
DA6774451A474A3B004F356A /* mpw-types.c in Sources */,
DA6774461A474A3B004F356A /* mpw-util.c in Sources */,
DA6774431A474A3B004F356A /* mpw-algorithm.c in Sources */,
@ -2471,6 +2499,7 @@
"\"$(SRCROOT)/External/Pearl/Pearl-Crypto/lib\"",
"$(inherited)",
);
OTHER_CFLAGS = "-DHAS_CPERCIVA=1";
PRODUCT_BUNDLE_IDENTIFIER = com.lyndir.lhunath.MasterPassword.Mac;
SKIP_INSTALL = NO;
WRAPPER_NAME = "Master Password.${WRAPPER_EXTENSION}";
@ -2517,8 +2546,19 @@
CLANG_WARN_DOCUMENTATION_COMMENTS = NO;
HEADER_SEARCH_PATHS = (
"$(inherited)",
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
/usr/include/libxml2,
/usr/local/include,
);
LIBRARY_SEARCH_PATHS = (
"$(SRCROOT)/External/Pearl/Pearl-Crypto/lib",
"$(inherited)",
"$(PROJECT_DIR)/External/Pearl/Pearl-Crypto/lib",
/usr/local/Cellar/libsodium/1.0.12/lib,
/usr/local/Cellar/libscrypt/1.21/lib,
);
OTHER_CFLAGS = (
"-DHAS_CPERCIVA=0",
"-DHAS_SCRYPT_SODIUM=1",
);
};
name = Test;
@ -2727,6 +2767,7 @@
"\"$(SRCROOT)/External/Pearl/Pearl-Crypto/lib\"",
"$(inherited)",
);
OTHER_CFLAGS = "-DHAS_CPERCIVA=1";
PRODUCT_BUNDLE_IDENTIFIER = com.lyndir.lhunath.MasterPassword.Mac;
SKIP_INSTALL = NO;
WRAPPER_NAME = "Master Password.${WRAPPER_EXTENSION}";
@ -2754,6 +2795,7 @@
"\"$(SRCROOT)/External/Pearl/Pearl-Crypto/lib\"",
"$(inherited)",
);
OTHER_CFLAGS = "-DHAS_CPERCIVA=1";
PRODUCT_BUNDLE_IDENTIFIER = com.lyndir.lhunath.MasterPassword.Mac;
SKIP_INSTALL = NO;
WRAPPER_NAME = "Master Password.${WRAPPER_EXTENSION}";
@ -2766,8 +2808,19 @@
CLANG_WARN_DOCUMENTATION_COMMENTS = NO;
HEADER_SEARCH_PATHS = (
"$(inherited)",
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
/usr/include/libxml2,
/usr/local/include,
);
LIBRARY_SEARCH_PATHS = (
"$(SRCROOT)/External/Pearl/Pearl-Crypto/lib",
"$(inherited)",
"$(PROJECT_DIR)/External/Pearl/Pearl-Crypto/lib",
/usr/local/Cellar/libsodium/1.0.12/lib,
/usr/local/Cellar/libscrypt/1.21/lib,
);
OTHER_CFLAGS = (
"-DHAS_CPERCIVA=0",
"-DHAS_SCRYPT_SODIUM=1",
);
};
name = Debug;
@ -2778,8 +2831,19 @@
CLANG_WARN_DOCUMENTATION_COMMENTS = NO;
HEADER_SEARCH_PATHS = (
"$(inherited)",
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
/usr/include/libxml2,
/usr/local/include,
);
LIBRARY_SEARCH_PATHS = (
"$(SRCROOT)/External/Pearl/Pearl-Crypto/lib",
"$(inherited)",
"$(PROJECT_DIR)/External/Pearl/Pearl-Crypto/lib",
/usr/local/Cellar/libsodium/1.0.12/lib,
/usr/local/Cellar/libscrypt/1.21/lib,
);
OTHER_CFLAGS = (
"-DHAS_CPERCIVA=0",
"-DHAS_SCRYPT_SODIUM=1",
);
};
name = Release;

View File

@ -46,7 +46,8 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
useCustomWorkingDirectory = "YES"
customWorkingDirectory = "/Users/lhunath/Documents/workspace/lyndir/MasterPassword/platform-independent/cli-c"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"

View File

@ -41,13 +41,11 @@ fi
# Optional features.
mpw_color=${mpw_color:-1} # Colorized Identicon, requires libncurses-dev
# Distribution specific configuration.
# Homebrew
if hash brew 2>/dev/null; then
opensslPath=$(brew --prefix openssl)
export CFLAGS="$CFLAGS -I$opensslPath/include"
export LDFLAGS="$LDFLAGS -L$opensslPath/lib"
fi
# Default build flags.
export CFLAGS="-O3 $CFLAGS"
export LDFLAGS="$LDFLAGS"
### DEPENDENCIES
@ -208,26 +206,40 @@ depend() {
popd
popd
}
depend_scrypt() {
if haslib scrypt && haslib sodium; then
CFLAGS+=" -DHAS_SCRYPT_SODIUM=1"
LDFLAGS+=" -lscrypt -lsodium"
return
fi
depend scrypt
local objects=(
"lib/scrypt/src/libcperciva/"*/*.o
"lib/scrypt/src/lib/crypto/"*.o
)
CFLAGS+=" -DHAS_CPERCIVA=1"
LDFLAGS+=" -Llib/scrypt/src ${objects[*]}"
}
### MPW
mpw() {
depend scrypt
depend_scrypt
echo
echo "Building target: $target..."
local CFLAGS=(
$CFLAGS
# library paths
-I"lib/include"
# mpw paths
-I"core" -I"cli"
)
local LDFLAGS=(
# scrypt
"lib/scrypt/src/libcperciva/"*/*.o
"lib/scrypt/src/lib/crypto/"*.o
# library paths
-L"lib/scrypt/src"
$LDFLAGS
# link libraries
-l"crypto"
)
@ -245,28 +257,28 @@ mpw() {
### MPW-BENCH
mpw-bench() {
depend scrypt
depend_scrypt
depend bcrypt
echo
echo "Building target: $target..."
local CFLAGS=(
$CFLAGS
# library paths
-I"lib/include"
# mpw paths
-I"core" -I"cli"
)
local LDFLAGS=(
# scrypt
"lib/scrypt/src/libcperciva/"*/*.o
"lib/scrypt/src/lib/crypto/"*.o
$LDFLAGS
# bcrypt
"lib/bcrypt/src/crypt_blowfish.o"
"lib/bcrypt/src/crypt_gensalt.o"
"lib/bcrypt/src/wrapper.o"
"lib/bcrypt/src/x86.o"
# library paths
-L"lib/scrypt/src"
-L"lib/bcrypt/src"
# link libraries
-l"crypto"
@ -283,11 +295,13 @@ mpw-bench() {
### MPW-TESTS
mpw-tests() {
depend scrypt
depend_scrypt
echo
echo "Building target: $target..."
local CFLAGS=(
$CFLAGS
# library paths
-I"lib/include"
-I"/usr/include/libxml2"
@ -296,11 +310,8 @@ mpw-tests() {
-I"core" -I"cli"
)
local LDFLAGS=(
# scrypt
"lib/scrypt/src/libcperciva/"*/*.o
"lib/scrypt/src/lib/crypto/"*.o
# library paths
-L"lib/scrypt/src"
$LDFLAGS
# link libraries
-l"crypto" -l"xml2"
)

View File

@ -13,7 +13,6 @@
#include <errno.h>
#include <sys/time.h>
#include <scrypt/sha256.h>
#include <bcrypt/ow-crypt.h>
#include "mpw-algorithm.h"

View File

@ -213,8 +213,12 @@ int main(int argc, char *const argv[]) {
// Summarize operation.
const char *identicon = mpw_identicon( fullName, masterPassword );
if (!identicon) {
err( "Couldn't determine identicon.\n" );
} else {
fprintf( stderr, "%s's password for %s:\n[ %s ]: ", fullName, siteName, identicon );
mpw_free_string( identicon );
}
// Output the password.
const uint8_t *masterKey = mpw_masterKeyForUser(

View File

@ -15,6 +15,11 @@ int main(int argc, char *const argv[]) {
int failedTests = 0;
xmlNodePtr tests = xmlDocGetRootElement( xmlParseFile( "mpw_tests.xml" ) );
if (!tests) {
ftl( "Couldn't find test case: mpw_tests.xml\n" );
abort();
}
for (xmlNodePtr testCase = tests->children; testCase; testCase = testCase->next) {
if (testCase->type != XML_ELEMENT_NODE || xmlStrcmp( testCase->name, BAD_CAST "case" ) != 0)
continue;