]> git.ozlabs.org Git - ccan/commitdiff
crypto/sha256: simplify interface.
authorRusty Russell <rusty@rustcorp.com.au>
Wed, 27 May 2015 04:19:20 +0000 (13:49 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Wed, 27 May 2015 04:19:20 +0000 (13:49 +0930)
I misused it by accident: the implied sizeof() is too tricky.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/crypto/sha256/sha256.c
ccan/crypto/sha256/sha256.h
ccan/crypto/shachain/shachain.c

index aed181e4978f21afd3f3a67379063319436bae50..4b1f8cdf303af328320691cc4ee8b8403c65fb5a 100644 (file)
@@ -36,15 +36,10 @@ void sha256_init(struct sha256_ctx *ctx)
        SHA256_Init(&ctx->c);
 }
 
        SHA256_Init(&ctx->c);
 }
 
-void sha256_update_arr(struct sha256_ctx *ctx, const void *p,
-                      size_t num, size_t size)
+void sha256_update_bytes(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);
        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)
 }
 
 void sha256_done(struct sha256_ctx *ctx, struct sha256 *res)
@@ -221,15 +216,10 @@ void sha256_init(struct sha256_ctx *ctx)
        *ctx = init;
 }
 
        *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);
        check_sha256(ctx);
-       add(ctx, p, total);
+       add(ctx, p, size);
 }
 
 void sha256_done(struct sha256_ctx *ctx, struct sha256 *res)
 }
 
 void sha256_done(struct sha256_ctx *ctx, struct sha256 *res)
@@ -249,69 +239,69 @@ void sha256_done(struct sha256_ctx *ctx, struct sha256 *res)
 }
 #endif
 
 }
 #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);
 {
        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_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)
 {
 }
 
 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)
 {
 }
 
 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)
 {
 }
 
 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);
 }
 
 /* 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);
 }
        
 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);
 }
        
 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);
 }
 
 /* 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);
 }
        
 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);
 }
        
 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));
 }
 }
index 812091a3283069282beaaee03e6098053523fec6..eedaa94ee77df0cfe3e875ec45597f364bbea55f 100644 (file)
@@ -29,15 +29,15 @@ struct sha256 {
 };
 
 /**
 };
 
 /**
- * sha256 - return sha256 of an array of bytes.
+ * sha256 - return sha256 of an object.
  * @sha256: the sha256 to fill in
  * @sha256: the sha256 to fill in
- * @p: array or pointer to first element
- * @num: the number of elements to hash
+ * @p: pointer to memory,
+ * @size: the number of bytes pointed to by @p
  *
  *
- * The bytes pointed to by @p is SHA256 hashes into @sha256.  This is
+ * The bytes pointed to by @p is SHA256 hashed into @sha256.  This is
  * equivalent to sha256_init(), sha256_update() then sha256_done().
  */
  * equivalent to sha256_init(), sha256_update() then sha256_done().
  */
-#define sha256(sha256, p, num) sha256_arr((sha256), (p), (num), sizeof(*(p)))
+void sha256(struct sha256 *sha, const void *p, size_t size);
 
 /**
  * struct sha256_ctx - structure to store running context for sha256
 
 /**
  * struct sha256_ctx - structure to store running context for sha256
@@ -110,16 +110,15 @@ void sha256_init(struct sha256_ctx *ctx);
 #endif
 
 /**
 #endif
 
 /**
- * sha256_update - include an array of data in the hash.
+ * sha256_update - include some memory in the hash.
  * @ctx: the sha256_ctx to use
  * @ctx: the sha256_ctx to use
- * @p: array or pointer to first element
- * @num: the number of elements to hash
+ * @p: pointer to memory,
+ * @size: the number of bytes pointed to by @p
  *
  * You can call this multiple times to hash more data, before calling
  * sha256_done().
  */
  *
  * You can call this multiple times to hash more data, before calling
  * sha256_done().
  */
-#define sha256_update(ctx, p, num) \
-       sha256_update_arr((ctx), (p), (num), sizeof(*(p)))
+void sha256_update(struct sha256_ctx *ctx, const void *p, size_t size);
 
 /**
  * sha256_done - finish SHA256 and return the hash
 
 /**
  * sha256_done - finish SHA256 and return the hash
@@ -146,8 +145,4 @@ void sha256_le64(struct sha256_ctx *ctx, uint64_t v);
 void sha256_be16(struct sha256_ctx *ctx, uint16_t v);
 void sha256_be32(struct sha256_ctx *ctx, uint32_t v);
 void sha256_be64(struct sha256_ctx *ctx, uint64_t v);
 void sha256_be16(struct sha256_ctx *ctx, uint16_t v);
 void sha256_be32(struct sha256_ctx *ctx, uint32_t v);
 void sha256_be64(struct sha256_ctx *ctx, uint64_t v);
-
-void sha256_update_arr(struct sha256_ctx *ctx, const void *p,
-                      size_t num, size_t size);
-void sha256_arr(struct sha256 *sha, const void *p, size_t num, size_t size);
 #endif /* CCAN_CRYPTO_SHA256_H */
 #endif /* CCAN_CRYPTO_SHA256_H */
index 94d7d6466c0a05b4fdac6c57b9bcbc6f3b9b0f13..8c9fdcf50fe2de50f72ce64524e2e9c216f1e827 100644 (file)
@@ -33,7 +33,7 @@ static void derive(shachain_index_t from, shachain_index_t to,
        for (i = ilog64(branches) - 1; i >= 0; i--) {
                if (((branches >> i) & 1)) {
                        change_bit(hash->u.u8, i);
        for (i = ilog64(branches) - 1; i >= 0; i--) {
                if (((branches >> i) & 1)) {
                        change_bit(hash->u.u8, i);
-                       sha256(hash, hash, 1);
+                       sha256(hash, hash, sizeof(*hash));
                }
        }
 }
                }
        }
 }