Commit 1c664d35 authored by Niels Möller's avatar Niels Möller
Browse files

* hmac.h: General cleanup. Added declarations of hmac-md5,

hmac-sha1 and hmac-sha256.

* Makefile.am (libnettle_a_SOURCES): Added hmac.c and hmac-md5.c.
(libnettleinclude_HEADERS): Added hmac.h.

Rev: src/nettle/hmac.h:1.2
parent 7964f11d
...@@ -28,150 +28,86 @@ ...@@ -28,150 +28,86 @@
#include "nettle-meta.h" #include "nettle-meta.h"
#include "md5.h"
#include "sha.h"
#include <inttypes.h> #include <inttypes.h>
struct hmac_info
{
/* Size of digests, both internal and the final MAC */
unsigned digest_size;
/* Internal block size */
unsigned block_size;
/* Size of the context struct for the underlying hash function. */
unsigned ctx_size;
/* Init function */
void (*init)(void *ctx);
/* Update function */
void (*update)(void *ctx,
unsigned length,
const uint8_t *data);
/* Digest extraction function */
void (*digest)(void *ctx,
unsigned length,
uint8_t *digest);
};
#define _HMAC_INFO(name, NAME)
{
NAME##_DIGEST_SIZE,
NAME##_DATA_SIZE,
sizeof(struct name##_ctx);
(void (*)(void *ctx)) name##_init,
(void (*)(void *ctx,
unsigned length,
const uint8_t *data)) name##_update,
(void (*)(void *ctx,
unsigned length,
uint8_t *digest)) name##_digest
}
extern const struct hmac_info hmac_md5_info;
extern const struct hmac_info hmac_sha1_info;
#if 0
void void
hmac_init(void *outer, void *inner, void *state, hmac_set_key(void *outer, void *inner, void *state,
struct hmac_info *info, const struct nettle_hash *hash,
unsigned key_length, const uint8_t *key); unsigned length, const uint8_t *key);
/* This function is not strictly needed, it's s just the same as the
* hash update function. */
void void
hmac_update(void *state, void (*update)(void *ctx, hmac_update(void *state,
unsigned length, const struct nettle_hash *hash,
const uint8_t *data)); unsigned length, const uint8_t *data);
void
hmac_digest(void *outer, void *inner, void *state
struct hmac_info *info,
unsigned length, uint8_t *dst);
#endif
void void
hmac_set_key(void *outer, void *inner, void *state, hmac_digest(const void *outer, const void *inner, void *state,
(void (*update)(void *ctx, const struct nettle_hash *hash,
unsigned length, unsigned length, uint8_t *digest);
const uint8_t *data))(update),
(void (*digest)(void *ctx,
unsigned length,
uint8_t *digest))(digest),
unsigned block_size, unsigned digest_size,
unsigned context_size,
unsigned key_length, const uint8_t *key);
void
hmac_digest(void *outer, void *inner, void *state,
(void (*update)(void *ctx,
unsigned length,
const uint8_t *data))(update),
(void (*digest)(void *ctx,
unsigned length,
uint8_t *digest))(digest),
unsigned digest_size,
unsigned context_size,
unsigned digest_length, uint8_t *digest);
#define HMAC_CTX(type) \ #define HMAC_CTX(type) \
{ type outer; type inner; type state; } { type outer; type inner; type state; }
#define HMAC_INIT(ctx, init) \ #define HMAC_SET_KEY(ctx, hash, length, key) \
((init)((ctx)->outer), (init)((ctx)->inner), (init)((ctx)->state)) hmac_set_key( &(ctx)->outer, &(ctx)->inner, &(ctx)->state, \
(hash), (length), (key) )
#define HMAC_SET_KEY(ctx, update, digest, block_size, digest_size, length, key) \
(0 ? ( (update)(ctx->outer, 0, NULL), (digest)(ctx->outer, 0, NULL)) \ #define HMAC_DIGEST(ctx, hash, length, digest) \
: hmac_set_key( (void *) (ctx)->outer, \ hmac_digest( &(ctx)->outer, &(ctx)->inner, &(ctx)->state, \
(void *) (ctx)->inner, \ (hash), (length), (digest) )
(void *) (ctx)->state, \
(void (*)(void *ctx, \ /* HMAC using specific hash functions */
unsigned length, \
const uint8_t *data))(update), \ /* hmac-md5 */
(void (*)(void *ctx, \ struct hmac_md5_ctx HMAC_CTX(struct md5_ctx);
unsigned length, \
uint8_t *digest))(digest), \ void
(block_size), (digest_size), \ hmac_md5_set_key(struct hmac_md5_ctx *ctx,
sizeof(*(ctx)->state), \ unsigned key_length, const uint8_t *key);
(length), (key)))
void
#define HMAC_UPDATE(ctx, f, length, data) \ hmac_md5_update(struct hmac_md5_ctx *ctx,
((f)((ctx)->state, (length), (data))) unsigned length, const uint8_t *data);
#define HMAC_DIGEST(ctx, update, digest, digest_size, length, digest) \ void
(0 ? ( (update)(ctx->outer, 0, NULL), (digest)(ctx->outer, 0, NULL)) \ hmac_md5_digest(struct hmac_md5_ctx *ctx,
: hmac_digest( (void *) (ctx)->outer, \ unsigned length, uint8_t *digest);
(void *) (ctx)->inner, \
(void *) (ctx)->state, \
(void (*)(void *ctx, \ /* hmac-sha1 */
unsigned length, \ struct hmac_sha1_ctx HMAC_CTX(struct sha1_ctx);
const uint8_t *data))(update), \
(void (*)(void *ctx, \ void
unsigned length, \ hmac_sha1_set_key(struct hmac_sha1_ctx *ctx,
uint8_t *digest))(digest), \ unsigned key_length, const uint8_t *key);
(digest_size), \
sizeof(*(ctx)->state), \ void
(length), (digest))) hmac_sha1_update(struct hmac_sha1_ctx *ctx,
unsigned length, const uint8_t *data);
#if 0
#define HMAC_INIT(info, ctx, length, key) \ void
(hmac_init( \ hmac_sha1_digest(struct hmac_sha1_ctx *ctx,
(void *) ((ctx)->outer), (void *) ((ctx)->inner), (void *) ((ctx)->state), \ unsigned length, uint8_t *digest);
(info), \
(length), (key))) /* hmac-sha256 */
struct hmac_sha256_ctx HMAC_CTX(struct sha256_ctx);
#define HMAC_UPDATE(info, ctx, length, data) \
((info)->update( \ void
(void *) ((ctx)->state), \ hmac_sha256_set_key(struct hmac_sha256_ctx *ctx,
(length), (data))) unsigned key_length, const uint8_t *key);
#define HMAC_DIGEST(info, ctx, length, digest) \ void
(hmac_digest( \ hmac_sha256_update(struct hmac_sha256_ctx *ctx,
(void *) ((ctx)->outer), (void *) ((ctx)->inner), (void *) ((ctx)->state), \ unsigned length, const uint8_t *data);
(info), \
(length), (digest))) void
hmac_sha256_digest(struct hmac_sha256_ctx *ctx,
#endif unsigned length, uint8_t *digest);
#endif /* NETTLE_HMAC_H_INCLUDED */ #endif /* NETTLE_HMAC_H_INCLUDED */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment