Skip to content

Commit

Permalink
hmac: Fixed crashes on CPUs that don't allow unaligned memory access.
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen committed Sep 8, 2013
1 parent 1c1ded8 commit d8361cc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/lib/hmac-cram-md5.c
Expand Up @@ -9,9 +9,10 @@
#include "md5.h"
#include "hmac-cram-md5.h"

void hmac_md5_get_cram_context(struct hmac_context *hmac_ctx,
void hmac_md5_get_cram_context(struct hmac_context *_hmac_ctx,
unsigned char context_digest[CRAM_MD5_CONTEXTLEN])
{
struct hmac_context_priv *hmac_ctx = &_hmac_ctx->u.priv;
unsigned char *cdp;

struct md5_context *ctx = (void*)hmac_ctx->ctx;
Expand All @@ -34,9 +35,10 @@ void hmac_md5_get_cram_context(struct hmac_context *hmac_ctx,
CDPUT(cdp, ctx->d);
}

void hmac_md5_set_cram_context(struct hmac_context *hmac_ctx,
void hmac_md5_set_cram_context(struct hmac_context *_hmac_ctx,
const unsigned char context_digest[CRAM_MD5_CONTEXTLEN])
{
struct hmac_context_priv *hmac_ctx = &_hmac_ctx->u.priv;
const unsigned char *cdp;

struct md5_context *ctx = (void*)hmac_ctx->ctx;
Expand Down
7 changes: 5 additions & 2 deletions src/lib/hmac.c
Expand Up @@ -11,9 +11,10 @@
#include "hmac.h"
#include "safe-memset.h"

void hmac_init(struct hmac_context *ctx, const unsigned char *key,
void hmac_init(struct hmac_context *_ctx, const unsigned char *key,
size_t key_len, const struct hash_method *meth)
{
struct hmac_context_priv *ctx = &_ctx->u.priv;
int i;
unsigned char k_ipad[64];
unsigned char k_opad[64];
Expand Down Expand Up @@ -49,8 +50,10 @@ void hmac_init(struct hmac_context *ctx, const unsigned char *key,
safe_memset(k_opad, 0, 64);
}

void hmac_final(struct hmac_context *ctx, unsigned char *digest)
void hmac_final(struct hmac_context *_ctx, unsigned char *digest)
{
struct hmac_context_priv *ctx = &_ctx->u.priv;

ctx->hash->result(ctx->ctx, digest);

ctx->hash->loop(ctx->ctxo, digest, ctx->hash->digest_size);
Expand Down
13 changes: 11 additions & 2 deletions src/lib/hmac.h
Expand Up @@ -6,20 +6,29 @@

#define HMAC_MAX_CONTEXT_SIZE 256

struct hmac_context {
struct hmac_context_priv {
char ctx[HMAC_MAX_CONTEXT_SIZE];
char ctxo[HMAC_MAX_CONTEXT_SIZE];
const struct hash_method *hash;
};

struct hmac_context {
union {
struct hmac_context_priv priv;
uint64_t padding_requirement;
} u;
};

void hmac_init(struct hmac_context *ctx, const unsigned char *key,
size_t key_len, const struct hash_method *meth);
void hmac_final(struct hmac_context *ctx, unsigned char *digest);


static inline void
hmac_update(struct hmac_context *ctx, const void *data, size_t size)
hmac_update(struct hmac_context *_ctx, const void *data, size_t size)
{
struct hmac_context_priv *ctx = &_ctx->u.priv;

ctx->hash->loop(ctx->ctx, data, size);
}

Expand Down

0 comments on commit d8361cc

Please sign in to comment.