]> git.ozlabs.org Git - ccan/blobdiff - ccan/crypto/sha256/sha256.c
sha256: Use fewer magic magic numbers
[ccan] / ccan / crypto / sha256 / sha256.c
index aed181e4978f21afd3f3a67379063319436bae50..21d17c40dd9d97562f128c830ba6f46f34fb75a0 100644 (file)
@@ -8,6 +8,7 @@
  */
 #include <ccan/crypto/sha256/sha256.h>
 #include <ccan/endian/endian.h>
+#include <ccan/compiler/compiler.h>
 #include <stdbool.h>
 #include <assert.h>
 #include <string.h>
@@ -17,16 +18,16 @@ static void invalidate_sha256(struct sha256_ctx *ctx)
 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
        ctx->c.md_len = 0;
 #else
-       ctx->bytes = -1ULL;
+       ctx->bytes = (size_t)-1;
 #endif
 }
 
-static void check_sha256(struct sha256_ctx *ctx)
+static void check_sha256(struct sha256_ctx *ctx UNUSED)
 {
 #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
        assert(ctx->c.md_len != 0);
 #else
-       assert(ctx->bytes != -1ULL);
+       assert(ctx->bytes != (size_t)-1);
 #endif
 }
 
@@ -36,15 +37,10 @@ void sha256_init(struct sha256_ctx *ctx)
        SHA256_Init(&ctx->c);
 }
 
-void sha256_update_arr(struct sha256_ctx *ctx, const void *p,
-                      size_t num, size_t size)
+void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size)
 {
-       size_t total = num * size;
-
-       /* Don't overflow. */
-       assert(size == 0 || total / size == num);
        check_sha256(ctx);
-       SHA256_Update(&ctx->c, p, total);
+       SHA256_Update(&ctx->c, p, size);
 }
 
 void sha256_done(struct sha256_ctx *ctx, struct sha256 *res)
@@ -171,7 +167,7 @@ static void Transform(uint32_t *s, const uint32_t *chunk)
        s[7] += h;
 }
 
-static bool alignment_ok(const void *p, size_t n)
+static bool alignment_ok(const void *p UNUSED, size_t n UNUSED)
 {
 #if HAVE_UNALIGNED_ACCESS
        return true;
@@ -221,15 +217,10 @@ void sha256_init(struct sha256_ctx *ctx)
        *ctx = init;
 }
 
-void sha256_update_arr(struct sha256_ctx *ctx, const void *p,
-                      size_t num, size_t size)
+void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size)
 {
-       size_t total = num * size;
-
-       /* Don't overflow. */
-       assert(size == 0 || total / size == num);
        check_sha256(ctx);
-       add(ctx, p, total);
+       add(ctx, p, size);
 }
 
 void sha256_done(struct sha256_ctx *ctx, struct sha256 *res)
@@ -238,9 +229,9 @@ void sha256_done(struct sha256_ctx *ctx, struct sha256 *res)
        uint64_t sizedesc;
        size_t i;
 
-       sizedesc = cpu_to_be64(ctx->bytes << 3);
+       sizedesc = cpu_to_be64((uint64_t)ctx->bytes << 3);
        /* Add '1' bit to terminate, then all 0 bits, up to next block - 8. */
-       add(ctx, pad, 1 + ((119 - (ctx->bytes % 64)) % 64));
+       add(ctx, pad, 1 + ((128 - 8 - (ctx->bytes % 64) - 1) % 64));
        /* Add number of bits of data (big endian) */
        add(ctx, &sizedesc, 8);
        for (i = 0; i < sizeof(ctx->s) / sizeof(ctx->s[0]); i++)
@@ -249,69 +240,69 @@ void sha256_done(struct sha256_ctx *ctx, struct sha256 *res)
 }
 #endif
 
-void sha256_arr(struct sha256 *sha, const void *p, size_t num, size_t size)
+void sha256(struct sha256 *sha, const void *p, size_t size)
 {
        struct sha256_ctx ctx;
 
        sha256_init(&ctx);
-       sha256_update_arr(&ctx, p, num, size);
+       sha256_update(&ctx, p, size);
        sha256_done(&ctx, sha);
 }
        
 void sha256_u8(struct sha256_ctx *ctx, uint8_t v)
 {
-       sha256_update_arr(ctx, &v, sizeof(v), 1);
+       sha256_update(ctx, &v, sizeof(v));
 }
 
 void sha256_u16(struct sha256_ctx *ctx, uint16_t v)
 {
-       sha256_update_arr(ctx, &v, sizeof(v), 1);
+       sha256_update(ctx, &v, sizeof(v));
 }
 
 void sha256_u32(struct sha256_ctx *ctx, uint32_t v)
 {
-       sha256_update_arr(ctx, &v, sizeof(v), 1);
+       sha256_update(ctx, &v, sizeof(v));
 }
 
 void sha256_u64(struct sha256_ctx *ctx, uint64_t v)
 {
-       sha256_update_arr(ctx, &v, sizeof(v), 1);
+       sha256_update(ctx, &v, sizeof(v));
 }
 
 /* Add as little-endian */
 void sha256_le16(struct sha256_ctx *ctx, uint16_t v)
 {
        leint16_t lev = cpu_to_le16(v);
-       sha256_update_arr(ctx, &lev, sizeof(lev), 1);
+       sha256_update(ctx, &lev, sizeof(lev));
 }
        
 void sha256_le32(struct sha256_ctx *ctx, uint32_t v)
 {
        leint32_t lev = cpu_to_le32(v);
-       sha256_update_arr(ctx, &lev, sizeof(lev), 1);
+       sha256_update(ctx, &lev, sizeof(lev));
 }
        
 void sha256_le64(struct sha256_ctx *ctx, uint64_t v)
 {
        leint64_t lev = cpu_to_le64(v);
-       sha256_update_arr(ctx, &lev, sizeof(lev), 1);
+       sha256_update(ctx, &lev, sizeof(lev));
 }
 
 /* Add as big-endian */
 void sha256_be16(struct sha256_ctx *ctx, uint16_t v)
 {
        beint16_t bev = cpu_to_be16(v);
-       sha256_update_arr(ctx, &bev, sizeof(bev), 1);
+       sha256_update(ctx, &bev, sizeof(bev));
 }
        
 void sha256_be32(struct sha256_ctx *ctx, uint32_t v)
 {
        beint32_t bev = cpu_to_be32(v);
-       sha256_update_arr(ctx, &bev, sizeof(bev), 1);
+       sha256_update(ctx, &bev, sizeof(bev));
 }
        
 void sha256_be64(struct sha256_ctx *ctx, uint64_t v)
 {
        beint64_t bev = cpu_to_be64(v);
-       sha256_update_arr(ctx, &bev, sizeof(bev), 1);
+       sha256_update(ctx, &bev, sizeof(bev));
 }