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

Add interface to TweetNaCl library #566

Merged
merged 37 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4edc9b5
Add TweetNaCl library
noloader Jan 15, 2018
a6adcc0
Re-add C++11 lambda detection
noloader Jan 15, 2018
fe88ae3
Add TweetNaCl to Visual Studio and FileList
noloader Jan 15, 2018
e3680a3
Udate documentation
noloader Jan 15, 2018
30396ee
Merge branch 'master' of ssh://github.com/weidai11/cryptopp
noloader Jan 15, 2018
f36aea1
Fix Travis testing with OS X 8.3 images
noloader Jan 15, 2018
9f9fe2a
Add missing crypto_box_BEFORENMBYTES
noloader Jan 15, 2018
dcac266
Update documentation
noloader Jan 15, 2018
203b9f2
Fix Visual Studio project files
noloader Jan 15, 2018
aa38b2b
Update documentation
noloader Jan 15, 2018
c3ce204
Update documentation
noloader Jan 15, 2018
c1381e0
Clear Visual Studio warnings
noloader Jan 15, 2018
18bc00c
Fix Visual Studio compile
noloader Jan 15, 2018
46ff44a
Update documentation
noloader Jan 15, 2018
dcfec9d
Add initial NaCl self tests
noloader Jan 15, 2018
09f47eb
Add validat4.cpp to Filelist.txt
noloader Jan 15, 2018
e0b21bd
Update tweetnacl.cpp to test for 0-key per RFC 7748
noloader Jan 16, 2018
3da7de0
Fix line endings. Ugh...
noloader Jan 16, 2018
1109852
Whitespace cleanup
noloader Jan 16, 2018
17172f4
Add CryptoBox and CryptoBoxOpen self tests
noloader Jan 16, 2018
a30a5fa
Add missing crypto_box_BOXZEROBYTES constant
noloader Jan 16, 2018
d46eb2e
Switch to cURL for source file downloads
noloader Jan 16, 2018
0e98baf
Update documentation
noloader Jan 16, 2018
94b6dd7
Update documentation
noloader Jan 16, 2018
c5fb5be
Update documentation
noloader Jan 16, 2018
5fcec08
Add CRYPTOPP_DISABLE_NACL guard
noloader Jan 16, 2018
e1096ad
Add NaCl gear to cryptest.nmake
noloader Jan 16, 2018
4073f1d
Add missing crypto_box_MACBYTES define
noloader Jan 16, 2018
ccdf6e9
Merge branch 'master' of ssh://github.com/weidai11/cryptopp
noloader Jan 17, 2018
bb3483d
Add patch file to Makefile conversion
noloader Jan 17, 2018
6fb023f
Add missing crypto_sign_SEEDBYTES define
noloader Jan 17, 2018
78146f0
Add CryptoSign and CryptoSignOpen self tests
noloader Jan 17, 2018
0e65592
Update documentation
noloader Jan 17, 2018
f2e5251
Whitespace check-in
noloader Jan 17, 2018
b19897e
Merge branch 'master' of ssh://github.com/weidai11/cryptopp
noloader Jan 18, 2018
5be0c0a
Clear UBSan findings
noloader Jan 18, 2018
4dbcc75
Update patch
noloader Jan 18, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add TweetNaCl library
This is Bernstein's compact, portable NaCl library. According to his paper on the library, it is 2x to 4x faster than the functions it was intended to replace, like RSA. However, it is also 2x to 4x times slower than optimized versions of NaCl algorithms
  • Loading branch information
noloader committed Jan 15, 2018
commit 4edc9b5f3d4a988dae470de899b2fcf409b9204e
187 changes: 187 additions & 0 deletions TestScripts/tweetnacl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#!/usr/bin/env bash

# Written and placed in public domain by Jeffrey Walton
#
# This script fetches TweetNaCl from Bernstein's site, and then
# prepares it for use in Crypto++. The script should be run from
# the Crypto++ root directory on a Unix machine because of the
# use of wget, sed, awk and friends.

wget --no-check-certificate https://tweetnacl.cr.yp.to/20140427/tweetnacl.h -O tweetnacl.h
wget --no-check-certificate https://tweetnacl.cr.yp.to/20140427/tweetnacl.c -O tweetnacl.c

########## Remove unwanted stuff ##########

echo "Removing tweetnacl.h header"
sed -e '/#include "tweetnacl.h"/d' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

echo "Removing data type typedefs"
sed -e '/typedef unsigned char u8;/d' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '/typedef unsigned long u32;/d' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '/typedef unsigned long long u64;/d' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '/typedef long long i64;/d' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

sed -e '/#define FOR(i,n)/d' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

echo "Removing random number generator"
sed -e '/extern void randombytes/d' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

########## Add wanted stuff ##########

echo "Adding headnotes"
sed -e '1i// tweetnacl.cpp - modified tweetnacl.c and placed in public domain by Jeffrey Walton' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '2i// tweetnacl.c written by Daniel J. Bernstein, Bernard van Gastel,' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '3i// Wesley Janssen, Tanja Lange, Peter Schwabe and Sjaak Smetsers' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '4i
' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

echo "Adding headers"
sed -e '5i#include "pch.h"' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '6i#include "config.h"' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '7i#include "nacl.h"' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '8i#include "misc.h"' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '9i#include "osrng.h"' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '10i#include "stdcpp.h"' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '11i
' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

echo "Adding opening namespace"
sed -e '13iNAMESPACE_BEGIN(CryptoPP)' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e '14iNAMESPACE_BEGIN(NaCl)' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

echo "Adding random number generator"
sed -e '33istatic void randombytes(uint8_t * block, uint64_t size)\
{\
DefaultAutoSeededRNG prng;\
prng.GenerateBlock\(block, size\);\
}\
' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

########## Fix other stuff ##########

echo "Fixing data types"
sed -e 's/u8/uint8_t/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/u16/uint16_t/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/u32/uint32_t/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/u64/uint64_t/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/i64/int64_t/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

echo "Fixing uninitalized variables"
sed -e 's/_0\[16\],$/_0\[16\] = {0},/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/gf0,$/gf0 = {0},/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

echo "Fixing for loops"
sed -e 's/FOR(i,n)/for(i=0; i<n; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(i,b)/for(i=0; i<b; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(j,2)/for(j=0; j<2; ++j)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(i,4)/for(i=0; i<4; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(j,4)/for(j=0; j<4; ++j)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(m,4)/for(m=0; m<4; ++m)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(i,8)/for(i=0; i<8; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(j,8)/for(j=0; j<8; ++j)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(i,15)/for(i=0; i<15; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(a,16)/for(a=0; a<16; ++a)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(i,16)/for(i=0; i<16; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(j,16)/for(j=0; j<16; ++j)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(m,16)/for(m=0; m<16; ++m)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(i,17)/for(i=0; i<17; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(j,17)/for(j=0; j<17; ++j)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(i,20)/for(i=0; i<20; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(i,31)/for(i=0; i<31; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(i,32)/for(i=0; i<32; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(j,32)/for(j=0; j<32; ++j)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(i,64)/for(i=0; i<64; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(i,80)/for(i=0; i<80; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/FOR(i,256)/for(i=0; i<256; ++i)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

sed -e 's/int n)/uint32_t n)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/int64_t i,j,x\[64\];/uint64_t i; int64_t j,x\[64\];/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

echo "Fixing initializer string"
sed -e 's/"expand 32-byte k"/{0x65,0x78,0x70,0x61,0x6E,0x64,0x20,0x33,0x32,0x2D,0x62,0x79,0x74,0x65,0x20,0x6B}/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

echo "Improving readibility"
sed -e '/#define sv static void/d' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/sv/static void/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/vn/verify_n/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

echo "Adding closing namespace"
echo "" >> tweetnacl.c
echo "NAMESPACE_END // CryptoPP" >> tweetnacl.c
echo "NAMESPACE_END // NaCl" >> tweetnacl.c

echo "Table of 64-bit constants"
sed -e 's/0x[0-9a-f]\{16\}ULL/W64LIT(&)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c
sed -e 's/ULL)/)/g' tweetnacl.c > tweetnacl.fixed
mv tweetnacl.fixed tweetnacl.c

########## Cleanup ##########

echo "Renaming tweetnacl.c source file"
echo "" >> tweetnacl.c
mv tweetnacl.c tweetnacl.cpp

echo "Compiling tweetnacl.cpp source file"
g++ -Wall tweetnacl.cpp -c

unix2dos tweetnacl.h tweetnacl.c tweetnacl.cpp

# echo "Testing symbols"
# nm tweetnacl.o | grep " T " | c++filt
7 changes: 1 addition & 6 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
/// the namespace, there are two additional namespaces.
/// <ul>
/// <li>Name - namespace for names used with \p NameValuePairs and documented in argnames.h
/// <li>NaCl - namespace for NaCl crypto_box functions
/// <li>Test - namespace for testing and benchmarks classes
/// <li>Weak - namespace for weak and wounded algorithms, like ARC4, MD5 and Pananma
/// </ul>
Expand Down Expand Up @@ -1010,12 +1011,6 @@ NAMESPACE_END
# define CRYPTOPP_CXX11_ALIGNOF 1
#endif // alignof

// lambdas: MS at VS2012 (17.00); GCC at 4.9; Clang at 3.3; Intel 12.0; SunCC 5.14.
#if (CRYPTOPP_MSC_VERSION >= 1700) || __has_feature(cxx_lambda) || \
(__INTEL_COMPILER >= 1200) || (CRYPTOPP_GCC_VERSION >= 40900) || (__SUNPRO_CC >= 0x5140)
# define CRYPTOPP_CXX11_LAMBDA 1
#endif // lambdas

// noexcept: MS at VS2015 (19.00); GCC at 4.6; Clang at 3.0; Intel 14.0; SunCC 5.13.
#if (CRYPTOPP_MSC_VERSION >= 1900) || __has_feature(cxx_noexcept) || \
(__INTEL_COMPILER >= 1400) || (CRYPTOPP_GCC_VERSION >= 40600) || (__SUNPRO_CC >= 0x5130)
Expand Down
6 changes: 6 additions & 0 deletions cryptlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,12 @@ DOCUMENTED_NAMESPACE_BEGIN(Weak)
DOCUMENTED_NAMESPACE_END
#endif

/// \brief Namespace containing NaCl library functions
/// \details TweetNaCl is a compact and portable reimplementation of the NaCl library.
DOCUMENTED_NAMESPACE_BEGIN(NaCl)
// crypto_box, crypto_box_open, crypto_sign, and crypto_sign_verify (and friends)
DOCUMENTED_NAMESPACE_END

/// \brief Namespace containing testing and benchmark classes.
/// \details Source files for classes in the Test namespaces include
/// <tt>test.cpp</tt>, <tt>validat#.cpp</tt> and <tt>bench#.cpp</tt>.
Expand Down
Loading