3 * HMAC message authentication code (RFC-2104).
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2001 Niels Möller
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31 /* Needed for alloca on freebsd */
38 #include "nettle-internal.h"
44 hmac_set_key(void *outer, void *inner, void *state,
45 const struct nettle_hash *hash,
46 unsigned key_length, const uint8_t *key)
48 TMP_DECL(pad, uint8_t, NETTLE_MAX_HASH_BLOCK_SIZE);
49 TMP_ALLOC(pad, hash->block_size);
54 if (key_length > hash->block_size)
56 /* Reduce key to the algorithm's hash size. Use the area pointed
57 * to by state for the temporary state. */
59 TMP_DECL(digest, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);
60 TMP_ALLOC(digest, hash->digest_size);
63 hash->update(state, key_length, key);
64 hash->digest(state, hash->digest_size, digest);
67 key_length = hash->digest_size;
70 assert(key_length <= hash->block_size);
72 memset(pad, OPAD, hash->block_size);
73 memxor(pad, key, key_length);
75 hash->update(outer, hash->block_size, pad);
77 memset(pad, IPAD, hash->block_size);
78 memxor(pad, key, key_length);
80 hash->update(inner, hash->block_size, pad);
82 memcpy(state, inner, hash->context_size);
86 hmac_update(void *state,
87 const struct nettle_hash *hash,
88 unsigned length, const uint8_t *data)
90 hash->update(state, length, data);
94 hmac_digest(const void *outer, const void *inner, void *state,
95 const struct nettle_hash *hash,
96 unsigned length, uint8_t *dst)
98 TMP_DECL(digest, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);
99 TMP_ALLOC(digest, hash->digest_size);
101 hash->digest(state, hash->digest_size, digest);
103 memcpy(state, outer, hash->context_size);
105 hash->update(state, hash->digest_size, digest);
106 hash->digest(state, length, dst);
108 memcpy(state, inner, hash->context_size);