cc2538: Add crypto drivers and examples for AES-CCM and SHA-256
Signed-off-by: Benoît Thébaudeau <benoit.thebaudeau.dev@gmail.com>
This commit is contained in:
parent
c03536f04e
commit
117dc4e5e3
16 changed files with 2363 additions and 0 deletions
6
examples/cc2538dk/crypto/Makefile
Normal file
6
examples/cc2538dk/crypto/Makefile
Normal file
|
@ -0,0 +1,6 @@
|
|||
CONTIKI_PROJECT = ccm-test sha256-test
|
||||
|
||||
all: $(CONTIKI_PROJECT)
|
||||
|
||||
CONTIKI = ../../..
|
||||
include $(CONTIKI)/Makefile.include
|
1
examples/cc2538dk/crypto/Makefile.target
Normal file
1
examples/cc2538dk/crypto/Makefile.target
Normal file
|
@ -0,0 +1 @@
|
|||
TARGET = cc2538dk
|
345
examples/cc2538dk/crypto/ccm-test.c
Normal file
345
examples/cc2538dk/crypto/ccm-test.c
Normal file
|
@ -0,0 +1,345 @@
|
|||
/*
|
||||
* Original file:
|
||||
* Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
|
||||
* All rights reserved.
|
||||
*
|
||||
* Port to Contiki:
|
||||
* Copyright (c) 2013, ADVANSEE - http://www.advansee.com/
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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 copyright holder 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 COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT HOLDER 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-ccm-test cc2538dk AES-CCM Test Project
|
||||
*
|
||||
* AES-CCM access example for CC2538 on SmartRF06EB.
|
||||
*
|
||||
* This example shows how AES-CCM should be used. The example also verifies
|
||||
* the AES-CCM functionality.
|
||||
*
|
||||
* @{
|
||||
*
|
||||
* \file
|
||||
* Example demonstrating AES-CCM on the cc2538dk platform
|
||||
*/
|
||||
#include "contiki.h"
|
||||
#include "sys/rtimer.h"
|
||||
#include "dev/rom-util.h"
|
||||
#include "dev/ccm.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS(ccm_test_process, "ccm test process");
|
||||
AUTOSTART_PROCESSES(&ccm_test_process);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS_THREAD(ccm_test_process, ev, data)
|
||||
{
|
||||
static const char *const str_res[] = {
|
||||
"success",
|
||||
"invalid param",
|
||||
"NULL error",
|
||||
"DMA bus error",
|
||||
"keystore read error",
|
||||
"keystore write error",
|
||||
"authentication failed"
|
||||
};
|
||||
static struct {
|
||||
bool encrypt;
|
||||
uint8_t len_len;
|
||||
uint8_t key[16];
|
||||
uint8_t key_area;
|
||||
uint8_t nonce[13];
|
||||
uint8_t adata[26];
|
||||
uint16_t adata_len;
|
||||
uint8_t mdata[24];
|
||||
uint16_t mdata_len;
|
||||
uint8_t mic[8];
|
||||
uint8_t mic_len;
|
||||
uint8_t expected[24];
|
||||
} vectors[] = {
|
||||
{
|
||||
true, /* encrypt */
|
||||
2, /* len_len */
|
||||
{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* key */
|
||||
0, /* key_area */
|
||||
{ 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
|
||||
0x00, 0x00, 0x00, 0x00, 0x05 }, /* nonce */
|
||||
{}, /* adata */
|
||||
0, /* adata_len */
|
||||
{ 0x14, 0xaa, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03,
|
||||
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
|
||||
0x0c, 0x0d, 0x0e, 0x0f }, /* mdata */
|
||||
20, /* mdata_len */
|
||||
{}, /* mic */
|
||||
0, /* mic_len */
|
||||
{ 0x92, 0xe8, 0xad, 0xca, 0x53, 0x81, 0xbf, 0xd0,
|
||||
0x5b, 0xdd, 0xf3, 0x61, 0x09, 0x09, 0x82, 0xe6,
|
||||
0x2c, 0x61, 0x01, 0x4e } /* expected */
|
||||
}, {
|
||||
true, /* encrypt */
|
||||
2, /* len_len */
|
||||
{ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
|
||||
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf }, /* key */
|
||||
0, /* key_area */
|
||||
{ 0xac, 0xde, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01,
|
||||
0x00, 0x00, 0x00, 0x05, 0x02 }, /* nonce */
|
||||
{ 0x08, 0xd0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xde, 0xac, 0x02, 0x05, 0x00,
|
||||
0x00, 0x00, 0x55, 0xcf, 0x00, 0x00, 0x51, 0x52,
|
||||
0x53, 0x54 }, /* adata */
|
||||
26, /* adata_len */
|
||||
{}, /* mdata */
|
||||
0, /* mdata_len */
|
||||
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* mic */
|
||||
8, /* mic_len */
|
||||
{ 0x22, 0x3b, 0xc1, 0xec, 0x84, 0x1a, 0xb5, 0x53 } /* expected */
|
||||
}, {
|
||||
true, /* encrypt */
|
||||
2, /* len_len */
|
||||
{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* key */
|
||||
0, /* key_area */
|
||||
{ 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
|
||||
0x00, 0x00, 0x00, 0x00, 0x05 }, /* nonce */
|
||||
{ 0x69, 0x98, 0x03, 0x33, 0x63, 0xbb, 0xaa, 0x01,
|
||||
0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x03 }, /* adata */
|
||||
15, /* adata_len */
|
||||
{ 0x14, 0xaa, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03,
|
||||
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
|
||||
0x0c, 0x0d, 0x0e, 0x0f }, /* mdata */
|
||||
20, /* mdata_len */
|
||||
{ 0x00, 0x00, 0x00, 0x00 }, /* mic */
|
||||
4, /* mic_len */
|
||||
{ 0x92, 0xe8, 0xad, 0xca, 0x53, 0x81, 0xbf, 0xd0,
|
||||
0x5b, 0xdd, 0xf3, 0x61, 0x09, 0x09, 0x82, 0xe6,
|
||||
0x2c, 0x61, 0x01, 0x4e, 0x7b, 0x34, 0x4f, 0x09 } /* expected */
|
||||
}, {
|
||||
false, /* decrypt */
|
||||
2, /* len_len */
|
||||
{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* key */
|
||||
0, /* key_area */
|
||||
{ 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
|
||||
0x00, 0x00, 0x00, 0x00, 0x05 }, /* nonce */
|
||||
{}, /* adata */
|
||||
0, /* adata_len */
|
||||
{ 0x92, 0xe8, 0xad, 0xca, 0x53, 0x81, 0xbf, 0xd0,
|
||||
0x5b, 0xdd, 0xf3, 0x61, 0x09, 0x09, 0x82, 0xe6,
|
||||
0x2c, 0x61, 0x01, 0x4e }, /* mdata */
|
||||
20, /* mdata_len */
|
||||
{}, /* mic */
|
||||
0, /* mic_len */
|
||||
{ 0x14, 0xaa, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03,
|
||||
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
|
||||
0x0c, 0x0d, 0x0e, 0x0f } /* expected */
|
||||
}, {
|
||||
false, /* decrypt */
|
||||
2, /* len_len */
|
||||
{ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
|
||||
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf }, /* key */
|
||||
0, /* key_area */
|
||||
{ 0xac, 0xde, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01,
|
||||
0x00, 0x00, 0x00, 0x05, 0x02 }, /* nonce */
|
||||
{ 0x08, 0xd0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xde, 0xac, 0x02, 0x05, 0x00,
|
||||
0x00, 0x00, 0x55, 0xcf, 0x00, 0x00, 0x51, 0x52,
|
||||
0x53, 0x54 }, /* adata */
|
||||
26, /* adata_len */
|
||||
{ 0x22, 0x3b, 0xc1, 0xec, 0x84, 0x1a, 0xb5, 0x53 }, /* mdata */
|
||||
8, /* mdata_len */
|
||||
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* mic */
|
||||
8, /* mic_len */
|
||||
{} /* expected */
|
||||
}, {
|
||||
false, /* decrypt */
|
||||
2, /* len_len */
|
||||
{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, /* key */
|
||||
0, /* key_area */
|
||||
{ 0x00, 0x00, 0xf0, 0xe0, 0xd0, 0xc0, 0xb0, 0xa0,
|
||||
0x00, 0x00, 0x00, 0x00, 0x05 }, /* nonce */
|
||||
{ 0x69, 0x98, 0x03, 0x33, 0x63, 0xbb, 0xaa, 0x01,
|
||||
0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x03 }, /* adata */
|
||||
15, /* adata_len */
|
||||
{ 0x92, 0xe8, 0xad, 0xca, 0x53, 0x81, 0xbf, 0xd0,
|
||||
0x5b, 0xdd, 0xf3, 0x61, 0x09, 0x09, 0x82, 0xe6,
|
||||
0x2c, 0x61, 0x01, 0x4e, 0x7b, 0x34, 0x4f, 0x09 }, /* mdata */
|
||||
24, /* mdata_len */
|
||||
{ 0x00, 0x00, 0x00, 0x00 }, /* mic */
|
||||
4, /* mic_len */
|
||||
{ 0x14, 0xaa, 0xbb, 0x00, 0x00, 0x01, 0x02, 0x03,
|
||||
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
|
||||
0x0c, 0x0d, 0x0e, 0x0f } /* expected */
|
||||
}
|
||||
};
|
||||
static int i;
|
||||
static uint8_t ret;
|
||||
static rtimer_clock_t time, time2, total_time;
|
||||
|
||||
PROCESS_BEGIN();
|
||||
|
||||
puts("-----------------------------------------\n"
|
||||
"Initializing cryptoprocessor...");
|
||||
crypto_init();
|
||||
|
||||
for(i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
|
||||
printf("-----------------------------------------\n"
|
||||
"Test vector #%d: %s\n"
|
||||
"len_len=%d key_area=%d\n"
|
||||
"adata_len=%d mdata_len=%d mic_len=%d\n",
|
||||
i, vectors[i].encrypt ? "encrypt" : "decrypt",
|
||||
vectors[i].len_len, vectors[i].key_area,
|
||||
vectors[i].adata_len, vectors[i].mdata_len, vectors[i].mic_len);
|
||||
|
||||
time = RTIMER_NOW();
|
||||
ret = aes_load_key(vectors[i].key, vectors[i].key_area);
|
||||
time = RTIMER_NOW() - time;
|
||||
total_time = time;
|
||||
printf("aes_load_key(): %s, %lu us\n", str_res[ret],
|
||||
(uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
|
||||
PROCESS_PAUSE();
|
||||
if(ret != CRYPTO_SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
time = RTIMER_NOW();
|
||||
if(vectors[i].encrypt) {
|
||||
ret = ccm_auth_encrypt_start(vectors[i].len_len, vectors[i].key_area,
|
||||
vectors[i].nonce, vectors[i].adata,
|
||||
vectors[i].adata_len, vectors[i].mdata,
|
||||
vectors[i].mdata_len, vectors[i].mic_len);
|
||||
time2 = RTIMER_NOW();
|
||||
time = time2 - time;
|
||||
total_time += time;
|
||||
if(ret == CRYPTO_SUCCESS) {
|
||||
do {
|
||||
PROCESS_PAUSE();
|
||||
} while(!ccm_auth_encrypt_check_status());
|
||||
time2 = RTIMER_NOW() - time2;
|
||||
total_time += time2;
|
||||
}
|
||||
printf("ccm_auth_encrypt_start(): %s, %lu us\n", str_res[ret],
|
||||
(uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
|
||||
if(ret != CRYPTO_SUCCESS) {
|
||||
PROCESS_PAUSE();
|
||||
continue;
|
||||
}
|
||||
printf("ccm_auth_encrypt_check_status() wait: %lu us\n",
|
||||
(uint32_t)((uint64_t)time2 * 1000000 / RTIMER_SECOND));
|
||||
|
||||
time = RTIMER_NOW();
|
||||
ret = ccm_auth_encrypt_get_result(vectors[i].mic, vectors[i].mic_len);
|
||||
time = RTIMER_NOW() - time;
|
||||
total_time += time;
|
||||
printf("ccm_auth_encrypt_get_result(): %s, %lu us\n", str_res[ret],
|
||||
(uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
|
||||
PROCESS_PAUSE();
|
||||
if(ret != CRYPTO_SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(rom_util_memcmp(vectors[i].mdata, vectors[i].expected,
|
||||
vectors[i].mdata_len)) {
|
||||
puts("Encrypted message does not match expected one");
|
||||
} else {
|
||||
puts("Encrypted message OK");
|
||||
}
|
||||
|
||||
if(rom_util_memcmp(vectors[i].mic,
|
||||
vectors[i].expected + vectors[i].mdata_len,
|
||||
vectors[i].mic_len)) {
|
||||
puts("MIC does not match expected one");
|
||||
} else {
|
||||
puts("MIC OK");
|
||||
}
|
||||
} else {
|
||||
ret = ccm_auth_decrypt_start(vectors[i].len_len, vectors[i].key_area,
|
||||
vectors[i].nonce, vectors[i].adata,
|
||||
vectors[i].adata_len, vectors[i].mdata,
|
||||
vectors[i].mdata_len, vectors[i].mic_len);
|
||||
time2 = RTIMER_NOW();
|
||||
time = time2 - time;
|
||||
total_time += time;
|
||||
if(ret == CRYPTO_SUCCESS) {
|
||||
do {
|
||||
PROCESS_PAUSE();
|
||||
} while(!ccm_auth_decrypt_check_status());
|
||||
time2 = RTIMER_NOW() - time2;
|
||||
total_time += time2;
|
||||
}
|
||||
printf("ccm_auth_decrypt_start(): %s, %lu us\n", str_res[ret],
|
||||
(uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
|
||||
if(ret != CRYPTO_SUCCESS) {
|
||||
PROCESS_PAUSE();
|
||||
continue;
|
||||
}
|
||||
printf("ccm_auth_decrypt_check_status() wait: %lu us\n",
|
||||
(uint32_t)((uint64_t)time2 * 1000000 / RTIMER_SECOND));
|
||||
|
||||
time = RTIMER_NOW();
|
||||
ret = ccm_auth_decrypt_get_result(vectors[i].mdata, vectors[i].mdata_len,
|
||||
vectors[i].mic, vectors[i].mic_len);
|
||||
time = RTIMER_NOW() - time;
|
||||
total_time += time;
|
||||
printf("ccm_auth_decrypt_get_result(): %s, %lu us\n", str_res[ret],
|
||||
(uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
|
||||
PROCESS_PAUSE();
|
||||
if(ret != CRYPTO_SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(rom_util_memcmp(vectors[i].mdata, vectors[i].expected,
|
||||
vectors[i].mdata_len - vectors[i].mic_len)) {
|
||||
puts("Decrypted message does not match expected one");
|
||||
} else {
|
||||
puts("Decrypted message OK");
|
||||
}
|
||||
}
|
||||
|
||||
printf("Total duration: %lu us\n",
|
||||
(uint32_t)((uint64_t)total_time * 1000000 / RTIMER_SECOND));
|
||||
}
|
||||
|
||||
puts("-----------------------------------------\n"
|
||||
"Disabling cryptoprocessor...");
|
||||
crypto_disable();
|
||||
|
||||
puts("Done!");
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
*/
|
260
examples/cc2538dk/crypto/sha256-test.c
Normal file
260
examples/cc2538dk/crypto/sha256-test.c
Normal file
|
@ -0,0 +1,260 @@
|
|||
/*
|
||||
* Original file:
|
||||
* Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com/
|
||||
* All rights reserved.
|
||||
*
|
||||
* Port to Contiki:
|
||||
* Copyright (c) 2013, ADVANSEE - http://www.advansee.com/
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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 copyright holder 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 COPYRIGHT HOLDERS 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
|
||||
* COPYRIGHT HOLDER 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-sha256-test cc2538dk SHA-256 Test Project
|
||||
*
|
||||
* SHA-256 access example for CC2538 on SmartRF06EB.
|
||||
*
|
||||
* This example shows how SHA-256 should be used. The example also verifies
|
||||
* the SHA-256 functionality.
|
||||
*
|
||||
* @{
|
||||
*
|
||||
* \file
|
||||
* Example demonstrating SHA-256 on the cc2538dk platform
|
||||
*/
|
||||
#include "contiki.h"
|
||||
#include "sys/rtimer.h"
|
||||
#include "dev/rom-util.h"
|
||||
#include "dev/sha256.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS(sha256_test_process, "sha256 test process");
|
||||
AUTOSTART_PROCESSES(&sha256_test_process);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS_THREAD(sha256_test_process, ev, data)
|
||||
{
|
||||
static const char *const str_res[] = {
|
||||
"success",
|
||||
"invalid param",
|
||||
"NULL error",
|
||||
"DMA bus error"
|
||||
};
|
||||
static const struct {
|
||||
const char *data[3];
|
||||
uint8_t sha256[32];
|
||||
} vectors[] = {
|
||||
{ /* Simple */
|
||||
{
|
||||
"abc",
|
||||
NULL,
|
||||
NULL
|
||||
}, {
|
||||
0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
|
||||
0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
|
||||
0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
|
||||
0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
|
||||
}
|
||||
}, { /* Simple */
|
||||
{
|
||||
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
|
||||
NULL,
|
||||
NULL,
|
||||
}, {
|
||||
0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
|
||||
0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
|
||||
0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
|
||||
0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
|
||||
}
|
||||
}, { /* Message of length 130 */
|
||||
{
|
||||
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklabcd"
|
||||
"efghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmn",
|
||||
NULL,
|
||||
NULL
|
||||
}, {
|
||||
0x15, 0xd2, 0x3e, 0xea, 0x57, 0xb3, 0xd4, 0x61,
|
||||
0xbf, 0x38, 0x91, 0x12, 0xab, 0x4c, 0x43, 0xce,
|
||||
0x85, 0xe1, 0x68, 0x23, 0x8a, 0xaa, 0x54, 0x8e,
|
||||
0xc8, 0x6f, 0x0c, 0x9d, 0x65, 0xf9, 0xb9, 0x23
|
||||
}
|
||||
}, { /* Message of length 128 */
|
||||
{
|
||||
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklabcd"
|
||||
"efghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl",
|
||||
NULL,
|
||||
NULL
|
||||
}, {
|
||||
0xf8, 0xa3, 0xf2, 0x26, 0xfc, 0x42, 0x10, 0xe9,
|
||||
0x0d, 0x13, 0x0c, 0x7f, 0x41, 0xf2, 0xbe, 0x66,
|
||||
0x45, 0x53, 0x85, 0xd2, 0x92, 0x0a, 0xda, 0x78,
|
||||
0x15, 0xf8, 0xf7, 0x95, 0xd9, 0x44, 0x90, 0x5f
|
||||
}
|
||||
}, { /* Message of length 64 */
|
||||
{
|
||||
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl",
|
||||
NULL,
|
||||
NULL
|
||||
}, {
|
||||
0x2f, 0xcd, 0x5a, 0x0d, 0x60, 0xe4, 0xc9, 0x41,
|
||||
0x38, 0x1f, 0xcc, 0x4e, 0x00, 0xa4, 0xbf, 0x8b,
|
||||
0xe4, 0x22, 0xc3, 0xdd, 0xfa, 0xfb, 0x93, 0xc8,
|
||||
0x09, 0xe8, 0xd1, 0xe2, 0xbf, 0xff, 0xae, 0x8e
|
||||
}
|
||||
}, { /* Message of length 66 */
|
||||
{
|
||||
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmn",
|
||||
NULL,
|
||||
NULL
|
||||
}, {
|
||||
0x92, 0x90, 0x1c, 0x85, 0x82, 0xe3, 0x1c, 0x05,
|
||||
0x69, 0xb5, 0x36, 0x26, 0x9c, 0xe2, 0x2c, 0xc8,
|
||||
0x30, 0x8b, 0xa4, 0x17, 0xab, 0x36, 0xc1, 0xbb,
|
||||
0xaf, 0x08, 0x4f, 0xf5, 0x8b, 0x18, 0xdc, 0x6a
|
||||
}
|
||||
}, {
|
||||
{
|
||||
"abcdbcdecdefde",
|
||||
"fgefghfghighijhijkijkljklmklmnlmnomnopnopq",
|
||||
NULL
|
||||
}, {
|
||||
0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
|
||||
0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
|
||||
0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
|
||||
0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1
|
||||
}
|
||||
}, {
|
||||
{
|
||||
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl",
|
||||
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl",
|
||||
NULL
|
||||
}, {
|
||||
0xf8, 0xa3, 0xf2, 0x26, 0xfc, 0x42, 0x10, 0xe9,
|
||||
0x0d, 0x13, 0x0c, 0x7f, 0x41, 0xf2, 0xbe, 0x66,
|
||||
0x45, 0x53, 0x85, 0xd2, 0x92, 0x0a, 0xda, 0x78,
|
||||
0x15, 0xf8, 0xf7, 0x95, 0xd9, 0x44, 0x90, 0x5f
|
||||
}
|
||||
}, {
|
||||
{
|
||||
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefgh",
|
||||
"ijkl",
|
||||
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"
|
||||
}, {
|
||||
0xf8, 0xa3, 0xf2, 0x26, 0xfc, 0x42, 0x10, 0xe9,
|
||||
0x0d, 0x13, 0x0c, 0x7f, 0x41, 0xf2, 0xbe, 0x66,
|
||||
0x45, 0x53, 0x85, 0xd2, 0x92, 0x0a, 0xda, 0x78,
|
||||
0x15, 0xf8, 0xf7, 0x95, 0xd9, 0x44, 0x90, 0x5f
|
||||
}
|
||||
}
|
||||
};
|
||||
static sha256_state_t state;
|
||||
static uint8_t sha256[32];
|
||||
static int i, j;
|
||||
static uint8_t ret;
|
||||
static rtimer_clock_t total_time;
|
||||
rtimer_clock_t time;
|
||||
size_t len;
|
||||
|
||||
PROCESS_BEGIN();
|
||||
|
||||
puts("-----------------------------------------\n"
|
||||
"Initializing cryptoprocessor...");
|
||||
crypto_init();
|
||||
|
||||
for(i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
|
||||
printf("-----------------------------------------\n"
|
||||
"Test vector #%d:\n", i);
|
||||
|
||||
time = RTIMER_NOW();
|
||||
ret = sha256_init(&state);
|
||||
time = RTIMER_NOW() - time;
|
||||
total_time = time;
|
||||
printf("sha256_init(): %s, %lu us\n", str_res[ret],
|
||||
(uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
|
||||
PROCESS_PAUSE();
|
||||
if(ret != CRYPTO_SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for(j = 0; j < sizeof(vectors[i].data) / sizeof(vectors[i].data[0]) &&
|
||||
vectors[i].data[j] != NULL; j++) {
|
||||
len = strlen(vectors[i].data[j]);
|
||||
printf("Buffer #%d (length: %u):\n", j, len);
|
||||
time = RTIMER_NOW();
|
||||
ret = sha256_process(&state, vectors[i].data[j], len);
|
||||
time = RTIMER_NOW() - time;
|
||||
total_time += time;
|
||||
printf("sha256_process(): %s, %lu us\n", str_res[ret],
|
||||
(uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
|
||||
PROCESS_PAUSE();
|
||||
if(ret != CRYPTO_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(ret != CRYPTO_SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
time = RTIMER_NOW();
|
||||
ret = sha256_done(&state, sha256);
|
||||
time = RTIMER_NOW() - time;
|
||||
total_time += time;
|
||||
printf("sha256_done(): %s, %lu us\n", str_res[ret],
|
||||
(uint32_t)((uint64_t)time * 1000000 / RTIMER_SECOND));
|
||||
PROCESS_PAUSE();
|
||||
if(ret != CRYPTO_SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(rom_util_memcmp(sha256, vectors[i].sha256, sizeof(sha256))) {
|
||||
puts("Computed SHA-256 hash does not match expected hash");
|
||||
} else {
|
||||
puts("Computed SHA-256 hash OK");
|
||||
}
|
||||
printf("Total duration: %lu us\n",
|
||||
(uint32_t)((uint64_t)total_time * 1000000 / RTIMER_SECOND));
|
||||
}
|
||||
|
||||
puts("-----------------------------------------\n"
|
||||
"Disabling cryptoprocessor...");
|
||||
crypto_disable();
|
||||
|
||||
puts("Done!");
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
*/
|
Loading…
Add table
Add a link
Reference in a new issue