1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

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.
This commit is contained in:
Tobias Markmann 2014-12-16 17:36:38 +01:00 committed by Oleg Hahm
parent 0250d62509
commit 57b8a24466
13 changed files with 251 additions and 0 deletions

View File

@ -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",

Binary file not shown.

Binary file not shown.

44
pkg/relic/Makefile Normal file
View File

@ -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

View File

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

9
pkg/relic/README.md Normal file
View File

@ -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>```.

View File

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

View File

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

View File

@ -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";

View File

@ -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

View File

@ -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

View File

@ -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());
}