[BACK]Return to umac.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/umac.c, Revision 1.9

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