Add SHA256 and SHA512 support

This commit is contained in:
Sebastian Huber
2014-11-20 10:30:21 +01:00
parent a2b68952b1
commit 1bcd037810
14 changed files with 1003 additions and 12 deletions
+19
View File
@@ -0,0 +1,19 @@
rtems_tests_PROGRAMS = sha
sha_SOURCES = init.c
dist_rtems_tests_DATA = sha.scn sha.doc
include $(RTEMS_ROOT)/make/custom/@RTEMS_BSP@.cfg
include $(top_srcdir)/../automake/compile.am
include $(top_srcdir)/../automake/leaf.am
AM_CPPFLAGS += -I$(top_srcdir)/../support/include
LINK_OBJS = $(sha_OBJECTS)
LINK_LIBS = $(sha_LDLIBS)
sha$(EXEEXT): $(sha_OBJECTS) $(sha_DEPENDENCIES)
@rm -f sha$(EXEEXT)
$(make-exe)
include $(top_srcdir)/../automake/local.am
+191
View File
@@ -0,0 +1,191 @@
/*
* Copyright (c) 2014 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.org/license/LICENSE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sha256.h>
#include <sha512.h>
#include <stdio.h>
#include <string.h>
#include "tmacros.h"
const char rtems_test_name[] = "SHA";
static const char *const test_vectors[] = {
"abc",
"",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
};
static const unsigned char
test_sha256_results[RTEMS_ARRAY_SIZE(test_vectors)][32] = {
{
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
}, {
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
}, {
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
}, {
0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80,
0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37,
0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51,
0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1
}
};
static const unsigned char
test_sha512_results[RTEMS_ARRAY_SIZE(test_vectors)][64] = {
{
0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
}, {
0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd,
0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07,
0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc,
0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce,
0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0,
0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f,
0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81,
0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e
}, {
0x20, 0x4a, 0x8f, 0xc6, 0xdd, 0xa8, 0x2f, 0x0a,
0x0c, 0xed, 0x7b, 0xeb, 0x8e, 0x08, 0xa4, 0x16,
0x57, 0xc1, 0x6e, 0xf4, 0x68, 0xb2, 0x28, 0xa8,
0x27, 0x9b, 0xe3, 0x31, 0xa7, 0x03, 0xc3, 0x35,
0x96, 0xfd, 0x15, 0xc1, 0x3b, 0x1b, 0x07, 0xf9,
0xaa, 0x1d, 0x3b, 0xea, 0x57, 0x78, 0x9c, 0xa0,
0x31, 0xad, 0x85, 0xc7, 0xa7, 0x1d, 0xd7, 0x03,
0x54, 0xec, 0x63, 0x12, 0x38, 0xca, 0x34, 0x45
}, {
0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda,
0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f,
0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1,
0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18,
0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4,
0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a,
0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54,
0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09
}
};
static void print_result(const unsigned char *r, size_t n)
{
size_t i;
for (i = 0; i < n; ++i) {
if (i == 0) {
/* Do nothing */
} else if (i % 32 == 0) {
printf("\n");
} else if (i % 8 == 0) {
printf(" ");
}
printf("%02x", r[i]);
}
printf("\n");
}
static void test_sha256(void)
{
size_t i;
printf("test SHA256\n");
for (i = 0; i < RTEMS_ARRAY_SIZE(test_vectors); ++i) {
SHA256_CTX ctx;
unsigned char r[32];
const char *s = test_vectors[i];
SHA256_Init(&ctx);
SHA256_Update(&ctx, s, strlen(s));
SHA256_Final(r, &ctx);
print_result(&r[0], sizeof(r));
rtems_test_assert(
memcmp(&r[0], &test_sha256_results[i][0], sizeof(r)) == 0
);
}
}
static void test_sha512(void)
{
size_t i;
printf("test SHA512\n");
for (i = 0; i < RTEMS_ARRAY_SIZE(test_vectors); ++i) {
SHA512_CTX ctx;
unsigned char r[64];
const char *s = test_vectors[i];
SHA512_Init(&ctx);
SHA512_Update(&ctx, s, strlen(s));
SHA512_Final(r, &ctx);
print_result(&r[0], sizeof(r));
rtems_test_assert(
memcmp(&r[0], &test_sha512_results[i][0], sizeof(r)) == 0
);
}
}
static void Init(rtems_task_argument arg)
{
TEST_BEGIN();
test_sha256();
test_sha512();
TEST_END();
rtems_test_exit(0);
}
#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
#define CONFIGURE_MAXIMUM_TASKS 1
#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
#define CONFIGURE_INIT
#include <rtems/confdefs.h>
+17
View File
@@ -0,0 +1,17 @@
This file describes the directives and concepts tested by this test set.
test set name: sha01
directives:
- SHA256_Init
- SHA256_Update
- SHA256_Final
- SHA512_Init
- SHA512_Update
- SHA512_Final
concepts:
- Ensure that the SHA256 and SHA512 implementations yield the expected
results for some standard test vectors.
+16
View File
@@ -0,0 +1,16 @@
*** BEGIN OF TEST SHA ***
test SHA256
ba7816bf8f01cfea 414140de5dae2223 b00361a396177a9c b410ff61f20015ad
e3b0c44298fc1c14 9afbf4c8996fb924 27ae41e4649b934c a495991b7852b855
248d6a61d20638b8 e5c026930c3e6039 a33ce45964ff2167 f6ecedd419db06c1
cf5b16a778af8380 036ce59e7b049237 0b249b11e8f07a51 afac45037afee9d1
test SHA512
ddaf35a193617aba cc417349ae204131 12e6fa4e89a97ea2 0a9eeee64b55d39a
2192992a274fc1a8 36ba3c23a3feebbd 454d4423643ce80e 2a9ac94fa54ca49f
cf83e1357eefb8bd f1542850d66d8007 d620e4050b5715dc 83f4a921d36ce9ce
47d0d13c5d85f2b0 ff8318d2877eec2f 63b931bd47417a81 a538327af927da3e
204a8fc6dda82f0a 0ced7beb8e08a416 57c16ef468b228a8 279be331a703c335
96fd15c13b1b07f9 aa1d3bea57789ca0 31ad85c7a71dd703 54ec631238ca3445
8e959b75dae313da 8cf4f72814fc143f 8f7779c6eb9f7fa1 7299aeadb6889018
501d289e4900f7e4 331b99dec4b5433a c7d329eeb6dd2654 5e96e55b874be909
*** END OF TEST SHA ***