Commit 78e1b32e authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Shut FlexeLint up about SHA256 implementation.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@3449 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 85dc2aa0
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
typedef struct SHA256Context { typedef struct SHA256Context {
uint32_t state[8]; uint32_t state[8];
uint32_t count[2]; uint64_t count;
unsigned char buf[64]; unsigned char buf[64];
} SHA256_CTX; } SHA256_CTX;
......
...@@ -33,18 +33,19 @@ ...@@ -33,18 +33,19 @@
#ifdef HAVE_ENDIAN_H #ifdef HAVE_ENDIAN_H
#include <endian.h> #include <endian.h>
#define BYTE_ORDER __BYTE_ORDER #define VBYTE_ORDER __BYTE_ORDER
#define BIG_ENDIAN __BIG_ENDIAN #define VBIG_ENDIAN __BIG_ENDIAN
#endif #endif
#ifdef HAVE_SYS_ENDIAN_H #ifdef HAVE_SYS_ENDIAN_H
#include <sys/endian.h> #include <sys/endian.h>
#define BYTE_ORDER _BYTE_ORDER #define VBYTE_ORDER _BYTE_ORDER
#define BIG_ENDIAN _BIG_ENDIAN #define VBIG_ENDIAN _BIG_ENDIAN
#endif #endif
#include "libvarnish.h"
#include "vsha256.h" #include "vsha256.h"
#if defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN #if defined(VBYTE_ORDER) && VBYTE_ORDER == VBIG_ENDIAN
/* Copy a vector of big-endian uint32_t into a vector of bytes */ /* Copy a vector of big-endian uint32_t into a vector of bytes */
#define be32enc_vect(dst, src, len) \ #define be32enc_vect(dst, src, len) \
...@@ -61,7 +62,7 @@ mybe32dec(const void *pp) ...@@ -61,7 +62,7 @@ mybe32dec(const void *pp)
{ {
unsigned char const *p = (unsigned char const *)pp; unsigned char const *p = (unsigned char const *)pp;
return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
} }
static __inline void static __inline void
...@@ -75,6 +76,15 @@ mybe32enc(void *pp, uint32_t u) ...@@ -75,6 +76,15 @@ mybe32enc(void *pp, uint32_t u)
p[3] = u & 0xff; p[3] = u & 0xff;
} }
static __inline void
mybe64enc(void *pp, uint64_t u)
{
unsigned char *p = (unsigned char *)pp;
be32enc(p, u >> 32);
be32enc(p + 4, u & 0xffffffff);
}
/* /*
* Encode a length len/4 vector of (uint32_t) into a length len vector of * Encode a length len/4 vector of (uint32_t) into a length len vector of
* (unsigned char) in big-endian form. Assumes len is a multiple of 4. * (unsigned char) in big-endian form. Assumes len is a multiple of 4.
...@@ -126,7 +136,7 @@ be32dec_vect(uint32_t *dst, const unsigned char *src, size_t len) ...@@ -126,7 +136,7 @@ be32dec_vect(uint32_t *dst, const unsigned char *src, size_t len)
S[(66 - i) % 8], S[(67 - i) % 8], \ S[(66 - i) % 8], S[(67 - i) % 8], \
S[(68 - i) % 8], S[(69 - i) % 8], \ S[(68 - i) % 8], S[(69 - i) % 8], \
S[(70 - i) % 8], S[(71 - i) % 8], \ S[(70 - i) % 8], S[(71 - i) % 8], \
W[i] + k) (W[i] + k))
/* /*
* SHA256 block compression function. The 256-bit state is transformed via * SHA256 block compression function. The 256-bit state is transformed via
...@@ -219,7 +229,7 @@ SHA256_Transform(uint32_t * state, const unsigned char block[64]) ...@@ -219,7 +229,7 @@ SHA256_Transform(uint32_t * state, const unsigned char block[64])
state[i] += S[i]; state[i] += S[i];
} }
static unsigned char PAD[64] = { static const unsigned char PAD[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
...@@ -233,15 +243,21 @@ SHA256_Pad(SHA256_CTX * ctx) ...@@ -233,15 +243,21 @@ SHA256_Pad(SHA256_CTX * ctx)
unsigned char len[8]; unsigned char len[8];
uint32_t r, plen; uint32_t r, plen;
/*
* Rescale from bytes to bits
*/
ctx->count <<= 3;
/* /*
* Convert length to a vector of bytes -- we do this now rather * Convert length to a vector of bytes -- we do this now rather
* than later because the length will change after we pad. * than later because the length will change after we pad.
*/ */
be32enc_vect(len, ctx->count, 8); mybe64enc(len, ctx->count);
/* Add 1--64 bytes so that the resulting length is 56 mod 64 */ /* Add 1--64 bytes so that the resulting length is 56 mod 64 */
r = (ctx->count[1] >> 3) & 0x3f; r = ctx->count & 0x3f;
plen = (r < 56) ? (56 - r) : (120 - r); plen = (r < 56) ? (56 - r) : (120 - r);
assert(plen <= sizeof PAD);
SHA256_Update(ctx, PAD, (size_t)plen); SHA256_Update(ctx, PAD, (size_t)plen);
/* Add the terminating bit-count */ /* Add the terminating bit-count */
...@@ -254,7 +270,7 @@ SHA256_Init(SHA256_CTX * ctx) ...@@ -254,7 +270,7 @@ SHA256_Init(SHA256_CTX * ctx)
{ {
/* Zero bits processed so far */ /* Zero bits processed so far */
ctx->count[0] = ctx->count[1] = 0; ctx->count = 0;
/* Magic initialization constants */ /* Magic initialization constants */
ctx->state[0] = 0x6A09E667; ctx->state[0] = 0x6A09E667;
...@@ -271,43 +287,23 @@ SHA256_Init(SHA256_CTX * ctx) ...@@ -271,43 +287,23 @@ SHA256_Init(SHA256_CTX * ctx)
void void
SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len) SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len)
{ {
uint32_t bitlen[2]; uint32_t r, l;
uint32_t r;
const unsigned char *src = in; const unsigned char *src = in;
/* Number of bytes left in the buffer from previous updates */ /* Number of bytes left in the buffer from previous updates */
r = (ctx->count[1] >> 3) & 0x3f; r = ctx->count & 0x3f;
while (len > 0) {
/* Convert the length into a number of bits */ l = 64 - r;
bitlen[1] = ((uint32_t)len) << 3; if (l > len)
bitlen[0] = (uint32_t)(len >> 29); l = len;
memcpy(&ctx->buf[r], src, l);
/* Update number of bits */ len -= l;
if ((ctx->count[1] += bitlen[1]) < bitlen[1]) src += l;
ctx->count[0]++; ctx->count += l;
ctx->count[0] += bitlen[0]; r = ctx->count & 0x3f;
if (r == 0)
/* Handle the case where we don't need to perform any transforms */ SHA256_Transform(ctx->state, ctx->buf);
if (len < 64 - r) {
memcpy(&ctx->buf[r], src, len);
return;
} }
/* Finish the current block */
memcpy(&ctx->buf[r], src, 64 - r);
SHA256_Transform(ctx->state, ctx->buf);
src += 64 - r;
len -= 64 - r;
/* Perform complete blocks */
while (len >= 64) {
SHA256_Transform(ctx->state, src);
src += 64;
len -= 64;
}
/* Copy left over data into buffer */
memcpy(ctx->buf, src, len);
} }
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment