#include <string.h>
-static void HMAC_set_key(Mac *m, const void *key, size_t key_len)
+static void hmac_set_key(Mac *m, const void *key, size_t key_len)
{
- HMAC_Context *ctx = (HMAC_Context *)m;
+ HmacContext *ctx = (HmacContext *)m;
memset(ctx->key, 0, ctx->m.key_len);
if (key_len <= ctx->m.key_len)
xor_block_const(ctx->key, ctx->key, 0x5C, ctx->m.key_len);
}
-static void HMAC_begin(Mac *m)
+static void hmac_begin(Mac *m)
{
- HMAC_Context *ctx = (HMAC_Context *)m;
+ HmacContext *ctx = (HmacContext *)m;
int klen = ctx->m.key_len;
xor_block_const(ctx->key, ctx->key, 0x36^0x5C, klen);
hash_update(ctx->h, ctx->key, klen);
}
-static void HMAC_update(Mac *m, const void *data, size_t len)
+static void hmac_update(Mac *m, const void *data, size_t len)
{
- HMAC_Context *ctx = (HMAC_Context *)m;
+ HmacContext *ctx = (HmacContext *)m;
hash_update(ctx->h, data, len);
}
-static uint8_t *HMAC_final(Mac *m)
+static uint8_t *hmac_final(Mac *m)
{
- HMAC_Context *ctx = (HMAC_Context *)m;
+ HmacContext *ctx = (HmacContext *)m;
int hlen = hash_digest_len(ctx->h);
uint8_t temp[hlen];
/*********************************************************************/
-void HMAC_init(HMAC_Context *ctx, Hash *h)
+void hmac_init(HmacContext *ctx, Hash *h)
{
ctx->h = h;
ctx->m.key_len = hash_block_len(h);
ctx->m.digest_len = hash_digest_len(h);
- ctx->m.set_key = HMAC_set_key;
- ctx->m.begin = HMAC_begin;
- ctx->m.update = HMAC_update;
- ctx->m.final = HMAC_final;
+ ctx->m.set_key = hmac_set_key;
+ ctx->m.begin = hmac_begin;
+ ctx->m.update = hmac_update;
+ ctx->m.final = hmac_final;
ASSERT(sizeof(ctx->key) >= ctx->m.key_len);
}
*
* \brief HMAC (RFC 2104) implementation
* \author Giovanni Bajo <rasky@develer.com>
- *
+ *
*/
#ifndef SEC_MAC_HMAC_H
#include <sec/hash.h>
#include <alloca.h>
-typedef struct HMAC_Context
+typedef struct HmacContext
{
Mac m;
- Hash *h;
+ Hash *h;
uint8_t key[64];
-} HMAC_Context;
+} HmacContext;
-void HMAC_init(HMAC_Context* hmac, Hash *h);
+void hmac_init(HmacContext* hmac, Hash *h);
-#define HMAC_stackinit(...) \
- ({ HMAC_Context *ctx = alloca(sizeof(HMAC_Context)); HMAC_init(ctx, ##__VA_ARGS__); &ctx->m; })
+#define hmac_stackinit(...) \
+ ({ HmacContext *ctx = alloca(sizeof(HmacContext)); hmac_init(ctx, ##__VA_ARGS__); &ctx->m; })
-int HMAC_testSetup(void);
-int HMAC_testRun(void);
-int HMAC_testTearDown(void);
+int hmac_testSetup(void);
+int hmac_testRun(void);
+int hmac_testTearDown(void);
#endif /* SEC_MAC_HMAC_H */
-#include <sec/mac/hmac.h>
+#include "hmac.h"
#include <cfg/test.h>
#include <cfg/debug.h>
#include <sec/hash/sha1.h>
#include <sec/hash/md5.h>
#include <string.h>
-int HMAC_testSetup(void)
+int hmac_testSetup(void)
{
kdbg_init();
return 0;
}
-int HMAC_testTearDown(void)
+int hmac_testTearDown(void)
{
return 0;
}
const struct Test_HMAC tests_hmac_md5[] =
{
- {
+ {
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", 16,
"Hi There", 8,
"\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d",
},
{
- "Jefe", 4,
+ "Jefe", 4,
"what do ya want for nothing?", 28,
"\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38",
},
"Jefe", 4,
"what do ya want for nothing?", 28,
"\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79",
- },
+ },
{
"\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa", 20,
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
}
}
-int HMAC_testRun(void)
+int hmac_testRun(void)
{
- algo_run_tests(HMAC_stackinit(MD5_stackinit()),
+ algo_run_tests(hmac_stackinit(MD5_stackinit()),
tests_hmac_md5, countof(tests_hmac_md5));
- algo_run_tests(HMAC_stackinit(SHA1_stackinit()),
+ algo_run_tests(hmac_stackinit(SHA1_stackinit()),
tests_hmac_sha1, countof(tests_hmac_sha1));
+
+ return 0;
}
TEST_MAIN(HMAC);
*
* \brief Generic utilities.
* \author Giovanni Bajo <rasky@develer.com>
- *
+ *
*/
#include "util.h"
void password2key(const char *pwd, size_t pwd_len,
uint8_t *key, size_t key_len)
{
- Kdf *kdf = PBKDF2_stackinit(HMAC_stackinit(SHA1_stackinit()));
-
+ Kdf *kdf = PBKDF2_stackinit(hmac_stackinit(SHA1_stackinit()));
+
kdf_begin(kdf, pwd, pwd_len, (uint8_t*)SALT, sizeof(SALT));
kdf_read(kdf, key, key_len);
-
+
// FIXME: how to purge the stack?
}