hmac.c 2.93 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* hmac.c
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 31 32

   HMAC message authentication code (RFC-2104).

   Copyright (C) 2001 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle 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
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
Niels Möller's avatar
Niels Möller committed
33

34
#if HAVE_CONFIG_H
35
# include "config.h"
36
#endif
Niels Möller's avatar
Niels Möller committed
37 38

#include <assert.h>
39 40
/* Needed for alloca on freebsd */
#include <stdlib.h>
Niels Möller's avatar
Niels Möller committed
41
#include <string.h>
Niels Möller's avatar
Niels Möller committed
42

43 44 45
#include "hmac.h"

#include "memxor.h"
46
#include "nettle-internal.h"
47

Niels Möller's avatar
Niels Möller committed
48 49 50 51
#define IPAD 0x36
#define OPAD 0x5c

void
Niels Möller's avatar
Niels Möller committed
52 53
hmac_set_key(void *outer, void *inner, void *state,
	     const struct nettle_hash *hash,
54
	     size_t key_length, const uint8_t *key)
Niels Möller's avatar
Niels Möller committed
55
{
56 57
  TMP_DECL(pad, uint8_t, NETTLE_MAX_HASH_BLOCK_SIZE);
  TMP_ALLOC(pad, hash->block_size);
Niels Möller's avatar
Niels Möller committed
58
  
Niels Möller's avatar
Niels Möller committed
59 60
  hash->init(outer);
  hash->init(inner);
Niels Möller's avatar
Niels Möller committed
61

Niels Möller's avatar
Niels Möller committed
62
  if (key_length > hash->block_size)
Niels Möller's avatar
Niels Möller committed
63 64 65 66
    {
      /* Reduce key to the algorithm's hash size. Use the area pointed
       * to by state for the temporary state. */

67 68
      TMP_DECL(digest, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);
      TMP_ALLOC(digest, hash->digest_size);
Niels Möller's avatar
Niels Möller committed
69

Niels Möller's avatar
Niels Möller committed
70 71 72
      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
73 74

      key = digest;
Niels Möller's avatar
Niels Möller committed
75
      key_length = hash->digest_size;
Niels Möller's avatar
Niels Möller committed
76 77
    }

Niels Möller's avatar
Niels Möller committed
78
  assert(key_length <= hash->block_size);
Niels Möller's avatar
Niels Möller committed
79
  
Niels Möller's avatar
Niels Möller committed
80
  memset(pad, OPAD, hash->block_size);
Niels Möller's avatar
Niels Möller committed
81 82
  memxor(pad, key, key_length);

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

Niels Möller's avatar
Niels Möller committed
85
  memset(pad, IPAD, hash->block_size);
Niels Möller's avatar
Niels Möller committed
86 87
  memxor(pad, key, key_length);

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

Niels Möller's avatar
Niels Möller committed
90
  memcpy(state, inner, hash->context_size);
Niels Möller's avatar
Niels Möller committed
91 92 93
}

void
Niels Möller's avatar
Niels Möller committed
94 95
hmac_update(void *state,
	    const struct nettle_hash *hash,
96
	    size_t length, const uint8_t *data)
Niels Möller's avatar
Niels Möller committed
97 98 99 100 101 102 103
{
  hash->update(state, length, data);
}

void
hmac_digest(const void *outer, const void *inner, void *state,
	    const struct nettle_hash *hash, 	    
104
	    size_t length, uint8_t *dst)
Niels Möller's avatar
Niels Möller committed
105
{
106 107
  TMP_DECL(digest, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);
  TMP_ALLOC(digest, hash->digest_size);
Niels Möller's avatar
Niels Möller committed
108

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

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

Niels Möller's avatar
Niels Möller committed
113 114
  hash->update(state, hash->digest_size, digest);
  hash->digest(state, length, dst);
Niels Möller's avatar
Niels Möller committed
115

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