Coverage Report

Created: 2026-01-08 06:56

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