Miniupnpd: update to 2.0
[tomato.git] / release / src / router / nettle / hmac.c
blob8c363b1365dea6659f59e49190f52ed9f194d619
1 /* hmac.c
3 * HMAC message authentication code (RFC-2104).
4 */
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2001 Niels Möller
9 *
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., 51 Franklin Street, Fifth Floor, Boston,
23 * MA 02111-1301, USA.
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <assert.h>
31 /* Needed for alloca on freebsd */
32 #include <stdlib.h>
33 #include <string.h>
35 #include "hmac.h"
37 #include "memxor.h"
38 #include "nettle-internal.h"
40 #define IPAD 0x36
41 #define OPAD 0x5c
43 void
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);
51 hash->init(outer);
52 hash->init(inner);
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);
62 hash->init(state);
63 hash->update(state, key_length, key);
64 hash->digest(state, hash->digest_size, digest);
66 key = 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);
85 void
86 hmac_update(void *state,
87 const struct nettle_hash *hash,
88 unsigned length, const uint8_t *data)
90 hash->update(state, length, data);
93 void
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);