3 HMAC message authentication code (RFC-2104).
5 Copyright (C) 2001 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
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.
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.
22 or both in parallel, as here.
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.
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/.
39 /* Needed for alloca on freebsd */
46 #include "nettle-internal.h"
52 hmac_set_key(void *outer, void *inner, void *state,
53 const struct nettle_hash *hash,
54 size_t key_length, const uint8_t *key)
56 TMP_DECL(pad, uint8_t, NETTLE_MAX_HASH_BLOCK_SIZE);
57 TMP_ALLOC(pad, hash->block_size);
62 if (key_length > hash->block_size)
64 /* Reduce key to the algorithm's hash size. Use the area pointed
65 * to by state for the temporary state. */
67 TMP_DECL(digest, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);
68 TMP_ALLOC(digest, hash->digest_size);
71 hash->update(state, key_length, key);
72 hash->digest(state, hash->digest_size, digest);
75 key_length = hash->digest_size;
78 assert(key_length <= hash->block_size);
80 memset(pad, OPAD, hash->block_size);
81 memxor(pad, key, key_length);
83 hash->update(outer, hash->block_size, pad);
85 memset(pad, IPAD, hash->block_size);
86 memxor(pad, key, key_length);
88 hash->update(inner, hash->block_size, pad);
90 memcpy(state, inner, hash->context_size);
94 hmac_update(void *state,
95 const struct nettle_hash *hash,
96 size_t length, const uint8_t *data)
98 hash->update(state, length, data);
102 hmac_digest(const void *outer, const void *inner, void *state,
103 const struct nettle_hash *hash,
104 size_t length, uint8_t *dst)
106 TMP_DECL(digest, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);
107 TMP_ALLOC(digest, hash->digest_size);
109 hash->digest(state, hash->digest_size, digest);
111 memcpy(state, outer, hash->context_size);
113 hash->update(state, hash->digest_size, digest);
114 hash->digest(state, length, dst);
116 memcpy(state, inner, hash->context_size);