hmac.h (2967B)
1 2 #ifndef CCAN_CRYPTO_HMAC_SHA256_H 3 #define CCAN_CRYPTO_HMAC_SHA256_H 4 /* BSD-MIT */ 5 #include <stdint.h> 6 #include <stdlib.h> 7 #include "sha256.h" 8 #include "sha512.h" 9 10 /* Number of bytes per block. */ 11 #define HMAC_SHA256_BLOCKSIZE 64 12 #define HMAC_SHA512_BLOCKSIZE 128 13 14 /** 15 * struct hmac_sha256 - structure representing a completed HMAC. 16 */ 17 struct hmac_sha256 { 18 struct sha256 sha; 19 }; 20 21 22 struct hmac_sha512 { 23 struct sha512 sha; 24 }; 25 26 /** 27 * hmac_sha256 - return hmac of an object with a key. 28 * @hmac: the hmac to fill in 29 * @k: pointer to the key, 30 * @ksize: the number of bytes pointed to by @k 31 * @d: pointer to memory, 32 * @dsize: the number of bytes pointed to by @d 33 */ 34 void hmac_sha256(struct hmac_sha256 *hmac, 35 const void *k, size_t ksize, 36 const void *d, size_t dsize); 37 38 void hmac_sha512(struct hmac_sha512 *hmac, 39 const void *k, size_t ksize, 40 const void *d, size_t dsize); 41 42 /** 43 * struct hmac_sha256_ctx - structure to store running context for hmac_sha256 44 */ 45 struct hmac_sha256_ctx { 46 struct sha256_ctx sha; 47 uint64_t k_opad[HMAC_SHA256_BLOCKSIZE / sizeof(uint64_t)]; 48 }; 49 50 51 struct hmac_sha512_ctx { 52 struct sha512_ctx sha; 53 uint64_t k_opad[HMAC_SHA512_BLOCKSIZE / sizeof(uint64_t)]; 54 }; 55 56 /** 57 * hmac_sha256_init - initialize an HMAC_SHA256 context. 58 * @ctx: the hmac_sha256_ctx to initialize 59 * @k: pointer to the key, 60 * @ksize: the number of bytes pointed to by @k 61 * 62 * This must be called before hmac_sha256_update or hmac_sha256_done. 63 * 64 * If it was already initialized, this forgets anything which was 65 * hashed before. 66 * 67 * Example: 68 * static void hmac_all(const char *key, 69 * const char **arr, struct hmac_sha256 *hash) 70 * { 71 * size_t i; 72 * struct hmac_sha256_ctx ctx; 73 * 74 * hmac_sha256_init(&ctx, key, strlen(key)); 75 * for (i = 0; arr[i]; i++) 76 * hmac_sha256_update(&ctx, arr[i], strlen(arr[i])); 77 * hmac_sha256_done(&ctx, hash); 78 * } 79 */ 80 void hmac_sha256_init(struct hmac_sha256_ctx *ctx, 81 const void *k, size_t ksize); 82 83 void hmac_sha512_init(struct hmac_sha512_ctx *ctx, 84 const void *k, size_t ksize); 85 86 87 /** 88 * hmac_sha256_update - include some memory in the hash. 89 * @ctx: the hmac_sha256_ctx to use 90 * @p: pointer to memory, 91 * @size: the number of bytes pointed to by @p 92 * 93 * You can call this multiple times to hash more data, before calling 94 * hmac_sha256_done(). 95 */ 96 void hmac_sha256_update(struct hmac_sha256_ctx *ctx, 97 const void *p, size_t size); 98 99 void hmac_sha512_update(struct hmac_sha512_ctx *ctx, 100 const void *p, size_t size); 101 102 /** 103 * hmac_sha256_done - finish HMAC_SHA256 and return the hash 104 * @ctx: the hmac_sha256_ctx to complete 105 * @res: the hash to return. 106 * 107 * Note that @ctx is *destroyed* by this, and must be reinitialized. 108 * To avoid that, pass a copy instead. 109 */ 110 void hmac_sha256_done(struct hmac_sha256_ctx *hmac_sha256, 111 struct hmac_sha256 *res); 112 113 void hmac_sha512_done(struct hmac_sha512_ctx *hmac_sha256, 114 struct hmac_sha512 *res); 115 116 #endif /* CCAN_CRYPTO_HMAC_SHA256_H */