Merge pull request #1078 from drandreas/cc2538-crypto
cc2538: Add PKA drivers, ECC algorithms and examples
This commit is contained in:
commit
bf41de1be5
19 changed files with 4307 additions and 2 deletions
6
examples/cc2538dk/pka/Makefile
Normal file
6
examples/cc2538dk/pka/Makefile
Normal file
|
@ -0,0 +1,6 @@
|
|||
CONTIKI_PROJECT = ecc-ecdh ecc-sign ecc-verify
|
||||
|
||||
all: $(CONTIKI_PROJECT)
|
||||
|
||||
CONTIKI = ../../..
|
||||
include $(CONTIKI)/Makefile.include
|
188
examples/cc2538dk/pka/ecc-ecdh.c
Normal file
188
examples/cc2538dk/pka/ecc-ecdh.c
Normal file
|
@ -0,0 +1,188 @@
|
|||
/*
|
||||
* Copyright (c) 2014, Institute for Pervasive Computing, ETH Zurich.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Author: Andreas Dröscher <contiki@anticat.ch>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
/**
|
||||
* \addtogroup cc2538-examples
|
||||
* @{
|
||||
*
|
||||
* \defgroup cc2538-ecdh-test cc2538dk ECDH Test Project
|
||||
*
|
||||
* ECDH example for CC2538 on SmartRF06EB.
|
||||
*
|
||||
* This example shows how ECDH should be used. The example also verifies
|
||||
* the ECDH functionality.
|
||||
*
|
||||
* @{
|
||||
*
|
||||
* \file
|
||||
* Example demonstrating ECDH on the cc2538dk platform
|
||||
*/
|
||||
#include "contiki.h"
|
||||
#include "ecc-algorithm.h"
|
||||
#include "ecc-curve.h"
|
||||
#include "random.h"
|
||||
#include "rtimer.h"
|
||||
#include "pt.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static void
|
||||
ecc_set_random(uint32_t *secret)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < 8; ++i) {
|
||||
secret[i] = (uint32_t)random_rand() | (uint32_t)random_rand() << 16;
|
||||
}
|
||||
}
|
||||
|
||||
PROCESS(ecdh_test, "ecdh test");
|
||||
AUTOSTART_PROCESSES(&ecdh_test);
|
||||
|
||||
PROCESS_THREAD(ecdh_test, ev, data) {
|
||||
PROCESS_BEGIN();
|
||||
|
||||
/*
|
||||
* Variable for Time Measurement
|
||||
*/
|
||||
static rtimer_clock_t time;
|
||||
|
||||
/*
|
||||
* Activate Engine
|
||||
*/
|
||||
puts("-----------------------------------------\n"
|
||||
"Initializing pka...");
|
||||
pka_init();
|
||||
|
||||
/*
|
||||
* Generate secrets make sure they are valid (smaller as order)
|
||||
*/
|
||||
static ecc_compare_state_t state = {
|
||||
.process = &ecdh_test,
|
||||
.size = 8,
|
||||
};
|
||||
memcpy(state.b, nist_p_256.n, sizeof(uint32_t) * 8);
|
||||
static uint32_t secret_a[8];
|
||||
do {
|
||||
ecc_set_random(secret_a);
|
||||
memcpy(state.a, secret_a, sizeof(uint32_t) * 8);
|
||||
PT_SPAWN(&(ecdh_test.pt), &(state.pt), ecc_compare(&state));
|
||||
} while(state.result != PKA_STATUS_A_LT_B);
|
||||
|
||||
static uint32_t secret_b[8];
|
||||
ecc_set_random(secret_b);
|
||||
do {
|
||||
ecc_set_random(secret_b);
|
||||
memcpy(state.a, secret_b, sizeof(uint32_t) * 8);
|
||||
PT_SPAWN(&(ecdh_test.pt), &(state.pt), ecc_compare(&state));
|
||||
} while(state.result != PKA_STATUS_A_LT_B);
|
||||
|
||||
/*
|
||||
* Prepare Points
|
||||
*/
|
||||
static ecc_multiply_state_t side_a = {
|
||||
.process = &ecdh_test,
|
||||
.curve_info = &nist_p_256,
|
||||
};
|
||||
memcpy(side_a.point_in.x, nist_p_256.x, sizeof(uint32_t) * 8);
|
||||
memcpy(side_a.point_in.y, nist_p_256.y, sizeof(uint32_t) * 8);
|
||||
memcpy(side_a.secret, secret_a, sizeof(secret_a));
|
||||
|
||||
static ecc_multiply_state_t side_b = {
|
||||
.process = &ecdh_test,
|
||||
.curve_info = &nist_p_256,
|
||||
};
|
||||
memcpy(side_b.point_in.x, nist_p_256.x, sizeof(uint32_t) * 8);
|
||||
memcpy(side_b.point_in.y, nist_p_256.y, sizeof(uint32_t) * 8);
|
||||
memcpy(side_b.secret, secret_b, sizeof(secret_b));
|
||||
|
||||
/*
|
||||
* Round 1
|
||||
*/
|
||||
time = RTIMER_NOW();
|
||||
PT_SPAWN(&(ecdh_test.pt), &(side_a.pt), ecc_multiply(&side_a));
|
||||
time = RTIMER_NOW() - time;
|
||||
printf("Round 1, Side a: %i, %lu ms\n", (unsigned)side_a.result,
|
||||
(uint32_t)((uint64_t)time * 1000 / RTIMER_SECOND));
|
||||
|
||||
time = RTIMER_NOW();
|
||||
PT_SPAWN(&(ecdh_test.pt), &(side_b.pt), ecc_multiply(&side_b));
|
||||
time = RTIMER_NOW() - time;
|
||||
printf("Round 1, Side b: %i, %lu ms\n", (unsigned)side_b.result,
|
||||
(uint32_t)((uint64_t)time * 1000 / RTIMER_SECOND));
|
||||
|
||||
/*
|
||||
* Key Exchange
|
||||
*/
|
||||
memcpy(side_a.point_in.x, side_b.point_out.x, sizeof(uint32_t) * 8);
|
||||
memcpy(side_a.point_in.y, side_b.point_out.y, sizeof(uint32_t) * 8);
|
||||
memcpy(side_b.point_in.x, side_a.point_out.x, sizeof(uint32_t) * 8);
|
||||
memcpy(side_b.point_in.y, side_a.point_out.y, sizeof(uint32_t) * 8);
|
||||
|
||||
/*
|
||||
* Round 2
|
||||
*/
|
||||
time = RTIMER_NOW();
|
||||
PT_SPAWN(&(ecdh_test.pt), &(side_a.pt), ecc_multiply(&side_a));
|
||||
time = RTIMER_NOW() - time;
|
||||
printf("Round 2, Side a: %i, %lu ms\n", (unsigned)side_a.result,
|
||||
(uint32_t)((uint64_t)time * 1000 / RTIMER_SECOND));
|
||||
time = RTIMER_NOW();
|
||||
PT_SPAWN(&(ecdh_test.pt), &(side_b.pt), ecc_multiply(&side_b));
|
||||
time = RTIMER_NOW() - time;
|
||||
printf("Round 2, Side b: %i, %lu ms\n", (unsigned)side_b.result,
|
||||
(uint32_t)((uint64_t)time * 1000 / RTIMER_SECOND));
|
||||
|
||||
/*
|
||||
* Check Result
|
||||
*/
|
||||
memcpy(state.a, side_a.point_out.x, sizeof(uint32_t) * 8);
|
||||
memcpy(state.b, side_b.point_out.x, sizeof(uint32_t) * 8);
|
||||
|
||||
PT_SPAWN(&(ecdh_test.pt), &(state.pt), ecc_compare(&state));
|
||||
if(state.result) {
|
||||
puts("shared secrets do not match");
|
||||
} else {
|
||||
puts("shared secrets MATCH");
|
||||
}
|
||||
|
||||
puts("-----------------------------------------\n"
|
||||
"Disabling pka...");
|
||||
pka_disable();
|
||||
|
||||
puts("Done!");
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
*/
|
139
examples/cc2538dk/pka/ecc-sign.c
Normal file
139
examples/cc2538dk/pka/ecc-sign.c
Normal file
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
* Copyright (c) 2014, Institute for Pervasive Computing, ETH Zurich.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Author: Andreas Dröscher <contiki@anticat.ch>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
/**
|
||||
* \addtogroup cc2538-examples
|
||||
* @{
|
||||
*
|
||||
* \defgroup cc2538-ecdsa-sign-test cc2538dk ECDSA-Sign Test Project
|
||||
*
|
||||
* ECDSA-Sign example for CC2538 on SmartRF06EB.
|
||||
*
|
||||
* This example shows how ECDSA-Sign should be used. The example also verifies
|
||||
* the ECDSA-Sign functionality.
|
||||
*
|
||||
* @{
|
||||
*
|
||||
* \file
|
||||
* Example demonstrating ECDSA-Sign on the cc2538dk platform
|
||||
*/
|
||||
#include "contiki.h"
|
||||
#include "ecc-algorithm.h"
|
||||
#include "ecc-curve.h"
|
||||
#include "rtimer.h"
|
||||
#include "pt.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
PROCESS(ecdsa_sign_test, "ecdsa sign test");
|
||||
AUTOSTART_PROCESSES(&ecdsa_sign_test);
|
||||
|
||||
PROCESS_THREAD(ecdsa_sign_test, ev, data) {
|
||||
PROCESS_BEGIN();
|
||||
|
||||
/*
|
||||
* Variable for Time Measurement
|
||||
*/
|
||||
static rtimer_clock_t time;
|
||||
|
||||
/*
|
||||
* Activate Engine
|
||||
*/
|
||||
puts("-----------------------------------------\n"
|
||||
"Initializing pka...");
|
||||
pka_init();
|
||||
|
||||
/*
|
||||
* Setup Variables
|
||||
*/
|
||||
static ecc_compare_state_t comp_state = {
|
||||
.process = &ecdsa_sign_test,
|
||||
.size = 8,
|
||||
};
|
||||
static ecc_dsa_sign_state_t state = {
|
||||
.process = &ecdsa_sign_test,
|
||||
.curve_info = &nist_p_256,
|
||||
.secret = { 0x94A949FA, 0x401455A1, 0xAD7294CA, 0x896A33BB,
|
||||
0x7A80E714, 0x4321435B, 0x51247A14, 0x41C1CB6B },
|
||||
.k_e = { 0x1D1E1F20, 0x191A1B1C, 0x15161718, 0x11121314,
|
||||
0x0D0E0F10, 0x090A0B0C, 0x05060708, 0x01020304 },
|
||||
.hash = { 0x65637572, 0x20612073, 0x68206F66, 0x20686173,
|
||||
0x69732061, 0x68697320, 0x6F2C2054, 0x48616C6C },
|
||||
};
|
||||
|
||||
/*
|
||||
* Sign
|
||||
*/
|
||||
time = RTIMER_NOW();
|
||||
PT_SPAWN(&(ecdsa_sign_test.pt), &(state.pt), ecc_dsa_sign(&state));
|
||||
time = RTIMER_NOW() - time;
|
||||
printf("ecc_dsa_sign(), %lu ms\n",
|
||||
(uint32_t)((uint64_t)time * 1000 / RTIMER_SECOND));
|
||||
|
||||
/*
|
||||
* Check Result
|
||||
*/
|
||||
static uint32_t ecdsaTestresultR1[] = { 0xC3B4035F, 0x515AD0A6, 0xBF375DCA, 0x0CC1E997,
|
||||
0x7F54FDCD, 0x04D3FECA, 0xB9E396B9, 0x515C3D6E };
|
||||
static uint32_t ecdsaTestresultS1[] = { 0x5366B1AB, 0x0F1DBF46, 0xB0C8D3C4, 0xDB755B6F,
|
||||
0xB9BF9243, 0xE644A8BE, 0x55159A59, 0x6F9E52A6 };
|
||||
|
||||
memcpy(comp_state.a, state.point_r.x, sizeof(uint32_t) * 8);
|
||||
memcpy(comp_state.b, ecdsaTestresultR1, sizeof(uint32_t) * 8);
|
||||
PT_SPAWN(&(ecdsa_sign_test.pt), &(comp_state.pt), ecc_compare(&comp_state));
|
||||
if(comp_state.result) {
|
||||
puts("r1 of signature does not match");
|
||||
} else {
|
||||
puts("r1 of signature OK");
|
||||
}
|
||||
|
||||
memcpy(comp_state.a, state.signature_s, sizeof(uint32_t) * 8);
|
||||
memcpy(comp_state.b, ecdsaTestresultS1, sizeof(uint32_t) * 8);
|
||||
PT_SPAWN(&(ecdsa_sign_test.pt), &(comp_state.pt), ecc_compare(&comp_state));
|
||||
if(comp_state.result) {
|
||||
puts("s1 of signature does not match");
|
||||
} else {
|
||||
puts("s1 of signature OK");
|
||||
}
|
||||
|
||||
puts("-----------------------------------------\n"
|
||||
"Disabling pka...");
|
||||
pka_disable();
|
||||
|
||||
puts("Done!");
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
*/
|
122
examples/cc2538dk/pka/ecc-verify.c
Normal file
122
examples/cc2538dk/pka/ecc-verify.c
Normal file
|
@ -0,0 +1,122 @@
|
|||
/*
|
||||
* Copyright (c) 2014, Institute for Pervasive Computing, ETH Zurich.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Author: Andreas Dröscher <contiki@anticat.ch>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the Institute nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
/**
|
||||
* \addtogroup cc2538-examples
|
||||
* @{
|
||||
*
|
||||
* \defgroup cc2538-ecdsa-verify-test cc2538dk ECDSA-Verify Test Project
|
||||
*
|
||||
* ECDSA-Verify example for CC2538 on SmartRF06EB.
|
||||
*
|
||||
* This example shows how ECDSA-Verify should be used. The example also verifies
|
||||
* the ECDSA-Verify functionality.
|
||||
*
|
||||
* @{
|
||||
*
|
||||
* \file
|
||||
* Example demonstrating ECDSA-Verify on the cc2538dk platform
|
||||
*/
|
||||
#include "contiki.h"
|
||||
#include "ecc-algorithm.h"
|
||||
#include "ecc-curve.h"
|
||||
#include "rtimer.h"
|
||||
#include "pt.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
PROCESS(ecdsa_verify_test, "ecdsa verify test");
|
||||
AUTOSTART_PROCESSES(&ecdsa_verify_test);
|
||||
|
||||
PROCESS_THREAD(ecdsa_verify_test, ev, data) {
|
||||
PROCESS_BEGIN();
|
||||
|
||||
/*
|
||||
* Variable for Time Measurement
|
||||
*/
|
||||
static rtimer_clock_t time;
|
||||
|
||||
/*
|
||||
* Activate Engine
|
||||
*/
|
||||
puts("-----------------------------------------\n"
|
||||
"Initializing pka...");
|
||||
pka_init();
|
||||
|
||||
/*
|
||||
* Setup Variables
|
||||
*/
|
||||
static ecc_dsa_verify_state_t state = {
|
||||
.process = &ecdsa_verify_test,
|
||||
.curve_info = &nist_p_256,
|
||||
.signature_r = { 0xC3B4035F, 0x515AD0A6, 0xBF375DCA, 0x0CC1E997,
|
||||
0x7F54FDCD, 0x04D3FECA, 0xB9E396B9, 0x515C3D6E },
|
||||
.signature_s = { 0x5366B1AB, 0x0F1DBF46, 0xB0C8D3C4, 0xDB755B6F,
|
||||
0xB9BF9243, 0xE644A8BE, 0x55159A59, 0x6F9E52A6 },
|
||||
.hash = { 0x65637572, 0x20612073, 0x68206F66, 0x20686173,
|
||||
0x69732061, 0x68697320, 0x6F2C2054, 0x48616C6C },
|
||||
};
|
||||
static uint32_t public_x[8] = { 0x5fa58f52, 0xe47cfbf2, 0x300c28c5, 0x6375ba10,
|
||||
0x62684e91, 0xda0a9a8f, 0xf9f2ed29, 0x36dfe2c6 };
|
||||
static uint32_t public_y[8] = { 0xc772f829, 0x4fabc36f, 0x09daed0b, 0xe93f9872,
|
||||
0x35a7cfab, 0x5a3c7869, 0xde1ab878, 0x71a0d4fc };
|
||||
|
||||
memcpy(state.public.x, public_x, sizeof(public_x));
|
||||
memcpy(state.public.y, public_y, sizeof(public_y));
|
||||
|
||||
/*
|
||||
* Verify
|
||||
*/
|
||||
time = RTIMER_NOW();
|
||||
PT_SPAWN(&(ecdsa_verify_test.pt), &(state.pt), ecc_dsa_verify(&state));
|
||||
time = RTIMER_NOW() - time;
|
||||
printf("ecc_dsa_verify(), %lu ms\n",
|
||||
(uint32_t)((uint64_t)time * 1000 / RTIMER_SECOND));
|
||||
|
||||
if(state.result) {
|
||||
puts("signature verification failed");
|
||||
} else {
|
||||
puts("signature verification OK");
|
||||
}
|
||||
|
||||
puts("-----------------------------------------\n"
|
||||
"Disabling pka...");
|
||||
pka_disable();
|
||||
|
||||
puts("Done!");
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
*/
|
Loading…
Add table
Add a link
Reference in a new issue