From 735f7f5b0acfeb2c69bbca1f588baadc0aa9116a Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Sun, 28 Oct 2012 23:48:18 +0200 Subject: [PATCH] crypto: reorder arguments in _final() The old one was final(dst, ctx) which is some sort of historical accident. Lets get rid of it. Then all operations on hash context take context as first argument. --- test/test_crypto.c | 12 ++++++------ usual/crypto/hmac.c | 8 ++++---- usual/crypto/hmac.h | 2 +- usual/crypto/md5.c | 2 +- usual/crypto/md5.h | 9 +-------- usual/crypto/sha1.c | 2 +- usual/crypto/sha1.h | 9 +-------- 7 files changed, 15 insertions(+), 29 deletions(-) diff --git a/test/test_crypto.c b/test/test_crypto.c index 3b0cdd1..a3517d2 100644 --- a/test/test_crypto.c +++ b/test/test_crypto.c @@ -36,7 +36,7 @@ static const char *run_md5(const char *str) md5_reset(ctx); md5_update(ctx, str, len); - md5_final(res, ctx); + md5_final(ctx, res); md5_reset(ctx); step = 3; @@ -44,7 +44,7 @@ static const char *run_md5(const char *str) md5_update(ctx, str+i, (i + step <= len) ? (step) : (len - i)); - md5_final(res2, ctx); + md5_final(ctx, res2); if (memcmp(res, res2, MD5_DIGEST_LENGTH) != 0) return "FAIL"; @@ -77,7 +77,7 @@ static const char *run_sha1(const char *str) sha1_reset(ctx); sha1_update(ctx, str, len); - sha1_final(res, ctx); + sha1_final(ctx, res); sha1_reset(ctx); step = 3; @@ -85,7 +85,7 @@ static const char *run_sha1(const char *str) sha1_update(ctx, str+i, (i + step <= len) ? (step) : (len - i)); - sha1_final(res2, ctx); + sha1_final(ctx, res2); if (memcmp(res, res2, SHA1_DIGEST_LENGTH) != 0) return "FAIL"; @@ -120,7 +120,7 @@ static const char *run_hmac_sha1(const char *key, const char *str) /* Compute HMAC all at once */ hmac_sha1_reset(ctx, (void *) key, strlen(key)); hmac_sha1_update(ctx, str, len); - hmac_sha1_final(monolithic_res, ctx); + hmac_sha1_final(ctx, monolithic_res); /* Compute HMAC incrementally */ hmac_sha1_reset(ctx, (void *) key, strlen(key)); @@ -129,7 +129,7 @@ static const char *run_hmac_sha1(const char *key, const char *str) hmac_sha1_update(ctx, str+i, (i + step <= len) ? (step) : (len - i)); - hmac_sha1_final(incremental_res, ctx); + hmac_sha1_final(ctx, incremental_res); if (memcmp(monolithic_res, incremental_res, SHA1_DIGEST_LENGTH) != 0) return "FAIL"; diff --git a/usual/crypto/hmac.c b/usual/crypto/hmac.c index c2990e3..adad90b 100644 --- a/usual/crypto/hmac.c +++ b/usual/crypto/hmac.c @@ -32,7 +32,7 @@ hmac_sha1_reset(struct hmac_sha1_ctx *ctx, if (key_len > SHA1_BLOCK_SIZE) { sha1_reset(&ctx->ctx); sha1_update(&ctx->ctx, key, key_len); - sha1_final(ctx->key, &ctx->ctx); + sha1_final(&ctx->ctx, ctx->key); ctx->key_len = SHA1_DIGEST_LENGTH; } else { memcpy(ctx->key, key, key_len); @@ -66,12 +66,12 @@ hmac_sha1_update(struct hmac_sha1_ctx *ctx, /* Get final HMAC-SHA1 result */ -void hmac_sha1_final(uint8_t *dst, struct hmac_sha1_ctx *ctx) +void hmac_sha1_final(struct hmac_sha1_ctx *ctx, uint8_t *dst) { uint8_t k_opad[SHA1_BLOCK_SIZE]; int i; - sha1_final(dst, &ctx->ctx); + sha1_final(&ctx->ctx, dst); memset(k_opad, 0, sizeof k_opad); memcpy(k_opad, ctx->key, ctx->key_len); @@ -81,7 +81,7 @@ void hmac_sha1_final(uint8_t *dst, struct hmac_sha1_ctx *ctx) sha1_reset(&ctx->ctx); sha1_update(&ctx->ctx, k_opad, SHA1_BLOCK_SIZE); sha1_update(&ctx->ctx, dst, SHA1_DIGEST_LENGTH); - sha1_final(dst, &ctx->ctx); + sha1_final(&ctx->ctx, dst); /* * Seen in OpenBSD source, presumably to prevent key leakage through diff --git a/usual/crypto/hmac.h b/usual/crypto/hmac.h index 0c7296f..000aead 100644 --- a/usual/crypto/hmac.h +++ b/usual/crypto/hmac.h @@ -42,6 +42,6 @@ void hmac_sha1_reset(struct hmac_sha1_ctx *ctx, const uint8_t *key, unsigned int void hmac_sha1_update(struct hmac_sha1_ctx *ctx, const void *data, unsigned int len); /** Get final result */ -void hmac_sha1_final(uint8_t *dst, struct hmac_sha1_ctx *ctx); +void hmac_sha1_final(struct hmac_sha1_ctx *ctx, uint8_t *dst); #endif /* _USUAL_HMAC_H_ */ diff --git a/usual/crypto/md5.c b/usual/crypto/md5.c index 5621112..4a1b60b 100644 --- a/usual/crypto/md5.c +++ b/usual/crypto/md5.c @@ -168,7 +168,7 @@ void md5_update(struct md5_ctx *ctx, const void *data, unsigned int len) } } -void md5_final(uint8_t *dst, struct md5_ctx *ctx) +void md5_final(struct md5_ctx *ctx, uint8_t *dst) { static const uint8_t padding[MD5_BLOCK_LENGTH] = { 0x80 }; uint64_t final_len = ctx->nbytes * 8; diff --git a/usual/crypto/md5.h b/usual/crypto/md5.h index dde433d..098eefd 100644 --- a/usual/crypto/md5.h +++ b/usual/crypto/md5.h @@ -47,14 +47,7 @@ void md5_reset(struct md5_ctx *ctx); void md5_update(struct md5_ctx *ctx, const void *data, unsigned int len); /** Get final result */ -void md5_final(uint8_t *dst, struct md5_ctx *ctx); - -#ifdef MD5_COMPAT -typedef struct md5_ctx MD5_CTX; -#define MD5_Init(c) md5_reset(c) -#define MD5_Update(c, d, l) md5_update(c, d, l) -#define MD5_Final(d, c) md5_final(d, c) -#endif +void md5_final(struct md5_ctx *ctx, uint8_t *dst); #endif diff --git a/usual/crypto/sha1.c b/usual/crypto/sha1.c index 50df215..b0a6086 100644 --- a/usual/crypto/sha1.c +++ b/usual/crypto/sha1.c @@ -118,7 +118,7 @@ void sha1_update(struct sha1_ctx *ctx, const void *data, unsigned int len) } } -void sha1_final(uint8_t *dst, struct sha1_ctx *ctx) +void sha1_final(struct sha1_ctx *ctx, uint8_t *dst) { static const uint8_t padding[SHA1_BLOCK_SIZE] = { 0x80 }; uint64_t nbits = ctx->nbytes * 8; diff --git a/usual/crypto/sha1.h b/usual/crypto/sha1.h index 76b6f9d..6ef3705 100644 --- a/usual/crypto/sha1.h +++ b/usual/crypto/sha1.h @@ -48,14 +48,7 @@ void sha1_reset(struct sha1_ctx *ctx); void sha1_update(struct sha1_ctx *ctx, const void *data, unsigned int len); /** Get final result */ -void sha1_final(uint8_t *dst, struct sha1_ctx *ctx); - -#ifndef AVOID_SHA1_COMPAT -typedef struct sha1_ctx SHA1_CTX; -#define SHA1Init(ctx) sha1_reset(ctx) -#define SHA1Update(ctx, data, len) sha1_update(ctx, data, len) -#define SHA1Final(dst, ctx) sha1_final(dst, ctx) -#endif +void sha1_final(struct sha1_ctx *ctx, uint8_t *dst); #endif -- 2.39.5