1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00

Compiling the Crypto library on a host system

This commit is contained in:
Rhys Weatherley
2016-03-17 19:19:14 +10:00
parent 8cd6916fe8
commit 72715b172b
15 changed files with 598 additions and 13 deletions

1
host/Crypto/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
Test*

127
host/Crypto/Makefile Normal file
View File

@@ -0,0 +1,127 @@
.PHONY: all clean check
TOPDIR = ../..
SRCDIR = $(TOPDIR)/libraries/Crypto
#VPATH = $(SRCDIR)
vpath %.cpp $(SRCDIR)
vpath %.o .
vpath %.ino $(SRCDIR)/examples
vpath %.sketch .
LIBRARY = libCrypto.a
CPPFLAGS = \
-I$(TOPDIR)/host/emulation \
-I$(TOPDIR)/libraries/Crypto \
-DHOST_BUILD
CXXFLAGS = -g -Wall $(CPPFLAGS)
SOURCES = \
AES128.cpp \
AES192.cpp \
AES256.cpp \
AESCommon.cpp \
AuthenticatedCipher.cpp \
BigNumberUtil.cpp \
BLAKE2b.cpp \
BLAKE2s.cpp \
BlockCipher.cpp \
CBC.cpp \
CFB.cpp \
ChaCha.cpp \
ChaChaPoly.cpp \
Cipher.cpp \
Crypto.cpp \
CTR.cpp \
Curve25519.cpp \
EAX.cpp \
Ed25519.cpp \
GCM.cpp \
GF128.cpp \
GHASH.cpp \
Hash.cpp \
KeccakCore.cpp \
NoiseSource.cpp \
OFB.cpp \
OMAC.cpp \
Poly1305.cpp \
RNG_host.cpp \
SHA256.cpp \
SHA3.cpp \
SHA512.cpp \
SHAKE.cpp \
Speck.cpp \
SpeckSmall.cpp \
SpeckTiny.cpp \
XOF.cpp \
XTS.cpp
SKETCHES = \
TestAES/TestAES.ino \
TestBigNumberUtil/TestBigNumberUtil.ino \
TestBLAKE2b/TestBLAKE2b.ino \
TestBLAKE2s/TestBLAKE2s.ino \
TestCBC/TestCBC.ino \
TestCFB/TestCFB.ino \
TestChaCha/TestChaCha.ino \
TestChaChaPoly/TestChaChaPoly.ino \
TestCTR/TestCTR.ino \
TestCurve25519/TestCurve25519.ino \
TestCurve25519Math/TestCurve25519Math.ino \
TestEAX/TestEAX.ino \
TestEd25519/TestEd25519.ino \
TestGCM/TestGCM.ino \
TestGHASH/TestGHASH.ino \
TestOFB/TestOFB.ino \
TestPoly1305/TestPoly1305.ino \
TestSHA256/TestSHA256.ino \
TestSHA3_256/TestSHA3_256.ino \
TestSHA3_512/TestSHA3_512.ino \
TestSHA512/TestSHA512.ino \
TestSHAKE128/TestSHAKE128.ino \
TestSHAKE256/TestSHAKE256.ino \
TestSpeck/TestSpeck.ino \
TestXTS/TestXTS.ino \
OBJECTS = $(patsubst %.cpp,%.o,$(SOURCES))
DEPS = $(patsubst %.cpp,.depend/%.d,$(SOURCES))
SKETCH_OUTPUTS = $(patsubst %.ino,%.sketch,$(SKETCHES))
all: $(LIBRARY)
$(LIBRARY): $(OBJECTS)
$(RM) $(LIBRARY)
$(AR) cr $(LIBRARY) $(OBJECTS)
clean:
$(RM) $(OBJECTS) $(LIBRARY)
$(RM) $(SKETCH_OUTPUTS)
$(RM) -r .depend Test*
check: all $(SKETCH_OUTPUTS)
@for sketch in $(SKETCH_OUTPUTS); do \
echo Running $$sketch; \
$$sketch | grep -i fail; \
done; exit 0
%.o: %.cpp
$(CXX) $(CXXFLAGS) -o $@ -c $<
%.sketch: %.ino $(LIBRARY)
mkdir -p `dirname $@`
$(CXX) -x c++ $(CXXFLAGS) \
-include $(TOPDIR)/host/emulation/Arduino.h \
-include $(TOPDIR)/host/emulation/Arduino.cpp \
-o $@ $< -L. -lCrypto
.depend/%.d: %.cpp
@set -e; rm -f $@; mkdir -p `dirname $@`; \
$(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\(.*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
-include $(DEPS)

165
host/Crypto/RNG_host.cpp Normal file
View File

@@ -0,0 +1,165 @@
/*
* Copyright (C) 2015 Southern Storm Software, Pty Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "RNG.h"
#include "Crypto.h"
#include "ChaCha.h"
#include <string.h>
#if defined(__linux__)
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#endif
// Host emulation for the RNG class, using the native random number generator.
#define RNG_ROUNDS 20
#define RNG_REKEY_BLOCKS 16
#define RNG_MORE_ENTROPY 16384
static const char tagRNG[16] = {
'e', 'x', 'p', 'a', 'n', 'd', ' ', '3',
'2', '-', 'b', 'y', 't', 'e', ' ', 'k'
};
RNGClass::RNGClass()
: timer(0)
{
memcpy(block, tagRNG, sizeof(tagRNG));
memset(block + 4, 0, 48);
}
RNGClass::~RNGClass()
{
clean(block);
clean(stream);
}
void RNGClass::begin(const char *tag, int eepromAddress)
{
}
void RNGClass::addNoiseSource(NoiseSource &source)
{
}
void RNGClass::setAutoSaveTime(uint16_t minutes)
{
}
// Get more entropy from the underlying operating system.
static void getMoreEntropy(uint8_t *data, size_t len)
{
#if defined(__linux__)
int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) {
perror("/dev/urandom");
exit(1);
}
int ret = read(fd, data, len);
if (ret != (int)len) {
if (ret < 0)
perror("/dev/urandom");
close(fd);
exit(1);
}
close(fd);
#else
#warning "TODO: No random entropy on this system!"
memset(data, 0xAA, len);
#endif
}
void RNGClass::rand(uint8_t *data, size_t len)
{
// Generate the random data.
uint8_t count = 0;
while (len > 0) {
// Get more entropy from the operating system if necessary.
if (!timer) {
getMoreEntropy((uint8_t *)(block + 4), 48);
rekey();
timer = RNG_MORE_ENTROPY;
}
// Force a rekey if we have generated too many blocks in this request.
if (count >= RNG_REKEY_BLOCKS) {
rekey();
count = 1;
} else {
++count;
}
// Increment the low counter word and generate a new keystream block.
++(block[12]);
ChaCha::hashCore(stream, block, RNG_ROUNDS);
// Copy the data to the return buffer.
size_t size = (len < 64) ? len : 64;
memcpy(data, stream, size);
data += size;
len -= size;
// Keep track of the number of bytes we have generated so that
// we can fetch more entropy from the operating system if necessary.
if (timer >= size)
timer -= size;
else
timer = 0;
}
// Force a rekey after every request.
rekey();
}
bool RNGClass::available(size_t len) const
{
return true;
}
void RNGClass::stir(const uint8_t *data, size_t len, unsigned int credit)
{
}
void RNGClass::save()
{
}
void RNGClass::loop()
{
}
void RNGClass::destroy()
{
}
void RNGClass::rekey()
{
++(block[12]);
ChaCha::hashCore(stream, block, RNG_ROUNDS);
memcpy(block + 4, stream, 48);
}
RNGClass RNG;