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