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

   UMAC message authentication code (RFC-4418).

   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
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

#ifndef NETTLE_UMAC_H_INCLUDED
#define NETTLE_UMAC_H_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

/* Namespace mangling */
#define umac32_set_key  nettle_umac32_set_key
#define umac64_set_key  nettle_umac64_set_key
#define umac96_set_key  nettle_umac96_set_key
#define umac128_set_key nettle_umac128_set_key
#define umac32_set_nonce  nettle_umac32_set_nonce
#define umac64_set_nonce  nettle_umac64_set_nonce
#define umac96_set_nonce  nettle_umac96_set_nonce
#define umac128_set_nonce nettle_umac128_set_nonce
#define umac32_update  nettle_umac32_update
#define umac64_update  nettle_umac64_update
#define umac96_update  nettle_umac96_update
#define umac128_update nettle_umac128_update
#define umac32_digest  nettle_umac32_digest
#define umac64_digest  nettle_umac64_digest
#define umac96_digest  nettle_umac96_digest
#define umac128_digest nettle_umac128_digest
#define _umac_set_key _nettle_umac_set_key
#define _umac_nh _nettle_umac_nh
#define _umac_nh_n _nettle_umac_nh_n
#define _umac_poly64 _nettle_umac_poly64
#define _umac_poly128 _nettle_umac_poly128
#define _umac_l2_init _nettle_umac_l2_init
#define _umac_l2 _nettle_umac_l2
#define _umac_l2_final _nettle_umac_l2_final
#define _umac_l3_init _nettle_umac_l3_init
#define _umac_l3 _nettle_umac_l3

#include "nettle-types.h"
#include "aes.h"

72
#define UMAC_KEY_SIZE AES128_KEY_SIZE
73 74 75 76
#define UMAC32_DIGEST_SIZE 4
#define UMAC64_DIGEST_SIZE 8
#define UMAC96_DIGEST_SIZE 12
#define UMAC128_DIGEST_SIZE 16
77 78 79
#define UMAC_BLOCK_SIZE 1024
/* For backwards compatibility */
#define UMAC_DATA_SIZE UMAC_BLOCK_SIZE
Niels Möller's avatar
Niels Möller committed
80 81 82

/* Subkeys and state for UMAC with tag size 32*n bits. */
#define _UMAC_STATE(n)					\
83
  uint32_t l1_key[UMAC_BLOCK_SIZE/4 + 4*((n)-1)];	\
Niels Möller's avatar
Niels Möller committed
84 85 86 87 88
  /* Keys in 32-bit pieces, high first */		\
  uint32_t l2_key[6*(n)];				\
  uint64_t l3_key1[8*(n)];				\
  uint32_t l3_key2[(n)];				\
  /* AES cipher for encrypting the nonce */		\
89
  struct aes128_ctx pdf_key;				\
90 91 92 93
  /* The l2_state consists of 2*n uint64_t, for poly64	\
     and poly128 hashing, followed by n additional	\
     uint64_t used as an input buffer. */		\
  uint64_t l2_state[3*(n)];				\
Niels Möller's avatar
Niels Möller committed
94 95 96
  /* Input to the pdf_key, zero-padded and low bits	\
     cleared if appropriate. */				\
  uint8_t nonce[AES_BLOCK_SIZE];			\
97 98 99 100
  unsigned short nonce_length /* For incrementing */

  /* Buffering */ 
#define _UMAC_BUFFER					\
Niels Möller's avatar
Niels Möller committed
101
  unsigned index;					\
Niels Möller's avatar
Niels Möller committed
102
  /* Complete blocks processed */			\
103
  uint64_t count;					\
104
  uint8_t block[UMAC_BLOCK_SIZE]
105
  
Niels Möller's avatar
Niels Möller committed
106 107 108 109 110
#define _UMAC_NONCE_CACHED 0x80

struct umac32_ctx
{
  _UMAC_STATE(1);
111 112
  /* Low bits and cache flag. */
  unsigned short nonce_low;
Niels Möller's avatar
Niels Möller committed
113 114
  /* Previous padding block */
  uint32_t pad_cache[AES_BLOCK_SIZE / 4];
115
  _UMAC_BUFFER;
Niels Möller's avatar
Niels Möller committed
116 117 118 119 120
};

struct umac64_ctx
{
  _UMAC_STATE(2);
121 122
  /* Low bit and cache flag. */
  unsigned short nonce_low;
Niels Möller's avatar
Niels Möller committed
123 124
  /* Previous padding block */
  uint32_t pad_cache[AES_BLOCK_SIZE/4];
125
  _UMAC_BUFFER;
Niels Möller's avatar
Niels Möller committed
126 127 128 129 130
};

struct umac96_ctx
{
  _UMAC_STATE(3);
131
  _UMAC_BUFFER;
Niels Möller's avatar
Niels Möller committed
132 133 134 135 136
};

struct umac128_ctx
{
  _UMAC_STATE(4);
137
  _UMAC_BUFFER;
Niels Möller's avatar
Niels Möller committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
};

/* The _set_key function initialize the nonce to zero. */
void
umac32_set_key (struct umac32_ctx *ctx, const uint8_t *key);
void
umac64_set_key (struct umac64_ctx *ctx, const uint8_t *key);
void
umac96_set_key (struct umac96_ctx *ctx, const uint8_t *key);
void
umac128_set_key (struct umac128_ctx *ctx, const uint8_t *key);

/* Optional, if not used, messages get incrementing nonces starting from zero. */
void
umac32_set_nonce (struct umac32_ctx *ctx,
153
		  size_t nonce_length, const uint8_t *nonce);
Niels Möller's avatar
Niels Möller committed
154 155
void
umac64_set_nonce (struct umac64_ctx *ctx,
156
		  size_t nonce_length, const uint8_t *nonce);
Niels Möller's avatar
Niels Möller committed
157 158
void
umac96_set_nonce (struct umac96_ctx *ctx,
159
		  size_t nonce_length, const uint8_t *nonce);
Niels Möller's avatar
Niels Möller committed
160 161
void
umac128_set_nonce (struct umac128_ctx *ctx,
162
		   size_t nonce_length, const uint8_t *nonce);
Niels Möller's avatar
Niels Möller committed
163 164 165

void
umac32_update (struct umac32_ctx *ctx,
166
	       size_t length, const uint8_t *data);
Niels Möller's avatar
Niels Möller committed
167 168
void
umac64_update (struct umac64_ctx *ctx,
169
	       size_t length, const uint8_t *data);
Niels Möller's avatar
Niels Möller committed
170 171
void
umac96_update (struct umac96_ctx *ctx,
172
	       size_t length, const uint8_t *data);
Niels Möller's avatar
Niels Möller committed
173 174
void
umac128_update (struct umac128_ctx *ctx,
175
		size_t length, const uint8_t *data);
Niels Möller's avatar
Niels Möller committed
176 177 178 179

/* The _digest functions increment the nonce */
void
umac32_digest (struct umac32_ctx *ctx,
180
	       size_t length, uint8_t *digest);
Niels Möller's avatar
Niels Möller committed
181 182
void
umac64_digest (struct umac64_ctx *ctx,
183
	       size_t length, uint8_t *digest);
Niels Möller's avatar
Niels Möller committed
184 185
void
umac96_digest (struct umac96_ctx *ctx,
186
	       size_t length, uint8_t *digest);
Niels Möller's avatar
Niels Möller committed
187 188
void
umac128_digest (struct umac128_ctx *ctx,
189
		size_t length, uint8_t *digest);
Niels Möller's avatar
Niels Möller committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204


/* Internal functions */
#define UMAC_POLY64_BLOCKS 16384

#define UMAC_P64_OFFSET 59
#define UMAC_P64 (- (uint64_t) UMAC_P64_OFFSET)

#define UMAC_P128_OFFSET 159
#define UMAC_P128_HI (~(uint64_t) 0)
#define UMAC_P128_LO (-(uint64_t) UMAC_P128_OFFSET)

void
_umac_set_key (uint32_t *l1_key, uint32_t *l2_key,
	       uint64_t *l3_key1, uint32_t *l3_key2,
205
	       struct aes128_ctx *pad, const uint8_t *key, unsigned n);
Niels Möller's avatar
Niels Möller committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

uint64_t
_umac_nh (const uint32_t *key, unsigned length, const uint8_t *msg);

/* Equivalent to

   for (i = 0; i < n; i++)
     out[i] = _umac_nh (key + 4*i, length, msg);

   but processing input only once.
*/
void
_umac_nh_n (uint64_t *out, unsigned n, const uint32_t *key,
	    unsigned length, const uint8_t *msg);

/* Returns y*k + m (mod p), including "marker" processing. Return
   value is *not* in canonical representation, and must be normalized
   before the output is used. */
uint64_t
_umac_poly64 (uint32_t kh, uint32_t kl, uint64_t y, uint64_t m);

void
_umac_poly128 (const uint32_t *k, uint64_t *y, uint64_t mh, uint64_t ml);

void
_umac_l2_init (unsigned size, uint32_t *k);

void
_umac_l2(const uint32_t *key, uint64_t *state, unsigned n,
235
	 uint64_t count, const uint64_t *m);
Niels Möller's avatar
Niels Möller committed
236 237 238

void
_umac_l2_final(const uint32_t *key, uint64_t *state, unsigned n,
239
	       uint64_t count);
Niels Möller's avatar
Niels Möller committed
240 241 242 243 244

void
_umac_l3_init (unsigned size, uint64_t *k);

uint32_t
245
_umac_l3 (const uint64_t *key, const uint64_t *m);
Niels Möller's avatar
Niels Möller committed
246 247 248 249 250 251

#ifdef __cplusplus
}
#endif

#endif /* NETTLE_UMAC_H_INCLUDED */