hmac.c 2.59 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
/* hmac.c
 *
Niels Möller's avatar
Niels Möller committed
3
 * HMAC message authentication code (RFC-2104).
Niels Möller's avatar
Niels Möller committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 */

/* nettle, low-level cryptographics library
 *
 * Copyright (C) 2001 Niels Mller
 *  
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

26
#if HAVE_CONFIG_H
27
# include "config.h"
28
#endif
Niels Möller's avatar
Niels Möller committed
29
30

#include <assert.h>
Niels Möller's avatar
Niels Möller committed
31
#include <string.h>
Niels Möller's avatar
Niels Möller committed
32

33
34
35
36
#include "hmac.h"

#include "memxor.h"

Niels Möller's avatar
Niels Möller committed
37
38
39
40
#define IPAD 0x36
#define OPAD 0x5c

void
Niels Möller's avatar
Niels Möller committed
41
42
43
hmac_set_key(void *outer, void *inner, void *state,
	     const struct nettle_hash *hash,
	     unsigned key_length, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
44
{
Niels Möller's avatar
Niels Möller committed
45
  uint8_t *pad = alloca(hash->block_size);
Niels Möller's avatar
Niels Möller committed
46
  
Niels Möller's avatar
Niels Möller committed
47
48
  hash->init(outer);
  hash->init(inner);
Niels Möller's avatar
Niels Möller committed
49

Niels Möller's avatar
Niels Möller committed
50
  if (key_length > hash->block_size)
Niels Möller's avatar
Niels Möller committed
51
52
53
54
    {
      /* Reduce key to the algorithm's hash size. Use the area pointed
       * to by state for the temporary state. */

Niels Möller's avatar
Niels Möller committed
55
      uint8_t *digest = alloca(hash->digest_size);
Niels Möller's avatar
Niels Möller committed
56

Niels Möller's avatar
Niels Möller committed
57
58
59
      hash->init(state);
      hash->update(state, key_length, key);
      hash->digest(state, hash->digest_size, digest);
Niels Möller's avatar
Niels Möller committed
60
61

      key = digest;
Niels Möller's avatar
Niels Möller committed
62
      key_length = hash->digest_size;
Niels Möller's avatar
Niels Möller committed
63
64
    }

Niels Möller's avatar
Niels Möller committed
65
  assert(key_length <= hash->block_size);
Niels Möller's avatar
Niels Möller committed
66
  
Niels Möller's avatar
Niels Möller committed
67
  memset(pad, OPAD, hash->block_size);
Niels Möller's avatar
Niels Möller committed
68
69
  memxor(pad, key, key_length);

Niels Möller's avatar
Niels Möller committed
70
  hash->update(outer, hash->block_size, pad);
Niels Möller's avatar
Niels Möller committed
71

Niels Möller's avatar
Niels Möller committed
72
  memset(pad, IPAD, hash->block_size);
Niels Möller's avatar
Niels Möller committed
73
74
  memxor(pad, key, key_length);

Niels Möller's avatar
Niels Möller committed
75
  hash->update(inner, hash->block_size, pad);
Niels Möller's avatar
Niels Möller committed
76

Niels Möller's avatar
Niels Möller committed
77
  memcpy(state, inner, hash->context_size);
Niels Möller's avatar
Niels Möller committed
78
79
80
}

void
Niels Möller's avatar
Niels Möller committed
81
82
83
84
85
86
87
88
89
90
hmac_update(void *state,
	    const struct nettle_hash *hash,
	    unsigned length, const uint8_t *data)
{
  hash->update(state, length, data);
}

void
hmac_digest(const void *outer, const void *inner, void *state,
	    const struct nettle_hash *hash, 	    
Niels Möller's avatar
Niels Möller committed
91
92
	    unsigned length, uint8_t *dst)
{
Niels Möller's avatar
Niels Möller committed
93
  uint8_t *digest = alloca(hash->digest_size);
Niels Möller's avatar
Niels Möller committed
94

Niels Möller's avatar
Niels Möller committed
95
  hash->digest(state, hash->digest_size, digest);
Niels Möller's avatar
Niels Möller committed
96

Niels Möller's avatar
Niels Möller committed
97
  memcpy(state, outer, hash->context_size);
Niels Möller's avatar
Niels Möller committed
98

Niels Möller's avatar
Niels Möller committed
99
100
  hash->update(state, hash->digest_size, digest);
  hash->digest(state, length, dst);
Niels Möller's avatar
Niels Möller committed
101

Niels Möller's avatar
Niels Möller committed
102
  memcpy(state, inner, hash->context_size);
Niels Möller's avatar
Niels Möller committed
103
}