Coverage Report

Created: 2025-10-28 06:16

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