Add a RELIC package and unit test that shows how to use it

Add install of cmake to .travis.yml.

Test-Information:

This has been tested on OS X and on Ubuntu for both the native target
and STM32F4Discovery.
cc430
Tobias Markmann 9 years ago committed by Oleg Hahm
parent 0250d62509
commit 57b8a24466

@ -30,6 +30,10 @@ known_mcu_groups = arm_mcu_groups + msp_mcu_groups + x86_mcu_groups + \
avr8_mcu_groups + static_tests_groups
common_pkgs = ["pcregrep", "libpcre3", "python3"]
# testing the relic pkg and its RIOT specific unit test requires cmake
common_pkgs = common_pkgs + ["cmake"]
arm_pkgs = ["gcc-arm-none-eabi"]
msp_pkgs = ["gcc-msp430"]
x86_pkgs = ["qemu-system-x86", "g++-multilib", "gcc-multilib",

@ -0,0 +1,26 @@
From 96f92673ac7b719f745958738e6652491bff2c3b Mon Sep 17 00:00:00 2001
From: Oleg Hahm <oleg@hobbykeller.org>
Date: Sat, 28 Nov 2015 16:01:18 +0100
Subject: fixed signedness of counter variable
---
src/cp/relic_cp_bdpe.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/cp/relic_cp_bdpe.c b/src/cp/relic_cp_bdpe.c
index c63621c..0a253a5 100644
--- a/src/cp/relic_cp_bdpe.c
+++ b/src/cp/relic_cp_bdpe.c
@@ -160,7 +160,8 @@ int cp_bdpe_enc(uint8_t *out, int *out_len, dig_t in, bdpe_t pub) {
int cp_bdpe_dec(dig_t *out, uint8_t *in, int in_len, bdpe_t prv) {
bn_t m, t, z;
- int i, size, result = STS_OK;
+ unsigned i;
+ int size, result = STS_OK;
size = bn_size_bin(prv->n);
--
2.6.2

@ -0,0 +1,69 @@
From 381a12925143ba4f6910c15ccb1fabb2a7c8c614 Mon Sep 17 00:00:00 2001
From: Oleg Hahm <oleg@hobbykeller.org>
Date: Sat, 28 Nov 2015 15:54:24 +0100
Subject: don't redefine ALIGN
---
include/relic_types.h | 9 +++++++--
src/md/blake2.h | 10 +++-------
2 files changed, 10 insertions(+), 9 deletions(-)
diff --git a/include/relic_types.h b/include/relic_types.h
index afc4870..d9ef4f5 100644
--- a/include/relic_types.h
+++ b/include/relic_types.h
@@ -129,9 +129,14 @@ typedef unsigned long long ull_t;
* Specification for aligned variables.
*/
#if ALIGN > 1
-#define align __attribute__ ((aligned (ALIGN)))
+# if defined(_MSC_VER)
+# define ALIGNME(x) __declspec(align(x))
+# else
+# define ALIGNME(x) __attribute__((aligned(x)))
+# endif
#else
-#define align /* empty*/
+# define align /* empty*/
+# define ALIGNME(x) /* empty*/
#endif
/**
diff --git a/src/md/blake2.h b/src/md/blake2.h
index f8aba83..48e314f 100644
--- a/src/md/blake2.h
+++ b/src/md/blake2.h
@@ -17,11 +17,7 @@
#include <stddef.h>
#include <stdint.h>
-#if defined(_MSC_VER)
-#define ALIGN(x) __declspec(align(x))
-#else
-#define ALIGN(x) __attribute__((aligned(x)))
-#endif
+#include "relic_types.h"
#if defined(__cplusplus)
extern "C" {
@@ -61,7 +57,7 @@ extern "C" {
uint8_t personal[BLAKE2S_PERSONALBYTES]; // 32
} blake2s_param;
- ALIGN( 64 ) typedef struct __blake2s_state
+ ALIGNME( 64 ) typedef struct __blake2s_state
{
uint32_t h[8];
uint32_t t[2];
@@ -86,7 +82,7 @@ extern "C" {
uint8_t personal[BLAKE2B_PERSONALBYTES]; // 64
} blake2b_param;
- ALIGN( 64 ) typedef struct __blake2b_state
+ ALIGNME( 64 ) typedef struct __blake2b_state
{
uint64_t h[8];
uint64_t t[2];
--
2.6.2

@ -0,0 +1,23 @@
From 814aec72218b194bdff51702fcecd3a06222efbd Mon Sep 17 00:00:00 2001
From: Oleg Hahm <oleg@hobbykeller.org>
Date: Tue, 1 Dec 2015 17:42:16 +0100
Subject: [PATCH 3/3] require only CMake version 2.8
---
CMakeLists.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8a26feb..2ca537a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,5 @@
project(RELIC C CXX)
-cmake_minimum_required(VERSION 3.1)
+cmake_minimum_required(VERSION 2.8)
set(PROJECT_VERSION_MAJOR "0")
set(PROJECT_VERSION_MINOR "4")
--
2.6.2

@ -0,0 +1,44 @@
RELIC_URL=http://github.com/relic-toolkit/relic.git
RELIC_BRANCH=master
PKG_NAME=relic
PKG_URL=$(RELIC_URL)
PKG_VERSION=$(RELIC_BRANCH)
PKG_DIR=$(CURDIR)/$(PKG_NAME)
ifneq ($(RIOTBOARD),)
include $(RIOTBOARD)/$(BOARD)/Makefile.include
endif
ifneq ($(RIOTBASE),)
INCLUDES += -I$(RIOTBASE)/sys/include -I$(RIOTBASE)/sys/net/include \
-I$(RIOTBASE)/sys/posix/include -I$(RIOTBASE)/sys/posix/pnet/include
endif
.PHONY: all clean reset
all: $(PKG_DIR)/Makefile
"$(MAKE)" -C $(PKG_DIR) && \
cp $(PKG_DIR)/lib/librelic_s.a $(BINDIR)$(PKG_NAME).a
$(PKG_DIR)/comp-options.cmake: $(PKG_DIR)/.git/config
cd "$(PKG_DIR)" && perl ../generate-cmake-xcompile.perl > comp-options.cmake
$(PKG_DIR)/Makefile: $(PKG_DIR)/comp-options.cmake
cd "$(PKG_DIR)" && COMP="$(filter-out -Werror=old-style-definition -Werror=strict-prototypes, $(CFLAGS) ) " cmake -DCMAKE_TOOLCHAIN_FILE=comp-options.cmake -DCHECK=off -DTESTS=0 -DBENCH=0 -DSHLIB=off -Wno-dev $(RELIC_CONFIG_FLAGS) .
$(PKG_DIR)/.git/config:
test -d "$(PKG_DIR)" || git clone "$(PKG_URL)" "$(PKG_DIR)"; \
cd "$(PKG_DIR)" && git checkout -f "$(PKG_VERSION)"
cd "$(PKG_DIR)" && git am --ignore-whitespace $(CURDIR)/*.patch
./fix-util_print_wo_args.sh .
./fix-old-style-definitions.sh .
clean::
@echo "Cleaning up relic package..."
rm -rf "$(PKG_DIR)"
distclean::
rm -rf "$(PKG_DIR)"
Makefile.include:
@true

@ -0,0 +1 @@
INCLUDES += -I$(RIOTBASE)/pkg/relic/relic/include

@ -0,0 +1,9 @@
# Configuration Options
You can pass along configuration flags for RELIC from your project makefile via:
```export RELIC_CONFIG_FLAGS=-DARCH=NONE -DQUIET=off -DWORD=32 -DFP_PRIME=255 -DWITH="BN;MD;DV;FP;EP;CP;BC;EC" -DSEED=ZERO```
This should happen before the ```USEPKG``` line.
# Usage
Just put ```USEPKG += relic``` in your Makefile and ```#include <relic.h>```.

@ -0,0 +1,2 @@
#!/bin/sh
find ${1} -name "*.[ch]" | xargs sed -i 's/() {/(void) {/'

@ -0,0 +1,2 @@
#!/bin/sh
find ${1} -name "*.[ch]" | xargs sed -i 's/util_print("\(.*\)")/util_print("\1", NULL)/g'

@ -0,0 +1,29 @@
#!/usr/bin/env perl
print "INCLUDE(CMakeForceCompiler)\n";
print "\n";
print "\n";
print "SET(CMAKE_SYSTEM_NAME Generic)\n";
print "SET(CMAKE_SYSTEM_VERSION 1)\n";
print "\n";
print "SET(CMAKE_C_COMPILER \"$ENV{CC}\" CACHE STRING \"\")\n";
print "SET(CMAKE_CXX_COMPILER \"$ENV{CXX}\" CACHE STRING \"\")\n";
print "SET(CMAKE_RANLIB \"echo\" CACHE STRING \"\")\n";
print "\n";
print "# specify the cross compiler\n";
print "CMAKE_FORCE_C_COMPILER(\${CMAKE_C_COMPILER} GNU)\n";
print "CMAKE_FORCE_CXX_COMPILER(\${CMAKE_CXX_COMPILER} GNU)\n";
print "SET(CMAKE_LINKER \"$ENV{LINK}\" CACHE STRING \"\")\n";
print "\n";
my $esc_c_flags = "$ENV{CFLAGS}";
$esc_c_flags =~ s/"/\\"/g;
print "SET(CMAKE_C_FLAGS \"$esc_c_flags\" CACHE STRING \"\")\n";
print "\n";
print "SET(CMAKE_EXE_LINKER_FLAGS \"$ENV{LINKFLAGS}\" CACHE STRING \"\")\n";
print "\n";
print "# search for programs in the build host directories\n";
print "SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)\n";
print "# for libraries and headers in the target directories\n";
print "SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)\n";
print "SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)\n";

@ -0,0 +1,6 @@
MODULE = tests-relic
# The following boards are known to fail or have not been tested.
BOARD_BLACKLIST := arduino-mega2560 chronos f4vi1 msb-430 msb-430h msbiot qemu-i386 redbee-econotag stm32f0discovery stm32f3discovery telosb wsn430-v1_3b wsn430-v1_4 z1
include $(RIOTBASE)/Makefile.base

@ -0,0 +1,8 @@
USEPKG += relic
# -DWORD=32 : Specifies the word width of the target system. This is
# currently not automatically detected so adjusted to your target
# platform.
# The rest of the parameters are configuration parameters for RELIC described in its documentation.
export RELIC_CONFIG_FLAGS=-DARCH=NONE -DOPSYS=NONE -DQUIET=off -DWORD=32 -DFP_PRIME=255 -DWITH="BN;MD;DV;FP;EP;CP;BC;EC" -DSEED=ZERO

@ -0,0 +1,146 @@
/*
* Copyright (C) 2014 Tobias Markmann <tm@ayena.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
#define TEST_RELIC_SHOW_OUTPUT (0) /**< set if encoded/decoded string is displayed */
#if (TEST_RELIC_SHOW_OUTPUT == 1)
#include <stdio.h>
#endif
#include <assert.h>
#include <stdlib.h>
#include "relic.h"
#include "embUnit.h"
void print_mem(void *mem, int len) {
int i;
unsigned char *p = (unsigned char *)mem;
for (i=0;i<len;i++) {
printf("0x%02x ", p[i]);
}
printf("\n");
}
static void setUp(void)
{
/* Initialize RELIC */
TEST_ASSERT_EQUAL_INT(STS_OK, core_init());
}
static void tearDown(void)
{
/* Finalize RELIC */
core_clean();
}
static void tests_relic_ecdh(void)
{
/* The following is an example for doing an elliptic-curve Diffie-Hellman
key exchange.
*/
/* Select an elliptic curve configuration */
if (ec_param_set_any() == STS_OK) {
#if (TEST_RELIC_SHOW_OUTPUT == 1)
ec_param_print();
#endif
bn_t privateA;
ec_t publicA;
uint8_t sharedKeyA[MD_LEN];
bn_t privateB;
ec_t publicB;
uint8_t sharedKeyB[MD_LEN];
bn_null(privateA);
ec_null(publicA);
bn_new(privateA);
ec_new(publicA);
bn_null(privateB);
ec_null(publicB);
bn_new(privateB);
ec_new(publicB);
/* User A generates private/public key pair */
TEST_ASSERT_EQUAL_INT(STS_OK, cp_ecdh_gen(privateA, publicA));
#if (TEST_RELIC_SHOW_OUTPUT == 1)
printf("User A\n");
printf("======\n");
printf("private key: ");
bn_print(privateA);
printf("\npublic key: ");
ec_print(publicA);
printf("\n");
#endif
/* User B generates private/public key pair */
TEST_ASSERT_EQUAL_INT(STS_OK, cp_ecdh_gen(privateB, publicB));
#if (TEST_RELIC_SHOW_OUTPUT == 1)
printf("User B\n");
printf("======\n");
printf("private key: ");
bn_print(privateB);
printf("\npublic key: ");
ec_print(publicB);
printf("\n");
#endif
/* In a protocol you would exchange the public keys now */
/* User A calculates shared secret */
TEST_ASSERT_EQUAL_INT(STS_OK, cp_ecdh_key(sharedKeyA, MD_LEN, privateA, publicB));
#if (TEST_RELIC_SHOW_OUTPUT == 1)
printf("\nshared key computed by user A: ");
print_mem(sharedKeyA, MD_LEN);
#endif
/* User B calculates shared secret */
TEST_ASSERT_EQUAL_INT(STS_OK, cp_ecdh_key(sharedKeyB, MD_LEN, privateB, publicA));
#if (TEST_RELIC_SHOW_OUTPUT == 1)
printf("\nshared key computed by user B: ");
print_mem(sharedKeyB, MD_LEN);
#endif
/* The secrets should be the same now */
TEST_ASSERT_EQUAL_INT(CMP_EQ, util_cmp_const(sharedKeyA, sharedKeyB, MD_LEN));
bn_free(privateA);
ec_free(publicA);
bn_free(privateB);
ec_free(publicB);
#if (TEST_RELIC_SHOW_OUTPUT == 1)
printf("\nRELIC EC-DH test successful\n");
#endif
}
}
TestRef tests_relic_all(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(tests_relic_ecdh)
};
EMB_UNIT_TESTCALLER(RELICTest, setUp, tearDown, fixtures);
return (TestRef)&RELICTest;
}
void tests_relic(void)
{
TESTS_RUN(tests_relic_all());
}
Loading…
Cancel
Save