[Title] Add packaging/nettle.spec to build nettle on OBS system
[external/nettle.git] / hmac.c
1 /* hmac.c
2  *
3  * HMAC message authentication code (RFC-2104).
4  */
5
6 /* nettle, low-level cryptographics library
7  *
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.
14  * 
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.
19  * 
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,
23  * MA 02111-1307, USA.
24  */
25
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <assert.h>
31 /* Needed for alloca on freebsd */
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "hmac.h"
36
37 #include "memxor.h"
38 #include "nettle-internal.h"
39
40 #define IPAD 0x36
41 #define OPAD 0x5c
42
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)
47 {
48   TMP_DECL(pad, uint8_t, NETTLE_MAX_HASH_BLOCK_SIZE);
49   TMP_ALLOC(pad, hash->block_size);
50   
51   hash->init(outer);
52   hash->init(inner);
53
54   if (key_length > hash->block_size)
55     {
56       /* Reduce key to the algorithm's hash size. Use the area pointed
57        * to by state for the temporary state. */
58
59       TMP_DECL(digest, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);
60       TMP_ALLOC(digest, hash->digest_size);
61
62       hash->init(state);
63       hash->update(state, key_length, key);
64       hash->digest(state, hash->digest_size, digest);
65
66       key = digest;
67       key_length = hash->digest_size;
68     }
69
70   assert(key_length <= hash->block_size);
71   
72   memset(pad, OPAD, hash->block_size);
73   memxor(pad, key, key_length);
74
75   hash->update(outer, hash->block_size, pad);
76
77   memset(pad, IPAD, hash->block_size);
78   memxor(pad, key, key_length);
79
80   hash->update(inner, hash->block_size, pad);
81
82   memcpy(state, inner, hash->context_size);
83 }
84
85 void
86 hmac_update(void *state,
87             const struct nettle_hash *hash,
88             unsigned length, const uint8_t *data)
89 {
90   hash->update(state, length, data);
91 }
92
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)
97 {
98   TMP_DECL(digest, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);
99   TMP_ALLOC(digest, hash->digest_size);
100
101   hash->digest(state, hash->digest_size, digest);
102
103   memcpy(state, outer, hash->context_size);
104
105   hash->update(state, hash->digest_size, digest);
106   hash->digest(state, length, dst);
107
108   memcpy(state, inner, hash->context_size);
109 }