1/* hmac.h
2
3 HMAC message authentication code (RFC-2104).
4
5 Copyright (C) 2001, 2002 Niels Möller
6
7 This file is part of GNU Nettle.
8
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
11
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
15
16 or
17
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
21
22 or both in parallel, as here.
23
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
28
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
32*/
33
34#ifndef NETTLE_HMAC_H_INCLUDED
35#define NETTLE_HMAC_H_INCLUDED
36
37#include "nettle-meta.h"
38
39#include "md5.h"
40#include "ripemd160.h"
41#include "sha1.h"
42#include "sha2.h"
43
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48/* Namespace mangling */
49#define hmac_set_key nettle_hmac_set_key
50#define hmac_update nettle_hmac_update
51#define hmac_digest nettle_hmac_digest
52#define hmac_md5_set_key nettle_hmac_md5_set_key
53#define hmac_md5_update nettle_hmac_md5_update
54#define hmac_md5_digest nettle_hmac_md5_digest
55#define hmac_ripemd160_set_key nettle_hmac_ripemd160_set_key
56#define hmac_ripemd160_update nettle_hmac_ripemd160_update
57#define hmac_ripemd160_digest nettle_hmac_ripemd160_digest
58#define hmac_sha1_set_key nettle_hmac_sha1_set_key
59#define hmac_sha1_update nettle_hmac_sha1_update
60#define hmac_sha1_digest nettle_hmac_sha1_digest
61#define hmac_sha224_set_key nettle_hmac_sha224_set_key
62#define hmac_sha224_digest nettle_hmac_sha224_digest
63#define hmac_sha256_set_key nettle_hmac_sha256_set_key
64#define hmac_sha256_update nettle_hmac_sha256_update
65#define hmac_sha256_digest nettle_hmac_sha256_digest
66#define hmac_sha384_set_key nettle_hmac_sha384_set_key
67#define hmac_sha384_digest nettle_hmac_sha384_digest
68#define hmac_sha512_set_key nettle_hmac_sha512_set_key
69#define hmac_sha512_update nettle_hmac_sha512_update
70#define hmac_sha512_digest nettle_hmac_sha512_digest
71
72void
73hmac_set_key(void *outer, void *inner, void *state,
74 const struct nettle_hash *hash,
75 size_t length, const uint8_t *key);
76
77/* This function is not strictly needed, it's s just the same as the
78 * hash update function. */
79void
80hmac_update(void *state,
81 const struct nettle_hash *hash,
82 size_t length, const uint8_t *data);
83
84void
85hmac_digest(const void *outer, const void *inner, void *state,
86 const struct nettle_hash *hash,
87 size_t length, uint8_t *digest);
88
89
90#define HMAC_CTX(type) \
91{ type outer; type inner; type state; }
92
93#define HMAC_SET_KEY(ctx, hash, length, key) \
94 hmac_set_key( &(ctx)->outer, &(ctx)->inner, &(ctx)->state, \
95 (hash), (length), (key) )
96
97#define HMAC_DIGEST(ctx, hash, length, digest) \
98 hmac_digest( &(ctx)->outer, &(ctx)->inner, &(ctx)->state, \
99 (hash), (length), (digest) )
100
101/* HMAC using specific hash functions */
102
103/* hmac-md5 */
104struct hmac_md5_ctx HMAC_CTX(struct md5_ctx);
105
106void
107hmac_md5_set_key(struct hmac_md5_ctx *ctx,
108 size_t key_length, const uint8_t *key);
109
110void
111hmac_md5_update(struct hmac_md5_ctx *ctx,
112 size_t length, const uint8_t *data);
113
114void
115hmac_md5_digest(struct hmac_md5_ctx *ctx,
116 size_t length, uint8_t *digest);
117
118
119/* hmac-ripemd160 */
120struct hmac_ripemd160_ctx HMAC_CTX(struct ripemd160_ctx);
121
122void
123hmac_ripemd160_set_key(struct hmac_ripemd160_ctx *ctx,
124 size_t key_length, const uint8_t *key);
125
126void
127hmac_ripemd160_update(struct hmac_ripemd160_ctx *ctx,
128 size_t length, const uint8_t *data);
129
130void
131hmac_ripemd160_digest(struct hmac_ripemd160_ctx *ctx,
132 size_t length, uint8_t *digest);
133
134
135/* hmac-sha1 */
136struct hmac_sha1_ctx HMAC_CTX(struct sha1_ctx);
137
138void
139hmac_sha1_set_key(struct hmac_sha1_ctx *ctx,
140 size_t key_length, const uint8_t *key);
141
142void
143hmac_sha1_update(struct hmac_sha1_ctx *ctx,
144 size_t length, const uint8_t *data);
145
146void
147hmac_sha1_digest(struct hmac_sha1_ctx *ctx,
148 size_t length, uint8_t *digest);
149
150/* hmac-sha256 */
151struct hmac_sha256_ctx HMAC_CTX(struct sha256_ctx);
152
153void
154hmac_sha256_set_key(struct hmac_sha256_ctx *ctx,
155 size_t key_length, const uint8_t *key);
156
157void
158hmac_sha256_update(struct hmac_sha256_ctx *ctx,
159 size_t length, const uint8_t *data);
160
161void
162hmac_sha256_digest(struct hmac_sha256_ctx *ctx,
163 size_t length, uint8_t *digest);
164
165/* hmac-sha224 */
166#define hmac_sha224_ctx hmac_sha256_ctx
167
168void
169hmac_sha224_set_key(struct hmac_sha224_ctx *ctx,
170 size_t key_length, const uint8_t *key);
171
172#define hmac_sha224_update nettle_hmac_sha256_update
173
174void
175hmac_sha224_digest(struct hmac_sha224_ctx *ctx,
176 size_t length, uint8_t *digest);
177
178/* hmac-sha512 */
179struct hmac_sha512_ctx HMAC_CTX(struct sha512_ctx);
180
181void
182hmac_sha512_set_key(struct hmac_sha512_ctx *ctx,
183 size_t key_length, const uint8_t *key);
184
185void
186hmac_sha512_update(struct hmac_sha512_ctx *ctx,
187 size_t length, const uint8_t *data);
188
189void
190hmac_sha512_digest(struct hmac_sha512_ctx *ctx,
191 size_t length, uint8_t *digest);
192
193/* hmac-sha384 */
194#define hmac_sha384_ctx hmac_sha512_ctx
195
196void
197hmac_sha384_set_key(struct hmac_sha512_ctx *ctx,
198 size_t key_length, const uint8_t *key);
199
200#define hmac_sha384_update nettle_hmac_sha512_update
201
202void
203hmac_sha384_digest(struct hmac_sha512_ctx *ctx,
204 size_t length, uint8_t *digest);
205
206#ifdef __cplusplus
207}
208#endif
209
210#endif /* NETTLE_HMAC_H_INCLUDED */
211