1 #ifndef CCAN_CRYPTO_HMAC_SHA256_H
2 #define CCAN_CRYPTO_HMAC_SHA256_H
3 /* BSD-MIT - see LICENSE file for details */
7 #include <ccan/crypto/sha256/sha256.h>
9 /* Number of bytes per block. */
10 #define HMAC_SHA256_BLOCKSIZE 64
13 * struct hmac_sha256 - structure representing a completed HMAC.
20 * hmac_sha256 - return hmac of an object with a key.
21 * @hmac: the hmac to fill in
22 * @k: pointer to the key,
23 * @ksize: the number of bytes pointed to by @k
24 * @d: pointer to memory,
25 * @dsize: the number of bytes pointed to by @d
27 void hmac_sha256(struct hmac_sha256 *hmac,
28 const void *k, size_t ksize,
29 const void *d, size_t dsize);
32 * struct hmac_sha256_ctx - structure to store running context for hmac_sha256
34 struct hmac_sha256_ctx {
35 struct sha256_ctx sha;
36 uint64_t k_opad[HMAC_SHA256_BLOCKSIZE / sizeof(uint64_t)];
40 * hmac_sha256_init - initialize an HMAC_SHA256 context.
41 * @ctx: the hmac_sha256_ctx to initialize
42 * @k: pointer to the key,
43 * @ksize: the number of bytes pointed to by @k
45 * This must be called before hmac_sha256_update or hmac_sha256_done.
47 * If it was already initialized, this forgets anything which was
51 * static void hmac_all(const char *key,
52 * const char **arr, struct hmac_sha256 *hash)
55 * struct hmac_sha256_ctx ctx;
57 * hmac_sha256_init(&ctx, key, strlen(key));
58 * for (i = 0; arr[i]; i++)
59 * hmac_sha256_update(&ctx, arr[i], strlen(arr[i]));
60 * hmac_sha256_done(&ctx, hash);
63 void hmac_sha256_init(struct hmac_sha256_ctx *ctx,
64 const void *k, size_t ksize);
67 * hmac_sha256_update - include some memory in the hash.
68 * @ctx: the hmac_sha256_ctx to use
69 * @p: pointer to memory,
70 * @size: the number of bytes pointed to by @p
72 * You can call this multiple times to hash more data, before calling
75 void hmac_sha256_update(struct hmac_sha256_ctx *ctx, const void *p, size_t size);
78 * hmac_sha256_done - finish HMAC_SHA256 and return the hash
79 * @ctx: the hmac_sha256_ctx to complete
80 * @res: the hash to return.
82 * Note that @ctx is *destroyed* by this, and must be reinitialized.
83 * To avoid that, pass a copy instead.
85 void hmac_sha256_done(struct hmac_sha256_ctx *hmac_sha256, struct hmac_sha256 *res);
86 #endif /* CCAN_CRYPTO_HMAC_SHA256_H */