hmac.c 2.54 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* hmac.c
 *
 * HMAC message authentication code.
 */

/* 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.
 */

#include "hmac.h"

#include "memxor.h"

#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

#define IPAD 0x36
#define OPAD 0x5c

void
Niels Möller's avatar
Niels Möller committed
37
38
39
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
40
{
Niels Möller's avatar
Niels Möller committed
41
  uint8_t *pad = alloca(hash->block_size);
Niels Möller's avatar
Niels Möller committed
42
  
Niels Möller's avatar
Niels Möller committed
43
44
  hash->init(outer);
  hash->init(inner);
Niels Möller's avatar
Niels Möller committed
45

Niels Möller's avatar
Niels Möller committed
46
  if (key_length > hash->block_size)
Niels Möller's avatar
Niels Möller committed
47
48
49
50
    {
      /* 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
51
      uint8_t *digest = alloca(hash->digest_size);
Niels Möller's avatar
Niels Möller committed
52

Niels Möller's avatar
Niels Möller committed
53
54
55
      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
56
57

      key = digest;
Niels Möller's avatar
Niels Möller committed
58
      key_length = hash->digest_size;
Niels Möller's avatar
Niels Möller committed
59
60
    }

Niels Möller's avatar
Niels Möller committed
61
  assert(key_length <= hash->block_size);
Niels Möller's avatar
Niels Möller committed
62
  
Niels Möller's avatar
Niels Möller committed
63
  memset(pad, OPAD, hash->block_size);
Niels Möller's avatar
Niels Möller committed
64
65
  memxor(pad, key, key_length);

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

Niels Möller's avatar
Niels Möller committed
68
  memset(pad, IPAD, hash->block_size);
Niels Möller's avatar
Niels Möller committed
69
70
  memxor(pad, key, key_length);

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

Niels Möller's avatar
Niels Möller committed
73
  memcpy(state, inner, hash->context_size);
Niels Möller's avatar
Niels Möller committed
74
75
76
}

void
Niels Möller's avatar
Niels Möller committed
77
78
79
80
81
82
83
84
85
86
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
87
88
	    unsigned length, uint8_t *dst)
{
Niels Möller's avatar
Niels Möller committed
89
  uint8_t *digest = alloca(hash->digest_size);
Niels Möller's avatar
Niels Möller committed
90

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

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

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

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