nostril

A C cli tool for creating nostr events
git clone git://jb55.com/nostril
Log | Files | Refs | Submodules | README | LICENSE

hmac_sha256.h (2411B)


      1 #ifndef CCAN_CRYPTO_HMAC_SHA256_H
      2 #define CCAN_CRYPTO_HMAC_SHA256_H
      3 /* BSD-MIT - see LICENSE file for details */
      4 #include "config.h"
      5 #include <stdint.h>
      6 #include <stdlib.h>
      7 #include "sha256.h"
      8 
      9 /* Number of bytes per block. */
     10 #define HMAC_SHA256_BLOCKSIZE 64
     11 
     12 /**
     13  * struct hmac_sha256 - structure representing a completed HMAC.
     14  */
     15 struct hmac_sha256 {
     16 	struct sha256 sha;
     17 };
     18 
     19 /**
     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
     26  */
     27 void hmac_sha256(struct hmac_sha256 *hmac,
     28 		 const void *k, size_t ksize,
     29 		 const void *d, size_t dsize);
     30 
     31 /**
     32  * struct hmac_sha256_ctx - structure to store running context for hmac_sha256
     33  */
     34 struct hmac_sha256_ctx {
     35 	struct sha256_ctx sha;
     36 	uint64_t k_opad[HMAC_SHA256_BLOCKSIZE / sizeof(uint64_t)];
     37 };
     38 
     39 /**
     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
     44  *
     45  * This must be called before hmac_sha256_update or hmac_sha256_done.
     46  *
     47  * If it was already initialized, this forgets anything which was
     48  * hashed before.
     49  *
     50  * Example:
     51  * static void hmac_all(const char *key,
     52  *			const char **arr, struct hmac_sha256 *hash)
     53  * {
     54  *	size_t i;
     55  *	struct hmac_sha256_ctx ctx;
     56  *
     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);
     61  * }
     62  */
     63 void hmac_sha256_init(struct hmac_sha256_ctx *ctx,
     64 		      const void *k, size_t ksize);
     65 
     66 /**
     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
     71  *
     72  * You can call this multiple times to hash more data, before calling
     73  * hmac_sha256_done().
     74  */
     75 void hmac_sha256_update(struct hmac_sha256_ctx *ctx, const void *p, size_t size);
     76 
     77 /**
     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.
     81  *
     82  * Note that @ctx is *destroyed* by this, and must be reinitialized.
     83  * To avoid that, pass a copy instead.
     84  */
     85 void hmac_sha256_done(struct hmac_sha256_ctx *hmac_sha256, struct hmac_sha256 *res);
     86 #endif /* CCAN_CRYPTO_HMAC_SHA256_H */