Commit fcb3c398 authored by Geoff Simmons's avatar Geoff Simmons

add the blob object interface

parent 1d06f969
......@@ -27,6 +27,8 @@ import blobsha256 [from "path"] ;
CONTENTS
========
* Object blob
* BLOB blob.hash()
* BLOB hash(BLOB)
* Object hmac
* BLOB hmac.hmac(BLOB)
......@@ -46,8 +48,8 @@ Description
given ``key``.
Example
``new key = blobcode.blob(BASE64, "a2V5");``
``new hmac = blobsha256.hmac(key.get());``
new key = blobcode.blob(BASE64, "a2V5");
new hmac = blobsha256.hmac(key.get());
.. _func_hmac.hmac:
......@@ -62,7 +64,38 @@ Description
``msg`` based on the key provided in the constructor.
Example
``set req.http.hmac = hmac.hmac(blobcode.decode(BASE64, "Zm9v"));``
set req.http.hmac = hmac.hmac(blobcode.decode(BASE64, "Zm9v"));
.. _obj_blob:
Object blob
===========
Prototype
new OBJ = blobsha256.blob(BLOB blob)
Description
Creates an object that returns the SHA256 digest of the given
``blob``.
Example
new key = blobcode.blob(BASE64, "Zm9v");
new foo = blobsha256.blob(key.get());
.. _func_blob.hash:
BLOB blob.hash()
----------------
Prototype
BLOB blob.hash()
Description
Returns the SHA256 digest for the blob given in the constructor.
Example
set req.http.X-Hash = blobcode.encode(BASE64, foo.hash());
.. _func_hash:
......@@ -87,4 +120,4 @@ Description
Returns the version string for this VMOD.
Example
``std.log("Using VMOD blobsha256 version " + blobsha256.version());``
std.log("Using VMOD blobsha256 version " + blobsha256.version());
......@@ -60,6 +60,12 @@ struct vmod_blobsha256_hmac {
SHA256_CTX outer_ctx;
};
struct vmod_blobsha256_blob {
unsigned magic;
#define VMOD_BLOBSHA256_BLOB_MAGIC 0x2067c219
struct vmod_priv hash;
};
static void
errmsg(VRT_CTX, const char *fmt, ...)
{
......@@ -88,7 +94,7 @@ digest(SHA256_CTX *restrict const ctx, VCL_BLOB restrict const b,
SHA256_Final(digest, ctx);
}
/* Objects */
/* Object hmac */
VCL_VOID
vmod_hmac__init(VRT_CTX, struct vmod_blobsha256_hmac **hmacp,
......@@ -128,6 +134,7 @@ vmod_hmac_hmac(VRT_CTX, struct vmod_blobsha256_hmac *h, VCL_BLOB msg)
struct vmod_priv *b;
char *snap;
SHA256_CTX inner_ctx, outer_ctx;
uint8_t inner_digest[SHA256_LEN];
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(h, VMOD_BLOBSHA256_HMAC_MAGIC);
......@@ -146,7 +153,6 @@ vmod_hmac_hmac(VRT_CTX, struct vmod_blobsha256_hmac *h, VCL_BLOB msg)
b->len = SHA256_LEN;
b->free = NULL;
uint8_t inner_digest[SHA256_LEN];
memcpy(&inner_ctx, &h->inner_ctx, sizeof(SHA256_CTX));
memcpy(&outer_ctx, &h->outer_ctx, sizeof(SHA256_CTX));
......@@ -172,6 +178,54 @@ vmod_hmac__fini(struct vmod_blobsha256_hmac **hmacp)
FREE_OBJ(hmac);
}
/* Object blob */
VCL_VOID
vmod_blob__init(VRT_CTX, struct vmod_blobsha256_blob **blobp,
const char *vcl_name, VCL_BLOB b)
{
struct vmod_blobsha256_blob *blob;
SHA256_CTX hctx[1];
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
AN(blobp);
AZ(*blobp);
AN(vcl_name);
ALLOC_OBJ(blob, VMOD_BLOBSHA256_BLOB_MAGIC);
AN(blob);
*blobp = blob;
blob->hash.priv = malloc(SHA256_LEN);
if (blob->hash.priv == NULL) {
ERRNOMEM(ctx, "allocating hash in blob constructor");
return;
}
blob->hash.len = SHA256_LEN;
blob->hash.free = NULL;
digest(hctx, b, blob->hash.priv);
}
VCL_BLOB
vmod_blob_hash(VRT_CTX, struct vmod_blobsha256_blob *blob)
{
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(blob, VMOD_BLOBSHA256_BLOB_MAGIC);
return &blob->hash;
}
VCL_VOID
vmod_blob__fini(struct vmod_blobsha256_blob **blobp)
{
struct vmod_blobsha256_blob *blob;
AN(*blobp);
blob = *blobp;
*blobp = NULL;
CHECK_OBJ_NOTNULL(blob, VMOD_BLOBSHA256_BLOB_MAGIC);
FREE_OBJ(blob);
}
/* Functions */
VCL_BLOB
......
......@@ -19,8 +19,8 @@ Description
given ``key``.
Example
``new key = blobcode.blob(BASE64, "a2V5");``
``new hmac = blobsha256.hmac(key.get());``
new key = blobcode.blob(BASE64, "a2V5");
new hmac = blobsha256.hmac(key.get());
$Method BLOB .hmac(BLOB msg)
......@@ -29,7 +29,28 @@ Description
``msg`` based on the key provided in the constructor.
Example
``set req.http.hmac = hmac.hmac(blobcode.decode(BASE64, "Zm9v"));``
set req.http.hmac = hmac.hmac(blobcode.decode(BASE64, "Zm9v"));
$Object blob(BLOB blob)
Prototype
new OBJ = blobsha256.blob(BLOB blob)
Description
Creates an object that returns the SHA256 digest of the given
``blob``.
Example
new key = blobcode.blob(BASE64, "Zm9v");
new foo = blobsha256.blob(key.get());
$Method BLOB .hash()
Description
Returns the SHA256 digest for the blob given in the constructor.
Example
set req.http.X-Hash = blobcode.encode(BASE64, foo.hash());
$Function BLOB hash(BLOB msg)
......@@ -42,4 +63,4 @@ Description
Returns the version string for this VMOD.
Example
``std.log("Using VMOD blobsha256 version " + blobsha256.version());``
std.log("Using VMOD blobsha256 version " + blobsha256.version());
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