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