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