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,
36
#include "nettle-internal.h"
42
hmac_set_key(void *outer, void *inner, void *state,
43
const struct nettle_hash *hash,
44
unsigned key_length, const uint8_t *key)
46
TMP_DECL(pad, uint8_t, NETTLE_MAX_HASH_BLOCK_SIZE);
47
TMP_ALLOC(pad, hash->block_size);
52
if (key_length > hash->block_size)
54
/* Reduce key to the algorithm's hash size. Use the area pointed
55
* to by state for the temporary state. */
57
TMP_DECL(digest, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);
58
TMP_ALLOC(digest, hash->digest_size);
61
hash->update(state, key_length, key);
62
hash->digest(state, hash->digest_size, digest);
65
key_length = hash->digest_size;
68
assert(key_length <= hash->block_size);
70
memset(pad, OPAD, hash->block_size);
71
memxor(pad, key, key_length);
73
hash->update(outer, hash->block_size, pad);
75
memset(pad, IPAD, hash->block_size);
76
memxor(pad, key, key_length);
78
hash->update(inner, hash->block_size, pad);
80
memcpy(state, inner, hash->context_size);
84
hmac_update(void *state,
85
const struct nettle_hash *hash,
86
unsigned length, const uint8_t *data)
88
hash->update(state, length, data);
92
hmac_digest(const void *outer, const void *inner, void *state,
93
const struct nettle_hash *hash,
94
unsigned length, uint8_t *dst)
96
TMP_DECL(digest, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);
97
TMP_ALLOC(digest, hash->digest_size);
99
hash->digest(state, hash->digest_size, digest);
101
memcpy(state, outer, hash->context_size);
103
hash->update(state, hash->digest_size, digest);
104
hash->digest(state, length, dst);
106
memcpy(state, inner, hash->context_size);