Update copyright headers for dual licensing.
[gd/nettle] / hmac.h
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
45 extern "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
72 void
73 hmac_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. */
79 void
80 hmac_update(void *state,
81             const struct nettle_hash *hash,
82             size_t length, const uint8_t *data);
83
84 void
85 hmac_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 */
104 struct hmac_md5_ctx HMAC_CTX(struct md5_ctx);
105
106 void
107 hmac_md5_set_key(struct hmac_md5_ctx *ctx,
108                  size_t key_length, const uint8_t *key);
109
110 void
111 hmac_md5_update(struct hmac_md5_ctx *ctx,
112                 size_t length, const uint8_t *data);
113
114 void
115 hmac_md5_digest(struct hmac_md5_ctx *ctx,
116                 size_t length, uint8_t *digest);
117
118
119 /* hmac-ripemd160 */
120 struct hmac_ripemd160_ctx HMAC_CTX(struct ripemd160_ctx);
121
122 void
123 hmac_ripemd160_set_key(struct hmac_ripemd160_ctx *ctx,
124                        size_t key_length, const uint8_t *key);
125
126 void
127 hmac_ripemd160_update(struct hmac_ripemd160_ctx *ctx,
128                       size_t length, const uint8_t *data);
129
130 void
131 hmac_ripemd160_digest(struct hmac_ripemd160_ctx *ctx,
132                       size_t length, uint8_t *digest);
133
134
135 /* hmac-sha1 */
136 struct hmac_sha1_ctx HMAC_CTX(struct sha1_ctx);
137
138 void
139 hmac_sha1_set_key(struct hmac_sha1_ctx *ctx,
140                   size_t key_length, const uint8_t *key);
141
142 void
143 hmac_sha1_update(struct hmac_sha1_ctx *ctx,
144                  size_t length, const uint8_t *data);
145
146 void
147 hmac_sha1_digest(struct hmac_sha1_ctx *ctx,
148                  size_t length, uint8_t *digest);
149
150 /* hmac-sha256 */
151 struct hmac_sha256_ctx HMAC_CTX(struct sha256_ctx);
152
153 void
154 hmac_sha256_set_key(struct hmac_sha256_ctx *ctx,
155                     size_t key_length, const uint8_t *key);
156
157 void
158 hmac_sha256_update(struct hmac_sha256_ctx *ctx,
159                    size_t length, const uint8_t *data);
160
161 void
162 hmac_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
168 void
169 hmac_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
174 void
175 hmac_sha224_digest(struct hmac_sha224_ctx *ctx,
176                    size_t length, uint8_t *digest);
177
178 /* hmac-sha512 */
179 struct hmac_sha512_ctx HMAC_CTX(struct sha512_ctx);
180
181 void
182 hmac_sha512_set_key(struct hmac_sha512_ctx *ctx,
183                     size_t key_length, const uint8_t *key);
184
185 void
186 hmac_sha512_update(struct hmac_sha512_ctx *ctx,
187                    size_t length, const uint8_t *data);
188
189 void
190 hmac_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
196 void
197 hmac_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
202 void
203 hmac_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 */