umac-l3.c 2.17 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* umac-l3.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

   Copyright (C) 2013 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
31 32 33 34 35 36

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

#include "umac.h"
37
#include "umac-internal.h"
Niels Möller's avatar
Niels Möller committed
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

#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
86
_umac_l3 (const uint64_t *key, const uint64_t *m)
Niels Möller's avatar
Niels Möller committed
87
{
88 89 90
  uint32_t y = (umac_l3_word (key, m[0])
		+ umac_l3_word (key + 4, m[1])) % P;

Niels Möller's avatar
Niels Möller committed
91 92 93 94 95 96
#if !WORDS_BIGENDIAN
  y = ((ROTL32(8,  y) & 0x00FF00FFUL)
       | (ROTL32(24, y) & 0xFF00FF00UL));
#endif
  return y;
}