Skip to content

Commit

Permalink
Add libsodium v1.0.12
Browse files Browse the repository at this point in the history
Run some unit test cases based (poorly) around sodium's test infrastructure.

Increase in unity test task stack is due to signature tests, load a lot of data onto the stack.
  • Loading branch information
projectgus committed Jun 14, 2017
1 parent e7db29b commit 211580b
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
[submodule "components/esp32/lib"]
path = components/esp32/lib
url = https://github.com/espressif/esp32-wifi-lib.git

[submodule "components/esptool_py/esptool"]
path = components/esptool_py/esptool
url = https://github.com/espressif/esptool.git

[submodule "components/bt/lib"]
path = components/bt/lib
url = https://github.com/espressif/esp32-bt-lib.git

[submodule "components/micro-ecc/micro-ecc"]
path = components/micro-ecc/micro-ecc
url = https://github.com/kmackay/micro-ecc.git

[submodule "components/coap/libcoap"]
path = components/coap/libcoap
url = https://github.com/obgm/libcoap.git

[submodule "components/aws_iot/aws-iot-device-sdk-embedded-C"]
path = components/aws_iot/aws-iot-device-sdk-embedded-C
url = https://github.com/espressif/aws-iot-device-sdk-embedded-C.git

[submodule "components/nghttp/nghttp2"]
path = components/nghttp/nghttp2
url = https://github.com/nghttp2/nghttp2.git

[submodule "components/libsodium/libsodium"]
path = components/libsodium/libsodium
url = https://github.com/jedisct1/libsodium.git
81 changes: 81 additions & 0 deletions components/libsodium/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
COMPONENT_SUBMODULES += libsodium

# Common root directory for all source directories
LSRC := libsodium/src/libsodium

COMPONENT_SRCDIRS := private

# Derived from libsodium/src/libsodium/Makefile.am
# (ignoring the !MINIMAL set)
COMPONENT_SRCDIRS += \
$(LSRC)/crypto_aead/chacha20poly1305/sodium \
$(LSRC)/crypto_aead/xchacha20poly1305/sodium \
$(LSRC)/crypto_auth \
$(LSRC)/crypto_auth/hmacsha256 \
$(LSRC)/crypto_auth/hmacsha512 \
$(LSRC)/crypto_auth/hmacsha512256 \
$(LSRC)/crypto_box \
$(LSRC)/crypto_box/curve25519xsalsa20poly1305 \
$(LSRC)/crypto_core/curve25519/ref10 \
$(LSRC)/crypto_core/hchacha20 \
$(LSRC)/crypto_core/hsalsa20/ref2 \
$(LSRC)/crypto_core/hsalsa20 \
$(LSRC)/crypto_core/salsa/ref \
$(LSRC)/crypto_generichash \
$(LSRC)/crypto_generichash/blake2b \
$(LSRC)/crypto_generichash/blake2b/ref \
$(LSRC)/crypto_hash \
$(LSRC)/crypto_hash/sha256 \
$(LSRC)/crypto_hash/sha256/cp \
$(LSRC)/crypto_hash/sha512 \
$(LSRC)/crypto_hash/sha512/cp \
$(LSRC)/crypto_kdf/blake2b \
$(LSRC)/crypto_kdf \
$(LSRC)/crypto_kx \
$(LSRC)/crypto_onetimeauth \
$(LSRC)/crypto_onetimeauth/poly1305 \
$(LSRC)/crypto_onetimeauth/poly1305/donna \
$(LSRC)/crypto_pwhash/argon2 \
$(LSRC)/crypto_pwhash \
$(LSRC)/crypto_pwhash/scryptsalsa208sha256 \
$(LSRC)/crypto_pwhash/scryptsalsa208sha256/nosse \
$(LSRC)/crypto_scalarmult \
$(LSRC)/crypto_scalarmult/curve25519 \
$(LSRC)/crypto_scalarmult/curve25519/ref10 \
$(LSRC)/crypto_secretbox \
$(LSRC)/crypto_secretbox/xsalsa20poly1305 \
$(LSRC)/crypto_shorthash \
$(LSRC)/crypto_shorthash/siphash24 \
$(LSRC)/crypto_shorthash/siphash24/ref \
$(LSRC)/crypto_sign \
$(LSRC)/crypto_sign/ed25519 \
$(LSRC)/crypto_sign/ed25519/ref10 \
$(LSRC)/crypto_stream/chacha20 \
$(LSRC)/crypto_stream/chacha20/ref \
$(LSRC)/crypto_stream \
$(LSRC)/crypto_stream/salsa20 \
$(LSRC)/crypto_stream/salsa20/ref \
$(LSRC)/crypto_stream/xsalsa20 \
$(LSRC)/crypto_verify/sodium \
$(LSRC)/randombytes \
$(LSRC)/sodium

# Fix some warnings in current libsodium source files
# (not applied to whole component as we compile some of our own files, also.)
$(LSRC)/crypto_pwhash/argon2/argon2-fill-block-ref.o: CFLAGS += -Wno-unknown-pragmas
$(LSRC)/crypto_pwhash/argon2/pwhash_argon2i.o: CFLAGS += -Wno-type-limits
$(LSRC)/crypto_pwhash/argon2/argon2-core.o: CFLAGS += -Wno-type-limits
$(LSRC)/crypto_pwhash/scryptsalsa208sha256/pwhash_scryptsalsa208sha256.o: CFLAGS += -Wno-type-limits
$(LSRC)/sodium/utils.o: CFLAGS += -Wno-unused-variable

COMPONENT_ADD_INCLUDEDIRS := $(LSRC)/include port_include
COMPONENT_PRIV_INCLUDEDIRS := $(LSRC)/include/sodium port_include/sodium private

# Not using autoconf, but this needs to be set
CFLAGS += -DCONFIGURED

# Add the options from configure.ac (this needs checking if new versions are added )
CFLAGS += -DNATIVE_LITTLE_ENDIAN -DHAVE_WEAK_SYMBOLS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS

# randombytes.c needs to pull in platform-specific implementation
$(LSRC)/randombytes/randombytes.o: CFLAGS+=-DRANDOMBYTES_DEFAULT_IMPLEMENTATION
1 change: 1 addition & 0 deletions components/libsodium/libsodium
Submodule libsodium added at 70170c
35 changes: 35 additions & 0 deletions components/libsodium/port_include/sodium/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#ifndef sodium_version_H
#define sodium_version_H

#include <sodium/export.h>

/* IMPORTANT: As we don't use autotools, these version are not automatically
updated if we change submodules. They need to be changed manually.
*/

#define SODIUM_VERSION_STRING "1.0.12-idf"

/* Note: these are not the same as the overall version, see
configure.ac for the relevant macros */
#define SODIUM_LIBRARY_VERSION_MAJOR 9
#define SODIUM_LIBRARY_VERSION_MINOR 4

#ifdef __cplusplus
extern "C" {
#endif

SODIUM_EXPORT
const char *sodium_version_string(void);

SODIUM_EXPORT
int sodium_library_version_major(void);

SODIUM_EXPORT
int sodium_library_version_minor(void);

#ifdef __cplusplus
}
#endif

#endif
28 changes: 28 additions & 0 deletions components/libsodium/private/randombytes_default.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2017 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once

# include "export.h"
# include "randombytes.h"

SODIUM_EXPORT
extern const struct randombytes_implementation randombytes_esp32_implementation;

/* Defining RANDOMBYTES_DEFAULT_IMPLEMENTATION here allows us to compile with the ESP32 hardware
implementation as the default. No need to call randombytes_set_implementation().
Doing it in the header like this is easier than passing it via a -D argument to gcc.
*/
#undef RANDOMBYTES_DEFAULT_IMPLEMENTATION
#define RANDOMBYTES_DEFAULT_IMPLEMENTATION &randombytes_esp32_implementation
45 changes: 45 additions & 0 deletions components/libsodium/private/randombytes_esp32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2017 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "randombytes_default.h"
#include "esp_system.h"

static void randombytes_esp32_random_buf(void * const buf, const size_t size)
{
uint8_t *p = (uint8_t *)buf;
for (size_t i = 0; i < size; i++) {
p[i] = esp_random();
}
}

static const char *randombytes_esp32_implementation_name(void)
{
return "esp32";
}

/*
Plug the ESP32 hardware RNG into libsodium's custom RNG support, as per
https://download.libsodium.org/doc/advanced/custom_rng.html
Note that this RNG is selected by default (see randombytes_default.h), so there
is no need to call randombytes_set_implementation().
*/
const struct randombytes_implementation randombytes_esp32_implementation = {
.implementation_name = randombytes_esp32_implementation_name,
.random = esp_random,
.stir = NULL,
.uniform = NULL,
.buf = randombytes_esp32_random_buf,
.close = NULL,
};

39 changes: 39 additions & 0 deletions components/libsodium/test/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
#Component Makefile
#

LS_TESTDIR := ../libsodium/test/default

COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive

COMPONENT_SRCDIRS := . $(LS_TESTDIR)

COMPONENT_PRIV_INCLUDEDIRS := $(LS_TESTDIR)/../quirks

COMPONENT_OBJS := test_sodium.o

# The libsodium test suite is designed to be run each test case as an executable on a desktop computer and uses
# filesytem to write & then compare contents of each file.
#
# For now, use their "BROWSER_TEST" mode with these hacks so that
# multiple test cases can be combined into one ELF file.
#
# Run each test case from test_sodium.c as CASENAME_xmain().

define sodium_testcase
# this generates 'warning "main" redefined' warnings at
# runtime. Only solution involves patching libsodium's cmptest.h
$(LS_TESTDIR)/$(1).o: CFLAGS+=-Dxmain=$(1)_xmain -Dmain=$(1)_main
ote:
COMPONENT_OBJS += $(LS_TESTDIR)/$(1).o
endef

TEST_CASES := chacha20 aead_chacha20poly1305 box box2 ed25519_convert sign

$(foreach case,$(TEST_CASES),$(eval $(call sodium_testcase,$(case))))

# this seems odd, but it prevents the libsodium test harness from
# trying to write to a file!
CFLAGS += -DBROWSER_TESTS


54 changes: 54 additions & 0 deletions components/libsodium/test/test_sodium.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "unity.h"

/* Note: a lot of these libsodium test programs assert() things, but they're not complete unit tests - most expect
output to be compared to the matching .exp file.
We don't do this automatically yet, maybe once we have more options for
internal filesystem storage.
*/

extern int aead_chacha20poly1305_xmain();

TEST_CASE("aead_chacha20poly1305 test vectors", "[libsodium]")
{
printf("Running aead_chacha20poly1305\n");
TEST_ASSERT_EQUAL(0, aead_chacha20poly1305_xmain());
}

extern int chacha20_xmain();

TEST_CASE("chacha20 test vectors", "[libsodium]")
{
printf("Running chacha20\n");
TEST_ASSERT_EQUAL(0, chacha20_xmain());
}

extern int box_xmain();
extern int box2_xmain();

TEST_CASE("box tests", "[libsodium]")
{
printf("Running box\n");
TEST_ASSERT_EQUAL(0, box_xmain());

printf("Running box2\n");
TEST_ASSERT_EQUAL(0, box2_xmain());
}

extern int ed25519_convert_xmain();

TEST_CASE("ed25519_convert tests", "[libsodium]")
{
printf("Running ed25519_convert\n");
TEST_ASSERT_EQUAL(0, ed25519_convert_xmain() );
}

extern int sign_xmain();

TEST_CASE("sign tests", "[libsodium]")
{
printf("Running sign\n");
TEST_ASSERT_EQUAL(0, sign_xmain() );
}


2 changes: 1 addition & 1 deletion tools/unit-test-app/main/app_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ void app_main()
{
// Note: if unpinning this task, change the way run times are calculated in
// unity_platform
xTaskCreatePinnedToCore(unityTask, "unityTask", 4096, NULL,
xTaskCreatePinnedToCore(unityTask, "unityTask", 8192, NULL,
UNITY_FREERTOS_PRIORITY, NULL, UNITY_FREERTOS_CPU);
}

0 comments on commit 211580b

Please sign in to comment.