Commit a9f58f0f authored by Geoff Simmons's avatar Geoff Simmons

consolidate code for allocating space for a digest in workspace

parent 58858a78
Pipeline #47 skipped
......@@ -48,6 +48,9 @@
#define VERR(ctx, fmt, ...) \
errmsg((ctx), "vmod blobdigest error: " fmt, __VA_ARGS__)
#define VERRNOMEM(ctx, fmt, ...) \
VERR((ctx), fmt ", out of space", __VA_ARGS__)
#define ERRNOMEM(ctx, msg) \
ERR((ctx), msg ", out of space")
......@@ -73,11 +76,12 @@ struct vmod_blobdigest_digest {
};
struct vmod_blobdigest_hmac {
unsigned magic;
unsigned magic;
#define VMOD_BLOBDIGEST_HMAC_MAGIC 0x85678153
hash_ctx inner_ctx;
hash_ctx outer_ctx;
enum algorithm hash;
hash_ctx inner_ctx;
hash_ctx outer_ctx;
char *vcl_name;
enum algorithm hash;
};
static void
......@@ -211,6 +215,31 @@ digest(const enum algorithm hash, hash_ctx *restrict const hctx,
final(hash, hctx, digest);
}
static struct vmod_priv *
ws_alloc_digest(VRT_CTX, const size_t digestsz,
const char * const restrict context,
const char * const restrict caller)
{
struct vmod_priv *b;
char *snap;
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
snap = WS_Snapshot(ctx->ws);
if ((b = WS_Alloc(ctx->ws, sizeof(struct vmod_priv))) == NULL) {
VERRNOMEM(ctx, "allocating blob in %s.%s()", context, caller);
return NULL;
}
if ((b->priv = WS_Alloc(ctx->ws, digestsz)) == NULL) {
WS_Reset(ctx->ws, snap);
VERRNOMEM(ctx, "allocating hash result in %s.%s()", context,
caller);
return NULL;
}
b->len = digestsz;
b->free = NULL;
return b;
}
/* Objects */
static void
......@@ -323,7 +352,6 @@ vmod_digest_final(VRT_CTX, struct vmod_blobdigest_digest *h)
{
struct vmod_priv *b;
struct digest_task *task;
char *snap;
enum algorithm hash;
size_t digestsz;
......@@ -353,20 +381,9 @@ vmod_digest_final(VRT_CTX, struct vmod_blobdigest_digest *h)
if (task->result != NULL)
return task->result;
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
snap = WS_Snapshot(ctx->ws);
/* XXX: use vcl_name in err messages */
if ((b = WS_Alloc(ctx->ws, sizeof(struct vmod_priv))) == NULL) {
ERRNOMEM(ctx, "allocating blob in digest.final()");
b = ws_alloc_digest(ctx, digestsz, h->vcl_name, "final");
if (b == NULL)
return NULL;
}
if ((b->priv = WS_Alloc(ctx->ws, digestsz)) == NULL) {
WS_Reset(ctx->ws, snap);
ERRNOMEM(ctx, "allocating hash result in digest.final()");
return NULL;
}
b->len = digestsz;
b->free = NULL;
final(hash, &task->ctx, b->priv);
task->result = b;
return b;
......@@ -389,6 +406,7 @@ vmod_hmac__init(VRT_CTX, struct vmod_blobdigest_hmac **hmacp,
AN(hmac);
*hmacp = hmac;
hmac->hash = hash;
hmac->vcl_name = strdup(vcl_name);
memset(k, 0, blocksz);
if (key->len <= blocksz)
......@@ -413,7 +431,6 @@ VCL_BLOB
vmod_hmac_hmac(VRT_CTX, struct vmod_blobdigest_hmac *h, VCL_BLOB msg)
{
struct vmod_priv *b;
char *snap;
hash_ctx inner_ctx, outer_ctx;
enum algorithm hash;
size_t digestsz;
......@@ -423,19 +440,9 @@ vmod_hmac_hmac(VRT_CTX, struct vmod_blobdigest_hmac *h, VCL_BLOB msg)
hash = h->hash;
digestsz = hashspec[hash].digestsz;
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
snap = WS_Snapshot(ctx->ws);
if ((b = WS_Alloc(ctx->ws, sizeof(struct vmod_priv))) == NULL) {
ERRNOMEM(ctx, "allocating blob in hmac.hmac()");
b = ws_alloc_digest(ctx, digestsz, h->vcl_name, "hmac");
if (b == NULL)
return NULL;
}
if ((b->priv = WS_Alloc(ctx->ws, digestsz)) == NULL) {
WS_Reset(ctx->ws, snap);
ERRNOMEM(ctx, "allocating hash result in hmac.hmac()");
return NULL;
}
b->len = digestsz;
b->free = NULL;
uint8_t inner_digest[digestsz];
memcpy(&inner_ctx, &h->inner_ctx, sizeof(hash_ctx));
......@@ -460,6 +467,8 @@ vmod_hmac__fini(struct vmod_blobdigest_hmac **hmacp)
hmac = *hmacp;
*hmacp = NULL;
CHECK_OBJ_NOTNULL(hmac, VMOD_BLOBDIGEST_HMAC_MAGIC);
if (hmac->vcl_name != NULL)
free(hmac->vcl_name);
FREE_OBJ(hmac);
}
......@@ -470,7 +479,6 @@ vmod_hash(VRT_CTX, VCL_ENUM hashs, VCL_BLOB msg)
{
enum algorithm hash = parse_algorithm(hashs);
struct vmod_priv *b;
char *snap;
hash_ctx hctx[1];
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
......@@ -481,19 +489,9 @@ vmod_hash(VRT_CTX, VCL_ENUM hashs, VCL_BLOB msg)
if (msg == NULL)
return NULL;
CHECK_OBJ_NOTNULL(ctx->ws, WS_MAGIC);
snap = WS_Snapshot(ctx->ws);
if ((b = WS_Alloc(ctx->ws, sizeof(struct vmod_priv))) == NULL) {
ERRNOMEM(ctx, "allocating blob in hash()");
return NULL;
}
if ((b->priv = WS_Alloc(ctx->ws, hashspec[hash].digestsz)) == NULL) {
WS_Reset(ctx->ws, snap);
ERRNOMEM(ctx, "allocating hash result in hash()");
b = ws_alloc_digest(ctx, hashspec[hash].digestsz, "blobdigest", "hash");
if (b == NULL)
return NULL;
}
b->len = hashspec[hash].digestsz;
b->free = NULL;
digest(hash, hctx, msg, b->priv);
return b;
}
......
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