* 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;
*
* -->
*
- * \brief SHA-1 Hashing algorithm.
+ * \brief MD5 Hashing algorithm.
* \author Giovanni Bajo <rasky@develer.com>
*
- * $WIZ$ module_name = "sha1"
+ * $WIZ$ module_name = "md5"
*/
#ifndef SEC_HASH_MD5_H
#include <sec/hash.h>
#include <cfg/compiler.h>
+#include <alloca.h>
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);
#include <cfg/compiler.h>
#include <sec/hash.h>
+#include <alloca.h>
/**
* Context for SHA1 computation.
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);
#include <sec/mac.h>
#include <sec/hash.h>
+#include <alloca.h>
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);
},
};
-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; i<count; ++i, ++t)
{
- mac_set_key(&hmac->m, (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);