Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-add EdDSA support + static libsodium #67

Merged
merged 5 commits into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ cabal-dev
.cabal-sandbox
cabal.sandbox.config
/tmp
/gen

123 changes: 123 additions & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#!/usr/bin/env runhaskell

import Data.Char (isDigit, toLower)
import Data.Function (on)
import Data.List (intercalate, sortBy)
import Data.Monoid ((<>))
import Data.Version (showVersion)

import Distribution.InstalledPackageInfo
import Distribution.PackageDescription
import Distribution.Simple
import Distribution.Simple.Setup (BuildFlags(..), ReplFlags(..), TestFlags(..), fromFlag)
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.PackageIndex
import Distribution.Simple.BuildPaths (autogenModulesDir)
import Distribution.Simple.Utils (createDirectoryIfMissingVerbose, rewriteFile, rawSystemStdout)
import Distribution.Verbosity

import System.Directory (createDirectoryIfMissing, getCurrentDirectory, setCurrentDirectory)
import System.FilePath ((</>))
import System.Process (callProcess)

main :: IO ()
main =
let hooks = simpleUserHooks
in defaultMainWithHooks hooks {
preConf = \args flags -> do
createDirectoryIfMissingVerbose silent True "gen"
(preConf hooks) args flags
, sDistHook = \pd mlbi uh flags -> do
genBuildInfo silent pd
(sDistHook hooks) pd mlbi uh flags
, buildHook = \pd lbi uh flags -> do
genBuildInfo (fromFlag $ buildVerbosity flags) pd
genDependencyInfo (fromFlag $ buildVerbosity flags) pd lbi
buildLibSodium
(buildHook hooks) pd lbi uh flags
, replHook = \pd lbi uh flags args -> do
genBuildInfo (fromFlag $ replVerbosity flags) pd
genDependencyInfo (fromFlag $ replVerbosity flags) pd lbi
(replHook hooks) pd lbi uh flags args
, testHook = \args pd lbi uh flags -> do
genBuildInfo (fromFlag $ testVerbosity flags) pd
genDependencyInfo (fromFlag $ testVerbosity flags) pd lbi
(testHook hooks) args pd lbi uh flags
}

buildLibSodium :: IO ()
buildLibSodium = do
cwd <- getCurrentDirectory
let
sodiumDir = cwd </> "gen" </> "libsodium"
createDirectoryIfMissing True sodiumDir
setCurrentDirectory $ cwd </> "lib" </> "libsodium"
callProcess "./configure" ["--prefix=" <> sodiumDir]
callProcess "make" ["-j"]
callProcess "make" ["install"]
setCurrentDirectory cwd

genBuildInfo :: Verbosity -> PackageDescription -> IO ()
genBuildInfo verbosity pkg = do
createDirectoryIfMissingVerbose verbosity True "gen"
let (PackageName pname) = pkgName . package $ pkg
version = pkgVersion . package $ pkg
name = "BuildInfo_" ++ (map (\c -> if c == '-' then '_' else c) pname)
targetHs = "gen/" ++ name ++ ".hs"
targetText = "gen/version.txt"
t <- timestamp verbosity
gv <- gitVersion verbosity
let v = showVersion version
let buildVersion = intercalate "-" [v, t, gv]
rewriteFile targetHs $ unlines [
"module " ++ name ++ " where"
, "import Prelude"
, "data RuntimeBuildInfo = RuntimeBuildInfo { buildVersion :: String, timestamp :: String, gitVersion :: String }"
, "buildInfo :: RuntimeBuildInfo"
, "buildInfo = RuntimeBuildInfo \"" ++ v ++ "\" \"" ++ t ++ "\" \"" ++ gv ++ "\""
, "buildInfoVersion :: String"
, "buildInfoVersion = \"" ++ buildVersion ++ "\""
]
rewriteFile targetText buildVersion

genDependencyInfo :: Verbosity -> PackageDescription -> LocalBuildInfo -> IO ()
genDependencyInfo verbosity pkg info = do
let
(PackageName pname) = pkgName . package $ pkg
name = "DependencyInfo_" ++ (map (\c -> if c == '-' then '_' else c) pname)
targetHs = autogenModulesDir info ++ "/" ++ name ++ ".hs"
render p =
let
n = unPackageName $ pkgName p
v = intercalate "." . fmap show . versionBranch $ pkgVersion p
in
n ++ "-" ++ v
deps = fmap (render . sourcePackageId) . allPackages $ installedPkgs info
sdeps = sortBy (compare `on` fmap toLower) deps
strs = flip fmap sdeps $ \d -> "\"" ++ d ++ "\""

createDirectoryIfMissingVerbose verbosity True (autogenModulesDir info)

rewriteFile targetHs $ unlines [
"module " ++ name ++ " where"
, "import Prelude"
, "dependencyInfo :: [String]"
, "dependencyInfo = [\n " ++ intercalate "\n , " strs ++ "\n ]"
]

gitVersion :: Verbosity -> IO String
gitVersion verbosity = do
ver <- rawSystemStdout verbosity "git" ["log", "--pretty=format:%h", "-n", "1"]
notModified <- ((>) 1 . length) `fmap` rawSystemStdout verbosity "git" ["status", "--porcelain"]
return $ ver ++ if notModified then "" else "-M"

timestamp :: Verbosity -> IO String
timestamp verbosity =
rawSystemStdout verbosity "date" ["+%Y%m%d%H%M%S"] >>= \s ->
case splitAt 14 s of
(d, n : []) ->
if (length d == 14 && filter isDigit d == d)
then return d
else fail $ "date has failed to produce the correct format [" <> s <> "]."
_ ->
fail $ "date has failed to produce a date long enough [" <> s <> "]."
19 changes: 14 additions & 5 deletions ambiata-tinfoil.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ copyright: (c) 2015 Ambiata.
synopsis: Paranoid crypto primitives
category: System
cabal-version: >= 1.8
build-type: Simple
build-type: Custom
description: Primitives for cryptographic random number generation,
key deriviation, credential storage and verification,
et cetera.
Expand Down Expand Up @@ -56,11 +56,14 @@ library
Tinfoil.MAC
Tinfoil.Random
Tinfoil.Random.Internal
Tinfoil.Signing.Ed25519
Tinfoil.Signing.Ed25519.Internal
Tinfoil.Token

c-sources:
-- tinfoil's own c bits
cbits/tinfoil/memory.c
cbits/tinfoil/sodium/constants.c

-- scrypt (https://github.com/Tarsnap/scrypt)
, cbits/scrypt/insecure_memzero.c
Expand All @@ -71,11 +74,17 @@ library
, cbits/scrypt/crypto_scrypt_smix.c
, cbits/scrypt/crypto_scrypt_smix_sse2.c
, cbits/scrypt/crypto_scrypt.c
cc-options: -msse2

cc-options: -msse2 -Wall -Wextra

include-dirs: cbits/scrypt
, cbits/tinfoil
, gen/libsodium/include

includes: crypto_scrypt.h
, tinfoil.h
, sodium.h

install-includes: crypto_scrypt.h
, tinfoil.h

Expand All @@ -84,7 +93,7 @@ test-suite test

main-is: test.hs

ghc-options: -Wall -threaded -O2
ghc-options: -Wall -threaded -O2 -pgml ./bin/salted-gcc

hs-source-dirs:
test
Expand All @@ -107,7 +116,7 @@ test-suite test-io

main-is: test-io.hs

ghc-options: -Wall -threaded -O2
ghc-options: -Wall -threaded -O2 -pgml ./bin/salted-gcc

hs-source-dirs:
test
Expand Down Expand Up @@ -154,7 +163,7 @@ benchmark bench

main-is: bench.hs

ghc-options: -Wall -threaded -O2
ghc-options: -Wall -threaded -O2 -pgml ./bin/salted-gcc
Copy link

@erikd-ambiata erikd-ambiata Dec 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want -Wextra there as well just to see the extra warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-Wextra for the C code right? You're right, but shouldn't it go in cc-opts?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, misread that, but -Wextra for the C code, always :).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, added, annoyed I didn't have it on from the start.


hs-source-dirs:
test
Expand Down
5 changes: 5 additions & 0 deletions bin/salted-gcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /bin/sh -eux

echo "$@" | grep -q --version \
&& gcc $@ \
|| gcc $@ "$(pwd)/gen/libsodium/lib/libsodium.a"
16 changes: 16 additions & 0 deletions cbits/tinfoil/sodium/constants.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <sodium.h>

#include "constants.h"

size_t tinfoil_sodium_pubkey_len(void) {
return crypto_sign_PUBLICKEYBYTES;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need void in the (empty) parameter list when doing C.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep thanks, I thought I'd fixed that here but must have missed a few functions.

}

size_t tinfoil_sodium_seckey_len(void) {
return crypto_sign_SECRETKEYBYTES;
}

size_t tinfoil_sodium_sig_len(void) {
return crypto_sign_BYTES;
}

14 changes: 14 additions & 0 deletions cbits/tinfoil/sodium/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef H_TINFOIL_SODIUM_CONSTANTS
#define H_TINFOIL_SODIUM_CONSTANTS

#include <stdlib.h>

#include <sodium.h>

size_t tinfoil_sodium_pubkey_len(void);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

size_t tinfoil_sodium_seckey_len(void);

size_t tinfoil_sodium_sig_len(void);

#endif
1 change: 1 addition & 0 deletions cbits/tinfoil/tinfoil.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
#define H_TINFOIL

#include "memory.h"
#include "sodium/constants.h"

#endif
125 changes: 125 additions & 0 deletions lib/libsodium/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
*.bc
*.cmake
*.dSYM
*.done
*.final
*.gcda
*.gcno
*.i
*.la
*.lo
*.log
*.mem
*.nexe
*.o
*.plist
*.s
*.scan
*.sdf
*.status
*.tar.*
*~
.DS_Store
.deps
.dirstamp
.done
.libs
/bin/
/obj/
Build
INSTALL
Makefile
Vagrantfile
aclocal.m4
android-toolchain
android-toolchain-*
autom4te.cache
build
confdefs.h
coverage.info
libsodium-*.tar.bz2
libsodium-*.tar.gz
libsodium-*.vcproj
libsodium-*.vcproj.filters
libsodium-*.vcxproj
libsodium-*.vcxproj.filters
libsodium-android-*
libsodium-ios
libsodium-js
libsodium-js-*
libsodium-nativeclient
libsodium-nativeclient-*
libsodium-osx
libsodium-uninstalled.pc
libsodium-win32
libsodium-win64
libsodium.pc
libtool
man/*.html
src/libsodium/*.def
src/libsodium/include/sodium/version.h
stamp-*
test/default/browser
test/default/*.res
test/default/*.trs
test/default/aead_aes256gcm
test/default/aead_chacha20poly1305
test/default/auth
test/default/auth2
test/default/auth3
test/default/auth5
test/default/auth6
test/default/auth7
test/default/box
test/default/box2
test/default/box7
test/default/box8
test/default/box_easy
test/default/box_easy2
test/default/box_seal
test/default/box_seed
test/default/chacha20
test/default/core1
test/default/core2
test/default/core3
test/default/core4
test/default/core5
test/default/core6
test/default/ed25519_convert
test/default/generichash
test/default/generichash2
test/default/generichash3
test/default/hash
test/default/hash3
test/default/onetimeauth
test/default/onetimeauth2
test/default/onetimeauth7
test/default/pwhash
test/default/pwhash_scrypt
test/default/pwhash_scrypt_ll
test/default/randombytes
test/default/scalarmult
test/default/scalarmult2
test/default/scalarmult5
test/default/scalarmult6
test/default/scalarmult7
test/default/secretbox
test/default/secretbox2
test/default/secretbox7
test/default/secretbox8
test/default/secretbox_easy
test/default/secretbox_easy2
test/default/shorthash
test/default/sign
test/default/sodium_core
test/default/sodium_utils
test/default/sodium_utils2
test/default/sodium_utils3
test/default/sodium_version
test/default/stream
test/default/stream2
test/default/stream3
test/default/stream4
test/default/verify1
test/js.done
testing
Loading