From a7f5f68b9e3cdfc949edef0a5047cf911f13137f Mon Sep 17 00:00:00 2001 From: rasky Date: Fri, 24 Sep 2010 15:15:31 +0000 Subject: [PATCH] SEC: implement stackinit() functions to simplify initialization and composition of algorithms. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4303 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/sec/hash/md5.c | 2 +- bertos/sec/hash/md5.h | 8 ++++++-- bertos/sec/hash/sha1.h | 4 ++++ bertos/sec/mac/hmac.h | 4 ++++ bertos/sec/mac/hmac_test.c | 36 +++++++++--------------------------- 5 files changed, 24 insertions(+), 30 deletions(-) diff --git a/bertos/sec/hash/md5.c b/bertos/sec/hash/md5.c index f139cd6e..8e89907b 100644 --- a/bertos/sec/hash/md5.c +++ b/bertos/sec/hash/md5.c @@ -94,7 +94,7 @@ static void MD5_update(Hash *h, const void* vbuf, size_t len) * Final wrapup - pad to 64-byte boundary with the bit pattern * 1 0* (64-bit count of bits processed, MSB-first) */ -uint8_t* MD5_final(struct Hash *h) +static uint8_t* MD5_final(struct Hash *h) { MD5_Context *ctx = (MD5_Context *)h; unsigned count; diff --git a/bertos/sec/hash/md5.h b/bertos/sec/hash/md5.h index c08a912b..a6d886df 100644 --- a/bertos/sec/hash/md5.h +++ b/bertos/sec/hash/md5.h @@ -30,10 +30,10 @@ * * --> * - * \brief SHA-1 Hashing algorithm. + * \brief MD5 Hashing algorithm. * \author Giovanni Bajo * - * $WIZ$ module_name = "sha1" + * $WIZ$ module_name = "md5" */ #ifndef SEC_HASH_MD5_H @@ -41,6 +41,7 @@ #include #include +#include typedef struct { @@ -53,6 +54,9 @@ typedef struct void MD5_init(MD5_Context *ctx); +#define MD5_stackinit(...) \ + ({ MD5_Context *ctx = alloca(sizeof(MD5_Context)); MD5_init(ctx , ##__VA_ARGS__); &ctx->h; }) + int MD5_testSetup(void); int MD5_testRun(void); int MD5_testTearDown(void); diff --git a/bertos/sec/hash/sha1.h b/bertos/sec/hash/sha1.h index 734779ab..eff7fa70 100644 --- a/bertos/sec/hash/sha1.h +++ b/bertos/sec/hash/sha1.h @@ -41,6 +41,7 @@ #include #include +#include /** * Context for SHA1 computation. @@ -54,6 +55,9 @@ typedef struct { void SHA1_init(SHA1_Context *context); +#define SHA1_stackinit(...) \ + ({ SHA1_Context *ctx = alloca(sizeof(SHA1_Context)); SHA1_init(ctx, ##__VA_ARGS__); &ctx->h; }) + int SHA1_testSetup(void); int SHA1_testRun(void); int SHA1_testTearDown(void); diff --git a/bertos/sec/mac/hmac.h b/bertos/sec/mac/hmac.h index c22a695b..88de9fab 100644 --- a/bertos/sec/mac/hmac.h +++ b/bertos/sec/mac/hmac.h @@ -40,6 +40,7 @@ #include #include +#include typedef struct HMAC_Context { @@ -50,6 +51,9 @@ typedef struct HMAC_Context void HMAC_init(HMAC_Context* hmac, Hash *h); +#define HMAC_stackinit(...) \ + ({ HMAC_Context *ctx = alloca(sizeof(HMAC_Context)); HMAC_init(ctx, ##__VA_ARGS__); &ctx->m; }) + int HMAC_testSetup(void); int HMAC_testRun(void); int HMAC_testTearDown(void); diff --git a/bertos/sec/mac/hmac_test.c b/bertos/sec/mac/hmac_test.c index 8b160964..29b6620e 100644 --- a/bertos/sec/mac/hmac_test.c +++ b/bertos/sec/mac/hmac_test.c @@ -151,42 +151,24 @@ const struct Test_HMAC tests_hmac_sha1[] = }, }; -static void algo_run_tests(HMAC_Context *hmac, const struct Test_HMAC *t, int count) +static void algo_run_tests(Mac *mac, const struct Test_HMAC *t, int count) { for (int i=0; im, (const uint8_t*)t->key, t->key_len); - mac_begin(&hmac->m); - mac_update(&hmac->m, (const uint8_t*)t->data, t->data_len); - ASSERT(memcmp(mac_final(&hmac->m), t->digest, mac_digest_len(&hmac->m)) == 0); + mac_set_key(mac, (const uint8_t*)t->key, t->key_len); + mac_begin(mac); + mac_update(mac, (const uint8_t*)t->data, t->data_len); + ASSERT(memcmp(mac_final(mac), t->digest, mac_digest_len(mac)) == 0); } } int HMAC_testRun(void) { - if (1) - { - MD5_Context md5; - MD5_init(&md5); - - HMAC_Context hmac; - HMAC_init(&hmac, &md5.h); + algo_run_tests(HMAC_stackinit(MD5_stackinit()), + tests_hmac_md5, countof(tests_hmac_md5)); - algo_run_tests(&hmac, tests_hmac_md5, countof(tests_hmac_md5)); - } - - if (1) - { - SHA1_Context sha1; - SHA1_init(&sha1); - - HMAC_Context hmac; - HMAC_init(&hmac, &sha1.h); - - algo_run_tests(&hmac, tests_hmac_sha1, countof(tests_hmac_sha1)); - } - - return 0; + algo_run_tests(HMAC_stackinit(SHA1_stackinit()), + tests_hmac_sha1, countof(tests_hmac_sha1)); } TEST_MAIN(HMAC); -- 2.25.1