blob: 0cb64321ba53d90b93cfa0facec3afbfa57960a0 [file] [log] [blame]
Damien Miller88856692014-04-20 13:33:19 +10001/* $OpenBSD: umac.c,v 1.9 2014/04/20 02:30:25 djm Exp $ */
Damien Millere45796f2007-06-11 14:01:42 +10002/* -----------------------------------------------------------------------
3 *
4 * umac.c -- C Implementation UMAC Message Authentication
5 *
6 * Version 0.93b of rfc4418.txt -- 2006 July 18
7 *
8 * For a full description of UMAC message authentication see the UMAC
9 * world-wide-web page at http://www.cs.ucdavis.edu/~rogaway/umac
10 * Please report bugs and suggestions to the UMAC webpage.
11 *
12 * Copyright (c) 1999-2006 Ted Krovetz
13 *
14 * Permission to use, copy, modify, and distribute this software and
15 * its documentation for any purpose and with or without fee, is hereby
16 * granted provided that the above copyright notice appears in all copies
17 * and in supporting documentation, and that the name of the copyright
18 * holder not be used in advertising or publicity pertaining to
19 * distribution of the software without specific, written prior permission.
20 *
21 * Comments should be directed to Ted Krovetz (tdk@acm.org)
22 *
23 * ---------------------------------------------------------------------- */
24
25 /* ////////////////////// IMPORTANT NOTES /////////////////////////////////
26 *
27 * 1) This version does not work properly on messages larger than 16MB
28 *
29 * 2) If you set the switch to use SSE2, then all data must be 16-byte
30 * aligned
31 *
32 * 3) When calling the function umac(), it is assumed that msg is in
33 * a writable buffer of length divisible by 32 bytes. The message itself
34 * does not have to fill the entire buffer, but bytes beyond msg may be
35 * zeroed.
36 *
37 * 4) Three free AES implementations are supported by this implementation of
38 * UMAC. Paulo Barreto's version is in the public domain and can be found
39 * at http://www.esat.kuleuven.ac.be/~rijmen/rijndael/ (search for
40 * "Barreto"). The only two files needed are rijndael-alg-fst.c and
41 * rijndael-alg-fst.h. Brian Gladman's version is distributed with the GNU
42 * Public lisence at http://fp.gladman.plus.com/AES/index.htm. It
43 * includes a fast IA-32 assembly version. The OpenSSL crypo library is
44 * the third.
45 *
46 * 5) With FORCE_C_ONLY flags set to 0, incorrect results are sometimes
47 * produced under gcc with optimizations set -O3 or higher. Dunno why.
48 *
49 /////////////////////////////////////////////////////////////////////// */
50
51/* ---------------------------------------------------------------------- */
52/* --- User Switches ---------------------------------------------------- */
53/* ---------------------------------------------------------------------- */
54
Darren Tucker992faad2012-10-05 11:38:24 +100055#ifndef UMAC_OUTPUT_LEN
Damien Millere45796f2007-06-11 14:01:42 +100056#define UMAC_OUTPUT_LEN 8 /* Alowable: 4, 8, 12, 16 */
Darren Tucker992faad2012-10-05 11:38:24 +100057#endif
Darren Tucker50ce4472012-10-05 12:11:33 +100058
59#if UMAC_OUTPUT_LEN != 4 && UMAC_OUTPUT_LEN != 8 && \
60 UMAC_OUTPUT_LEN != 12 && UMAC_OUTPUT_LEN != 16
61# error UMAC_OUTPUT_LEN must be defined to 4, 8, 12 or 16
62#endif
63
Damien Millere45796f2007-06-11 14:01:42 +100064/* #define FORCE_C_ONLY 1 ANSI C and 64-bit integers req'd */
65/* #define AES_IMPLEMENTAION 1 1 = OpenSSL, 2 = Barreto, 3 = Gladman */
66/* #define SSE2 0 Is SSE2 is available? */
67/* #define RUN_TESTS 0 Run basic correctness/speed tests */
68/* #define UMAC_AE_SUPPORT 0 Enable auhthenticated encrytion */
69
70/* ---------------------------------------------------------------------- */
71/* -- Global Includes --------------------------------------------------- */
72/* ---------------------------------------------------------------------- */
73
74#include "includes.h"
75#include <sys/types.h>
Damien Miller88856692014-04-20 13:33:19 +100076#include <string.h>
77#include <stdio.h>
78#include <stdlib.h>
79#include <stddef.h>
Damien Millere45796f2007-06-11 14:01:42 +100080
Damien Miller83e04f22007-09-17 16:11:01 +100081#include "xmalloc.h"
Damien Millere45796f2007-06-11 14:01:42 +100082#include "umac.h"
Damien Miller88856692014-04-20 13:33:19 +100083#include "misc.h"
Damien Millere45796f2007-06-11 14:01:42 +100084
85/* ---------------------------------------------------------------------- */
86/* --- Primitive Data Types --- */
87/* ---------------------------------------------------------------------- */
88
89/* The following assumptions may need change on your system */
90typedef u_int8_t UINT8; /* 1 byte */
91typedef u_int16_t UINT16; /* 2 byte */
92typedef u_int32_t UINT32; /* 4 byte */
93typedef u_int64_t UINT64; /* 8 bytes */
94typedef unsigned int UWORD; /* Register */
95
96/* ---------------------------------------------------------------------- */
97/* --- Constants -------------------------------------------------------- */
98/* ---------------------------------------------------------------------- */
99
100#define UMAC_KEY_LEN 16 /* UMAC takes 16 bytes of external key */
101
102/* Message "words" are read from memory in an endian-specific manner. */
103/* For this implementation to behave correctly, __LITTLE_ENDIAN__ must */
104/* be set true if the host computer is little-endian. */
105
106#if BYTE_ORDER == LITTLE_ENDIAN
107#define __LITTLE_ENDIAN__ 1
108#else
109#define __LITTLE_ENDIAN__ 0
110#endif
111
112/* ---------------------------------------------------------------------- */
113/* ---------------------------------------------------------------------- */
114/* ----- Architecture Specific ------------------------------------------ */
115/* ---------------------------------------------------------------------- */
116/* ---------------------------------------------------------------------- */
117
118
119/* ---------------------------------------------------------------------- */
120/* ---------------------------------------------------------------------- */
121/* ----- Primitive Routines --------------------------------------------- */
122/* ---------------------------------------------------------------------- */
123/* ---------------------------------------------------------------------- */
124
125
126/* ---------------------------------------------------------------------- */
127/* --- 32-bit by 32-bit to 64-bit Multiplication ------------------------ */
128/* ---------------------------------------------------------------------- */
129
130#define MUL64(a,b) ((UINT64)((UINT64)(UINT32)(a) * (UINT64)(UINT32)(b)))
131
132/* ---------------------------------------------------------------------- */
133/* --- Endian Conversion --- Forcing assembly on some platforms */
134/* ---------------------------------------------------------------------- */
135
Damien Millere45796f2007-06-11 14:01:42 +1000136#if (__LITTLE_ENDIAN__)
Damien Miller88856692014-04-20 13:33:19 +1000137#define LOAD_UINT32_REVERSED(p) get_u32(p)
138#define STORE_UINT32_REVERSED(p,v) put_u32(p,v)
Damien Millere45796f2007-06-11 14:01:42 +1000139#else
Damien Miller88856692014-04-20 13:33:19 +1000140#define LOAD_UINT32_REVERSED(p) get_u32_le(p)
141#define STORE_UINT32_REVERSED(p,v) put_u32_le(p,v)
Damien Millere45796f2007-06-11 14:01:42 +1000142#endif
143
Damien Miller88856692014-04-20 13:33:19 +1000144#define LOAD_UINT32_LITTLE(p) (get_u32_le(p))
145#define STORE_UINT32_BIG(p,v) put_u32(p, v)
146
Damien Millere45796f2007-06-11 14:01:42 +1000147/* ---------------------------------------------------------------------- */
148/* ---------------------------------------------------------------------- */
149/* ----- Begin KDF & PDF Section ---------------------------------------- */
150/* ---------------------------------------------------------------------- */
151/* ---------------------------------------------------------------------- */
152
153/* UMAC uses AES with 16 byte block and key lengths */
154#define AES_BLOCK_LEN 16
155
156/* OpenSSL's AES */
Darren Tuckercb520172007-06-14 23:21:32 +1000157#include "openbsd-compat/openssl-compat.h"
158#ifndef USE_BUILTIN_RIJNDAEL
159# include <openssl/aes.h>
160#endif
Damien Millere45796f2007-06-11 14:01:42 +1000161typedef AES_KEY aes_int_key[1];
162#define aes_encryption(in,out,int_key) \
163 AES_encrypt((u_char *)(in),(u_char *)(out),(AES_KEY *)int_key)
164#define aes_key_setup(key,int_key) \
Damien Millerc331dbd2013-07-25 11:55:20 +1000165 AES_set_encrypt_key((const u_char *)(key),UMAC_KEY_LEN*8,int_key)
Damien Millere45796f2007-06-11 14:01:42 +1000166
167/* The user-supplied UMAC key is stretched using AES in a counter
168 * mode to supply all random bits needed by UMAC. The kdf function takes
169 * an AES internal key representation 'key' and writes a stream of
Damien Miller36d70562008-07-14 12:04:43 +1000170 * 'nbytes' bytes to the memory pointed at by 'bufp'. Each distinct
Damien Millere45796f2007-06-11 14:01:42 +1000171 * 'ndx' causes a distinct byte stream.
172 */
Damien Miller36d70562008-07-14 12:04:43 +1000173static void kdf(void *bufp, aes_int_key key, UINT8 ndx, int nbytes)
Damien Millere45796f2007-06-11 14:01:42 +1000174{
175 UINT8 in_buf[AES_BLOCK_LEN] = {0};
176 UINT8 out_buf[AES_BLOCK_LEN];
Damien Miller36d70562008-07-14 12:04:43 +1000177 UINT8 *dst_buf = (UINT8 *)bufp;
Damien Millere45796f2007-06-11 14:01:42 +1000178 int i;
179
180 /* Setup the initial value */
181 in_buf[AES_BLOCK_LEN-9] = ndx;
182 in_buf[AES_BLOCK_LEN-1] = i = 1;
183
184 while (nbytes >= AES_BLOCK_LEN) {
185 aes_encryption(in_buf, out_buf, key);
186 memcpy(dst_buf,out_buf,AES_BLOCK_LEN);
187 in_buf[AES_BLOCK_LEN-1] = ++i;
188 nbytes -= AES_BLOCK_LEN;
189 dst_buf += AES_BLOCK_LEN;
190 }
191 if (nbytes) {
192 aes_encryption(in_buf, out_buf, key);
193 memcpy(dst_buf,out_buf,nbytes);
194 }
195}
196
197/* The final UHASH result is XOR'd with the output of a pseudorandom
198 * function. Here, we use AES to generate random output and
199 * xor the appropriate bytes depending on the last bits of nonce.
200 * This scheme is optimized for sequential, increasing big-endian nonces.
201 */
202
203typedef struct {
204 UINT8 cache[AES_BLOCK_LEN]; /* Previous AES output is saved */
205 UINT8 nonce[AES_BLOCK_LEN]; /* The AES input making above cache */
206 aes_int_key prf_key; /* Expanded AES key for PDF */
207} pdf_ctx;
208
209static void pdf_init(pdf_ctx *pc, aes_int_key prf_key)
210{
211 UINT8 buf[UMAC_KEY_LEN];
212
213 kdf(buf, prf_key, 0, UMAC_KEY_LEN);
214 aes_key_setup(buf, pc->prf_key);
215
216 /* Initialize pdf and cache */
217 memset(pc->nonce, 0, sizeof(pc->nonce));
218 aes_encryption(pc->nonce, pc->cache, pc->prf_key);
219}
220
Damien Millerc331dbd2013-07-25 11:55:20 +1000221static void pdf_gen_xor(pdf_ctx *pc, const UINT8 nonce[8], UINT8 buf[8])
Damien Millere45796f2007-06-11 14:01:42 +1000222{
223 /* 'ndx' indicates that we'll be using the 0th or 1st eight bytes
224 * of the AES output. If last time around we returned the ndx-1st
225 * element, then we may have the result in the cache already.
226 */
227
228#if (UMAC_OUTPUT_LEN == 4)
229#define LOW_BIT_MASK 3
230#elif (UMAC_OUTPUT_LEN == 8)
231#define LOW_BIT_MASK 1
232#elif (UMAC_OUTPUT_LEN > 8)
233#define LOW_BIT_MASK 0
234#endif
Damien Miller32ecfa02013-07-20 13:22:13 +1000235 union {
236 UINT8 tmp_nonce_lo[4];
237 UINT32 align;
238 } t;
Damien Millere45796f2007-06-11 14:01:42 +1000239#if LOW_BIT_MASK != 0
240 int ndx = nonce[7] & LOW_BIT_MASK;
241#endif
Damien Millerc331dbd2013-07-25 11:55:20 +1000242 *(UINT32 *)t.tmp_nonce_lo = ((const UINT32 *)nonce)[1];
Damien Miller32ecfa02013-07-20 13:22:13 +1000243 t.tmp_nonce_lo[3] &= ~LOW_BIT_MASK; /* zero last bit */
Damien Millere45796f2007-06-11 14:01:42 +1000244
Damien Miller32ecfa02013-07-20 13:22:13 +1000245 if ( (((UINT32 *)t.tmp_nonce_lo)[0] != ((UINT32 *)pc->nonce)[1]) ||
Damien Millerc331dbd2013-07-25 11:55:20 +1000246 (((const UINT32 *)nonce)[0] != ((UINT32 *)pc->nonce)[0]) )
Damien Millere45796f2007-06-11 14:01:42 +1000247 {
Damien Millerc331dbd2013-07-25 11:55:20 +1000248 ((UINT32 *)pc->nonce)[0] = ((const UINT32 *)nonce)[0];
Damien Miller32ecfa02013-07-20 13:22:13 +1000249 ((UINT32 *)pc->nonce)[1] = ((UINT32 *)t.tmp_nonce_lo)[0];
Damien Millere45796f2007-06-11 14:01:42 +1000250 aes_encryption(pc->nonce, pc->cache, pc->prf_key);
251 }
252
253#if (UMAC_OUTPUT_LEN == 4)
254 *((UINT32 *)buf) ^= ((UINT32 *)pc->cache)[ndx];
255#elif (UMAC_OUTPUT_LEN == 8)
256 *((UINT64 *)buf) ^= ((UINT64 *)pc->cache)[ndx];
257#elif (UMAC_OUTPUT_LEN == 12)
258 ((UINT64 *)buf)[0] ^= ((UINT64 *)pc->cache)[0];
259 ((UINT32 *)buf)[2] ^= ((UINT32 *)pc->cache)[2];
260#elif (UMAC_OUTPUT_LEN == 16)
261 ((UINT64 *)buf)[0] ^= ((UINT64 *)pc->cache)[0];
262 ((UINT64 *)buf)[1] ^= ((UINT64 *)pc->cache)[1];
263#endif
264}
265
266/* ---------------------------------------------------------------------- */
267/* ---------------------------------------------------------------------- */
268/* ----- Begin NH Hash Section ------------------------------------------ */
269/* ---------------------------------------------------------------------- */
270/* ---------------------------------------------------------------------- */
271
272/* The NH-based hash functions used in UMAC are described in the UMAC paper
273 * and specification, both of which can be found at the UMAC website.
274 * The interface to this implementation has two
275 * versions, one expects the entire message being hashed to be passed
276 * in a single buffer and returns the hash result immediately. The second
277 * allows the message to be passed in a sequence of buffers. In the
278 * muliple-buffer interface, the client calls the routine nh_update() as
279 * many times as necessary. When there is no more data to be fed to the
280 * hash, the client calls nh_final() which calculates the hash output.
281 * Before beginning another hash calculation the nh_reset() routine
282 * must be called. The single-buffer routine, nh(), is equivalent to
283 * the sequence of calls nh_update() and nh_final(); however it is
284 * optimized and should be prefered whenever the multiple-buffer interface
285 * is not necessary. When using either interface, it is the client's
286 * responsability to pass no more than L1_KEY_LEN bytes per hash result.
287 *
288 * The routine nh_init() initializes the nh_ctx data structure and
289 * must be called once, before any other PDF routine.
290 */
291
292 /* The "nh_aux" routines do the actual NH hashing work. They
293 * expect buffers to be multiples of L1_PAD_BOUNDARY. These routines
294 * produce output for all STREAMS NH iterations in one call,
295 * allowing the parallel implementation of the streams.
296 */
297
298#define STREAMS (UMAC_OUTPUT_LEN / 4) /* Number of times hash is applied */
299#define L1_KEY_LEN 1024 /* Internal key bytes */
300#define L1_KEY_SHIFT 16 /* Toeplitz key shift between streams */
301#define L1_PAD_BOUNDARY 32 /* pad message to boundary multiple */
302#define ALLOC_BOUNDARY 16 /* Keep buffers aligned to this */
303#define HASH_BUF_BYTES 64 /* nh_aux_hb buffer multiple */
304
305typedef struct {
306 UINT8 nh_key [L1_KEY_LEN + L1_KEY_SHIFT * (STREAMS - 1)]; /* NH Key */
Darren Tucker8a057952011-11-04 10:53:31 +1100307 UINT8 data [HASH_BUF_BYTES]; /* Incoming data buffer */
Damien Millere45796f2007-06-11 14:01:42 +1000308 int next_data_empty; /* Bookeeping variable for data buffer. */
309 int bytes_hashed; /* Bytes (out of L1_KEY_LEN) incorperated. */
310 UINT64 state[STREAMS]; /* on-line state */
311} nh_ctx;
312
313
314#if (UMAC_OUTPUT_LEN == 4)
315
Damien Millerc331dbd2013-07-25 11:55:20 +1000316static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
Damien Millere45796f2007-06-11 14:01:42 +1000317/* NH hashing primitive. Previous (partial) hash result is loaded and
318* then stored via hp pointer. The length of the data pointed at by "dp",
319* "dlen", is guaranteed to be divisible by L1_PAD_BOUNDARY (32). Key
320* is expected to be endian compensated in memory at key setup.
321*/
322{
323 UINT64 h;
324 UWORD c = dlen / 32;
325 UINT32 *k = (UINT32 *)kp;
Damien Millerc331dbd2013-07-25 11:55:20 +1000326 const UINT32 *d = (const UINT32 *)dp;
Damien Millere45796f2007-06-11 14:01:42 +1000327 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
328 UINT32 k0,k1,k2,k3,k4,k5,k6,k7;
329
330 h = *((UINT64 *)hp);
331 do {
332 d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1);
333 d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3);
334 d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5);
335 d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7);
336 k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
337 k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
338 h += MUL64((k0 + d0), (k4 + d4));
339 h += MUL64((k1 + d1), (k5 + d5));
340 h += MUL64((k2 + d2), (k6 + d6));
341 h += MUL64((k3 + d3), (k7 + d7));
342
343 d += 8;
344 k += 8;
345 } while (--c);
346 *((UINT64 *)hp) = h;
347}
348
349#elif (UMAC_OUTPUT_LEN == 8)
350
Damien Millerc331dbd2013-07-25 11:55:20 +1000351static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
Damien Millere45796f2007-06-11 14:01:42 +1000352/* Same as previous nh_aux, but two streams are handled in one pass,
353 * reading and writing 16 bytes of hash-state per call.
354 */
355{
356 UINT64 h1,h2;
357 UWORD c = dlen / 32;
358 UINT32 *k = (UINT32 *)kp;
Damien Millerc331dbd2013-07-25 11:55:20 +1000359 const UINT32 *d = (const UINT32 *)dp;
Damien Millere45796f2007-06-11 14:01:42 +1000360 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
361 UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
362 k8,k9,k10,k11;
363
364 h1 = *((UINT64 *)hp);
365 h2 = *((UINT64 *)hp + 1);
366 k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
367 do {
368 d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1);
369 d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3);
370 d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5);
371 d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7);
372 k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
373 k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11);
374
375 h1 += MUL64((k0 + d0), (k4 + d4));
376 h2 += MUL64((k4 + d0), (k8 + d4));
377
378 h1 += MUL64((k1 + d1), (k5 + d5));
379 h2 += MUL64((k5 + d1), (k9 + d5));
380
381 h1 += MUL64((k2 + d2), (k6 + d6));
382 h2 += MUL64((k6 + d2), (k10 + d6));
383
384 h1 += MUL64((k3 + d3), (k7 + d7));
385 h2 += MUL64((k7 + d3), (k11 + d7));
386
387 k0 = k8; k1 = k9; k2 = k10; k3 = k11;
388
389 d += 8;
390 k += 8;
391 } while (--c);
392 ((UINT64 *)hp)[0] = h1;
393 ((UINT64 *)hp)[1] = h2;
394}
395
396#elif (UMAC_OUTPUT_LEN == 12)
397
Damien Millerc331dbd2013-07-25 11:55:20 +1000398static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
Damien Millere45796f2007-06-11 14:01:42 +1000399/* Same as previous nh_aux, but two streams are handled in one pass,
400 * reading and writing 24 bytes of hash-state per call.
401*/
402{
403 UINT64 h1,h2,h3;
404 UWORD c = dlen / 32;
405 UINT32 *k = (UINT32 *)kp;
Damien Millerc331dbd2013-07-25 11:55:20 +1000406 const UINT32 *d = (const UINT32 *)dp;
Damien Millere45796f2007-06-11 14:01:42 +1000407 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
408 UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
409 k8,k9,k10,k11,k12,k13,k14,k15;
410
411 h1 = *((UINT64 *)hp);
412 h2 = *((UINT64 *)hp + 1);
413 h3 = *((UINT64 *)hp + 2);
414 k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
415 k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
416 do {
417 d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1);
418 d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3);
419 d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5);
420 d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7);
421 k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11);
422 k12 = *(k+12); k13 = *(k+13); k14 = *(k+14); k15 = *(k+15);
423
424 h1 += MUL64((k0 + d0), (k4 + d4));
425 h2 += MUL64((k4 + d0), (k8 + d4));
426 h3 += MUL64((k8 + d0), (k12 + d4));
427
428 h1 += MUL64((k1 + d1), (k5 + d5));
429 h2 += MUL64((k5 + d1), (k9 + d5));
430 h3 += MUL64((k9 + d1), (k13 + d5));
431
432 h1 += MUL64((k2 + d2), (k6 + d6));
433 h2 += MUL64((k6 + d2), (k10 + d6));
434 h3 += MUL64((k10 + d2), (k14 + d6));
435
436 h1 += MUL64((k3 + d3), (k7 + d7));
437 h2 += MUL64((k7 + d3), (k11 + d7));
438 h3 += MUL64((k11 + d3), (k15 + d7));
439
440 k0 = k8; k1 = k9; k2 = k10; k3 = k11;
441 k4 = k12; k5 = k13; k6 = k14; k7 = k15;
442
443 d += 8;
444 k += 8;
445 } while (--c);
446 ((UINT64 *)hp)[0] = h1;
447 ((UINT64 *)hp)[1] = h2;
448 ((UINT64 *)hp)[2] = h3;
449}
450
451#elif (UMAC_OUTPUT_LEN == 16)
452
Damien Millerc331dbd2013-07-25 11:55:20 +1000453static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
Damien Millere45796f2007-06-11 14:01:42 +1000454/* Same as previous nh_aux, but two streams are handled in one pass,
455 * reading and writing 24 bytes of hash-state per call.
456*/
457{
458 UINT64 h1,h2,h3,h4;
459 UWORD c = dlen / 32;
460 UINT32 *k = (UINT32 *)kp;
Damien Millerc331dbd2013-07-25 11:55:20 +1000461 const UINT32 *d = (const UINT32 *)dp;
Damien Millere45796f2007-06-11 14:01:42 +1000462 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
463 UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
464 k8,k9,k10,k11,k12,k13,k14,k15,
465 k16,k17,k18,k19;
466
467 h1 = *((UINT64 *)hp);
468 h2 = *((UINT64 *)hp + 1);
469 h3 = *((UINT64 *)hp + 2);
470 h4 = *((UINT64 *)hp + 3);
471 k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
472 k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
473 do {
474 d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1);
475 d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3);
476 d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5);
477 d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7);
478 k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11);
479 k12 = *(k+12); k13 = *(k+13); k14 = *(k+14); k15 = *(k+15);
480 k16 = *(k+16); k17 = *(k+17); k18 = *(k+18); k19 = *(k+19);
481
482 h1 += MUL64((k0 + d0), (k4 + d4));
483 h2 += MUL64((k4 + d0), (k8 + d4));
484 h3 += MUL64((k8 + d0), (k12 + d4));
485 h4 += MUL64((k12 + d0), (k16 + d4));
486
487 h1 += MUL64((k1 + d1), (k5 + d5));
488 h2 += MUL64((k5 + d1), (k9 + d5));
489 h3 += MUL64((k9 + d1), (k13 + d5));
490 h4 += MUL64((k13 + d1), (k17 + d5));
491
492 h1 += MUL64((k2 + d2), (k6 + d6));
493 h2 += MUL64((k6 + d2), (k10 + d6));
494 h3 += MUL64((k10 + d2), (k14 + d6));
495 h4 += MUL64((k14 + d2), (k18 + d6));
496
497 h1 += MUL64((k3 + d3), (k7 + d7));
498 h2 += MUL64((k7 + d3), (k11 + d7));
499 h3 += MUL64((k11 + d3), (k15 + d7));
500 h4 += MUL64((k15 + d3), (k19 + d7));
501
502 k0 = k8; k1 = k9; k2 = k10; k3 = k11;
503 k4 = k12; k5 = k13; k6 = k14; k7 = k15;
504 k8 = k16; k9 = k17; k10 = k18; k11 = k19;
505
506 d += 8;
507 k += 8;
508 } while (--c);
509 ((UINT64 *)hp)[0] = h1;
510 ((UINT64 *)hp)[1] = h2;
511 ((UINT64 *)hp)[2] = h3;
512 ((UINT64 *)hp)[3] = h4;
513}
514
515/* ---------------------------------------------------------------------- */
516#endif /* UMAC_OUTPUT_LENGTH */
517/* ---------------------------------------------------------------------- */
518
519
520/* ---------------------------------------------------------------------- */
521
Damien Millerc331dbd2013-07-25 11:55:20 +1000522static void nh_transform(nh_ctx *hc, const UINT8 *buf, UINT32 nbytes)
Damien Millere45796f2007-06-11 14:01:42 +1000523/* This function is a wrapper for the primitive NH hash functions. It takes
524 * as argument "hc" the current hash context and a buffer which must be a
525 * multiple of L1_PAD_BOUNDARY. The key passed to nh_aux is offset
526 * appropriately according to how much message has been hashed already.
527 */
528{
529 UINT8 *key;
530
531 key = hc->nh_key + hc->bytes_hashed;
532 nh_aux(key, buf, hc->state, nbytes);
533}
534
535/* ---------------------------------------------------------------------- */
536
Darren Tucker2c91b282008-06-13 12:40:55 +1000537#if (__LITTLE_ENDIAN__)
Damien Millere45796f2007-06-11 14:01:42 +1000538static void endian_convert(void *buf, UWORD bpw, UINT32 num_bytes)
539/* We endian convert the keys on little-endian computers to */
540/* compensate for the lack of big-endian memory reads during hashing. */
541{
542 UWORD iters = num_bytes / bpw;
543 if (bpw == 4) {
544 UINT32 *p = (UINT32 *)buf;
545 do {
546 *p = LOAD_UINT32_REVERSED(p);
547 p++;
548 } while (--iters);
549 } else if (bpw == 8) {
550 UINT32 *p = (UINT32 *)buf;
551 UINT32 t;
552 do {
553 t = LOAD_UINT32_REVERSED(p+1);
554 p[1] = LOAD_UINT32_REVERSED(p);
555 p[0] = t;
556 p += 2;
557 } while (--iters);
558 }
559}
Damien Millere45796f2007-06-11 14:01:42 +1000560#define endian_convert_if_le(x,y,z) endian_convert((x),(y),(z))
561#else
562#define endian_convert_if_le(x,y,z) do{}while(0) /* Do nothing */
563#endif
564
565/* ---------------------------------------------------------------------- */
566
567static void nh_reset(nh_ctx *hc)
568/* Reset nh_ctx to ready for hashing of new data */
569{
570 hc->bytes_hashed = 0;
571 hc->next_data_empty = 0;
572 hc->state[0] = 0;
573#if (UMAC_OUTPUT_LEN >= 8)
574 hc->state[1] = 0;
575#endif
576#if (UMAC_OUTPUT_LEN >= 12)
577 hc->state[2] = 0;
578#endif
579#if (UMAC_OUTPUT_LEN == 16)
580 hc->state[3] = 0;
581#endif
582
583}
584
585/* ---------------------------------------------------------------------- */
586
587static void nh_init(nh_ctx *hc, aes_int_key prf_key)
588/* Generate nh_key, endian convert and reset to be ready for hashing. */
589{
590 kdf(hc->nh_key, prf_key, 1, sizeof(hc->nh_key));
591 endian_convert_if_le(hc->nh_key, 4, sizeof(hc->nh_key));
592 nh_reset(hc);
593}
594
595/* ---------------------------------------------------------------------- */
596
Damien Millerc331dbd2013-07-25 11:55:20 +1000597static void nh_update(nh_ctx *hc, const UINT8 *buf, UINT32 nbytes)
Damien Millere45796f2007-06-11 14:01:42 +1000598/* Incorporate nbytes of data into a nh_ctx, buffer whatever is not an */
599/* even multiple of HASH_BUF_BYTES. */
600{
601 UINT32 i,j;
602
603 j = hc->next_data_empty;
604 if ((j + nbytes) >= HASH_BUF_BYTES) {
605 if (j) {
606 i = HASH_BUF_BYTES - j;
607 memcpy(hc->data+j, buf, i);
608 nh_transform(hc,hc->data,HASH_BUF_BYTES);
609 nbytes -= i;
610 buf += i;
611 hc->bytes_hashed += HASH_BUF_BYTES;
612 }
613 if (nbytes >= HASH_BUF_BYTES) {
614 i = nbytes & ~(HASH_BUF_BYTES - 1);
615 nh_transform(hc, buf, i);
616 nbytes -= i;
617 buf += i;
618 hc->bytes_hashed += i;
619 }
620 j = 0;
621 }
622 memcpy(hc->data + j, buf, nbytes);
623 hc->next_data_empty = j + nbytes;
624}
625
626/* ---------------------------------------------------------------------- */
627
628static void zero_pad(UINT8 *p, int nbytes)
629{
630/* Write "nbytes" of zeroes, beginning at "p" */
631 if (nbytes >= (int)sizeof(UWORD)) {
632 while ((ptrdiff_t)p % sizeof(UWORD)) {
633 *p = 0;
634 nbytes--;
635 p++;
636 }
637 while (nbytes >= (int)sizeof(UWORD)) {
638 *(UWORD *)p = 0;
639 nbytes -= sizeof(UWORD);
640 p += sizeof(UWORD);
641 }
642 }
643 while (nbytes) {
644 *p = 0;
645 nbytes--;
646 p++;
647 }
648}
649
650/* ---------------------------------------------------------------------- */
651
652static void nh_final(nh_ctx *hc, UINT8 *result)
653/* After passing some number of data buffers to nh_update() for integration
654 * into an NH context, nh_final is called to produce a hash result. If any
655 * bytes are in the buffer hc->data, incorporate them into the
656 * NH context. Finally, add into the NH accumulation "state" the total number
657 * of bits hashed. The resulting numbers are written to the buffer "result".
658 * If nh_update was never called, L1_PAD_BOUNDARY zeroes are incorporated.
659 */
660{
661 int nh_len, nbits;
662
663 if (hc->next_data_empty != 0) {
664 nh_len = ((hc->next_data_empty + (L1_PAD_BOUNDARY - 1)) &
665 ~(L1_PAD_BOUNDARY - 1));
666 zero_pad(hc->data + hc->next_data_empty,
667 nh_len - hc->next_data_empty);
668 nh_transform(hc, hc->data, nh_len);
669 hc->bytes_hashed += hc->next_data_empty;
670 } else if (hc->bytes_hashed == 0) {
671 nh_len = L1_PAD_BOUNDARY;
672 zero_pad(hc->data, L1_PAD_BOUNDARY);
673 nh_transform(hc, hc->data, nh_len);
674 }
675
676 nbits = (hc->bytes_hashed << 3);
677 ((UINT64 *)result)[0] = ((UINT64 *)hc->state)[0] + nbits;
678#if (UMAC_OUTPUT_LEN >= 8)
679 ((UINT64 *)result)[1] = ((UINT64 *)hc->state)[1] + nbits;
680#endif
681#if (UMAC_OUTPUT_LEN >= 12)
682 ((UINT64 *)result)[2] = ((UINT64 *)hc->state)[2] + nbits;
683#endif
684#if (UMAC_OUTPUT_LEN == 16)
685 ((UINT64 *)result)[3] = ((UINT64 *)hc->state)[3] + nbits;
686#endif
687 nh_reset(hc);
688}
689
690/* ---------------------------------------------------------------------- */
691
Damien Millerc331dbd2013-07-25 11:55:20 +1000692static void nh(nh_ctx *hc, const UINT8 *buf, UINT32 padded_len,
Damien Millere45796f2007-06-11 14:01:42 +1000693 UINT32 unpadded_len, UINT8 *result)
694/* All-in-one nh_update() and nh_final() equivalent.
695 * Assumes that padded_len is divisible by L1_PAD_BOUNDARY and result is
696 * well aligned
697 */
698{
699 UINT32 nbits;
700
701 /* Initialize the hash state */
702 nbits = (unpadded_len << 3);
703
704 ((UINT64 *)result)[0] = nbits;
705#if (UMAC_OUTPUT_LEN >= 8)
706 ((UINT64 *)result)[1] = nbits;
707#endif
708#if (UMAC_OUTPUT_LEN >= 12)
709 ((UINT64 *)result)[2] = nbits;
710#endif
711#if (UMAC_OUTPUT_LEN == 16)
712 ((UINT64 *)result)[3] = nbits;
713#endif
714
715 nh_aux(hc->nh_key, buf, result, padded_len);
716}
717
718/* ---------------------------------------------------------------------- */
719/* ---------------------------------------------------------------------- */
720/* ----- Begin UHASH Section -------------------------------------------- */
721/* ---------------------------------------------------------------------- */
722/* ---------------------------------------------------------------------- */
723
724/* UHASH is a multi-layered algorithm. Data presented to UHASH is first
725 * hashed by NH. The NH output is then hashed by a polynomial-hash layer
726 * unless the initial data to be hashed is short. After the polynomial-
727 * layer, an inner-product hash is used to produce the final UHASH output.
728 *
729 * UHASH provides two interfaces, one all-at-once and another where data
730 * buffers are presented sequentially. In the sequential interface, the
731 * UHASH client calls the routine uhash_update() as many times as necessary.
732 * When there is no more data to be fed to UHASH, the client calls
733 * uhash_final() which
734 * calculates the UHASH output. Before beginning another UHASH calculation
735 * the uhash_reset() routine must be called. The all-at-once UHASH routine,
736 * uhash(), is equivalent to the sequence of calls uhash_update() and
737 * uhash_final(); however it is optimized and should be
738 * used whenever the sequential interface is not necessary.
739 *
740 * The routine uhash_init() initializes the uhash_ctx data structure and
741 * must be called once, before any other UHASH routine.
742 */
743
744/* ---------------------------------------------------------------------- */
745/* ----- Constants and uhash_ctx ---------------------------------------- */
746/* ---------------------------------------------------------------------- */
747
748/* ---------------------------------------------------------------------- */
749/* ----- Poly hash and Inner-Product hash Constants --------------------- */
750/* ---------------------------------------------------------------------- */
751
752/* Primes and masks */
753#define p36 ((UINT64)0x0000000FFFFFFFFBull) /* 2^36 - 5 */
754#define p64 ((UINT64)0xFFFFFFFFFFFFFFC5ull) /* 2^64 - 59 */
755#define m36 ((UINT64)0x0000000FFFFFFFFFull) /* The low 36 of 64 bits */
756
757
758/* ---------------------------------------------------------------------- */
759
760typedef struct uhash_ctx {
761 nh_ctx hash; /* Hash context for L1 NH hash */
762 UINT64 poly_key_8[STREAMS]; /* p64 poly keys */
763 UINT64 poly_accum[STREAMS]; /* poly hash result */
764 UINT64 ip_keys[STREAMS*4]; /* Inner-product keys */
765 UINT32 ip_trans[STREAMS]; /* Inner-product translation */
766 UINT32 msg_len; /* Total length of data passed */
767 /* to uhash */
768} uhash_ctx;
769typedef struct uhash_ctx *uhash_ctx_t;
770
771/* ---------------------------------------------------------------------- */
772
773
774/* The polynomial hashes use Horner's rule to evaluate a polynomial one
775 * word at a time. As described in the specification, poly32 and poly64
776 * require keys from special domains. The following implementations exploit
777 * the special domains to avoid overflow. The results are not guaranteed to
778 * be within Z_p32 and Z_p64, but the Inner-Product hash implementation
779 * patches any errant values.
780 */
781
782static UINT64 poly64(UINT64 cur, UINT64 key, UINT64 data)
783{
784 UINT32 key_hi = (UINT32)(key >> 32),
785 key_lo = (UINT32)key,
786 cur_hi = (UINT32)(cur >> 32),
787 cur_lo = (UINT32)cur,
788 x_lo,
789 x_hi;
790 UINT64 X,T,res;
791
792 X = MUL64(key_hi, cur_lo) + MUL64(cur_hi, key_lo);
793 x_lo = (UINT32)X;
794 x_hi = (UINT32)(X >> 32);
795
796 res = (MUL64(key_hi, cur_hi) + x_hi) * 59 + MUL64(key_lo, cur_lo);
797
798 T = ((UINT64)x_lo << 32);
799 res += T;
800 if (res < T)
801 res += 59;
802
803 res += data;
804 if (res < data)
805 res += 59;
806
807 return res;
808}
809
810
811/* Although UMAC is specified to use a ramped polynomial hash scheme, this
812 * implementation does not handle all ramp levels. Because we don't handle
813 * the ramp up to p128 modulus in this implementation, we are limited to
814 * 2^14 poly_hash() invocations per stream (for a total capacity of 2^24
815 * bytes input to UMAC per tag, ie. 16MB).
816 */
817static void poly_hash(uhash_ctx_t hc, UINT32 data_in[])
818{
819 int i;
820 UINT64 *data=(UINT64*)data_in;
821
822 for (i = 0; i < STREAMS; i++) {
823 if ((UINT32)(data[i] >> 32) == 0xfffffffful) {
824 hc->poly_accum[i] = poly64(hc->poly_accum[i],
825 hc->poly_key_8[i], p64 - 1);
826 hc->poly_accum[i] = poly64(hc->poly_accum[i],
827 hc->poly_key_8[i], (data[i] - 59));
828 } else {
829 hc->poly_accum[i] = poly64(hc->poly_accum[i],
830 hc->poly_key_8[i], data[i]);
831 }
832 }
833}
834
835
836/* ---------------------------------------------------------------------- */
837
838
839/* The final step in UHASH is an inner-product hash. The poly hash
840 * produces a result not neccesarily WORD_LEN bytes long. The inner-
841 * product hash breaks the polyhash output into 16-bit chunks and
842 * multiplies each with a 36 bit key.
843 */
844
845static UINT64 ip_aux(UINT64 t, UINT64 *ipkp, UINT64 data)
846{
847 t = t + ipkp[0] * (UINT64)(UINT16)(data >> 48);
848 t = t + ipkp[1] * (UINT64)(UINT16)(data >> 32);
849 t = t + ipkp[2] * (UINT64)(UINT16)(data >> 16);
850 t = t + ipkp[3] * (UINT64)(UINT16)(data);
851
852 return t;
853}
854
855static UINT32 ip_reduce_p36(UINT64 t)
856{
857/* Divisionless modular reduction */
858 UINT64 ret;
859
860 ret = (t & m36) + 5 * (t >> 36);
861 if (ret >= p36)
862 ret -= p36;
863
864 /* return least significant 32 bits */
865 return (UINT32)(ret);
866}
867
868
869/* If the data being hashed by UHASH is no longer than L1_KEY_LEN, then
870 * the polyhash stage is skipped and ip_short is applied directly to the
871 * NH output.
872 */
873static void ip_short(uhash_ctx_t ahc, UINT8 *nh_res, u_char *res)
874{
875 UINT64 t;
876 UINT64 *nhp = (UINT64 *)nh_res;
877
878 t = ip_aux(0,ahc->ip_keys, nhp[0]);
879 STORE_UINT32_BIG((UINT32 *)res+0, ip_reduce_p36(t) ^ ahc->ip_trans[0]);
880#if (UMAC_OUTPUT_LEN >= 8)
881 t = ip_aux(0,ahc->ip_keys+4, nhp[1]);
882 STORE_UINT32_BIG((UINT32 *)res+1, ip_reduce_p36(t) ^ ahc->ip_trans[1]);
883#endif
884#if (UMAC_OUTPUT_LEN >= 12)
885 t = ip_aux(0,ahc->ip_keys+8, nhp[2]);
886 STORE_UINT32_BIG((UINT32 *)res+2, ip_reduce_p36(t) ^ ahc->ip_trans[2]);
887#endif
888#if (UMAC_OUTPUT_LEN == 16)
889 t = ip_aux(0,ahc->ip_keys+12, nhp[3]);
890 STORE_UINT32_BIG((UINT32 *)res+3, ip_reduce_p36(t) ^ ahc->ip_trans[3]);
891#endif
892}
893
894/* If the data being hashed by UHASH is longer than L1_KEY_LEN, then
895 * the polyhash stage is not skipped and ip_long is applied to the
896 * polyhash output.
897 */
898static void ip_long(uhash_ctx_t ahc, u_char *res)
899{
900 int i;
901 UINT64 t;
902
903 for (i = 0; i < STREAMS; i++) {
904 /* fix polyhash output not in Z_p64 */
905 if (ahc->poly_accum[i] >= p64)
906 ahc->poly_accum[i] -= p64;
907 t = ip_aux(0,ahc->ip_keys+(i*4), ahc->poly_accum[i]);
908 STORE_UINT32_BIG((UINT32 *)res+i,
909 ip_reduce_p36(t) ^ ahc->ip_trans[i]);
910 }
911}
912
913
914/* ---------------------------------------------------------------------- */
915
916/* ---------------------------------------------------------------------- */
917
918/* Reset uhash context for next hash session */
919static int uhash_reset(uhash_ctx_t pc)
920{
921 nh_reset(&pc->hash);
922 pc->msg_len = 0;
923 pc->poly_accum[0] = 1;
924#if (UMAC_OUTPUT_LEN >= 8)
925 pc->poly_accum[1] = 1;
926#endif
927#if (UMAC_OUTPUT_LEN >= 12)
928 pc->poly_accum[2] = 1;
929#endif
930#if (UMAC_OUTPUT_LEN == 16)
931 pc->poly_accum[3] = 1;
932#endif
933 return 1;
934}
935
936/* ---------------------------------------------------------------------- */
937
938/* Given a pointer to the internal key needed by kdf() and a uhash context,
939 * initialize the NH context and generate keys needed for poly and inner-
940 * product hashing. All keys are endian adjusted in memory so that native
941 * loads cause correct keys to be in registers during calculation.
942 */
943static void uhash_init(uhash_ctx_t ahc, aes_int_key prf_key)
944{
945 int i;
946 UINT8 buf[(8*STREAMS+4)*sizeof(UINT64)];
947
948 /* Zero the entire uhash context */
949 memset(ahc, 0, sizeof(uhash_ctx));
950
951 /* Initialize the L1 hash */
952 nh_init(&ahc->hash, prf_key);
953
954 /* Setup L2 hash variables */
955 kdf(buf, prf_key, 2, sizeof(buf)); /* Fill buffer with index 1 key */
956 for (i = 0; i < STREAMS; i++) {
957 /* Fill keys from the buffer, skipping bytes in the buffer not
958 * used by this implementation. Endian reverse the keys if on a
959 * little-endian computer.
960 */
961 memcpy(ahc->poly_key_8+i, buf+24*i, 8);
962 endian_convert_if_le(ahc->poly_key_8+i, 8, 8);
963 /* Mask the 64-bit keys to their special domain */
964 ahc->poly_key_8[i] &= ((UINT64)0x01ffffffu << 32) + 0x01ffffffu;
965 ahc->poly_accum[i] = 1; /* Our polyhash prepends a non-zero word */
966 }
967
968 /* Setup L3-1 hash variables */
969 kdf(buf, prf_key, 3, sizeof(buf)); /* Fill buffer with index 2 key */
970 for (i = 0; i < STREAMS; i++)
971 memcpy(ahc->ip_keys+4*i, buf+(8*i+4)*sizeof(UINT64),
972 4*sizeof(UINT64));
973 endian_convert_if_le(ahc->ip_keys, sizeof(UINT64),
974 sizeof(ahc->ip_keys));
975 for (i = 0; i < STREAMS*4; i++)
976 ahc->ip_keys[i] %= p36; /* Bring into Z_p36 */
977
978 /* Setup L3-2 hash variables */
979 /* Fill buffer with index 4 key */
980 kdf(ahc->ip_trans, prf_key, 4, STREAMS * sizeof(UINT32));
981 endian_convert_if_le(ahc->ip_trans, sizeof(UINT32),
982 STREAMS * sizeof(UINT32));
983}
984
985/* ---------------------------------------------------------------------- */
986
987#if 0
988static uhash_ctx_t uhash_alloc(u_char key[])
989{
990/* Allocate memory and force to a 16-byte boundary. */
991 uhash_ctx_t ctx;
992 u_char bytes_to_add;
993 aes_int_key prf_key;
994
995 ctx = (uhash_ctx_t)malloc(sizeof(uhash_ctx)+ALLOC_BOUNDARY);
996 if (ctx) {
997 if (ALLOC_BOUNDARY) {
998 bytes_to_add = ALLOC_BOUNDARY -
999 ((ptrdiff_t)ctx & (ALLOC_BOUNDARY -1));
1000 ctx = (uhash_ctx_t)((u_char *)ctx + bytes_to_add);
1001 *((u_char *)ctx - 1) = bytes_to_add;
1002 }
1003 aes_key_setup(key,prf_key);
1004 uhash_init(ctx, prf_key);
1005 }
1006 return (ctx);
1007}
1008#endif
1009
1010/* ---------------------------------------------------------------------- */
1011
1012#if 0
1013static int uhash_free(uhash_ctx_t ctx)
1014{
1015/* Free memory allocated by uhash_alloc */
1016 u_char bytes_to_sub;
1017
1018 if (ctx) {
1019 if (ALLOC_BOUNDARY) {
1020 bytes_to_sub = *((u_char *)ctx - 1);
1021 ctx = (uhash_ctx_t)((u_char *)ctx - bytes_to_sub);
1022 }
1023 free(ctx);
1024 }
1025 return (1);
1026}
1027#endif
1028/* ---------------------------------------------------------------------- */
1029
Damien Millerc331dbd2013-07-25 11:55:20 +10001030static int uhash_update(uhash_ctx_t ctx, const u_char *input, long len)
Damien Millere45796f2007-06-11 14:01:42 +10001031/* Given len bytes of data, we parse it into L1_KEY_LEN chunks and
1032 * hash each one with NH, calling the polyhash on each NH output.
1033 */
1034{
1035 UWORD bytes_hashed, bytes_remaining;
Damien Miller0f30c872008-05-19 16:07:45 +10001036 UINT64 result_buf[STREAMS];
1037 UINT8 *nh_result = (UINT8 *)&result_buf;
Damien Millere45796f2007-06-11 14:01:42 +10001038
1039 if (ctx->msg_len + len <= L1_KEY_LEN) {
Damien Millerc331dbd2013-07-25 11:55:20 +10001040 nh_update(&ctx->hash, (const UINT8 *)input, len);
Damien Millere45796f2007-06-11 14:01:42 +10001041 ctx->msg_len += len;
1042 } else {
1043
1044 bytes_hashed = ctx->msg_len % L1_KEY_LEN;
1045 if (ctx->msg_len == L1_KEY_LEN)
1046 bytes_hashed = L1_KEY_LEN;
1047
1048 if (bytes_hashed + len >= L1_KEY_LEN) {
1049
1050 /* If some bytes have been passed to the hash function */
1051 /* then we want to pass at most (L1_KEY_LEN - bytes_hashed) */
1052 /* bytes to complete the current nh_block. */
1053 if (bytes_hashed) {
1054 bytes_remaining = (L1_KEY_LEN - bytes_hashed);
Damien Millerc331dbd2013-07-25 11:55:20 +10001055 nh_update(&ctx->hash, (const UINT8 *)input, bytes_remaining);
Damien Millere45796f2007-06-11 14:01:42 +10001056 nh_final(&ctx->hash, nh_result);
1057 ctx->msg_len += bytes_remaining;
1058 poly_hash(ctx,(UINT32 *)nh_result);
1059 len -= bytes_remaining;
1060 input += bytes_remaining;
1061 }
1062
1063 /* Hash directly from input stream if enough bytes */
1064 while (len >= L1_KEY_LEN) {
Damien Millerc331dbd2013-07-25 11:55:20 +10001065 nh(&ctx->hash, (const UINT8 *)input, L1_KEY_LEN,
Damien Millere45796f2007-06-11 14:01:42 +10001066 L1_KEY_LEN, nh_result);
1067 ctx->msg_len += L1_KEY_LEN;
1068 len -= L1_KEY_LEN;
1069 input += L1_KEY_LEN;
1070 poly_hash(ctx,(UINT32 *)nh_result);
1071 }
1072 }
1073
1074 /* pass remaining < L1_KEY_LEN bytes of input data to NH */
1075 if (len) {
Damien Millerc331dbd2013-07-25 11:55:20 +10001076 nh_update(&ctx->hash, (const UINT8 *)input, len);
Damien Millere45796f2007-06-11 14:01:42 +10001077 ctx->msg_len += len;
1078 }
1079 }
1080
1081 return (1);
1082}
1083
1084/* ---------------------------------------------------------------------- */
1085
1086static int uhash_final(uhash_ctx_t ctx, u_char *res)
1087/* Incorporate any pending data, pad, and generate tag */
1088{
Damien Miller0f30c872008-05-19 16:07:45 +10001089 UINT64 result_buf[STREAMS];
1090 UINT8 *nh_result = (UINT8 *)&result_buf;
Damien Millere45796f2007-06-11 14:01:42 +10001091
1092 if (ctx->msg_len > L1_KEY_LEN) {
1093 if (ctx->msg_len % L1_KEY_LEN) {
1094 nh_final(&ctx->hash, nh_result);
1095 poly_hash(ctx,(UINT32 *)nh_result);
1096 }
1097 ip_long(ctx, res);
1098 } else {
1099 nh_final(&ctx->hash, nh_result);
1100 ip_short(ctx,nh_result, res);
1101 }
1102 uhash_reset(ctx);
1103 return (1);
1104}
1105
1106/* ---------------------------------------------------------------------- */
1107
1108#if 0
1109static int uhash(uhash_ctx_t ahc, u_char *msg, long len, u_char *res)
1110/* assumes that msg is in a writable buffer of length divisible by */
1111/* L1_PAD_BOUNDARY. Bytes beyond msg[len] may be zeroed. */
1112{
1113 UINT8 nh_result[STREAMS*sizeof(UINT64)];
1114 UINT32 nh_len;
1115 int extra_zeroes_needed;
1116
1117 /* If the message to be hashed is no longer than L1_HASH_LEN, we skip
1118 * the polyhash.
1119 */
1120 if (len <= L1_KEY_LEN) {
1121 if (len == 0) /* If zero length messages will not */
1122 nh_len = L1_PAD_BOUNDARY; /* be seen, comment out this case */
1123 else
1124 nh_len = ((len + (L1_PAD_BOUNDARY - 1)) & ~(L1_PAD_BOUNDARY - 1));
1125 extra_zeroes_needed = nh_len - len;
1126 zero_pad((UINT8 *)msg + len, extra_zeroes_needed);
1127 nh(&ahc->hash, (UINT8 *)msg, nh_len, len, nh_result);
1128 ip_short(ahc,nh_result, res);
1129 } else {
1130 /* Otherwise, we hash each L1_KEY_LEN chunk with NH, passing the NH
1131 * output to poly_hash().
1132 */
1133 do {
1134 nh(&ahc->hash, (UINT8 *)msg, L1_KEY_LEN, L1_KEY_LEN, nh_result);
1135 poly_hash(ahc,(UINT32 *)nh_result);
1136 len -= L1_KEY_LEN;
1137 msg += L1_KEY_LEN;
1138 } while (len >= L1_KEY_LEN);
1139 if (len) {
1140 nh_len = ((len + (L1_PAD_BOUNDARY - 1)) & ~(L1_PAD_BOUNDARY - 1));
1141 extra_zeroes_needed = nh_len - len;
1142 zero_pad((UINT8 *)msg + len, extra_zeroes_needed);
1143 nh(&ahc->hash, (UINT8 *)msg, nh_len, len, nh_result);
1144 poly_hash(ahc,(UINT32 *)nh_result);
1145 }
1146
1147 ip_long(ahc, res);
1148 }
1149
1150 uhash_reset(ahc);
1151 return 1;
1152}
1153#endif
1154
1155/* ---------------------------------------------------------------------- */
1156/* ---------------------------------------------------------------------- */
1157/* ----- Begin UMAC Section --------------------------------------------- */
1158/* ---------------------------------------------------------------------- */
1159/* ---------------------------------------------------------------------- */
1160
1161/* The UMAC interface has two interfaces, an all-at-once interface where
1162 * the entire message to be authenticated is passed to UMAC in one buffer,
1163 * and a sequential interface where the message is presented a little at a
1164 * time. The all-at-once is more optimaized than the sequential version and
1165 * should be preferred when the sequential interface is not required.
1166 */
1167struct umac_ctx {
1168 uhash_ctx hash; /* Hash function for message compression */
1169 pdf_ctx pdf; /* PDF for hashed output */
1170 void *free_ptr; /* Address to free this struct via */
1171} umac_ctx;
1172
1173/* ---------------------------------------------------------------------- */
1174
1175#if 0
1176int umac_reset(struct umac_ctx *ctx)
1177/* Reset the hash function to begin a new authentication. */
1178{
1179 uhash_reset(&ctx->hash);
1180 return (1);
1181}
1182#endif
1183
1184/* ---------------------------------------------------------------------- */
1185
1186int umac_delete(struct umac_ctx *ctx)
1187/* Deallocate the ctx structure */
1188{
1189 if (ctx) {
1190 if (ALLOC_BOUNDARY)
1191 ctx = (struct umac_ctx *)ctx->free_ptr;
Darren Tuckera627d422013-06-02 07:31:17 +10001192 free(ctx);
Damien Millere45796f2007-06-11 14:01:42 +10001193 }
1194 return (1);
1195}
1196
1197/* ---------------------------------------------------------------------- */
1198
Damien Millerc331dbd2013-07-25 11:55:20 +10001199struct umac_ctx *umac_new(const u_char key[])
Damien Millere45796f2007-06-11 14:01:42 +10001200/* Dynamically allocate a umac_ctx struct, initialize variables,
1201 * generate subkeys from key. Align to 16-byte boundary.
1202 */
1203{
1204 struct umac_ctx *ctx, *octx;
1205 size_t bytes_to_add;
1206 aes_int_key prf_key;
1207
Damien Miller6c81fee2013-11-08 12:19:55 +11001208 octx = ctx = xcalloc(1, sizeof(*ctx) + ALLOC_BOUNDARY);
Damien Millere45796f2007-06-11 14:01:42 +10001209 if (ctx) {
1210 if (ALLOC_BOUNDARY) {
1211 bytes_to_add = ALLOC_BOUNDARY -
1212 ((ptrdiff_t)ctx & (ALLOC_BOUNDARY - 1));
1213 ctx = (struct umac_ctx *)((u_char *)ctx + bytes_to_add);
1214 }
1215 ctx->free_ptr = octx;
Damien Millerc331dbd2013-07-25 11:55:20 +10001216 aes_key_setup(key, prf_key);
Damien Millere45796f2007-06-11 14:01:42 +10001217 pdf_init(&ctx->pdf, prf_key);
1218 uhash_init(&ctx->hash, prf_key);
1219 }
1220
1221 return (ctx);
1222}
1223
1224/* ---------------------------------------------------------------------- */
1225
Damien Millerc331dbd2013-07-25 11:55:20 +10001226int umac_final(struct umac_ctx *ctx, u_char tag[], const u_char nonce[8])
Damien Millere45796f2007-06-11 14:01:42 +10001227/* Incorporate any pending data, pad, and generate tag */
1228{
1229 uhash_final(&ctx->hash, (u_char *)tag);
Damien Millerc331dbd2013-07-25 11:55:20 +10001230 pdf_gen_xor(&ctx->pdf, (const UINT8 *)nonce, (UINT8 *)tag);
Damien Millere45796f2007-06-11 14:01:42 +10001231
1232 return (1);
1233}
1234
1235/* ---------------------------------------------------------------------- */
1236
Damien Millerc331dbd2013-07-25 11:55:20 +10001237int umac_update(struct umac_ctx *ctx, const u_char *input, long len)
Damien Millere45796f2007-06-11 14:01:42 +10001238/* Given len bytes of data, we parse it into L1_KEY_LEN chunks and */
1239/* hash each one, calling the PDF on the hashed output whenever the hash- */
1240/* output buffer is full. */
1241{
1242 uhash_update(&ctx->hash, input, len);
1243 return (1);
1244}
1245
1246/* ---------------------------------------------------------------------- */
1247
1248#if 0
1249int umac(struct umac_ctx *ctx, u_char *input,
1250 long len, u_char tag[],
1251 u_char nonce[8])
1252/* All-in-one version simply calls umac_update() and umac_final(). */
1253{
1254 uhash(&ctx->hash, input, len, (u_char *)tag);
1255 pdf_gen_xor(&ctx->pdf, (UINT8 *)nonce, (UINT8 *)tag);
1256
1257 return (1);
1258}
1259#endif
1260
1261/* ---------------------------------------------------------------------- */
1262/* ---------------------------------------------------------------------- */
1263/* ----- End UMAC Section ----------------------------------------------- */
1264/* ---------------------------------------------------------------------- */
1265/* ---------------------------------------------------------------------- */