Use unsigned integer rather than enum for flags.
[u/mdw/catacomb] / hmac.h
CommitLineData
d03ab969 1/* -*-c-*-
2 *
36c67859 3 * $Id: hmac.h,v 1.5 2001/04/03 19:35:45 mdw Exp $
d03ab969 4 *
5 * Generic code for HMAC and NMAC
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
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.
18 *
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.
23 *
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
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: hmac.h,v $
36c67859 33 * Revision 1.5 2001/04/03 19:35:45 mdw
34 * Support the SSL HMAC variant (untested).
35 *
c850c0da 36 * Revision 1.4 2000/10/15 19:09:20 mdw
37 * Support HMAC mode for hash functions which need to store more state than
38 * the hash output size.
39 *
6ced98bd 40 * Revision 1.3 2000/06/17 11:23:57 mdw
41 * New key size interface.
42 *
79ba130c 43 * Revision 1.2 1999/12/10 23:17:39 mdw
44 * Split mode macros into interface and implementation.
45 *
d03ab969 46 * Revision 1.1 1999/09/03 08:41:12 mdw
47 * Initial import.
48 *
49 */
50
51/*----- Notes on the HMAC and NMAC constructions --------------------------*
52 *
79ba130c 53 * Designed by Mihir Bellare, Ran Canetti and Hugo Krawczyk, NMAC is a method
54 * for constructing keyed message authentication algorithms from unkeyed hash
55 * functions. It has been proven to provide useful security given reasonable
56 * assumptions about the underlying hash function. HMAC is an alternative
57 * formulation which doesn't require low-level access to the hash function's
58 * implementation. NMAC was designed to allow MD5 has a suitable underlying
59 * hash function, even though doubts were already being raised about its
60 * collision resistance.
d03ab969 61 */
62
79ba130c 63#ifndef CATACOMB_HMAC_H
64#define CATACOMB_HMAC_H
d03ab969 65
66#ifdef __cplusplus
67 extern "C" {
68#endif
69
70/*----- Header files ------------------------------------------------------*/
71
79ba130c 72#include <stddef.h>
d03ab969 73
74#include <mLib/bits.h>
75
79ba130c 76#ifndef CATACOMB_GMAC_H
77# include "gmac.h"
d03ab969 78#endif
79
80/*----- Macros ------------------------------------------------------------*/
81
82/* --- @HMAC_DECL@ --- *
83 *
84 * Arguments: @PRE@, @pre@ = prefixes for the underlying hash function
85 *
86 * Use: Creates declarations for the HMAC and NMAC functions.
87 */
88
89#define HMAC_DECL(PRE, pre) \
90 \
79ba130c 91/* --- An HMAC or NMAC key --- */ \
92 \
d03ab969 93typedef struct pre##_mackey { \
c850c0da 94 octet ochain[PRE##_STATESZ]; /* Chaining for outer hash */ \
79ba130c 95 unsigned ocount; /* Byte count for outer hash */ \
c850c0da 96 octet ichain[PRE##_STATESZ]; /* Chaining for inner hash */ \
79ba130c 97 unsigned icount; /* Byte count for inner hash */ \
d03ab969 98} pre##_mackey; \
99 \
79ba130c 100/* --- An HMAC or NMAC hashing context --- */ \
101 \
d03ab969 102typedef struct pre##_macctx { \
103 pre##_ctx ctx; /* Context for main hashing */ \
c850c0da 104 octet chain[PRE##_STATESZ]; /* Chaining for outer hash */ \
79ba130c 105 unsigned count; /* Byte count for outer hash */ \
d03ab969 106} pre##_macctx; \
107 \
6ced98bd 108/* --- Other useful constants --- */ \
109 \
110extern const octet pre##_mackeysz[]; \
111 \
79ba130c 112/* --- @pre_nmacinit@ --- * \
d03ab969 113 * \
114 * Arguments: @pre_macctx *key@ = pointer to a MAC key object \
115 * @const void *ok@ = pointer to outer hash init vector \
116 * @const void *ik@ = pointer to inner hash init vector \
117 * \
118 * Returns: --- \
119 * \
120 * Use: Initializes a MAC key for doing NMAC hashing. \
121 */ \
122 \
79ba130c 123extern void pre##_nmacinit(pre##_mackey */*key*/, \
124 const void */*ok*/, const void */*ik*/); \
d03ab969 125 \
79ba130c 126/* --- @pre_hmacinit@ --- * \
d03ab969 127 * \
128 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
129 * @const void *k@ = pointer to key to use \
130 * @size_t sz@ = size of key data \
131 * \
132 * Returns: --- \
133 * \
134 * Use: Initializes a MAC key for doing HMAC hashing. Keys \
135 * longer than the hash function's output size aren't very \
136 * useful, but are accepted. Keys longer than the hash's \
137 * block size are also accepted; they are hashed before \
138 * use, as specified in RFC2104. \
139 */ \
140 \
79ba130c 141extern void pre##_hmacinit(pre##_mackey */*key*/, \
142 const void */*k*/, size_t /*sz*/); \
d03ab969 143 \
36c67859 144/* --- @pre_sslmacinit@ --- * \
145 * \
146 * Arguments: @pre_mackey *key@ = pointer to MAC key object \
147 * @const void *k@ = pointer to key to use \
148 * @size_t sz@ = size of key data \
149 * \
150 * Returns: --- \
151 * \
152 * Use: Initializes a MAC key for doing hasing using the SSL3 \
153 * variant of HMAC. \
154 */ \
155 \
156extern void pre##_sslmacinit(pre##_mackey */*key*/, \
157 const void */*k*/, size_t /*sz*/); \
158 \
d03ab969 159/* --- @pre_macinit@ --- * \
160 * \
161 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
162 * @const pre_mackey *key@ = pointer to MAC key block \
163 * \
164 * Returns: --- \
165 * \
166 * Use: Instantiates a MAC context from a key block. \
167 */ \
168 \
79ba130c 169extern void pre##_macinit(pre##_macctx */*ctx*/, \
170 const pre##_mackey */*key*/); \
d03ab969 171 \
79ba130c 172/* --- @pre_machash@ --- * \
d03ab969 173 * \
174 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
175 * @const void *buf@ = pointer to buffer \
176 * @size_t sz@ = size of the buffer \
177 * \
178 * Returns: --- \
179 * \
180 * Use: Hashes a buffer. \
181 */ \
182 \
79ba130c 183extern void pre##_machash(pre##_macctx */*ctx*/, \
184 const void */*buf*/, size_t /*sz*/); \
d03ab969 185 \
186/* --- @pre_macdone@ --- * \
187 * \
188 * Arguments: @pre_macctx *ctx@ = pointer to MAC context block \
189 * @void *mac@ = pointer to buffer to receive MAC \
190 * \
191 * Returns: --- \
192 * \
193 * Use: Returns the result of a MAC computation. \
194 */ \
195 \
79ba130c 196extern void pre##_macdone(pre##_macctx */*ctx*/, void */*mac*/); \
d03ab969 197 \
79ba130c 198/* --- Generic MAC interface --- */ \
d03ab969 199 \
79ba130c 200extern const gcmac pre##_hmac;
d03ab969 201
202/*----- That's all, folks -------------------------------------------------*/
203
204#ifdef __cplusplus
205 }
206#endif
207
208#endif