~ubuntu-branches/ubuntu/dapper/nettle/dapper

« back to all changes in this revision

Viewing changes to hmac.c

  • Committer: Bazaar Package Importer
  • Author(s): Marek Habersack
  • Date: 2004-05-04 15:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040504155602-7jbhw5mabvwksl3j
Tags: upstream-1.10
Import upstream version 1.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include <string.h>
 
32
 
 
33
#include "hmac.h"
 
34
 
 
35
#include "memxor.h"
 
36
#include "nettle-internal.h"
 
37
 
 
38
#define IPAD 0x36
 
39
#define OPAD 0x5c
 
40
 
 
41
void
 
42
hmac_set_key(void *outer, void *inner, void *state,
 
43
             const struct nettle_hash *hash,
 
44
             unsigned key_length, const uint8_t *key)
 
45
{
 
46
  TMP_DECL(pad, uint8_t, NETTLE_MAX_HASH_BLOCK_SIZE);
 
47
  TMP_ALLOC(pad, hash->block_size);
 
48
  
 
49
  hash->init(outer);
 
50
  hash->init(inner);
 
51
 
 
52
  if (key_length > hash->block_size)
 
53
    {
 
54
      /* Reduce key to the algorithm's hash size. Use the area pointed
 
55
       * to by state for the temporary state. */
 
56
 
 
57
      TMP_DECL(digest, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);
 
58
      TMP_ALLOC(digest, hash->digest_size);
 
59
 
 
60
      hash->init(state);
 
61
      hash->update(state, key_length, key);
 
62
      hash->digest(state, hash->digest_size, digest);
 
63
 
 
64
      key = digest;
 
65
      key_length = hash->digest_size;
 
66
    }
 
67
 
 
68
  assert(key_length <= hash->block_size);
 
69
  
 
70
  memset(pad, OPAD, hash->block_size);
 
71
  memxor(pad, key, key_length);
 
72
 
 
73
  hash->update(outer, hash->block_size, pad);
 
74
 
 
75
  memset(pad, IPAD, hash->block_size);
 
76
  memxor(pad, key, key_length);
 
77
 
 
78
  hash->update(inner, hash->block_size, pad);
 
79
 
 
80
  memcpy(state, inner, hash->context_size);
 
81
}
 
82
 
 
83
void
 
84
hmac_update(void *state,
 
85
            const struct nettle_hash *hash,
 
86
            unsigned length, const uint8_t *data)
 
87
{
 
88
  hash->update(state, length, data);
 
89
}
 
90
 
 
91
void
 
92
hmac_digest(const void *outer, const void *inner, void *state,
 
93
            const struct nettle_hash *hash,         
 
94
            unsigned length, uint8_t *dst)
 
95
{
 
96
  TMP_DECL(digest, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);
 
97
  TMP_ALLOC(digest, hash->digest_size);
 
98
 
 
99
  hash->digest(state, hash->digest_size, digest);
 
100
 
 
101
  memcpy(state, outer, hash->context_size);
 
102
 
 
103
  hash->update(state, hash->digest_size, digest);
 
104
  hash->digest(state, length, dst);
 
105
 
 
106
  memcpy(state, inner, hash->context_size);
 
107
}