summaryrefslogtreecommitdiff
path: root/umac-l3.c
blob: 3a896e504386fff78c5abfee42dda9a7385bdc49 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* umac-l3.c
 */

/* nettle, low-level cryptographics library
 *
 * Copyright (C) 2013 Niels Möller
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02111-1301, USA.
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "umac.h"

#include "macros.h"

/* 2^36 - 5 */
#define P 0x0000000FFFFFFFFBULL

#if WORDS_BIGENDIAN
#define BE_SWAP64(x) x
#else
#define BE_SWAP64(x)				\
  (((x & 0xff) << 56)				\
   | ((x & 0xff00) << 40)			\
   | ((x & 0xff0000) << 24)			\
   | ((x & 0xff000000) << 8)			\
   | ((x >> 8) & 0xff000000)			\
   | ((x >> 24) & 0xff0000)			\
   | ((x >> 40) & 0xff00)			\
   | (x >> 56) )
#endif

void
_umac_l3_init (unsigned size, uint64_t *k)
{
  unsigned i;
  for (i = 0; i < size; i++)
    {
      uint64_t w = k[i];
      w = BE_SWAP64 (w);
      k[i] = w % P;
    }
}

static uint64_t
umac_l3_word (const uint64_t *k, uint64_t w)
{
  unsigned i;
  uint64_t y;

  /* Since it's easiest to process the input word from the low end,
   * loop over keys in reverse order. */

  for (i = y = 0; i < 4; i++, w >>= 16)
    y += (w & 0xffff) * k[3-i];

  return y;
}

uint32_t
_umac_l3 (const uint64_t *key, const uint64_t *m)
{
  uint32_t y = (umac_l3_word (key, m[0])
		+ umac_l3_word (key + 4, m[1])) % P;

#if !WORDS_BIGENDIAN
  y = ((ROTL32(8,  y) & 0x00FF00FFUL)
       | (ROTL32(24, y) & 0xFF00FF00UL));
#endif
  return y;
}