[master] 6299828 Add a V-prefix to the SHA256 functions
Dag Haavi Finstad
daghf at varnish-software.com
Tue Oct 31 10:34:05 UTC 2017
commit 6299828f879379d0cf10ab833b24a22f6517f8cd
Author: Dag Haavi Finstad <daghf at varnish-software.com>
Date: Tue Oct 31 11:31:09 2017 +0100
Add a V-prefix to the SHA256 functions
diff --git a/bin/varnishd/cache/cache_hash.c b/bin/varnishd/cache/cache_hash.c
index 004b86d..2aac129 100644
--- a/bin/varnishd/cache/cache_hash.c
+++ b/bin/varnishd/cache/cache_hash.c
@@ -177,10 +177,10 @@ HSH_AddString(struct req *req, void *ctx, const char *str)
CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
AN(ctx);
if (str != NULL) {
- SHA256_Update(ctx, str, strlen(str));
+ VSHA256_Update(ctx, str, strlen(str));
VSLb(req->vsl, SLT_Hash, "%s", str);
} else
- SHA256_Update(ctx, &str, 1);
+ VSHA256_Update(ctx, &str, 1);
}
/*---------------------------------------------------------------------
@@ -191,8 +191,8 @@ HSH_AddString(struct req *req, void *ctx, const char *str)
*/
static struct hsh_magiclist {
- unsigned char was[SHA256_LEN];
- unsigned char now[SHA256_LEN];
+ unsigned char was[VSHA256_LEN];
+ unsigned char now[VSHA256_LEN];
} hsh_magiclist[] = {
{ .now = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -241,19 +241,19 @@ hsh_testmagic(void *result)
static int nused = 0;
for (i = 0; i < nused; i++)
- if (!memcmp(hsh_magiclist[i].was, result, SHA256_LEN))
+ if (!memcmp(hsh_magiclist[i].was, result, VSHA256_LEN))
break;
if (i == nused && i < HSH_NMAGIC)
- memcpy(hsh_magiclist[nused++].was, result, SHA256_LEN);
+ memcpy(hsh_magiclist[nused++].was, result, VSHA256_LEN);
if (i == nused)
return;
assert(i < HSH_NMAGIC);
fprintf(stderr, "HASHMAGIC: <");
- for (j = 0; j < SHA256_LEN; j++)
+ for (j = 0; j < VSHA256_LEN; j++)
fprintf(stderr, "%02x", ((unsigned char*)result)[j]);
fprintf(stderr, "> -> <");
- memcpy(result, hsh_magiclist[i].now, SHA256_LEN);
- for (j = 0; j < SHA256_LEN; j++)
+ memcpy(result, hsh_magiclist[i].now, VSHA256_LEN);
+ for (j = 0; j < VSHA256_LEN; j++)
fprintf(stderr, "%02x", ((unsigned char*)result)[j]);
fprintf(stderr, ">\n");
}
@@ -978,7 +978,7 @@ void
HSH_Init(const struct hash_slinger *slinger)
{
- assert(DIGEST_LEN == SHA256_LEN); /* avoid #include pollution */
+ assert(DIGEST_LEN == VSHA256_LEN); /* avoid #include pollution */
hash = slinger;
if (hash->start != NULL)
hash->start();
diff --git a/bin/varnishd/cache/cache_req_fsm.c b/bin/varnishd/cache/cache_req_fsm.c
index ec0d715..f0f56cc 100644
--- a/bin/varnishd/cache/cache_req_fsm.c
+++ b/bin/varnishd/cache/cache_req_fsm.c
@@ -750,7 +750,7 @@ static enum req_fsm_nxt
cnt_recv(struct worker *wrk, struct req *req)
{
unsigned recv_handling;
- struct SHA256Context sha256ctx;
+ struct VSHA256Context sha256ctx;
const char *xff;
const char *ci, *cp;
@@ -841,13 +841,13 @@ cnt_recv(struct worker *wrk, struct req *req)
}
}
- SHA256_Init(&sha256ctx);
+ VSHA256_Init(&sha256ctx);
VCL_hash_method(req->vcl, wrk, req, NULL, &sha256ctx);
if (wrk->handling == VCL_RET_FAIL)
recv_handling = wrk->handling;
else
assert(wrk->handling == VCL_RET_LOOKUP);
- SHA256_Final(req->digest, &sha256ctx);
+ VSHA256_Final(req->digest, &sha256ctx);
switch (recv_handling) {
case VCL_RET_VCL:
diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index b90e76d..39f1059 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -264,8 +264,8 @@ mgt_tests(void)
assert(VTIM_parse("Sunday, 06-Nov-94 08:49:37 GMT") == 784111777);
assert(VTIM_parse("Sun Nov 6 08:49:37 1994") == 784111777);
- /* Check that our SHA256 works */
- SHA256_Test();
+ /* Check that our VSHA256 works */
+ VSHA256_Test();
}
static void
diff --git a/bin/varnishd/storage/storage_persistent.h b/bin/varnishd/storage/storage_persistent.h
index 0383567..7fe612f 100644
--- a/bin/varnishd/storage/storage_persistent.h
+++ b/bin/varnishd/storage/storage_persistent.h
@@ -122,7 +122,7 @@ struct smp_sign {
uint64_t length; /* NB: Must be last */
};
-#define SMP_SIGN_SPACE (sizeof(struct smp_sign) + SHA256_LEN)
+#define SMP_SIGN_SPACE (sizeof(struct smp_sign) + VSHA256_LEN)
/*
* A segment pointer.
@@ -168,7 +168,7 @@ struct smp_object {
struct smp_signctx {
struct smp_sign *ss;
- struct SHA256Context ctx;
+ struct VSHA256Context ctx;
uint32_t unique;
const char *id;
};
diff --git a/bin/varnishd/storage/storage_persistent_subr.c b/bin/varnishd/storage/storage_persistent_subr.c
index 2e15c3f..05ce659 100644
--- a/bin/varnishd/storage/storage_persistent_subr.c
+++ b/bin/varnishd/storage/storage_persistent_subr.c
@@ -84,8 +84,8 @@ smp_def_sign(const struct smp_sc *sc, struct smp_signctx *ctx,
int
smp_chk_sign(struct smp_signctx *ctx)
{
- struct SHA256Context cx;
- unsigned char sign[SHA256_LEN];
+ struct VSHA256Context cx;
+ unsigned char sign[VSHA256_LEN];
int r = 0;
if (strncmp(ctx->id, ctx->ss->ident, sizeof ctx->ss->ident))
@@ -95,13 +95,13 @@ smp_chk_sign(struct smp_signctx *ctx)
else if ((uintptr_t)ctx->ss != ctx->ss->mapped)
r = 3;
else {
- SHA256_Init(&ctx->ctx);
- SHA256_Update(&ctx->ctx, ctx->ss,
+ VSHA256_Init(&ctx->ctx);
+ VSHA256_Update(&ctx->ctx, ctx->ss,
offsetof(struct smp_sign, length));
- SHA256_Update(&ctx->ctx, SIGN_DATA(ctx), ctx->ss->length);
+ VSHA256_Update(&ctx->ctx, SIGN_DATA(ctx), ctx->ss->length);
cx = ctx->ctx;
- SHA256_Update(&cx, &ctx->ss->length, sizeof(ctx->ss->length));
- SHA256_Final(sign, &cx);
+ VSHA256_Update(&cx, &ctx->ss->length, sizeof(ctx->ss->length));
+ VSHA256_Final(sign, &cx);
if (memcmp(sign, SIGN_END(ctx), sizeof sign))
r = 4;
}
@@ -119,16 +119,16 @@ smp_chk_sign(struct smp_signctx *ctx)
static void
smp_append_sign(struct smp_signctx *ctx, const void *ptr, uint32_t len)
{
- struct SHA256Context cx;
- unsigned char sign[SHA256_LEN];
+ struct VSHA256Context cx;
+ unsigned char sign[VSHA256_LEN];
if (len != 0) {
- SHA256_Update(&ctx->ctx, ptr, len);
+ VSHA256_Update(&ctx->ctx, ptr, len);
ctx->ss->length += len;
}
cx = ctx->ctx;
- SHA256_Update(&cx, &ctx->ss->length, sizeof(ctx->ss->length));
- SHA256_Final(sign, &cx);
+ VSHA256_Update(&cx, &ctx->ss->length, sizeof(ctx->ss->length));
+ VSHA256_Final(sign, &cx);
memcpy(SIGN_END(ctx), sign, sizeof sign);
}
@@ -145,8 +145,8 @@ smp_reset_sign(struct smp_signctx *ctx)
strcpy(ctx->ss->ident, ctx->id);
ctx->ss->unique = ctx->unique;
ctx->ss->mapped = (uintptr_t)ctx->ss;
- SHA256_Init(&ctx->ctx);
- SHA256_Update(&ctx->ctx, ctx->ss,
+ VSHA256_Init(&ctx->ctx);
+ VSHA256_Update(&ctx->ctx, ctx->ss,
offsetof(struct smp_sign, length));
smp_append_sign(ctx, NULL, 0);
}
@@ -358,7 +358,7 @@ smp_valid_silo(struct smp_sc *sc)
/* XXX: Sanity check stuff[6] */
- assert(si->stuff[SMP_BAN1_STUFF] > sizeof *si + SHA256_LEN);
+ assert(si->stuff[SMP_BAN1_STUFF] > sizeof *si + VSHA256_LEN);
assert(si->stuff[SMP_BAN2_STUFF] > si->stuff[SMP_BAN1_STUFF]);
assert(si->stuff[SMP_SEG1_STUFF] > si->stuff[SMP_BAN2_STUFF]);
assert(si->stuff[SMP_SEG2_STUFF] > si->stuff[SMP_SEG1_STUFF]);
diff --git a/include/vrt.h b/include/vrt.h
index 5b79a80..391a9e3 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -166,7 +166,7 @@ struct vrt_ctx {
/*
* method specific argument:
- * hash: struct SHA256Context
+ * hash: struct VSHA256Context
* synth+error: struct vsb *
*/
void *specific;
diff --git a/include/vsha256.h b/include/vsha256.h
index e3852e9..9e98356 100644
--- a/include/vsha256.h
+++ b/include/vsha256.h
@@ -26,21 +26,21 @@
* $FreeBSD: head/lib/libmd/sha256.h 154479 2006-01-17 15:35:57Z phk $
*/
-#ifndef _SHA256_H_
-#define _SHA256_H_
+#ifndef _VSHA256_H_
+#define _VSHA256_H_
-#define SHA256_LEN 32
-#define SHA256_DIGEST_LENGTH 32
+#define VSHA256_LEN 32
+#define VSHA256_DIGEST_LENGTH 32
-typedef struct SHA256Context {
+typedef struct VSHA256Context {
uint32_t state[8];
uint64_t count;
unsigned char buf[64];
-} SHA256_CTX;
+} VSHA256_CTX;
-void SHA256_Init(SHA256_CTX *);
-void SHA256_Update(SHA256_CTX *, const void *, size_t);
-void SHA256_Final(unsigned char [SHA256_LEN], SHA256_CTX *);
-void SHA256_Test(void);
+void VSHA256_Init(VSHA256_CTX *);
+void VSHA256_Update(VSHA256_CTX *, const void *, size_t);
+void VSHA256_Final(unsigned char [VSHA256_LEN], VSHA256_CTX *);
+void VSHA256_Test(void);
-#endif /* !_SHA256_H_ */
+#endif /* !_VSHA256_H_ */
diff --git a/lib/libvarnish/vcli_proto.c b/lib/libvarnish/vcli_proto.c
index 8027fa6..3f0b165 100644
--- a/lib/libvarnish/vcli_proto.c
+++ b/lib/libvarnish/vcli_proto.c
@@ -47,24 +47,24 @@ void
VCLI_AuthResponse(int S_fd, const char *challenge,
char response[CLI_AUTH_RESPONSE_LEN + 1])
{
- SHA256_CTX ctx;
- uint8_t buf[SHA256_LEN];
+ VSHA256_CTX ctx;
+ uint8_t buf[VSHA256_LEN];
int i;
- assert(CLI_AUTH_RESPONSE_LEN == (SHA256_LEN * 2));
+ assert(CLI_AUTH_RESPONSE_LEN == (VSHA256_LEN * 2));
- SHA256_Init(&ctx);
- SHA256_Update(&ctx, challenge, 32);
- SHA256_Update(&ctx, "\n", 1);
+ VSHA256_Init(&ctx);
+ VSHA256_Update(&ctx, challenge, 32);
+ VSHA256_Update(&ctx, "\n", 1);
do {
i = read(S_fd, buf, 1);
if (i == 1)
- SHA256_Update(&ctx, buf, i);
+ VSHA256_Update(&ctx, buf, i);
} while (i > 0);
- SHA256_Update(&ctx, challenge, 32);
- SHA256_Update(&ctx, "\n", 1);
- SHA256_Final(buf, &ctx);
- for (i = 0; i < SHA256_LEN; i++)
+ VSHA256_Update(&ctx, challenge, 32);
+ VSHA256_Update(&ctx, "\n", 1);
+ VSHA256_Final(buf, &ctx);
+ for (i = 0; i < VSHA256_LEN; i++)
assert(snprintf(response + 2 * i, 3, "%02x", buf[i]) == 2);
}
diff --git a/lib/libvarnish/vsha256.c b/lib/libvarnish/vsha256.c
index 5b898a9..7836179 100644
--- a/lib/libvarnish/vsha256.c
+++ b/lib/libvarnish/vsha256.c
@@ -146,7 +146,7 @@ static const uint32_t K[64] = {
* the 512-bit input block to produce a new state.
*/
static void
-SHA256_Transform(uint32_t * state, const unsigned char block[64])
+VSHA256_Transform(uint32_t * state, const unsigned char block[64])
{
uint32_t W[64];
uint32_t S[8];
@@ -211,7 +211,7 @@ static const unsigned char PAD[64] = {
/* Add padding and terminating bit-count. */
static void
-SHA256_Pad(SHA256_CTX * ctx)
+VSHA256_Pad(VSHA256_CTX * ctx)
{
size_t r;
@@ -225,7 +225,7 @@ SHA256_Pad(SHA256_CTX * ctx)
} else {
/* Finish the current block and mix. */
memcpy(&ctx->buf[r], PAD, 64 - r);
- SHA256_Transform(ctx->state, ctx->buf);
+ VSHA256_Transform(ctx->state, ctx->buf);
/* The start of the final block is all zeroes. */
memset(&ctx->buf[0], 0, 56);
@@ -235,12 +235,12 @@ SHA256_Pad(SHA256_CTX * ctx)
vbe64enc(&ctx->buf[56], ctx->count);
/* Mix in the final block. */
- SHA256_Transform(ctx->state, ctx->buf);
+ VSHA256_Transform(ctx->state, ctx->buf);
}
/* SHA-256 initialization. Begins a SHA-256 operation. */
void
-SHA256_Init(SHA256_CTX * ctx)
+VSHA256_Init(VSHA256_CTX * ctx)
{
/* Zero bits processed so far */
@@ -259,7 +259,7 @@ SHA256_Init(SHA256_CTX * ctx)
/* Add bytes into the hash */
void
-SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len)
+VSHA256_Update(VSHA256_CTX * ctx, const void *in, size_t len)
{
uint64_t bitlen;
uint32_t r;
@@ -282,13 +282,13 @@ SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len)
/* Finish the current block */
memcpy(&ctx->buf[r], src, 64 - r);
- SHA256_Transform(ctx->state, ctx->buf);
+ VSHA256_Transform(ctx->state, ctx->buf);
src += 64 - r;
len -= 64 - r;
/* Perform complete blocks */
while (len >= 64) {
- SHA256_Transform(ctx->state, src);
+ VSHA256_Transform(ctx->state, src);
src += 64;
len -= 64;
}
@@ -302,14 +302,15 @@ SHA256_Update(SHA256_CTX * ctx, const void *in, size_t len)
* and clears the context state.
*/
void
-SHA256_Final(unsigned char digest[static SHA256_DIGEST_LENGTH], SHA256_CTX *ctx)
+VSHA256_Final(unsigned char digest[static VSHA256_DIGEST_LENGTH],
+ VSHA256_CTX *ctx)
{
/* Add padding */
- SHA256_Pad(ctx);
+ VSHA256_Pad(ctx);
/* Write the hash */
- be32enc_vect(digest, ctx->state, SHA256_DIGEST_LENGTH);
+ be32enc_vect(digest, ctx->state, VSHA256_DIGEST_LENGTH);
/* Clear the context state */
memset((void *)ctx, 0, sizeof(*ctx));
@@ -340,16 +341,16 @@ static const struct sha256test {
void
-SHA256_Test(void)
+VSHA256_Test(void)
{
- struct SHA256Context c;
+ struct VSHA256Context c;
const struct sha256test *p;
unsigned char o[32];
for (p = sha256test; p->input != NULL; p++) {
- SHA256_Init(&c);
- SHA256_Update(&c, p->input, strlen(p->input));
- SHA256_Final(o, &c);
+ VSHA256_Init(&c);
+ VSHA256_Update(&c, p->input, strlen(p->input));
+ VSHA256_Final(o, &c);
AZ(memcmp(o, p->output, 32));
}
}
diff --git a/lib/libvmod_directors/hash.c b/lib/libvmod_directors/hash.c
index 14f36f5..a8961df 100644
--- a/lib/libvmod_directors/hash.c
+++ b/lib/libvmod_directors/hash.c
@@ -98,10 +98,10 @@ VCL_BACKEND __match_proto__()
vmod_hash_backend(VRT_CTX, struct vmod_directors_hash *rr,
const char *arg, ...)
{
- struct SHA256Context sha_ctx;
+ struct VSHA256Context sha_ctx;
va_list ap;
const char *p;
- unsigned char sha256[SHA256_LEN];
+ unsigned char sha256[VSHA256_LEN];
VCL_BACKEND be;
double r;
@@ -109,16 +109,16 @@ vmod_hash_backend(VRT_CTX, struct vmod_directors_hash *rr,
CHECK_OBJ_ORNULL(ctx->bo, BUSYOBJ_MAGIC);
CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
- SHA256_Init(&sha_ctx);
+ VSHA256_Init(&sha_ctx);
va_start(ap, arg);
p = arg;
while (p != vrt_magic_string_end) {
if (p != NULL && *p != '\0')
- SHA256_Update(&sha_ctx, p, strlen(p));
+ VSHA256_Update(&sha_ctx, p, strlen(p));
p = va_arg(ap, const char *);
}
va_end(ap);
- SHA256_Final(sha256, &sha_ctx);
+ VSHA256_Final(sha256, &sha_ctx);
r = vbe32dec(sha256);
r = scalbn(r, -32);
diff --git a/lib/libvmod_directors/shard_hash.c b/lib/libvmod_directors/shard_hash.c
index 560ceb9..8ff0eec 100644
--- a/lib/libvmod_directors/shard_hash.c
+++ b/lib/libvmod_directors/shard_hash.c
@@ -59,16 +59,16 @@ shard_hash_crc32(VCL_STRING s)
static uint32_t __match_proto__(hash_func)
shard_hash_sha256(VCL_STRING s)
{
- struct SHA256Context sha256;
+ struct VSHA256Context sha256;
union {
unsigned char digest[32];
uint32_t uint32_digest[8];
} sha256_digest;
uint32_t r;
- SHA256_Init(&sha256);
- SHA256_Update(&sha256, s, strlen(s));
- SHA256_Final(sha256_digest.digest, &sha256);
+ VSHA256_Init(&sha256);
+ VSHA256_Update(&sha256, s, strlen(s));
+ VSHA256_Final(sha256_digest.digest, &sha256);
/*
* use low 32 bits only
More information about the varnish-commit
mailing list