d03ab969 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: hmac.h,v 1.7 2004/04/08 01:36:15 mdw Exp $ |
d03ab969 |
4 | * |
5 | * Generic code for HMAC and NMAC |
6 | * |
7 | * (c) 1998 Straylight/Edgeware |
8 | */ |
9 | |
45c0fd36 |
10 | /*----- Licensing notice --------------------------------------------------* |
d03ab969 |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
45c0fd36 |
18 | * |
d03ab969 |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
45c0fd36 |
23 | * |
d03ab969 |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
d03ab969 |
30 | /*----- Notes on the HMAC and NMAC constructions --------------------------* |
31 | * |
79ba130c |
32 | * Designed by Mihir Bellare, Ran Canetti and Hugo Krawczyk, NMAC is a method |
33 | * for constructing keyed message authentication algorithms from unkeyed hash |
34 | * functions. It has been proven to provide useful security given reasonable |
35 | * assumptions about the underlying hash function. HMAC is an alternative |
36 | * formulation which doesn't require low-level access to the hash function's |
37 | * implementation. NMAC was designed to allow MD5 has a suitable underlying |
38 | * hash function, even though doubts were already being raised about its |
39 | * collision resistance. |
d03ab969 |
40 | */ |
41 | |
79ba130c |
42 | #ifndef CATACOMB_HMAC_H |
43 | #define CATACOMB_HMAC_H |
d03ab969 |
44 | |
45 | #ifdef __cplusplus |
46 | extern "C" { |
47 | #endif |
48 | |
49 | /*----- Header files ------------------------------------------------------*/ |
50 | |
79ba130c |
51 | #include <stddef.h> |
d03ab969 |
52 | |
53 | #include <mLib/bits.h> |
54 | |
79ba130c |
55 | #ifndef CATACOMB_GMAC_H |
56 | # include "gmac.h" |
d03ab969 |
57 | #endif |
58 | |
59 | /*----- Macros ------------------------------------------------------------*/ |
60 | |
61 | /* --- @HMAC_DECL@ --- * |
62 | * |
63 | * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function |
64 | * |
65 | * Use: Creates declarations for the HMAC and NMAC functions. |
66 | */ |
67 | |
68 | #define HMAC_DECL(PRE, pre) \ |
69 | \ |
79ba130c |
70 | /* --- An HMAC or NMAC key --- */ \ |
71 | \ |
d03ab969 |
72 | typedef struct pre##_mackey { \ |
c850c0da |
73 | octet ochain[PRE##_STATESZ]; /* Chaining for outer hash */ \ |
79ba130c |
74 | unsigned ocount; /* Byte count for outer hash */ \ |
c850c0da |
75 | octet ichain[PRE##_STATESZ]; /* Chaining for inner hash */ \ |
79ba130c |
76 | unsigned icount; /* Byte count for inner hash */ \ |
d03ab969 |
77 | } pre##_mackey; \ |
78 | \ |
79ba130c |
79 | /* --- An HMAC or NMAC hashing context --- */ \ |
80 | \ |
d03ab969 |
81 | typedef struct pre##_macctx { \ |
82 | pre##_ctx ctx; /* Context for main hashing */ \ |
c850c0da |
83 | octet chain[PRE##_STATESZ]; /* Chaining for outer hash */ \ |
79ba130c |
84 | unsigned count; /* Byte count for outer hash */ \ |
d03ab969 |
85 | } pre##_macctx; \ |
86 | \ |
6ced98bd |
87 | /* --- Other useful constants --- */ \ |
88 | \ |
2a62e96d |
89 | extern const octet pre##_hmackeysz[]; \ |
90 | extern const octet pre##_nmackeysz[]; \ |
91 | extern const octet pre##_sslmackeysz[]; \ |
6ced98bd |
92 | \ |
79ba130c |
93 | /* --- @pre_nmacinit@ --- * \ |
d03ab969 |
94 | * \ |
95 | * Arguments: @pre_macctx *key@ = pointer to a MAC key object \ |
96 | * @const void *ok@ = pointer to outer hash init vector \ |
97 | * @const void *ik@ = pointer to inner hash init vector \ |
98 | * \ |
99 | * Returns: --- \ |
100 | * \ |
101 | * Use: Initializes a MAC key for doing NMAC hashing. \ |
102 | */ \ |
103 | \ |
79ba130c |
104 | extern void pre##_nmacinit(pre##_mackey */*key*/, \ |
105 | const void */*ok*/, const void */*ik*/); \ |
d03ab969 |
106 | \ |
79ba130c |
107 | /* --- @pre_hmacinit@ --- * \ |
d03ab969 |
108 | * \ |
109 | * Arguments: @pre_mackey *key@ = pointer to MAC key object \ |
110 | * @const void *k@ = pointer to key to use \ |
111 | * @size_t sz@ = size of key data \ |
112 | * \ |
113 | * Returns: --- \ |
114 | * \ |
115 | * Use: Initializes a MAC key for doing HMAC hashing. Keys \ |
116 | * longer than the hash function's output size aren't very \ |
117 | * useful, but are accepted. Keys longer than the hash's \ |
118 | * block size are also accepted; they are hashed before \ |
119 | * use, as specified in RFC2104. \ |
120 | */ \ |
121 | \ |
45c0fd36 |
122 | extern void pre##_hmacinit(pre##_mackey */*key*/, \ |
79ba130c |
123 | const void */*k*/, size_t /*sz*/); \ |
d03ab969 |
124 | \ |
36c67859 |
125 | /* --- @pre_sslmacinit@ --- * \ |
126 | * \ |
127 | * Arguments: @pre_mackey *key@ = pointer to MAC key object \ |
128 | * @const void *k@ = pointer to key to use \ |
129 | * @size_t sz@ = size of key data \ |
130 | * \ |
131 | * Returns: --- \ |
132 | * \ |
133 | * Use: Initializes a MAC key for doing hasing using the SSL3 \ |
134 | * variant of HMAC. \ |
135 | */ \ |
136 | \ |
137 | extern void pre##_sslmacinit(pre##_mackey */*key*/, \ |
138 | const void */*k*/, size_t /*sz*/); \ |
139 | \ |
d03ab969 |
140 | /* --- @pre_macinit@ --- * \ |
141 | * \ |
142 | * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \ |
143 | * @const pre_mackey *key@ = pointer to MAC key block \ |
144 | * \ |
145 | * Returns: --- \ |
146 | * \ |
147 | * Use: Instantiates a MAC context from a key block. \ |
148 | */ \ |
149 | \ |
79ba130c |
150 | extern void pre##_macinit(pre##_macctx */*ctx*/, \ |
151 | const pre##_mackey */*key*/); \ |
d03ab969 |
152 | \ |
79ba130c |
153 | /* --- @pre_machash@ --- * \ |
d03ab969 |
154 | * \ |
155 | * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \ |
156 | * @const void *buf@ = pointer to buffer \ |
157 | * @size_t sz@ = size of the buffer \ |
158 | * \ |
159 | * Returns: --- \ |
160 | * \ |
161 | * Use: Hashes a buffer. \ |
162 | */ \ |
163 | \ |
79ba130c |
164 | extern void pre##_machash(pre##_macctx */*ctx*/, \ |
165 | const void */*buf*/, size_t /*sz*/); \ |
d03ab969 |
166 | \ |
167 | /* --- @pre_macdone@ --- * \ |
168 | * \ |
169 | * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \ |
170 | * @void *mac@ = pointer to buffer to receive MAC \ |
171 | * \ |
172 | * Returns: --- \ |
173 | * \ |
174 | * Use: Returns the result of a MAC computation. \ |
175 | */ \ |
176 | \ |
79ba130c |
177 | extern void pre##_macdone(pre##_macctx */*ctx*/, void */*mac*/); \ |
d03ab969 |
178 | \ |
79ba130c |
179 | /* --- Generic MAC interface --- */ \ |
d03ab969 |
180 | \ |
2a62e96d |
181 | extern const gcmac pre##_hmac; \ |
182 | extern const gcmac pre##_nmac; \ |
183 | extern const gcmac pre##_sslmac; |
d03ab969 |
184 | |
185 | /*----- That's all, folks -------------------------------------------------*/ |
186 | |
187 | #ifdef __cplusplus |
188 | } |
189 | #endif |
190 | |
191 | #endif |