Bug Summary

File:src/usr.bin/ssh/ssh/../umac.c
Warning:line 516, column 9
Value stored to 'k8' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name umac128.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.bin/ssh/ssh/obj -resource-dir /usr/local/llvm16/lib/clang/16 -I /usr/src/usr.bin/ssh/ssh/.. -D WITH_OPENSSL -D WITH_ZLIB -D WITH_DSA -D ENABLE_PKCS11 -D HAVE_DLOPEN -internal-isystem /usr/local/llvm16/lib/clang/16/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -fdebug-compilation-dir=/usr/src/usr.bin/ssh/ssh/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fno-jump-tables -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/scan/2024-01-11-140451-98009-1 -x c /usr/src/usr.bin/ssh/ssh/../umac128.c
1/* $OpenBSD: umac.c,v 1.23 2023/03/07 01:30:52 djm Exp $ */
2/* -----------------------------------------------------------------------
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 license 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
55#ifndef UMAC_OUTPUT_LEN16
56#define UMAC_OUTPUT_LEN16 8 /* Alowable: 4, 8, 12, 16 */
57#endif
58/* #define FORCE_C_ONLY 1 ANSI C and 64-bit integers req'd */
59/* #define AES_IMPLEMENTAION 1 1 = OpenSSL, 2 = Barreto, 3 = Gladman */
60/* #define SSE2 0 Is SSE2 is available? */
61/* #define RUN_TESTS 0 Run basic correctness/speed tests */
62/* #define UMAC_AE_SUPPORT 0 Enable authenticated encryption */
63
64/* ---------------------------------------------------------------------- */
65/* -- Global Includes --------------------------------------------------- */
66/* ---------------------------------------------------------------------- */
67
68#include <sys/types.h>
69#include <endian.h>
70#include <string.h>
71#include <stdarg.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <stddef.h>
75
76#include "xmalloc.h"
77#include "umac.h"
78#include "misc.h"
79
80/* ---------------------------------------------------------------------- */
81/* --- Primitive Data Types --- */
82/* ---------------------------------------------------------------------- */
83
84/* The following assumptions may need change on your system */
85typedef u_int8_t UINT8; /* 1 byte */
86typedef u_int16_t UINT16; /* 2 byte */
87typedef u_int32_t UINT32; /* 4 byte */
88typedef u_int64_t UINT64; /* 8 bytes */
89typedef unsigned int UWORD; /* Register */
90
91/* ---------------------------------------------------------------------- */
92/* --- Constants -------------------------------------------------------- */
93/* ---------------------------------------------------------------------- */
94
95#define UMAC_KEY_LEN16 16 /* UMAC takes 16 bytes of external key */
96
97/* Message "words" are read from memory in an endian-specific manner. */
98/* For this implementation to behave correctly, __LITTLE_ENDIAN__ must */
99/* be set true if the host computer is little-endian. */
100
101#if BYTE_ORDER1234 == LITTLE_ENDIAN1234
102#define __LITTLE_ENDIAN__1 1
103#else
104#define __LITTLE_ENDIAN__1 0
105#endif
106
107/* ---------------------------------------------------------------------- */
108/* ---------------------------------------------------------------------- */
109/* ----- Architecture Specific ------------------------------------------ */
110/* ---------------------------------------------------------------------- */
111/* ---------------------------------------------------------------------- */
112
113
114/* ---------------------------------------------------------------------- */
115/* ---------------------------------------------------------------------- */
116/* ----- Primitive Routines --------------------------------------------- */
117/* ---------------------------------------------------------------------- */
118/* ---------------------------------------------------------------------- */
119
120
121/* ---------------------------------------------------------------------- */
122/* --- 32-bit by 32-bit to 64-bit Multiplication ------------------------ */
123/* ---------------------------------------------------------------------- */
124
125#define MUL64(a,b)((UINT64)((UINT64)(UINT32)(a) * (UINT64)(UINT32)(b))) ((UINT64)((UINT64)(UINT32)(a) * (UINT64)(UINT32)(b)))
126
127/* ---------------------------------------------------------------------- */
128/* --- Endian Conversion --- Forcing assembly on some platforms */
129/* ---------------------------------------------------------------------- */
130
131/* The following definitions use the above reversal-primitives to do the right
132 * thing on endian specific load and stores.
133 */
134
135#if BYTE_ORDER1234 == LITTLE_ENDIAN1234
136#define LOAD_UINT32_REVERSED(p)get_u32(p) get_u32(p)
137#define STORE_UINT32_REVERSED(p,v)put_u32(p,v) put_u32(p,v)
138#else
139#define LOAD_UINT32_REVERSED(p)get_u32(p) get_u32_le(p)
140#define STORE_UINT32_REVERSED(p,v)put_u32(p,v) put_u32_le(p,v)
141#endif
142
143#define LOAD_UINT32_LITTLE(p)(get_u32_le(p)) (get_u32_le(p))
144#define STORE_UINT32_BIG(p,v)put_u32(p, v) put_u32(p, v)
145
146
147
148/* ---------------------------------------------------------------------- */
149/* ---------------------------------------------------------------------- */
150/* ----- Begin KDF & PDF Section ---------------------------------------- */
151/* ---------------------------------------------------------------------- */
152/* ---------------------------------------------------------------------- */
153
154/* UMAC uses AES with 16 byte block and key lengths */
155#define AES_BLOCK_LEN16 16
156
157#ifdef WITH_OPENSSL1
158#include <openssl/aes.h>
159typedef AES_KEY aes_int_key[1];
160#define aes_encryption(in,out,int_key)AES_encrypt((u_char *)(in),(u_char *)(out),(AES_KEY *)int_key
)
\
161 AES_encrypt((u_char *)(in),(u_char *)(out),(AES_KEY *)int_key)
162#define aes_key_setup(key,int_key)AES_set_encrypt_key((const u_char *)(key),16*8,int_key) \
163 AES_set_encrypt_key((const u_char *)(key),UMAC_KEY_LEN16*8,int_key)
164#else
165#include "rijndael.h"
166#define AES_ROUNDS ((UMAC_KEY_LEN16 / 4) + 6)
167typedef UINT8 aes_int_key[AES_ROUNDS+1][4][4]; /* AES internal */
168#define aes_encryption(in,out,int_key)AES_encrypt((u_char *)(in),(u_char *)(out),(AES_KEY *)int_key
)
\
169 rijndaelEncrypt((u32 *)(int_key), AES_ROUNDS, (u8 *)(in), (u8 *)(out))
170#define aes_key_setup(key,int_key)AES_set_encrypt_key((const u_char *)(key),16*8,int_key) \
171 rijndaelKeySetupEnc((u32 *)(int_key), (const unsigned char *)(key), \
172 UMAC_KEY_LEN16*8)
173#endif
174
175/* The user-supplied UMAC key is stretched using AES in a counter
176 * mode to supply all random bits needed by UMAC. The kdf function takes
177 * an AES internal key representation 'key' and writes a stream of
178 * 'nbytes' bytes to the memory pointed at by 'buffer_ptr'. Each distinct
179 * 'ndx' causes a distinct byte stream.
180 */
181static void kdf(void *buffer_ptr, aes_int_key key, UINT8 ndx, int nbytes)
182{
183 UINT8 in_buf[AES_BLOCK_LEN16] = {0};
184 UINT8 out_buf[AES_BLOCK_LEN16];
185 UINT8 *dst_buf = (UINT8 *)buffer_ptr;
186 int i;
187
188 /* Setup the initial value */
189 in_buf[AES_BLOCK_LEN16-9] = ndx;
190 in_buf[AES_BLOCK_LEN16-1] = i = 1;
191
192 while (nbytes >= AES_BLOCK_LEN16) {
193 aes_encryption(in_buf, out_buf, key)AES_encrypt((u_char *)(in_buf),(u_char *)(out_buf),(AES_KEY *
)key)
;
194 memcpy(dst_buf,out_buf,AES_BLOCK_LEN16);
195 in_buf[AES_BLOCK_LEN16-1] = ++i;
196 nbytes -= AES_BLOCK_LEN16;
197 dst_buf += AES_BLOCK_LEN16;
198 }
199 if (nbytes) {
200 aes_encryption(in_buf, out_buf, key)AES_encrypt((u_char *)(in_buf),(u_char *)(out_buf),(AES_KEY *
)key)
;
201 memcpy(dst_buf,out_buf,nbytes);
202 }
203 explicit_bzero(in_buf, sizeof(in_buf));
204 explicit_bzero(out_buf, sizeof(out_buf));
205}
206
207/* The final UHASH result is XOR'd with the output of a pseudorandom
208 * function. Here, we use AES to generate random output and
209 * xor the appropriate bytes depending on the last bits of nonce.
210 * This scheme is optimized for sequential, increasing big-endian nonces.
211 */
212
213typedef struct {
214 UINT8 cache[AES_BLOCK_LEN16]; /* Previous AES output is saved */
215 UINT8 nonce[AES_BLOCK_LEN16]; /* The AES input making above cache */
216 aes_int_key prf_key; /* Expanded AES key for PDF */
217} pdf_ctx;
218
219static void pdf_init(pdf_ctx *pc, aes_int_key prf_key)
220{
221 UINT8 buf[UMAC_KEY_LEN16];
222
223 kdf(buf, prf_key, 0, UMAC_KEY_LEN16);
224 aes_key_setup(buf, pc->prf_key)AES_set_encrypt_key((const u_char *)(buf),16*8,pc->prf_key
)
;
225
226 /* Initialize pdf and cache */
227 memset(pc->nonce, 0, sizeof(pc->nonce));
228 aes_encryption(pc->nonce, pc->cache, pc->prf_key)AES_encrypt((u_char *)(pc->nonce),(u_char *)(pc->cache)
,(AES_KEY *)pc->prf_key)
;
229 explicit_bzero(buf, sizeof(buf));
230}
231
232static void pdf_gen_xor(pdf_ctx *pc, const UINT8 nonce[8],
233 UINT8 buf[UMAC_OUTPUT_LEN16])
234{
235 /* 'ndx' indicates that we'll be using the 0th or 1st eight bytes
236 * of the AES output. If last time around we returned the ndx-1st
237 * element, then we may have the result in the cache already.
238 */
239
240#if (UMAC_OUTPUT_LEN16 == 4)
241#define LOW_BIT_MASK0 3
242#elif (UMAC_OUTPUT_LEN16 == 8)
243#define LOW_BIT_MASK0 1
244#elif (UMAC_OUTPUT_LEN16 > 8)
245#define LOW_BIT_MASK0 0
246#endif
247 union {
248 UINT8 tmp_nonce_lo[4];
249 UINT32 align;
250 } t;
251#if LOW_BIT_MASK0 != 0
252 int ndx = nonce[7] & LOW_BIT_MASK0;
253#endif
254 *(UINT32 *)t.tmp_nonce_lo = ((const UINT32 *)nonce)[1];
255 t.tmp_nonce_lo[3] &= ~LOW_BIT_MASK0; /* zero last bit */
256
257 if ( (((UINT32 *)t.tmp_nonce_lo)[0] != ((UINT32 *)pc->nonce)[1]) ||
258 (((const UINT32 *)nonce)[0] != ((UINT32 *)pc->nonce)[0]) )
259 {
260 ((UINT32 *)pc->nonce)[0] = ((const UINT32 *)nonce)[0];
261 ((UINT32 *)pc->nonce)[1] = ((UINT32 *)t.tmp_nonce_lo)[0];
262 aes_encryption(pc->nonce, pc->cache, pc->prf_key)AES_encrypt((u_char *)(pc->nonce),(u_char *)(pc->cache)
,(AES_KEY *)pc->prf_key)
;
263 }
264
265#if (UMAC_OUTPUT_LEN16 == 4)
266 *((UINT32 *)buf) ^= ((UINT32 *)pc->cache)[ndx];
267#elif (UMAC_OUTPUT_LEN16 == 8)
268 *((UINT64 *)buf) ^= ((UINT64 *)pc->cache)[ndx];
269#elif (UMAC_OUTPUT_LEN16 == 12)
270 ((UINT64 *)buf)[0] ^= ((UINT64 *)pc->cache)[0];
271 ((UINT32 *)buf)[2] ^= ((UINT32 *)pc->cache)[2];
272#elif (UMAC_OUTPUT_LEN16 == 16)
273 ((UINT64 *)buf)[0] ^= ((UINT64 *)pc->cache)[0];
274 ((UINT64 *)buf)[1] ^= ((UINT64 *)pc->cache)[1];
275#endif
276}
277
278/* ---------------------------------------------------------------------- */
279/* ---------------------------------------------------------------------- */
280/* ----- Begin NH Hash Section ------------------------------------------ */
281/* ---------------------------------------------------------------------- */
282/* ---------------------------------------------------------------------- */
283
284/* The NH-based hash functions used in UMAC are described in the UMAC paper
285 * and specification, both of which can be found at the UMAC website.
286 * The interface to this implementation has two
287 * versions, one expects the entire message being hashed to be passed
288 * in a single buffer and returns the hash result immediately. The second
289 * allows the message to be passed in a sequence of buffers. In the
290 * multiple-buffer interface, the client calls the routine nh_update() as
291 * many times as necessary. When there is no more data to be fed to the
292 * hash, the client calls nh_final() which calculates the hash output.
293 * Before beginning another hash calculation the nh_reset() routine
294 * must be called. The single-buffer routine, nh(), is equivalent to
295 * the sequence of calls nh_update() and nh_final(); however it is
296 * optimized and should be preferred whenever the multiple-buffer interface
297 * is not necessary. When using either interface, it is the client's
298 * responsibility to pass no more than L1_KEY_LEN bytes per hash result.
299 *
300 * The routine nh_init() initializes the nh_ctx data structure and
301 * must be called once, before any other PDF routine.
302 */
303
304 /* The "nh_aux" routines do the actual NH hashing work. They
305 * expect buffers to be multiples of L1_PAD_BOUNDARY. These routines
306 * produce output for all STREAMS NH iterations in one call,
307 * allowing the parallel implementation of the streams.
308 */
309
310#define STREAMS(16 / 4) (UMAC_OUTPUT_LEN16 / 4) /* Number of times hash is applied */
311#define L1_KEY_LEN1024 1024 /* Internal key bytes */
312#define L1_KEY_SHIFT16 16 /* Toeplitz key shift between streams */
313#define L1_PAD_BOUNDARY32 32 /* pad message to boundary multiple */
314#define ALLOC_BOUNDARY16 16 /* Keep buffers aligned to this */
315#define HASH_BUF_BYTES64 64 /* nh_aux_hb buffer multiple */
316
317typedef struct {
318 UINT8 nh_key [L1_KEY_LEN1024 + L1_KEY_SHIFT16 * (STREAMS(16 / 4) - 1)]; /* NH Key */
319 UINT8 data [HASH_BUF_BYTES64]; /* Incoming data buffer */
320 int next_data_empty; /* Bookkeeping variable for data buffer. */
321 int bytes_hashed; /* Bytes (out of L1_KEY_LEN) incorporated. */
322 UINT64 state[STREAMS(16 / 4)]; /* on-line state */
323} nh_ctx;
324
325
326#if (UMAC_OUTPUT_LEN16 == 4)
327
328static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
329/* NH hashing primitive. Previous (partial) hash result is loaded and
330* then stored via hp pointer. The length of the data pointed at by "dp",
331* "dlen", is guaranteed to be divisible by L1_PAD_BOUNDARY (32). Key
332* is expected to be endian compensated in memory at key setup.
333*/
334{
335 UINT64 h;
336 UWORD c = dlen / 32;
337 UINT32 *k = (UINT32 *)kp;
338 const UINT32 *d = (const UINT32 *)dp;
339 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
340 UINT32 k0,k1,k2,k3,k4,k5,k6,k7;
341
342 h = *((UINT64 *)hp);
343 do {
344 d0 = LOAD_UINT32_LITTLE(d+0)(get_u32_le(d+0)); d1 = LOAD_UINT32_LITTLE(d+1)(get_u32_le(d+1));
345 d2 = LOAD_UINT32_LITTLE(d+2)(get_u32_le(d+2)); d3 = LOAD_UINT32_LITTLE(d+3)(get_u32_le(d+3));
346 d4 = LOAD_UINT32_LITTLE(d+4)(get_u32_le(d+4)); d5 = LOAD_UINT32_LITTLE(d+5)(get_u32_le(d+5));
347 d6 = LOAD_UINT32_LITTLE(d+6)(get_u32_le(d+6)); d7 = LOAD_UINT32_LITTLE(d+7)(get_u32_le(d+7));
348 k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
349 k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
350 h += MUL64((k0 + d0), (k4 + d4))((UINT64)((UINT64)(UINT32)((k0 + d0)) * (UINT64)(UINT32)((k4 +
d4))))
;
351 h += MUL64((k1 + d1), (k5 + d5))((UINT64)((UINT64)(UINT32)((k1 + d1)) * (UINT64)(UINT32)((k5 +
d5))))
;
352 h += MUL64((k2 + d2), (k6 + d6))((UINT64)((UINT64)(UINT32)((k2 + d2)) * (UINT64)(UINT32)((k6 +
d6))))
;
353 h += MUL64((k3 + d3), (k7 + d7))((UINT64)((UINT64)(UINT32)((k3 + d3)) * (UINT64)(UINT32)((k7 +
d7))))
;
354
355 d += 8;
356 k += 8;
357 } while (--c);
358 *((UINT64 *)hp) = h;
359}
360
361#elif (UMAC_OUTPUT_LEN16 == 8)
362
363static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
364/* Same as previous nh_aux, but two streams are handled in one pass,
365 * reading and writing 16 bytes of hash-state per call.
366 */
367{
368 UINT64 h1,h2;
369 UWORD c = dlen / 32;
370 UINT32 *k = (UINT32 *)kp;
371 const UINT32 *d = (const UINT32 *)dp;
372 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
373 UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
374 k8,k9,k10,k11;
375
376 h1 = *((UINT64 *)hp);
377 h2 = *((UINT64 *)hp + 1);
378 k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
379 do {
380 d0 = LOAD_UINT32_LITTLE(d+0)(get_u32_le(d+0)); d1 = LOAD_UINT32_LITTLE(d+1)(get_u32_le(d+1));
381 d2 = LOAD_UINT32_LITTLE(d+2)(get_u32_le(d+2)); d3 = LOAD_UINT32_LITTLE(d+3)(get_u32_le(d+3));
382 d4 = LOAD_UINT32_LITTLE(d+4)(get_u32_le(d+4)); d5 = LOAD_UINT32_LITTLE(d+5)(get_u32_le(d+5));
383 d6 = LOAD_UINT32_LITTLE(d+6)(get_u32_le(d+6)); d7 = LOAD_UINT32_LITTLE(d+7)(get_u32_le(d+7));
384 k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
385 k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11);
386
387 h1 += MUL64((k0 + d0), (k4 + d4))((UINT64)((UINT64)(UINT32)((k0 + d0)) * (UINT64)(UINT32)((k4 +
d4))))
;
388 h2 += MUL64((k4 + d0), (k8 + d4))((UINT64)((UINT64)(UINT32)((k4 + d0)) * (UINT64)(UINT32)((k8 +
d4))))
;
389
390 h1 += MUL64((k1 + d1), (k5 + d5))((UINT64)((UINT64)(UINT32)((k1 + d1)) * (UINT64)(UINT32)((k5 +
d5))))
;
391 h2 += MUL64((k5 + d1), (k9 + d5))((UINT64)((UINT64)(UINT32)((k5 + d1)) * (UINT64)(UINT32)((k9 +
d5))))
;
392
393 h1 += MUL64((k2 + d2), (k6 + d6))((UINT64)((UINT64)(UINT32)((k2 + d2)) * (UINT64)(UINT32)((k6 +
d6))))
;
394 h2 += MUL64((k6 + d2), (k10 + d6))((UINT64)((UINT64)(UINT32)((k6 + d2)) * (UINT64)(UINT32)((k10
+ d6))))
;
395
396 h1 += MUL64((k3 + d3), (k7 + d7))((UINT64)((UINT64)(UINT32)((k3 + d3)) * (UINT64)(UINT32)((k7 +
d7))))
;
397 h2 += MUL64((k7 + d3), (k11 + d7))((UINT64)((UINT64)(UINT32)((k7 + d3)) * (UINT64)(UINT32)((k11
+ d7))))
;
398
399 k0 = k8; k1 = k9; k2 = k10; k3 = k11;
400
401 d += 8;
402 k += 8;
403 } while (--c);
404 ((UINT64 *)hp)[0] = h1;
405 ((UINT64 *)hp)[1] = h2;
406}
407
408#elif (UMAC_OUTPUT_LEN16 == 12)
409
410static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
411/* Same as previous nh_aux, but two streams are handled in one pass,
412 * reading and writing 24 bytes of hash-state per call.
413*/
414{
415 UINT64 h1,h2,h3;
416 UWORD c = dlen / 32;
417 UINT32 *k = (UINT32 *)kp;
418 const UINT32 *d = (const UINT32 *)dp;
419 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
420 UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
421 k8,k9,k10,k11,k12,k13,k14,k15;
422
423 h1 = *((UINT64 *)hp);
424 h2 = *((UINT64 *)hp + 1);
425 h3 = *((UINT64 *)hp + 2);
426 k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
427 k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
428 do {
429 d0 = LOAD_UINT32_LITTLE(d+0)(get_u32_le(d+0)); d1 = LOAD_UINT32_LITTLE(d+1)(get_u32_le(d+1));
430 d2 = LOAD_UINT32_LITTLE(d+2)(get_u32_le(d+2)); d3 = LOAD_UINT32_LITTLE(d+3)(get_u32_le(d+3));
431 d4 = LOAD_UINT32_LITTLE(d+4)(get_u32_le(d+4)); d5 = LOAD_UINT32_LITTLE(d+5)(get_u32_le(d+5));
432 d6 = LOAD_UINT32_LITTLE(d+6)(get_u32_le(d+6)); d7 = LOAD_UINT32_LITTLE(d+7)(get_u32_le(d+7));
433 k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11);
434 k12 = *(k+12); k13 = *(k+13); k14 = *(k+14); k15 = *(k+15);
435
436 h1 += MUL64((k0 + d0), (k4 + d4))((UINT64)((UINT64)(UINT32)((k0 + d0)) * (UINT64)(UINT32)((k4 +
d4))))
;
437 h2 += MUL64((k4 + d0), (k8 + d4))((UINT64)((UINT64)(UINT32)((k4 + d0)) * (UINT64)(UINT32)((k8 +
d4))))
;
438 h3 += MUL64((k8 + d0), (k12 + d4))((UINT64)((UINT64)(UINT32)((k8 + d0)) * (UINT64)(UINT32)((k12
+ d4))))
;
439
440 h1 += MUL64((k1 + d1), (k5 + d5))((UINT64)((UINT64)(UINT32)((k1 + d1)) * (UINT64)(UINT32)((k5 +
d5))))
;
441 h2 += MUL64((k5 + d1), (k9 + d5))((UINT64)((UINT64)(UINT32)((k5 + d1)) * (UINT64)(UINT32)((k9 +
d5))))
;
442 h3 += MUL64((k9 + d1), (k13 + d5))((UINT64)((UINT64)(UINT32)((k9 + d1)) * (UINT64)(UINT32)((k13
+ d5))))
;
443
444 h1 += MUL64((k2 + d2), (k6 + d6))((UINT64)((UINT64)(UINT32)((k2 + d2)) * (UINT64)(UINT32)((k6 +
d6))))
;
445 h2 += MUL64((k6 + d2), (k10 + d6))((UINT64)((UINT64)(UINT32)((k6 + d2)) * (UINT64)(UINT32)((k10
+ d6))))
;
446 h3 += MUL64((k10 + d2), (k14 + d6))((UINT64)((UINT64)(UINT32)((k10 + d2)) * (UINT64)(UINT32)((k14
+ d6))))
;
447
448 h1 += MUL64((k3 + d3), (k7 + d7))((UINT64)((UINT64)(UINT32)((k3 + d3)) * (UINT64)(UINT32)((k7 +
d7))))
;
449 h2 += MUL64((k7 + d3), (k11 + d7))((UINT64)((UINT64)(UINT32)((k7 + d3)) * (UINT64)(UINT32)((k11
+ d7))))
;
450 h3 += MUL64((k11 + d3), (k15 + d7))((UINT64)((UINT64)(UINT32)((k11 + d3)) * (UINT64)(UINT32)((k15
+ d7))))
;
451
452 k0 = k8; k1 = k9; k2 = k10; k3 = k11;
453 k4 = k12; k5 = k13; k6 = k14; k7 = k15;
454
455 d += 8;
456 k += 8;
457 } while (--c);
458 ((UINT64 *)hp)[0] = h1;
459 ((UINT64 *)hp)[1] = h2;
460 ((UINT64 *)hp)[2] = h3;
461}
462
463#elif (UMAC_OUTPUT_LEN16 == 16)
464
465static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
466/* Same as previous nh_aux, but two streams are handled in one pass,
467 * reading and writing 24 bytes of hash-state per call.
468*/
469{
470 UINT64 h1,h2,h3,h4;
471 UWORD c = dlen / 32;
472 UINT32 *k = (UINT32 *)kp;
473 const UINT32 *d = (const UINT32 *)dp;
474 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
475 UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
476 k8,k9,k10,k11,k12,k13,k14,k15,
477 k16,k17,k18,k19;
478
479 h1 = *((UINT64 *)hp);
480 h2 = *((UINT64 *)hp + 1);
481 h3 = *((UINT64 *)hp + 2);
482 h4 = *((UINT64 *)hp + 3);
483 k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
484 k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
485 do {
486 d0 = LOAD_UINT32_LITTLE(d+0)(get_u32_le(d+0)); d1 = LOAD_UINT32_LITTLE(d+1)(get_u32_le(d+1));
487 d2 = LOAD_UINT32_LITTLE(d+2)(get_u32_le(d+2)); d3 = LOAD_UINT32_LITTLE(d+3)(get_u32_le(d+3));
488 d4 = LOAD_UINT32_LITTLE(d+4)(get_u32_le(d+4)); d5 = LOAD_UINT32_LITTLE(d+5)(get_u32_le(d+5));
489 d6 = LOAD_UINT32_LITTLE(d+6)(get_u32_le(d+6)); d7 = LOAD_UINT32_LITTLE(d+7)(get_u32_le(d+7));
490 k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11);
491 k12 = *(k+12); k13 = *(k+13); k14 = *(k+14); k15 = *(k+15);
492 k16 = *(k+16); k17 = *(k+17); k18 = *(k+18); k19 = *(k+19);
493
494 h1 += MUL64((k0 + d0), (k4 + d4))((UINT64)((UINT64)(UINT32)((k0 + d0)) * (UINT64)(UINT32)((k4 +
d4))))
;
495 h2 += MUL64((k4 + d0), (k8 + d4))((UINT64)((UINT64)(UINT32)((k4 + d0)) * (UINT64)(UINT32)((k8 +
d4))))
;
496 h3 += MUL64((k8 + d0), (k12 + d4))((UINT64)((UINT64)(UINT32)((k8 + d0)) * (UINT64)(UINT32)((k12
+ d4))))
;
497 h4 += MUL64((k12 + d0), (k16 + d4))((UINT64)((UINT64)(UINT32)((k12 + d0)) * (UINT64)(UINT32)((k16
+ d4))))
;
498
499 h1 += MUL64((k1 + d1), (k5 + d5))((UINT64)((UINT64)(UINT32)((k1 + d1)) * (UINT64)(UINT32)((k5 +
d5))))
;
500 h2 += MUL64((k5 + d1), (k9 + d5))((UINT64)((UINT64)(UINT32)((k5 + d1)) * (UINT64)(UINT32)((k9 +
d5))))
;
501 h3 += MUL64((k9 + d1), (k13 + d5))((UINT64)((UINT64)(UINT32)((k9 + d1)) * (UINT64)(UINT32)((k13
+ d5))))
;
502 h4 += MUL64((k13 + d1), (k17 + d5))((UINT64)((UINT64)(UINT32)((k13 + d1)) * (UINT64)(UINT32)((k17
+ d5))))
;
503
504 h1 += MUL64((k2 + d2), (k6 + d6))((UINT64)((UINT64)(UINT32)((k2 + d2)) * (UINT64)(UINT32)((k6 +
d6))))
;
505 h2 += MUL64((k6 + d2), (k10 + d6))((UINT64)((UINT64)(UINT32)((k6 + d2)) * (UINT64)(UINT32)((k10
+ d6))))
;
506 h3 += MUL64((k10 + d2), (k14 + d6))((UINT64)((UINT64)(UINT32)((k10 + d2)) * (UINT64)(UINT32)((k14
+ d6))))
;
507 h4 += MUL64((k14 + d2), (k18 + d6))((UINT64)((UINT64)(UINT32)((k14 + d2)) * (UINT64)(UINT32)((k18
+ d6))))
;
508
509 h1 += MUL64((k3 + d3), (k7 + d7))((UINT64)((UINT64)(UINT32)((k3 + d3)) * (UINT64)(UINT32)((k7 +
d7))))
;
510 h2 += MUL64((k7 + d3), (k11 + d7))((UINT64)((UINT64)(UINT32)((k7 + d3)) * (UINT64)(UINT32)((k11
+ d7))))
;
511 h3 += MUL64((k11 + d3), (k15 + d7))((UINT64)((UINT64)(UINT32)((k11 + d3)) * (UINT64)(UINT32)((k15
+ d7))))
;
512 h4 += MUL64((k15 + d3), (k19 + d7))((UINT64)((UINT64)(UINT32)((k15 + d3)) * (UINT64)(UINT32)((k19
+ d7))))
;
513
514 k0 = k8; k1 = k9; k2 = k10; k3 = k11;
515 k4 = k12; k5 = k13; k6 = k14; k7 = k15;
516 k8 = k16; k9 = k17; k10 = k18; k11 = k19;
Value stored to 'k8' is never read
517
518 d += 8;
519 k += 8;
520 } while (--c);
521 ((UINT64 *)hp)[0] = h1;
522 ((UINT64 *)hp)[1] = h2;
523 ((UINT64 *)hp)[2] = h3;
524 ((UINT64 *)hp)[3] = h4;
525}
526
527/* ---------------------------------------------------------------------- */
528#endif /* UMAC_OUTPUT_LENGTH */
529/* ---------------------------------------------------------------------- */
530
531
532/* ---------------------------------------------------------------------- */
533
534static void nh_transform(nh_ctx *hc, const UINT8 *buf, UINT32 nbytes)
535/* This function is a wrapper for the primitive NH hash functions. It takes
536 * as argument "hc" the current hash context and a buffer which must be a
537 * multiple of L1_PAD_BOUNDARY. The key passed to nh_aux is offset
538 * appropriately according to how much message has been hashed already.
539 */
540{
541 UINT8 *key;
542
543 key = hc->nh_key + hc->bytes_hashed;
544 nh_aux(key, buf, hc->state, nbytes);
545}
546
547/* ---------------------------------------------------------------------- */
548
549#if (__LITTLE_ENDIAN__1)
550static void endian_convert(void *buf, UWORD bpw, UINT32 num_bytes)
551/* We endian convert the keys on little-endian computers to */
552/* compensate for the lack of big-endian memory reads during hashing. */
553{
554 UWORD iters = num_bytes / bpw;
555 if (bpw == 4) {
556 UINT32 *p = (UINT32 *)buf;
557 do {
558 *p = LOAD_UINT32_REVERSED(p)get_u32(p);
559 p++;
560 } while (--iters);
561 } else if (bpw == 8) {
562 UINT32 *p = (UINT32 *)buf;
563 UINT32 t;
564 do {
565 t = LOAD_UINT32_REVERSED(p+1)get_u32(p+1);
566 p[1] = LOAD_UINT32_REVERSED(p)get_u32(p);
567 p[0] = t;
568 p += 2;
569 } while (--iters);
570 }
571}
572#define endian_convert_if_le(x,y,z)endian_convert((x),(y),(z)) endian_convert((x),(y),(z))
573#else
574#define endian_convert_if_le(x,y,z)endian_convert((x),(y),(z)) do{}while(0) /* Do nothing */
575#endif
576
577/* ---------------------------------------------------------------------- */
578
579static void nh_reset(nh_ctx *hc)
580/* Reset nh_ctx to ready for hashing of new data */
581{
582 hc->bytes_hashed = 0;
583 hc->next_data_empty = 0;
584 hc->state[0] = 0;
585#if (UMAC_OUTPUT_LEN16 >= 8)
586 hc->state[1] = 0;
587#endif
588#if (UMAC_OUTPUT_LEN16 >= 12)
589 hc->state[2] = 0;
590#endif
591#if (UMAC_OUTPUT_LEN16 == 16)
592 hc->state[3] = 0;
593#endif
594
595}
596
597/* ---------------------------------------------------------------------- */
598
599static void nh_init(nh_ctx *hc, aes_int_key prf_key)
600/* Generate nh_key, endian convert and reset to be ready for hashing. */
601{
602 kdf(hc->nh_key, prf_key, 1, sizeof(hc->nh_key));
603 endian_convert_if_le(hc->nh_key, 4, sizeof(hc->nh_key))endian_convert((hc->nh_key),(4),(sizeof(hc->nh_key)));
604 nh_reset(hc);
605}
606
607/* ---------------------------------------------------------------------- */
608
609static void nh_update(nh_ctx *hc, const UINT8 *buf, UINT32 nbytes)
610/* Incorporate nbytes of data into a nh_ctx, buffer whatever is not an */
611/* even multiple of HASH_BUF_BYTES. */
612{
613 UINT32 i,j;
614
615 j = hc->next_data_empty;
616 if ((j + nbytes) >= HASH_BUF_BYTES64) {
617 if (j) {
618 i = HASH_BUF_BYTES64 - j;
619 memcpy(hc->data+j, buf, i);
620 nh_transform(hc,hc->data,HASH_BUF_BYTES64);
621 nbytes -= i;
622 buf += i;
623 hc->bytes_hashed += HASH_BUF_BYTES64;
624 }
625 if (nbytes >= HASH_BUF_BYTES64) {
626 i = nbytes & ~(HASH_BUF_BYTES64 - 1);
627 nh_transform(hc, buf, i);
628 nbytes -= i;
629 buf += i;
630 hc->bytes_hashed += i;
631 }
632 j = 0;
633 }
634 memcpy(hc->data + j, buf, nbytes);
635 hc->next_data_empty = j + nbytes;
636}
637
638/* ---------------------------------------------------------------------- */
639
640static void zero_pad(UINT8 *p, int nbytes)
641{
642/* Write "nbytes" of zeroes, beginning at "p" */
643 if (nbytes >= (int)sizeof(UWORD)) {
644 while ((ptrdiff_t)p % sizeof(UWORD)) {
645 *p = 0;
646 nbytes--;
647 p++;
648 }
649 while (nbytes >= (int)sizeof(UWORD)) {
650 *(UWORD *)p = 0;
651 nbytes -= sizeof(UWORD);
652 p += sizeof(UWORD);
653 }
654 }
655 while (nbytes) {
656 *p = 0;
657 nbytes--;
658 p++;
659 }
660}
661
662/* ---------------------------------------------------------------------- */
663
664static void nh_final(nh_ctx *hc, UINT8 *result)
665/* After passing some number of data buffers to nh_update() for integration
666 * into an NH context, nh_final is called to produce a hash result. If any
667 * bytes are in the buffer hc->data, incorporate them into the
668 * NH context. Finally, add into the NH accumulation "state" the total number
669 * of bits hashed. The resulting numbers are written to the buffer "result".
670 * If nh_update was never called, L1_PAD_BOUNDARY zeroes are incorporated.
671 */
672{
673 int nh_len, nbits;
674
675 if (hc->next_data_empty != 0) {
676 nh_len = ((hc->next_data_empty + (L1_PAD_BOUNDARY32 - 1)) &
677 ~(L1_PAD_BOUNDARY32 - 1));
678 zero_pad(hc->data + hc->next_data_empty,
679 nh_len - hc->next_data_empty);
680 nh_transform(hc, hc->data, nh_len);
681 hc->bytes_hashed += hc->next_data_empty;
682 } else if (hc->bytes_hashed == 0) {
683 nh_len = L1_PAD_BOUNDARY32;
684 zero_pad(hc->data, L1_PAD_BOUNDARY32);
685 nh_transform(hc, hc->data, nh_len);
686 }
687
688 nbits = (hc->bytes_hashed << 3);
689 ((UINT64 *)result)[0] = ((UINT64 *)hc->state)[0] + nbits;
690#if (UMAC_OUTPUT_LEN16 >= 8)
691 ((UINT64 *)result)[1] = ((UINT64 *)hc->state)[1] + nbits;
692#endif
693#if (UMAC_OUTPUT_LEN16 >= 12)
694 ((UINT64 *)result)[2] = ((UINT64 *)hc->state)[2] + nbits;
695#endif
696#if (UMAC_OUTPUT_LEN16 == 16)
697 ((UINT64 *)result)[3] = ((UINT64 *)hc->state)[3] + nbits;
698#endif
699 nh_reset(hc);
700}
701
702/* ---------------------------------------------------------------------- */
703
704static void nh(nh_ctx *hc, const UINT8 *buf, UINT32 padded_len,
705 UINT32 unpadded_len, UINT8 *result)
706/* All-in-one nh_update() and nh_final() equivalent.
707 * Assumes that padded_len is divisible by L1_PAD_BOUNDARY and result is
708 * well aligned
709 */
710{
711 UINT32 nbits;
712
713 /* Initialize the hash state */
714 nbits = (unpadded_len << 3);
715
716 ((UINT64 *)result)[0] = nbits;
717#if (UMAC_OUTPUT_LEN16 >= 8)
718 ((UINT64 *)result)[1] = nbits;
719#endif
720#if (UMAC_OUTPUT_LEN16 >= 12)
721 ((UINT64 *)result)[2] = nbits;
722#endif
723#if (UMAC_OUTPUT_LEN16 == 16)
724 ((UINT64 *)result)[3] = nbits;
725#endif
726
727 nh_aux(hc->nh_key, buf, result, padded_len);
728}
729
730/* ---------------------------------------------------------------------- */
731/* ---------------------------------------------------------------------- */
732/* ----- Begin UHASH Section -------------------------------------------- */
733/* ---------------------------------------------------------------------- */
734/* ---------------------------------------------------------------------- */
735
736/* UHASH is a multi-layered algorithm. Data presented to UHASH is first
737 * hashed by NH. The NH output is then hashed by a polynomial-hash layer
738 * unless the initial data to be hashed is short. After the polynomial-
739 * layer, an inner-product hash is used to produce the final UHASH output.
740 *
741 * UHASH provides two interfaces, one all-at-once and another where data
742 * buffers are presented sequentially. In the sequential interface, the
743 * UHASH client calls the routine uhash_update() as many times as necessary.
744 * When there is no more data to be fed to UHASH, the client calls
745 * uhash_final() which
746 * calculates the UHASH output. Before beginning another UHASH calculation
747 * the uhash_reset() routine must be called. The all-at-once UHASH routine,
748 * uhash(), is equivalent to the sequence of calls uhash_update() and
749 * uhash_final(); however it is optimized and should be
750 * used whenever the sequential interface is not necessary.
751 *
752 * The routine uhash_init() initializes the uhash_ctx data structure and
753 * must be called once, before any other UHASH routine.
754 */
755
756/* ---------------------------------------------------------------------- */
757/* ----- Constants and uhash_ctx ---------------------------------------- */
758/* ---------------------------------------------------------------------- */
759
760/* ---------------------------------------------------------------------- */
761/* ----- Poly hash and Inner-Product hash Constants --------------------- */
762/* ---------------------------------------------------------------------- */
763
764/* Primes and masks */
765#define p36((UINT64)0x0000000FFFFFFFFBull) ((UINT64)0x0000000FFFFFFFFBull) /* 2^36 - 5 */
766#define p64((UINT64)0xFFFFFFFFFFFFFFC5ull) ((UINT64)0xFFFFFFFFFFFFFFC5ull) /* 2^64 - 59 */
767#define m36((UINT64)0x0000000FFFFFFFFFull) ((UINT64)0x0000000FFFFFFFFFull) /* The low 36 of 64 bits */
768
769
770/* ---------------------------------------------------------------------- */
771
772typedef struct uhash_ctx {
773 nh_ctx hash; /* Hash context for L1 NH hash */
774 UINT64 poly_key_8[STREAMS(16 / 4)]; /* p64 poly keys */
775 UINT64 poly_accum[STREAMS(16 / 4)]; /* poly hash result */
776 UINT64 ip_keys[STREAMS(16 / 4)*4]; /* Inner-product keys */
777 UINT32 ip_trans[STREAMS(16 / 4)]; /* Inner-product translation */
778 UINT32 msg_len; /* Total length of data passed */
779 /* to uhash */
780} uhash_ctx;
781typedef struct uhash_ctx *uhash_ctx_t;
782
783/* ---------------------------------------------------------------------- */
784
785
786/* The polynomial hashes use Horner's rule to evaluate a polynomial one
787 * word at a time. As described in the specification, poly32 and poly64
788 * require keys from special domains. The following implementations exploit
789 * the special domains to avoid overflow. The results are not guaranteed to
790 * be within Z_p32 and Z_p64, but the Inner-Product hash implementation
791 * patches any errant values.
792 */
793
794static UINT64 poly64(UINT64 cur, UINT64 key, UINT64 data)
795{
796 UINT32 key_hi = (UINT32)(key >> 32),
797 key_lo = (UINT32)key,
798 cur_hi = (UINT32)(cur >> 32),
799 cur_lo = (UINT32)cur,
800 x_lo,
801 x_hi;
802 UINT64 X,T,res;
803
804 X = MUL64(key_hi, cur_lo)((UINT64)((UINT64)(UINT32)(key_hi) * (UINT64)(UINT32)(cur_lo)
))
+ MUL64(cur_hi, key_lo)((UINT64)((UINT64)(UINT32)(cur_hi) * (UINT64)(UINT32)(key_lo)
))
;
805 x_lo = (UINT32)X;
806 x_hi = (UINT32)(X >> 32);
807
808 res = (MUL64(key_hi, cur_hi)((UINT64)((UINT64)(UINT32)(key_hi) * (UINT64)(UINT32)(cur_hi)
))
+ x_hi) * 59 + MUL64(key_lo, cur_lo)((UINT64)((UINT64)(UINT32)(key_lo) * (UINT64)(UINT32)(cur_lo)
))
;
809
810 T = ((UINT64)x_lo << 32);
811 res += T;
812 if (res < T)
813 res += 59;
814
815 res += data;
816 if (res < data)
817 res += 59;
818
819 return res;
820}
821
822
823/* Although UMAC is specified to use a ramped polynomial hash scheme, this
824 * implementation does not handle all ramp levels. Because we don't handle
825 * the ramp up to p128 modulus in this implementation, we are limited to
826 * 2^14 poly_hash() invocations per stream (for a total capacity of 2^24
827 * bytes input to UMAC per tag, ie. 16MB).
828 */
829static void poly_hash(uhash_ctx_t hc, UINT32 data_in[])
830{
831 int i;
832 UINT64 *data=(UINT64*)data_in;
833
834 for (i = 0; i < STREAMS(16 / 4); i++) {
835 if ((UINT32)(data[i] >> 32) == 0xfffffffful) {
836 hc->poly_accum[i] = poly64(hc->poly_accum[i],
837 hc->poly_key_8[i], p64((UINT64)0xFFFFFFFFFFFFFFC5ull) - 1);
838 hc->poly_accum[i] = poly64(hc->poly_accum[i],
839 hc->poly_key_8[i], (data[i] - 59));
840 } else {
841 hc->poly_accum[i] = poly64(hc->poly_accum[i],
842 hc->poly_key_8[i], data[i]);
843 }
844 }
845}
846
847
848/* ---------------------------------------------------------------------- */
849
850
851/* The final step in UHASH is an inner-product hash. The poly hash
852 * produces a result not necessarily WORD_LEN bytes long. The inner-
853 * product hash breaks the polyhash output into 16-bit chunks and
854 * multiplies each with a 36 bit key.
855 */
856
857static UINT64 ip_aux(UINT64 t, UINT64 *ipkp, UINT64 data)
858{
859 t = t + ipkp[0] * (UINT64)(UINT16)(data >> 48);
860 t = t + ipkp[1] * (UINT64)(UINT16)(data >> 32);
861 t = t + ipkp[2] * (UINT64)(UINT16)(data >> 16);
862 t = t + ipkp[3] * (UINT64)(UINT16)(data);
863
864 return t;
865}
866
867static UINT32 ip_reduce_p36(UINT64 t)
868{
869/* Divisionless modular reduction */
870 UINT64 ret;
871
872 ret = (t & m36((UINT64)0x0000000FFFFFFFFFull)) + 5 * (t >> 36);
873 if (ret >= p36((UINT64)0x0000000FFFFFFFFBull))
874 ret -= p36((UINT64)0x0000000FFFFFFFFBull);
875
876 /* return least significant 32 bits */
877 return (UINT32)(ret);
878}
879
880
881/* If the data being hashed by UHASH is no longer than L1_KEY_LEN, then
882 * the polyhash stage is skipped and ip_short is applied directly to the
883 * NH output.
884 */
885static void ip_short(uhash_ctx_t ahc, UINT8 *nh_res, u_char *res)
886{
887 UINT64 t;
888 UINT64 *nhp = (UINT64 *)nh_res;
889
890 t = ip_aux(0,ahc->ip_keys, nhp[0]);
891 STORE_UINT32_BIG((UINT32 *)res+0, ip_reduce_p36(t) ^ ahc->ip_trans[0])put_u32((UINT32 *)res+0, ip_reduce_p36(t) ^ ahc->ip_trans[
0])
;
892#if (UMAC_OUTPUT_LEN16 >= 8)
893 t = ip_aux(0,ahc->ip_keys+4, nhp[1]);
894 STORE_UINT32_BIG((UINT32 *)res+1, ip_reduce_p36(t) ^ ahc->ip_trans[1])put_u32((UINT32 *)res+1, ip_reduce_p36(t) ^ ahc->ip_trans[
1])
;
895#endif
896#if (UMAC_OUTPUT_LEN16 >= 12)
897 t = ip_aux(0,ahc->ip_keys+8, nhp[2]);
898 STORE_UINT32_BIG((UINT32 *)res+2, ip_reduce_p36(t) ^ ahc->ip_trans[2])put_u32((UINT32 *)res+2, ip_reduce_p36(t) ^ ahc->ip_trans[
2])
;
899#endif
900#if (UMAC_OUTPUT_LEN16 == 16)
901 t = ip_aux(0,ahc->ip_keys+12, nhp[3]);
902 STORE_UINT32_BIG((UINT32 *)res+3, ip_reduce_p36(t) ^ ahc->ip_trans[3])put_u32((UINT32 *)res+3, ip_reduce_p36(t) ^ ahc->ip_trans[
3])
;
903#endif
904}
905
906/* If the data being hashed by UHASH is longer than L1_KEY_LEN, then
907 * the polyhash stage is not skipped and ip_long is applied to the
908 * polyhash output.
909 */
910static void ip_long(uhash_ctx_t ahc, u_char *res)
911{
912 int i;
913 UINT64 t;
914
915 for (i = 0; i < STREAMS(16 / 4); i++) {
916 /* fix polyhash output not in Z_p64 */
917 if (ahc->poly_accum[i] >= p64((UINT64)0xFFFFFFFFFFFFFFC5ull))
918 ahc->poly_accum[i] -= p64((UINT64)0xFFFFFFFFFFFFFFC5ull);
919 t = ip_aux(0,ahc->ip_keys+(i*4), ahc->poly_accum[i]);
920 STORE_UINT32_BIG((UINT32 *)res+i,put_u32((UINT32 *)res+i, ip_reduce_p36(t) ^ ahc->ip_trans[
i])
921 ip_reduce_p36(t) ^ ahc->ip_trans[i])put_u32((UINT32 *)res+i, ip_reduce_p36(t) ^ ahc->ip_trans[
i])
;
922 }
923}
924
925
926/* ---------------------------------------------------------------------- */
927
928/* ---------------------------------------------------------------------- */
929
930/* Reset uhash context for next hash session */
931static int uhash_reset(uhash_ctx_t pc)
932{
933 nh_reset(&pc->hash);
934 pc->msg_len = 0;
935 pc->poly_accum[0] = 1;
936#if (UMAC_OUTPUT_LEN16 >= 8)
937 pc->poly_accum[1] = 1;
938#endif
939#if (UMAC_OUTPUT_LEN16 >= 12)
940 pc->poly_accum[2] = 1;
941#endif
942#if (UMAC_OUTPUT_LEN16 == 16)
943 pc->poly_accum[3] = 1;
944#endif
945 return 1;
946}
947
948/* ---------------------------------------------------------------------- */
949
950/* Given a pointer to the internal key needed by kdf() and a uhash context,
951 * initialize the NH context and generate keys needed for poly and inner-
952 * product hashing. All keys are endian adjusted in memory so that native
953 * loads cause correct keys to be in registers during calculation.
954 */
955static void uhash_init(uhash_ctx_t ahc, aes_int_key prf_key)
956{
957 int i;
958 UINT8 buf[(8*STREAMS(16 / 4)+4)*sizeof(UINT64)];
959
960 /* Zero the entire uhash context */
961 memset(ahc, 0, sizeof(uhash_ctx));
962
963 /* Initialize the L1 hash */
964 nh_init(&ahc->hash, prf_key);
965
966 /* Setup L2 hash variables */
967 kdf(buf, prf_key, 2, sizeof(buf)); /* Fill buffer with index 1 key */
968 for (i = 0; i < STREAMS(16 / 4); i++) {
969 /* Fill keys from the buffer, skipping bytes in the buffer not
970 * used by this implementation. Endian reverse the keys if on a
971 * little-endian computer.
972 */
973 memcpy(ahc->poly_key_8+i, buf+24*i, 8);
974 endian_convert_if_le(ahc->poly_key_8+i, 8, 8)endian_convert((ahc->poly_key_8+i),(8),(8));
975 /* Mask the 64-bit keys to their special domain */
976 ahc->poly_key_8[i] &= ((UINT64)0x01ffffffu << 32) + 0x01ffffffu;
977 ahc->poly_accum[i] = 1; /* Our polyhash prepends a non-zero word */
978 }
979
980 /* Setup L3-1 hash variables */
981 kdf(buf, prf_key, 3, sizeof(buf)); /* Fill buffer with index 2 key */
982 for (i = 0; i < STREAMS(16 / 4); i++)
983 memcpy(ahc->ip_keys+4*i, buf+(8*i+4)*sizeof(UINT64),
984 4*sizeof(UINT64));
985 endian_convert_if_le(ahc->ip_keys, sizeof(UINT64),endian_convert((ahc->ip_keys),(sizeof(UINT64)),(sizeof(ahc
->ip_keys)))
986 sizeof(ahc->ip_keys))endian_convert((ahc->ip_keys),(sizeof(UINT64)),(sizeof(ahc
->ip_keys)))
;
987 for (i = 0; i < STREAMS(16 / 4)*4; i++)
988 ahc->ip_keys[i] %= p36((UINT64)0x0000000FFFFFFFFBull); /* Bring into Z_p36 */
989
990 /* Setup L3-2 hash variables */
991 /* Fill buffer with index 4 key */
992 kdf(ahc->ip_trans, prf_key, 4, STREAMS(16 / 4) * sizeof(UINT32));
993 endian_convert_if_le(ahc->ip_trans, sizeof(UINT32),endian_convert((ahc->ip_trans),(sizeof(UINT32)),((16 / 4) *
sizeof(UINT32)))
994 STREAMS * sizeof(UINT32))endian_convert((ahc->ip_trans),(sizeof(UINT32)),((16 / 4) *
sizeof(UINT32)))
;
995 explicit_bzero(buf, sizeof(buf));
996}
997
998/* ---------------------------------------------------------------------- */
999
1000#if 0
1001static uhash_ctx_t uhash_alloc(u_char key[])
1002{
1003/* Allocate memory and force to a 16-byte boundary. */
1004 uhash_ctx_t ctx;
1005 u_char bytes_to_add;
1006 aes_int_key prf_key;
1007
1008 ctx = (uhash_ctx_t)malloc(sizeof(uhash_ctx)+ALLOC_BOUNDARY16);
1009 if (ctx) {
1010 if (ALLOC_BOUNDARY16) {
1011 bytes_to_add = ALLOC_BOUNDARY16 -
1012 ((ptrdiff_t)ctx & (ALLOC_BOUNDARY16 -1));
1013 ctx = (uhash_ctx_t)((u_char *)ctx + bytes_to_add);
1014 *((u_char *)ctx - 1) = bytes_to_add;
1015 }
1016 aes_key_setup(key,prf_key)AES_set_encrypt_key((const u_char *)(key),16*8,prf_key);
1017 uhash_init(ctx, prf_key);
1018 }
1019 return (ctx);
1020}
1021#endif
1022
1023/* ---------------------------------------------------------------------- */
1024
1025#if 0
1026static int uhash_free(uhash_ctx_t ctx)
1027{
1028/* Free memory allocated by uhash_alloc */
1029 u_char bytes_to_sub;
1030
1031 if (ctx) {
1032 if (ALLOC_BOUNDARY16) {
1033 bytes_to_sub = *((u_char *)ctx - 1);
1034 ctx = (uhash_ctx_t)((u_char *)ctx - bytes_to_sub);
1035 }
1036 free(ctx);
1037 }
1038 return (1);
1039}
1040#endif
1041/* ---------------------------------------------------------------------- */
1042
1043static int uhash_update(uhash_ctx_t ctx, const u_char *input, long len)
1044/* Given len bytes of data, we parse it into L1_KEY_LEN chunks and
1045 * hash each one with NH, calling the polyhash on each NH output.
1046 */
1047{
1048 UWORD bytes_hashed, bytes_remaining;
1049 UINT64 result_buf[STREAMS(16 / 4)];
1050 UINT8 *nh_result = (UINT8 *)&result_buf;
1051
1052 if (ctx->msg_len + len <= L1_KEY_LEN1024) {
1053 nh_update(&ctx->hash, (const UINT8 *)input, len);
1054 ctx->msg_len += len;
1055 } else {
1056
1057 bytes_hashed = ctx->msg_len % L1_KEY_LEN1024;
1058 if (ctx->msg_len == L1_KEY_LEN1024)
1059 bytes_hashed = L1_KEY_LEN1024;
1060
1061 if (bytes_hashed + len >= L1_KEY_LEN1024) {
1062
1063 /* If some bytes have been passed to the hash function */
1064 /* then we want to pass at most (L1_KEY_LEN - bytes_hashed) */
1065 /* bytes to complete the current nh_block. */
1066 if (bytes_hashed) {
1067 bytes_remaining = (L1_KEY_LEN1024 - bytes_hashed);
1068 nh_update(&ctx->hash, (const UINT8 *)input, bytes_remaining);
1069 nh_final(&ctx->hash, nh_result);
1070 ctx->msg_len += bytes_remaining;
1071 poly_hash(ctx,(UINT32 *)nh_result);
1072 len -= bytes_remaining;
1073 input += bytes_remaining;
1074 }
1075
1076 /* Hash directly from input stream if enough bytes */
1077 while (len >= L1_KEY_LEN1024) {
1078 nh(&ctx->hash, (const UINT8 *)input, L1_KEY_LEN1024,
1079 L1_KEY_LEN1024, nh_result);
1080 ctx->msg_len += L1_KEY_LEN1024;
1081 len -= L1_KEY_LEN1024;
1082 input += L1_KEY_LEN1024;
1083 poly_hash(ctx,(UINT32 *)nh_result);
1084 }
1085 }
1086
1087 /* pass remaining < L1_KEY_LEN bytes of input data to NH */
1088 if (len) {
1089 nh_update(&ctx->hash, (const UINT8 *)input, len);
1090 ctx->msg_len += len;
1091 }
1092 }
1093
1094 return (1);
1095}
1096
1097/* ---------------------------------------------------------------------- */
1098
1099static int uhash_final(uhash_ctx_t ctx, u_char *res)
1100/* Incorporate any pending data, pad, and generate tag */
1101{
1102 UINT64 result_buf[STREAMS(16 / 4)];
1103 UINT8 *nh_result = (UINT8 *)&result_buf;
1104
1105 if (ctx->msg_len > L1_KEY_LEN1024) {
1106 if (ctx->msg_len % L1_KEY_LEN1024) {
1107 nh_final(&ctx->hash, nh_result);
1108 poly_hash(ctx,(UINT32 *)nh_result);
1109 }
1110 ip_long(ctx, res);
1111 } else {
1112 nh_final(&ctx->hash, nh_result);
1113 ip_short(ctx,nh_result, res);
1114 }
1115 uhash_reset(ctx);
1116 return (1);
1117}
1118
1119/* ---------------------------------------------------------------------- */
1120
1121#if 0
1122static int uhash(uhash_ctx_t ahc, u_char *msg, long len, u_char *res)
1123/* assumes that msg is in a writable buffer of length divisible by */
1124/* L1_PAD_BOUNDARY. Bytes beyond msg[len] may be zeroed. */
1125{
1126 UINT8 nh_result[STREAMS(16 / 4)*sizeof(UINT64)];
1127 UINT32 nh_len;
1128 int extra_zeroes_needed;
1129
1130 /* If the message to be hashed is no longer than L1_HASH_LEN, we skip
1131 * the polyhash.
1132 */
1133 if (len <= L1_KEY_LEN1024) {
1134 if (len == 0) /* If zero length messages will not */
1135 nh_len = L1_PAD_BOUNDARY32; /* be seen, comment out this case */
1136 else
1137 nh_len = ((len + (L1_PAD_BOUNDARY32 - 1)) & ~(L1_PAD_BOUNDARY32 - 1));
1138 extra_zeroes_needed = nh_len - len;
1139 zero_pad((UINT8 *)msg + len, extra_zeroes_needed);
1140 nh(&ahc->hash, (UINT8 *)msg, nh_len, len, nh_result);
1141 ip_short(ahc,nh_result, res);
1142 } else {
1143 /* Otherwise, we hash each L1_KEY_LEN chunk with NH, passing the NH
1144 * output to poly_hash().
1145 */
1146 do {
1147 nh(&ahc->hash, (UINT8 *)msg, L1_KEY_LEN1024, L1_KEY_LEN1024, nh_result);
1148 poly_hash(ahc,(UINT32 *)nh_result);
1149 len -= L1_KEY_LEN1024;
1150 msg += L1_KEY_LEN1024;
1151 } while (len >= L1_KEY_LEN1024);
1152 if (len) {
1153 nh_len = ((len + (L1_PAD_BOUNDARY32 - 1)) & ~(L1_PAD_BOUNDARY32 - 1));
1154 extra_zeroes_needed = nh_len - len;
1155 zero_pad((UINT8 *)msg + len, extra_zeroes_needed);
1156 nh(&ahc->hash, (UINT8 *)msg, nh_len, len, nh_result);
1157 poly_hash(ahc,(UINT32 *)nh_result);
1158 }
1159
1160 ip_long(ahc, res);
1161 }
1162
1163 uhash_reset(ahc);
1164 return 1;
1165}
1166#endif
1167
1168/* ---------------------------------------------------------------------- */
1169/* ---------------------------------------------------------------------- */
1170/* ----- Begin UMAC Section --------------------------------------------- */
1171/* ---------------------------------------------------------------------- */
1172/* ---------------------------------------------------------------------- */
1173
1174/* The UMAC interface has two interfaces, an all-at-once interface where
1175 * the entire message to be authenticated is passed to UMAC in one buffer,
1176 * and a sequential interface where the message is presented a little at a
1177 * time. The all-at-once is more optimized than the sequential version and
1178 * should be preferred when the sequential interface is not required.
1179 */
1180struct umac_ctxumac128_ctx {
1181 uhash_ctx hash; /* Hash function for message compression */
1182 pdf_ctx pdf; /* PDF for hashed output */
1183 void *free_ptr; /* Address to free this struct via */
1184} umac_ctxumac128_ctx;
1185
1186/* ---------------------------------------------------------------------- */
1187
1188#if 0
1189int umac_reset(struct umac_ctxumac128_ctx *ctx)
1190/* Reset the hash function to begin a new authentication. */
1191{
1192 uhash_reset(&ctx->hash);
1193 return (1);
1194}
1195#endif
1196
1197/* ---------------------------------------------------------------------- */
1198
1199int umac_deleteumac128_delete(struct umac_ctxumac128_ctx *ctx)
1200/* Deallocate the ctx structure */
1201{
1202 if (ctx) {
1203 if (ALLOC_BOUNDARY16)
1204 ctx = (struct umac_ctxumac128_ctx *)ctx->free_ptr;
1205 freezero(ctx, sizeof(*ctx) + ALLOC_BOUNDARY16);
1206 }
1207 return (1);
1208}
1209
1210/* ---------------------------------------------------------------------- */
1211
1212struct umac_ctxumac128_ctx *umac_newumac128_new(const u_char key[])
1213/* Dynamically allocate a umac_ctx struct, initialize variables,
1214 * generate subkeys from key. Align to 16-byte boundary.
1215 */
1216{
1217 struct umac_ctxumac128_ctx *ctx, *octx;
1218 size_t bytes_to_add;
1219 aes_int_key prf_key;
1220
1221 octx = ctx = xcalloc(1, sizeof(*ctx) + ALLOC_BOUNDARY16);
1222 if (ctx) {
1223 if (ALLOC_BOUNDARY16) {
1224 bytes_to_add = ALLOC_BOUNDARY16 -
1225 ((ptrdiff_t)ctx & (ALLOC_BOUNDARY16 - 1));
1226 ctx = (struct umac_ctxumac128_ctx *)((u_char *)ctx + bytes_to_add);
1227 }
1228 ctx->free_ptr = octx;
1229 aes_key_setup(key, prf_key)AES_set_encrypt_key((const u_char *)(key),16*8,prf_key);
1230 pdf_init(&ctx->pdf, prf_key);
1231 uhash_init(&ctx->hash, prf_key);
1232 explicit_bzero(prf_key, sizeof(prf_key));
1233 }
1234
1235 return (ctx);
1236}
1237
1238/* ---------------------------------------------------------------------- */
1239
1240int umac_finalumac128_final(struct umac_ctxumac128_ctx *ctx, u_char tag[], const u_char nonce[8])
1241/* Incorporate any pending data, pad, and generate tag */
1242{
1243 uhash_final(&ctx->hash, (u_char *)tag);
1244 pdf_gen_xor(&ctx->pdf, (const UINT8 *)nonce, (UINT8 *)tag);
1245
1246 return (1);
1247}
1248
1249/* ---------------------------------------------------------------------- */
1250
1251int umac_updateumac128_update(struct umac_ctxumac128_ctx *ctx, const u_char *input, long len)
1252/* Given len bytes of data, we parse it into L1_KEY_LEN chunks and */
1253/* hash each one, calling the PDF on the hashed output whenever the hash- */
1254/* output buffer is full. */
1255{
1256 uhash_update(&ctx->hash, input, len);
1257 return (1);
1258}
1259
1260/* ---------------------------------------------------------------------- */
1261
1262#if 0
1263int umac(struct umac_ctxumac128_ctx *ctx, u_char *input,
1264 long len, u_char tag[],
1265 u_char nonce[8])
1266/* All-in-one version simply calls umac_update() and umac_final(). */
1267{
1268 uhash(&ctx->hash, input, len, (u_char *)tag);
1269 pdf_gen_xor(&ctx->pdf, (UINT8 *)nonce, (UINT8 *)tag);
1270
1271 return (1);
1272}
1273#endif
1274
1275/* ---------------------------------------------------------------------- */
1276/* ---------------------------------------------------------------------- */
1277/* ----- End UMAC Section ----------------------------------------------- */
1278/* ---------------------------------------------------------------------- */
1279/* ---------------------------------------------------------------------- */