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