]> git.ozlabs.org Git - ccan/commitdiff
Merge remote-tracking branch 'origin/pr/47'
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 23 Aug 2016 03:39:44 +0000 (13:09 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 23 Aug 2016 03:39:44 +0000 (13:09 +0930)
Closes: 47
1  2 
ccan/crypto/sha256/sha256.c
ccan/crypto/sha256/sha256.h

index 95c700692ce50a0f2a57163a41706f9bb8980c79,21d17c40dd9d97562f128c830ba6f46f34fb75a0..d42b47b0a587c3df97b0e67f785800715cc12da8
@@@ -8,6 -8,7 +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 +18,16 @@@ static void invalidate_sha256(struct sh
  #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
  }
  
@@@ -166,7 -167,7 +167,7 @@@ static void Transform(uint32_t *s, cons
        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;
@@@ -181,7 -182,7 +182,7 @@@ static void add(struct sha256_ctx *ctx
        size_t bufsize = ctx->bytes % 64;
  
        if (bufsize + len >= 64) {
 -              // Fill the buffer, and process it.
 +              /* Fill the buffer, and process it. */
                memcpy(ctx->buf.u8 + bufsize, data, 64 - bufsize);
                ctx->bytes += 64 - bufsize;
                data += 64 - bufsize;
        }
  
        while (len >= 64) {
 -              // Process full chunks directly from the source.
 +              /* Process full chunks directly from the source. */
                if (alignment_ok(data, sizeof(uint32_t)))
                        Transform(ctx->s, (const uint32_t *)data);
                else {
        }
            
        if (len) {
 -              // Fill the buffer with what remains.
 +              /* Fill the buffer with what remains. */
                memcpy(ctx->buf.u8 + bufsize, data, len);
                ctx->bytes += len;
        }
@@@ -228,9 -229,9 +229,9 @@@ void sha256_done(struct sha256_ctx *ctx
        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++)
index d8b683ecee3747a2081592a8e0b624e3c2b8d98e,fc5af21b8b9628065e6318e4ae09e32abccc53b7..9a310b9564c6be7bfcaca90a718d280788f303c7
@@@ -6,7 -6,7 +6,7 @@@
  #include <stdlib.h>
  
  /* Uncomment this to use openssl's SHA256 routines (and link with -lcrypto) */
 -//#define CCAN_CRYPTO_SHA256_USE_OPENSSL 1
 +/*#define CCAN_CRYPTO_SHA256_USE_OPENSSL 1*/
  
  #ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
  #include <openssl/sha.h>
   */
  struct sha256 {
        union {
-               /* Array of chars */
-               unsigned char u8[32];
-               /* Array of uint32_t */
                uint32_t u32[8];
+               unsigned char u8[32];
        } u;
  };
  
@@@ -47,11 -45,11 +45,11 @@@ struct sha256_ctx 
        SHA256_CTX c;
  #else
        uint32_t s[8];
-       uint64_t bytes;
        union {
-               uint32_t u32[8];
+               uint32_t u32[16];
                unsigned char u8[64];
        } buf;
+       size_t bytes;
  #endif
  };
  
@@@ -82,7 -80,7 +80,7 @@@ void sha256_init(struct sha256_ctx *ctx
  /**
   * SHA256_INIT - initializer for an SHA256 context.
   *
 - * This can be used to staticly initialize an SHA256 context (instead
 + * This can be used to statically initialize an SHA256 context (instead
   * of sha256_init()).
   *
   * Example:
  #else
  #define SHA256_INIT                                                   \
        { { 0x6a09e667ul, 0xbb67ae85ul, 0x3c6ef372ul, 0xa54ff53aul,     \
-           0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul }, 0 }
+           0x510e527ful, 0x9b05688cul, 0x1f83d9abul, 0x5be0cd19ul },   \
+         { { 0 } }, 0 }
  #endif
  
  /**