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