umac.c revision 181111
1101099Srwatson/* $OpenBSD: umac.c,v 1.3 2008/05/12 20:52:20 pvalchev Exp $ */ 2101099Srwatson/* ----------------------------------------------------------------------- 3115497Srwatson * 4101099Srwatson * umac.c -- C Implementation UMAC Message Authentication 5101099Srwatson * 6101099Srwatson * Version 0.93b of rfc4418.txt -- 2006 July 18 7101099Srwatson * 8106393Srwatson * For a full description of UMAC message authentication see the UMAC 9106393Srwatson * world-wide-web page at http://www.cs.ucdavis.edu/~rogaway/umac 10106393Srwatson * Please report bugs and suggestions to the UMAC webpage. 11106393Srwatson * 12101099Srwatson * Copyright (c) 1999-2006 Ted Krovetz 13101099Srwatson * 14101099Srwatson * Permission to use, copy, modify, and distribute this software and 15101099Srwatson * its documentation for any purpose and with or without fee, is hereby 16101099Srwatson * granted provided that the above copyright notice appears in all copies 17101099Srwatson * and in supporting documentation, and that the name of the copyright 18101099Srwatson * holder not be used in advertising or publicity pertaining to 19101099Srwatson * distribution of the software without specific, written prior permission. 20101099Srwatson * 21101099Srwatson * Comments should be directed to Ted Krovetz (tdk@acm.org) 22101099Srwatson * 23101099Srwatson * ---------------------------------------------------------------------- */ 24101099Srwatson 25101099Srwatson /* ////////////////////// IMPORTANT NOTES ///////////////////////////////// 26101099Srwatson * 27101099Srwatson * 1) This version does not work properly on messages larger than 16MB 28101099Srwatson * 29101099Srwatson * 2) If you set the switch to use SSE2, then all data must be 16-byte 30101099Srwatson * aligned 31101099Srwatson * 32101099Srwatson * 3) When calling the function umac(), it is assumed that msg is in 33101099Srwatson * a writable buffer of length divisible by 32 bytes. The message itself 34101099Srwatson * does not have to fill the entire buffer, but bytes beyond msg may be 35101099Srwatson * zeroed. 36101099Srwatson * 37101099Srwatson * 4) Three free AES implementations are supported by this implementation of 38101099Srwatson * UMAC. Paulo Barreto's version is in the public domain and can be found 39101099Srwatson * at http://www.esat.kuleuven.ac.be/~rijmen/rijndael/ (search for 40101099Srwatson * "Barreto"). The only two files needed are rijndael-alg-fst.c and 41101099Srwatson * rijndael-alg-fst.h. Brian Gladman's version is distributed with the GNU 42101099Srwatson * Public lisence at http://fp.gladman.plus.com/AES/index.htm. It 43101099Srwatson * includes a fast IA-32 assembly version. The OpenSSL crypo library is 44101099Srwatson * the third. 45101099Srwatson * 46105988Srwatson * 5) With FORCE_C_ONLY flags set to 0, incorrect results are sometimes 47101099Srwatson * produced under gcc with optimizations set -O3 or higher. Dunno why. 48101099Srwatson * 49103183Sbde /////////////////////////////////////////////////////////////////////// */ 50101099Srwatson 51101099Srwatson/* ---------------------------------------------------------------------- */ 52115497Srwatson/* --- User Switches ---------------------------------------------------- */ 53101099Srwatson/* ---------------------------------------------------------------------- */ 54101099Srwatson 55101099Srwatson#define UMAC_OUTPUT_LEN 8 /* Alowable: 4, 8, 12, 16 */ 56105696Srwatson/* #define FORCE_C_ONLY 1 ANSI C and 64-bit integers req'd */ 57101099Srwatson/* #define AES_IMPLEMENTAION 1 1 = OpenSSL, 2 = Barreto, 3 = Gladman */ 58101099Srwatson/* #define SSE2 0 Is SSE2 is available? */ 59101099Srwatson/* #define RUN_TESTS 0 Run basic correctness/speed tests */ 60101099Srwatson/* #define UMAC_AE_SUPPORT 0 Enable auhthenticated encrytion */ 61101099Srwatson 62101099Srwatson/* ---------------------------------------------------------------------- */ 63101099Srwatson/* -- Global Includes --------------------------------------------------- */ 64101099Srwatson/* ---------------------------------------------------------------------- */ 65101099Srwatson 66101099Srwatson#include "includes.h" 67101099Srwatson#include <sys/types.h> 68101099Srwatson 69101099Srwatson#include "xmalloc.h" 70101099Srwatson#include "umac.h" 71101099Srwatson#include <string.h> 72122875Srwatson#include <stdlib.h> 73101099Srwatson#include <stddef.h> 74101099Srwatson 75122879Srwatson/* ---------------------------------------------------------------------- */ 76101099Srwatson/* --- Primitive Data Types --- */ 77101099Srwatson/* ---------------------------------------------------------------------- */ 78101099Srwatson 79101099Srwatson/* The following assumptions may need change on your system */ 80101099Srwatsontypedef u_int8_t UINT8; /* 1 byte */ 81101099Srwatsontypedef u_int16_t UINT16; /* 2 byte */ 82101099Srwatsontypedef u_int32_t UINT32; /* 4 byte */ 83101099Srwatsontypedef u_int64_t UINT64; /* 8 bytes */ 84101099Srwatsontypedef unsigned int UWORD; /* Register */ 85101099Srwatson 86101099Srwatson/* ---------------------------------------------------------------------- */ 87105988Srwatson/* --- Constants -------------------------------------------------------- */ 88105988Srwatson/* ---------------------------------------------------------------------- */ 89105988Srwatson 90105988Srwatson#define UMAC_KEY_LEN 16 /* UMAC takes 16 bytes of external key */ 91107731Srwatson 92101099Srwatson/* Message "words" are read from memory in an endian-specific manner. */ 93101099Srwatson/* For this implementation to behave correctly, __LITTLE_ENDIAN__ must */ 94102980Srwatson/* be set true if the host computer is little-endian. */ 95101099Srwatson 96101099Srwatson#if BYTE_ORDER == LITTLE_ENDIAN 97101099Srwatson#define __LITTLE_ENDIAN__ 1 98101099Srwatson#else 99101099Srwatson#define __LITTLE_ENDIAN__ 0 100101099Srwatson#endif 101101099Srwatson 102101099Srwatson/* ---------------------------------------------------------------------- */ 103101099Srwatson/* ---------------------------------------------------------------------- */ 104101099Srwatson/* ----- Architecture Specific ------------------------------------------ */ 105101099Srwatson/* ---------------------------------------------------------------------- */ 106101099Srwatson/* ---------------------------------------------------------------------- */ 107101099Srwatson 108101099Srwatson 109101099Srwatson/* ---------------------------------------------------------------------- */ 110101099Srwatson/* ---------------------------------------------------------------------- */ 111105643Srwatson/* ----- Primitive Routines --------------------------------------------- */ 112105643Srwatson/* ---------------------------------------------------------------------- */ 113105643Srwatson/* ---------------------------------------------------------------------- */ 114105643Srwatson 115105606Srwatson 116105606Srwatson/* ---------------------------------------------------------------------- */ 117105606Srwatson/* --- 32-bit by 32-bit to 64-bit Multiplication ------------------------ */ 118105606Srwatson/* ---------------------------------------------------------------------- */ 119105606Srwatson 120105637Srwatson#define MUL64(a,b) ((UINT64)((UINT64)(UINT32)(a) * (UINT64)(UINT32)(b))) 121101099Srwatson 122105637Srwatson/* ---------------------------------------------------------------------- */ 123105637Srwatson/* --- Endian Conversion --- Forcing assembly on some platforms */ 124101099Srwatson/* ---------------------------------------------------------------------- */ 125101099Srwatson 126101099Srwatson#if HAVE_SWAP32 127101099Srwatson#define LOAD_UINT32_REVERSED(p) (swap32(*(UINT32 *)(p))) 128122879Srwatson#define STORE_UINT32_REVERSED(p,v) (*(UINT32 *)(p) = swap32(v)) 129101099Srwatson#else /* HAVE_SWAP32 */ 130105643Srwatson 131105643Srwatsonstatic UINT32 LOAD_UINT32_REVERSED(void *ptr) 132105643Srwatson{ 133105643Srwatson UINT32 temp = *(UINT32 *)ptr; 134105643Srwatson temp = (temp >> 24) | ((temp & 0x00FF0000) >> 8 ) 135105643Srwatson | ((temp & 0x0000FF00) << 8 ) | (temp << 24); 136105643Srwatson return (UINT32)temp; 137105643Srwatson} 138105643Srwatson 139105643Srwatson# if (__LITTLE_ENDIAN__) 140101099Srwatsonstatic void STORE_UINT32_REVERSED(void *ptr, UINT32 x) 141104514Srwatson{ 142101099Srwatson UINT32 i = (UINT32)x; 143101099Srwatson *(UINT32 *)ptr = (i >> 24) | ((i & 0x00FF0000) >> 8 ) 144122879Srwatson | ((i & 0x0000FF00) << 8 ) | (i << 24); 145101099Srwatson} 146101099Srwatson# endif /* __LITTLE_ENDIAN */ 147101099Srwatson#endif /* HAVE_SWAP32 */ 148101099Srwatson 149101099Srwatson/* The following definitions use the above reversal-primitives to do the right 150101099Srwatson * thing on endian specific load and stores. 151101099Srwatson */ 152122879Srwatson 153101099Srwatson#if (__LITTLE_ENDIAN__) 154101099Srwatson#define LOAD_UINT32_LITTLE(ptr) (*(UINT32 *)(ptr)) 155101099Srwatson#define STORE_UINT32_BIG(ptr,x) STORE_UINT32_REVERSED(ptr,x) 156101099Srwatson#else 157101099Srwatson#define LOAD_UINT32_LITTLE(ptr) LOAD_UINT32_REVERSED(ptr) 158105634Srwatson#define STORE_UINT32_BIG(ptr,x) (*(UINT32 *)(ptr) = (UINT32)(x)) 159105634Srwatson#endif 160105634Srwatson 161105634Srwatson/* ---------------------------------------------------------------------- */ 162105634Srwatson/* ---------------------------------------------------------------------- */ 163105634Srwatson/* ----- Begin KDF & PDF Section ---------------------------------------- */ 164105634Srwatson/* ---------------------------------------------------------------------- */ 165105634Srwatson/* ---------------------------------------------------------------------- */ 166105634Srwatson 167101099Srwatson/* UMAC uses AES with 16 byte block and key lengths */ 168101099Srwatson#define AES_BLOCK_LEN 16 169101099Srwatson 170105643Srwatson/* OpenSSL's AES */ 171101099Srwatson#include "openbsd-compat/openssl-compat.h" 172105736Srwatson#ifndef USE_BUILTIN_RIJNDAEL 173101099Srwatson# include <openssl/aes.h> 174101099Srwatson#endif 175101099Srwatsontypedef AES_KEY aes_int_key[1]; 176101099Srwatson#define aes_encryption(in,out,int_key) \ 177101099Srwatson AES_encrypt((u_char *)(in),(u_char *)(out),(AES_KEY *)int_key) 178101099Srwatson#define aes_key_setup(key,int_key) \ 179101099Srwatson AES_set_encrypt_key((u_char *)(key),UMAC_KEY_LEN*8,int_key) 180101099Srwatson 181101099Srwatson/* The user-supplied UMAC key is stretched using AES in a counter 182101099Srwatson * mode to supply all random bits needed by UMAC. The kdf function takes 183101099Srwatson * an AES internal key representation 'key' and writes a stream of 184101099Srwatson * 'nbytes' bytes to the memory pointed at by 'bufp'. Each distinct 185101099Srwatson * 'ndx' causes a distinct byte stream. 186101099Srwatson */ 187101099Srwatsonstatic void kdf(void *bufp, aes_int_key key, UINT8 ndx, int nbytes) 188101099Srwatson{ 189101099Srwatson UINT8 in_buf[AES_BLOCK_LEN] = {0}; 190101099Srwatson UINT8 out_buf[AES_BLOCK_LEN]; 191101099Srwatson UINT8 *dst_buf = (UINT8 *)bufp; 192101099Srwatson int i; 193101099Srwatson 194101099Srwatson /* Setup the initial value */ 195101099Srwatson in_buf[AES_BLOCK_LEN-9] = ndx; 196101099Srwatson in_buf[AES_BLOCK_LEN-1] = i = 1; 197101099Srwatson 198101099Srwatson while (nbytes >= AES_BLOCK_LEN) { 199101099Srwatson aes_encryption(in_buf, out_buf, key); 200101099Srwatson memcpy(dst_buf,out_buf,AES_BLOCK_LEN); 201105643Srwatson in_buf[AES_BLOCK_LEN-1] = ++i; 202105643Srwatson nbytes -= AES_BLOCK_LEN; 203105643Srwatson dst_buf += AES_BLOCK_LEN; 204105643Srwatson } 205105643Srwatson if (nbytes) { 206101099Srwatson aes_encryption(in_buf, out_buf, key); 207101099Srwatson memcpy(dst_buf,out_buf,nbytes); 208101099Srwatson } 209101099Srwatson} 210101099Srwatson 211101099Srwatson/* The final UHASH result is XOR'd with the output of a pseudorandom 212101099Srwatson * function. Here, we use AES to generate random output and 213101099Srwatson * xor the appropriate bytes depending on the last bits of nonce. 214101099Srwatson * This scheme is optimized for sequential, increasing big-endian nonces. 215101099Srwatson */ 216101099Srwatson 217101099Srwatsontypedef struct { 218101099Srwatson UINT8 cache[AES_BLOCK_LEN]; /* Previous AES output is saved */ 219101099Srwatson UINT8 nonce[AES_BLOCK_LEN]; /* The AES input making above cache */ 220105988Srwatson aes_int_key prf_key; /* Expanded AES key for PDF */ 221105988Srwatson} pdf_ctx; 222105988Srwatson 223105988Srwatsonstatic void pdf_init(pdf_ctx *pc, aes_int_key prf_key) 224106174Srwatson{ 225105988Srwatson UINT8 buf[UMAC_KEY_LEN]; 226105988Srwatson 227105988Srwatson kdf(buf, prf_key, 0, UMAC_KEY_LEN); 228105988Srwatson aes_key_setup(buf, pc->prf_key); 229105988Srwatson 230105988Srwatson /* Initialize pdf and cache */ 231105988Srwatson memset(pc->nonce, 0, sizeof(pc->nonce)); 232105988Srwatson aes_encryption(pc->nonce, pc->cache, pc->prf_key); 233101099Srwatson} 234101099Srwatson 235101099Srwatsonstatic void pdf_gen_xor(pdf_ctx *pc, UINT8 nonce[8], UINT8 buf[8]) 236101099Srwatson{ 237101099Srwatson /* 'ndx' indicates that we'll be using the 0th or 1st eight bytes 238101099Srwatson * of the AES output. If last time around we returned the ndx-1st 239101099Srwatson * element, then we may have the result in the cache already. 240101099Srwatson */ 241101099Srwatson 242101099Srwatson#if (UMAC_OUTPUT_LEN == 4) 243101099Srwatson#define LOW_BIT_MASK 3 244101099Srwatson#elif (UMAC_OUTPUT_LEN == 8) 245101099Srwatson#define LOW_BIT_MASK 1 246103750Srwatson#elif (UMAC_OUTPUT_LEN > 8) 247101099Srwatson#define LOW_BIT_MASK 0 248103750Srwatson#endif 249101099Srwatson 250101099Srwatson UINT8 tmp_nonce_lo[4]; 251101099Srwatson#if LOW_BIT_MASK != 0 252101099Srwatson int ndx = nonce[7] & LOW_BIT_MASK; 253101099Srwatson#endif 254101099Srwatson *(UINT32 *)tmp_nonce_lo = ((UINT32 *)nonce)[1]; 255101099Srwatson tmp_nonce_lo[3] &= ~LOW_BIT_MASK; /* zero last bit */ 256101099Srwatson 257101099Srwatson if ( (((UINT32 *)tmp_nonce_lo)[0] != ((UINT32 *)pc->nonce)[1]) || 258101099Srwatson (((UINT32 *)nonce)[0] != ((UINT32 *)pc->nonce)[0]) ) 259101099Srwatson { 260101099Srwatson ((UINT32 *)pc->nonce)[0] = ((UINT32 *)nonce)[0]; 261101099Srwatson ((UINT32 *)pc->nonce)[1] = ((UINT32 *)tmp_nonce_lo)[0]; 262101099Srwatson aes_encryption(pc->nonce, pc->cache, pc->prf_key); 263101099Srwatson } 264101099Srwatson 265101099Srwatson#if (UMAC_OUTPUT_LEN == 4) 266101099Srwatson *((UINT32 *)buf) ^= ((UINT32 *)pc->cache)[ndx]; 267101099Srwatson#elif (UMAC_OUTPUT_LEN == 8) 268101099Srwatson *((UINT64 *)buf) ^= ((UINT64 *)pc->cache)[ndx]; 269101099Srwatson#elif (UMAC_OUTPUT_LEN == 12) 270101099Srwatson ((UINT64 *)buf)[0] ^= ((UINT64 *)pc->cache)[0]; 271101099Srwatson ((UINT32 *)buf)[2] ^= ((UINT32 *)pc->cache)[2]; 272101099Srwatson#elif (UMAC_OUTPUT_LEN == 16) 273101099Srwatson ((UINT64 *)buf)[0] ^= ((UINT64 *)pc->cache)[0]; 274101099Srwatson ((UINT64 *)buf)[1] ^= ((UINT64 *)pc->cache)[1]; 275101099Srwatson#endif 276101099Srwatson} 277101099Srwatson 278101099Srwatson/* ---------------------------------------------------------------------- */ 279101099Srwatson/* ---------------------------------------------------------------------- */ 280101099Srwatson/* ----- Begin NH Hash Section ------------------------------------------ */ 281101099Srwatson/* ---------------------------------------------------------------------- */ 282101099Srwatson/* ---------------------------------------------------------------------- */ 283101099Srwatson 284101099Srwatson/* The NH-based hash functions used in UMAC are described in the UMAC paper 285101099Srwatson * and specification, both of which can be found at the UMAC website. 286101099Srwatson * The interface to this implementation has two 287101099Srwatson * versions, one expects the entire message being hashed to be passed 288101099Srwatson * in a single buffer and returns the hash result immediately. The second 289101099Srwatson * allows the message to be passed in a sequence of buffers. In the 290101099Srwatson * muliple-buffer interface, the client calls the routine nh_update() as 291101099Srwatson * many times as necessary. When there is no more data to be fed to the 292101099Srwatson * hash, the client calls nh_final() which calculates the hash output. 293101099Srwatson * Before beginning another hash calculation the nh_reset() routine 294105634Srwatson * must be called. The single-buffer routine, nh(), is equivalent to 295105634Srwatson * the sequence of calls nh_update() and nh_final(); however it is 296105634Srwatson * optimized and should be prefered whenever the multiple-buffer interface 297105634Srwatson * is not necessary. When using either interface, it is the client's 298105634Srwatson * responsability to pass no more than L1_KEY_LEN bytes per hash result. 299105634Srwatson * 300105634Srwatson * The routine nh_init() initializes the nh_ctx data structure and 301105634Srwatson * must be called once, before any other PDF routine. 302105634Srwatson */ 303105634Srwatson 304105634Srwatson /* The "nh_aux" routines do the actual NH hashing work. They 305105637Srwatson * expect buffers to be multiples of L1_PAD_BOUNDARY. These routines 306105634Srwatson * produce output for all STREAMS NH iterations in one call, 307105634Srwatson * allowing the parallel implementation of the streams. 308105634Srwatson */ 309105634Srwatson 310105634Srwatson#define STREAMS (UMAC_OUTPUT_LEN / 4) /* Number of times hash is applied */ 311105634Srwatson#define L1_KEY_LEN 1024 /* Internal key bytes */ 312106090Srwatson#define L1_KEY_SHIFT 16 /* Toeplitz key shift between streams */ 313105634Srwatson#define L1_PAD_BOUNDARY 32 /* pad message to boundary multiple */ 314105634Srwatson#define ALLOC_BOUNDARY 16 /* Keep buffers aligned to this */ 315105634Srwatson#define HASH_BUF_BYTES 64 /* nh_aux_hb buffer multiple */ 316105634Srwatson 317106090Srwatsontypedef struct { 318105634Srwatson UINT8 nh_key [L1_KEY_LEN + L1_KEY_SHIFT * (STREAMS - 1)]; /* NH Key */ 319105634Srwatson UINT8 data [HASH_BUF_BYTES]; /* Incomming data buffer */ 320105634Srwatson int next_data_empty; /* Bookeeping variable for data buffer. */ 321105634Srwatson int bytes_hashed; /* Bytes (out of L1_KEY_LEN) incorperated. */ 322105634Srwatson UINT64 state[STREAMS]; /* on-line state */ 323105634Srwatson} nh_ctx; 324105634Srwatson 325105634Srwatson 326105634Srwatson#if (UMAC_OUTPUT_LEN == 4) 327105634Srwatson 328105634Srwatsonstatic void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen) 329105634Srwatson/* NH hashing primitive. Previous (partial) hash result is loaded and 330105634Srwatson* then stored via hp pointer. The length of the data pointed at by "dp", 331105634Srwatson* "dlen", is guaranteed to be divisible by L1_PAD_BOUNDARY (32). Key 332105634Srwatson* is expected to be endian compensated in memory at key setup. 333105634Srwatson*/ 334105634Srwatson{ 335105634Srwatson UINT64 h; 336105634Srwatson UWORD c = dlen / 32; 337106091Srwatson UINT32 *k = (UINT32 *)kp; 338105988Srwatson UINT32 *d = (UINT32 *)dp; 339105988Srwatson UINT32 d0,d1,d2,d3,d4,d5,d6,d7; 340105988Srwatson UINT32 k0,k1,k2,k3,k4,k5,k6,k7; 341105988Srwatson 342105988Srwatson h = *((UINT64 *)hp); 343105988Srwatson do { 344105988Srwatson d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1); 345105988Srwatson d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3); 346105988Srwatson d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5); 347105634Srwatson d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7); 348101099Srwatson k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3); 349101099Srwatson k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7); 350101099Srwatson h += MUL64((k0 + d0), (k4 + d4)); 351101099Srwatson h += MUL64((k1 + d1), (k5 + d5)); 352101099Srwatson h += MUL64((k2 + d2), (k6 + d6)); 353101099Srwatson h += MUL64((k3 + d3), (k7 + d7)); 354101099Srwatson 355101099Srwatson d += 8; 356101099Srwatson k += 8; 357101099Srwatson } while (--c); 358101099Srwatson *((UINT64 *)hp) = h; 359105643Srwatson} 360105643Srwatson 361105643Srwatson#elif (UMAC_OUTPUT_LEN == 8) 362101099Srwatson 363101099Srwatsonstatic void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen) 364101099Srwatson/* Same as previous nh_aux, but two streams are handled in one pass, 365101099Srwatson * reading and writing 16 bytes of hash-state per call. 366101099Srwatson */ 367101099Srwatson{ 368101099Srwatson UINT64 h1,h2; 369101099Srwatson UWORD c = dlen / 32; 370101099Srwatson UINT32 *k = (UINT32 *)kp; 371101099Srwatson UINT32 *d = (UINT32 *)dp; 372101099Srwatson UINT32 d0,d1,d2,d3,d4,d5,d6,d7; 373101099Srwatson UINT32 k0,k1,k2,k3,k4,k5,k6,k7, 374101099Srwatson k8,k9,k10,k11; 375101099Srwatson 376101099Srwatson h1 = *((UINT64 *)hp); 377101099Srwatson h2 = *((UINT64 *)hp + 1); 378101099Srwatson k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3); 379101099Srwatson do { 380101099Srwatson d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1); 381105643Srwatson d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3); 382105643Srwatson d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5); 383105643Srwatson d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7); 384101099Srwatson k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7); 385101099Srwatson k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11); 386101099Srwatson 387101099Srwatson h1 += MUL64((k0 + d0), (k4 + d4)); 388101099Srwatson h2 += MUL64((k4 + d0), (k8 + d4)); 389101099Srwatson 390101099Srwatson h1 += MUL64((k1 + d1), (k5 + d5)); 391101099Srwatson h2 += MUL64((k5 + d1), (k9 + d5)); 392101099Srwatson 393101099Srwatson h1 += MUL64((k2 + d2), (k6 + d6)); 394101099Srwatson h2 += MUL64((k6 + d2), (k10 + d6)); 395101099Srwatson 396101099Srwatson h1 += MUL64((k3 + d3), (k7 + d7)); 397101099Srwatson h2 += MUL64((k7 + d3), (k11 + d7)); 398105643Srwatson 399105643Srwatson k0 = k8; k1 = k9; k2 = k10; k3 = k11; 400105643Srwatson 401101099Srwatson d += 8; 402101099Srwatson k += 8; 403101099Srwatson } while (--c); 404101099Srwatson ((UINT64 *)hp)[0] = h1; 405101099Srwatson ((UINT64 *)hp)[1] = h2; 406101099Srwatson} 407101099Srwatson 408101099Srwatson#elif (UMAC_OUTPUT_LEN == 12) 409101099Srwatson 410101099Srwatsonstatic void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen) 411101099Srwatson/* Same as previous nh_aux, but two streams are handled in one pass, 412101099Srwatson * reading and writing 24 bytes of hash-state per call. 413101099Srwatson*/ 414101099Srwatson{ 415101099Srwatson UINT64 h1,h2,h3; 416101099Srwatson UWORD c = dlen / 32; 417101099Srwatson UINT32 *k = (UINT32 *)kp; 418101099Srwatson UINT32 *d = (UINT32 *)dp; 419101099Srwatson UINT32 d0,d1,d2,d3,d4,d5,d6,d7; 420101099Srwatson UINT32 k0,k1,k2,k3,k4,k5,k6,k7, 421105643Srwatson k8,k9,k10,k11,k12,k13,k14,k15; 422105643Srwatson 423101099Srwatson h1 = *((UINT64 *)hp); 424101099Srwatson h2 = *((UINT64 *)hp + 1); 425101099Srwatson h3 = *((UINT64 *)hp + 2); 426101099Srwatson k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3); 427105643Srwatson k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7); 428105643Srwatson do { 429105643Srwatson d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1); 430105643Srwatson d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3); 431101099Srwatson d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5); 432101099Srwatson d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7); 433105643Srwatson k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11); 434105643Srwatson k12 = *(k+12); k13 = *(k+13); k14 = *(k+14); k15 = *(k+15); 435105643Srwatson 436105643Srwatson h1 += MUL64((k0 + d0), (k4 + d4)); 437101099Srwatson h2 += MUL64((k4 + d0), (k8 + d4)); 438101099Srwatson h3 += MUL64((k8 + d0), (k12 + d4)); 439101099Srwatson 440101099Srwatson h1 += MUL64((k1 + d1), (k5 + d5)); 441105643Srwatson h2 += MUL64((k5 + d1), (k9 + d5)); 442105643Srwatson h3 += MUL64((k9 + d1), (k13 + d5)); 443101099Srwatson 444101099Srwatson h1 += MUL64((k2 + d2), (k6 + d6)); 445101099Srwatson h2 += MUL64((k6 + d2), (k10 + d6)); 446101099Srwatson h3 += MUL64((k10 + d2), (k14 + d6)); 447105643Srwatson 448105643Srwatson h1 += MUL64((k3 + d3), (k7 + d7)); 449105643Srwatson h2 += MUL64((k7 + d3), (k11 + d7)); 450101099Srwatson h3 += MUL64((k11 + d3), (k15 + d7)); 451101099Srwatson 452101099Srwatson k0 = k8; k1 = k9; k2 = k10; k3 = k11; 453101099Srwatson k4 = k12; k5 = k13; k6 = k14; k7 = k15; 454101099Srwatson 455101099Srwatson d += 8; 456105643Srwatson k += 8; 457101099Srwatson } while (--c); 458101099Srwatson ((UINT64 *)hp)[0] = h1; 459101099Srwatson ((UINT64 *)hp)[1] = h2; 460101099Srwatson ((UINT64 *)hp)[2] = h3; 461101099Srwatson} 462101099Srwatson 463101099Srwatson#elif (UMAC_OUTPUT_LEN == 16) 464101099Srwatson 465101099Srwatsonstatic void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen) 466101099Srwatson/* Same as previous nh_aux, but two streams are handled in one pass, 467101099Srwatson * reading and writing 24 bytes of hash-state per call. 468101099Srwatson*/ 469101099Srwatson{ 470101099Srwatson UINT64 h1,h2,h3,h4; 471101099Srwatson UWORD c = dlen / 32; 472101099Srwatson UINT32 *k = (UINT32 *)kp; 473101099Srwatson UINT32 *d = (UINT32 *)dp; 474101099Srwatson UINT32 d0,d1,d2,d3,d4,d5,d6,d7; 475101099Srwatson UINT32 k0,k1,k2,k3,k4,k5,k6,k7, 476105656Srwatson k8,k9,k10,k11,k12,k13,k14,k15, 477105656Srwatson k16,k17,k18,k19; 478105656Srwatson 479105656Srwatson h1 = *((UINT64 *)hp); 480105656Srwatson h2 = *((UINT64 *)hp + 1); 481105656Srwatson h3 = *((UINT64 *)hp + 2); 482105656Srwatson h4 = *((UINT64 *)hp + 3); 483105656Srwatson k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3); 484105656Srwatson k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7); 485105656Srwatson do { 486101099Srwatson d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1); 487101099Srwatson d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3); 488101099Srwatson d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5); 489101099Srwatson d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7); 490101099Srwatson k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11); 491101099Srwatson k12 = *(k+12); k13 = *(k+13); k14 = *(k+14); k15 = *(k+15); 492101099Srwatson k16 = *(k+16); k17 = *(k+17); k18 = *(k+18); k19 = *(k+19); 493122879Srwatson 494122879Srwatson h1 += MUL64((k0 + d0), (k4 + d4)); 495101099Srwatson h2 += MUL64((k4 + d0), (k8 + d4)); 496101099Srwatson h3 += MUL64((k8 + d0), (k12 + d4)); 497101099Srwatson h4 += MUL64((k12 + d0), (k16 + d4)); 498101099Srwatson 499101099Srwatson h1 += MUL64((k1 + d1), (k5 + d5)); 500101099Srwatson h2 += MUL64((k5 + d1), (k9 + d5)); 501104514Srwatson h3 += MUL64((k9 + d1), (k13 + d5)); 502101099Srwatson h4 += MUL64((k13 + d1), (k17 + d5)); 503101099Srwatson 504111119Simp h1 += MUL64((k2 + d2), (k6 + d6)); 505101099Srwatson h2 += MUL64((k6 + d2), (k10 + d6)); 506101099Srwatson h3 += MUL64((k10 + d2), (k14 + d6)); 507101099Srwatson h4 += MUL64((k14 + d2), (k18 + d6)); 508104514Srwatson 509101099Srwatson h1 += MUL64((k3 + d3), (k7 + d7)); 510101099Srwatson h2 += MUL64((k7 + d3), (k11 + d7)); 511104514Srwatson h3 += MUL64((k11 + d3), (k15 + d7)); 512101099Srwatson h4 += MUL64((k15 + d3), (k19 + d7)); 513101099Srwatson 514101099Srwatson k0 = k8; k1 = k9; k2 = k10; k3 = k11; 515101099Srwatson k4 = k12; k5 = k13; k6 = k14; k7 = k15; 516101099Srwatson k8 = k16; k9 = k17; k10 = k18; k11 = k19; 517101099Srwatson 518101099Srwatson d += 8; 519104514Srwatson k += 8; 520101099Srwatson } while (--c); 521101099Srwatson ((UINT64 *)hp)[0] = h1; 522101099Srwatson ((UINT64 *)hp)[1] = h2; 523101099Srwatson ((UINT64 *)hp)[2] = h3; 524101099Srwatson ((UINT64 *)hp)[3] = h4; 525101099Srwatson} 526105696Srwatson 527115497Srwatson/* ---------------------------------------------------------------------- */ 528115497Srwatson#endif /* UMAC_OUTPUT_LENGTH */ 529115497Srwatson/* ---------------------------------------------------------------------- */ 530105696Srwatson 531115497Srwatson 532115497Srwatson/* ---------------------------------------------------------------------- */ 533105696Srwatson 534115497Srwatsonstatic void nh_transform(nh_ctx *hc, UINT8 *buf, UINT32 nbytes) 535105696Srwatson/* This function is a wrapper for the primitive NH hash functions. It takes 536105696Srwatson * as argument "hc" the current hash context and a buffer which must be a 537105696Srwatson * multiple of L1_PAD_BOUNDARY. The key passed to nh_aux is offset 538115497Srwatson * appropriately according to how much message has been hashed already. 539105696Srwatson */ 540105696Srwatson{ 541115497Srwatson UINT8 *key; 542105696Srwatson 543105696Srwatson key = hc->nh_key + hc->bytes_hashed; 544115497Srwatson nh_aux(key, buf, hc->state, nbytes); 545105696Srwatson} 546105696Srwatson 547115497Srwatson/* ---------------------------------------------------------------------- */ 548115497Srwatson 549115497Srwatson#if (__LITTLE_ENDIAN__) 550115497Srwatsonstatic void endian_convert(void *buf, UWORD bpw, UINT32 num_bytes) 551115497Srwatson/* We endian convert the keys on little-endian computers to */ 552115497Srwatson/* compensate for the lack of big-endian memory reads during hashing. */ 553115497Srwatson{ 554115497Srwatson UWORD iters = num_bytes / bpw; 555115497Srwatson if (bpw == 4) { 556115497Srwatson UINT32 *p = (UINT32 *)buf; 557115497Srwatson do { 558115497Srwatson *p = LOAD_UINT32_REVERSED(p); 559115497Srwatson p++; 560115497Srwatson } while (--iters); 561115497Srwatson } else if (bpw == 8) { 562115497Srwatson UINT32 *p = (UINT32 *)buf; 563115497Srwatson UINT32 t; 564105696Srwatson do { 565115497Srwatson t = LOAD_UINT32_REVERSED(p+1); 566105696Srwatson p[1] = LOAD_UINT32_REVERSED(p); 567105696Srwatson p[0] = t; 568105696Srwatson p += 2; 569105696Srwatson } while (--iters); 570105696Srwatson } 571105696Srwatson} 572105696Srwatson#define endian_convert_if_le(x,y,z) endian_convert((x),(y),(z)) 573115497Srwatson#else 574116701Srwatson#define endian_convert_if_le(x,y,z) do{}while(0) /* Do nothing */ 575116701Srwatson#endif 576116701Srwatson 577116701Srwatson/* ---------------------------------------------------------------------- */ 578116701Srwatson 579115497Srwatsonstatic void nh_reset(nh_ctx *hc) 580101099Srwatson/* Reset nh_ctx to ready for hashing of new data */ 581116701Srwatson{ 582101099Srwatson hc->bytes_hashed = 0; 583105696Srwatson hc->next_data_empty = 0; 584105696Srwatson hc->state[0] = 0; 585116701Srwatson#if (UMAC_OUTPUT_LEN >= 8) 586115497Srwatson hc->state[1] = 0; 587105696Srwatson#endif 588105696Srwatson#if (UMAC_OUTPUT_LEN >= 12) 589105696Srwatson hc->state[2] = 0; 590105696Srwatson#endif 591116701Srwatson#if (UMAC_OUTPUT_LEN == 16) 592105696Srwatson hc->state[3] = 0; 593105696Srwatson#endif 594116701Srwatson 595115497Srwatson} 596105696Srwatson 597105696Srwatson/* ---------------------------------------------------------------------- */ 598116701Srwatson 599105696Srwatsonstatic void nh_init(nh_ctx *hc, aes_int_key prf_key) 600105696Srwatson/* Generate nh_key, endian convert and reset to be ready for hashing. */ 601116701Srwatson{ 602115497Srwatson kdf(hc->nh_key, prf_key, 1, sizeof(hc->nh_key)); 603105696Srwatson endian_convert_if_le(hc->nh_key, 4, sizeof(hc->nh_key)); 604105696Srwatson nh_reset(hc); 605116701Srwatson} 606105696Srwatson 607105696Srwatson/* ---------------------------------------------------------------------- */ 608105696Srwatson 609105696Srwatsonstatic void nh_update(nh_ctx *hc, UINT8 *buf, UINT32 nbytes) 610105696Srwatson/* Incorporate nbytes of data into a nh_ctx, buffer whatever is not an */ 611105696Srwatson/* even multiple of HASH_BUF_BYTES. */ 612105696Srwatson{ 613105696Srwatson UINT32 i,j; 614116701Srwatson 615105696Srwatson j = hc->next_data_empty; 616101099Srwatson if ((j + nbytes) >= HASH_BUF_BYTES) { 617101099Srwatson if (j) { 618105696Srwatson i = HASH_BUF_BYTES - j; 619105696Srwatson memcpy(hc->data+j, buf, i); 620105696Srwatson nh_transform(hc,hc->data,HASH_BUF_BYTES); 621105696Srwatson nbytes -= i; 622105696Srwatson buf += i; 623101099Srwatson hc->bytes_hashed += HASH_BUF_BYTES; 624116701Srwatson } 625105696Srwatson if (nbytes >= HASH_BUF_BYTES) { 626105696Srwatson i = nbytes & ~(HASH_BUF_BYTES - 1); 627105696Srwatson nh_transform(hc, buf, i); 628105696Srwatson nbytes -= i; 629101099Srwatson buf += i; 630115395Srwatson hc->bytes_hashed += i; 631115395Srwatson } 632105696Srwatson j = 0; 633105696Srwatson } 634105696Srwatson memcpy(hc->data + j, buf, nbytes); 635105696Srwatson hc->next_data_empty = j + nbytes; 636105696Srwatson} 637105696Srwatson 638105696Srwatson/* ---------------------------------------------------------------------- */ 639105696Srwatson 640105696Srwatsonstatic void zero_pad(UINT8 *p, int nbytes) 641105696Srwatson{ 642105696Srwatson/* Write "nbytes" of zeroes, beginning at "p" */ 643105696Srwatson if (nbytes >= (int)sizeof(UWORD)) { 644105696Srwatson while ((ptrdiff_t)p % sizeof(UWORD)) { 645105696Srwatson *p = 0; 646115395Srwatson nbytes--; 647105696Srwatson p++; 648115395Srwatson } 649115395Srwatson while (nbytes >= (int)sizeof(UWORD)) { 650115395Srwatson *(UWORD *)p = 0; 651115395Srwatson nbytes -= sizeof(UWORD); 652115395Srwatson p += sizeof(UWORD); 653115395Srwatson } 654105696Srwatson } 655115395Srwatson while (nbytes) { 656115395Srwatson *p = 0; 657115395Srwatson nbytes--; 658105696Srwatson p++; 659115395Srwatson } 660115395Srwatson} 661115395Srwatson 662115395Srwatson/* ---------------------------------------------------------------------- */ 663115395Srwatson 664115395Srwatsonstatic void nh_final(nh_ctx *hc, UINT8 *result) 665115395Srwatson/* After passing some number of data buffers to nh_update() for integration 666115395Srwatson * into an NH context, nh_final is called to produce a hash result. If any 667115395Srwatson * bytes are in the buffer hc->data, incorporate them into the 668105696Srwatson * NH context. Finally, add into the NH accumulation "state" the total number 669115395Srwatson * of bits hashed. The resulting numbers are written to the buffer "result". 670115395Srwatson * If nh_update was never called, L1_PAD_BOUNDARY zeroes are incorporated. 671115395Srwatson */ 672105696Srwatson{ 673115395Srwatson int nh_len, nbits; 674105696Srwatson 675115395Srwatson if (hc->next_data_empty != 0) { 676105696Srwatson nh_len = ((hc->next_data_empty + (L1_PAD_BOUNDARY - 1)) & 677105696Srwatson ~(L1_PAD_BOUNDARY - 1)); 678105696Srwatson zero_pad(hc->data + hc->next_data_empty, 679105696Srwatson nh_len - hc->next_data_empty); 680105696Srwatson nh_transform(hc, hc->data, nh_len); 681105696Srwatson hc->bytes_hashed += hc->next_data_empty; 682105696Srwatson } else if (hc->bytes_hashed == 0) { 683105696Srwatson nh_len = L1_PAD_BOUNDARY; 684105696Srwatson zero_pad(hc->data, L1_PAD_BOUNDARY); 685105696Srwatson nh_transform(hc, hc->data, nh_len); 686105696Srwatson } 687105696Srwatson 688105696Srwatson nbits = (hc->bytes_hashed << 3); 689115395Srwatson ((UINT64 *)result)[0] = ((UINT64 *)hc->state)[0] + nbits; 690101099Srwatson#if (UMAC_OUTPUT_LEN >= 8) 691101099Srwatson ((UINT64 *)result)[1] = ((UINT64 *)hc->state)[1] + nbits; 692115395Srwatson#endif 693115395Srwatson#if (UMAC_OUTPUT_LEN >= 12) 694105696Srwatson ((UINT64 *)result)[2] = ((UINT64 *)hc->state)[2] + nbits; 695115395Srwatson#endif 696115395Srwatson#if (UMAC_OUTPUT_LEN == 16) 697115395Srwatson ((UINT64 *)result)[3] = ((UINT64 *)hc->state)[3] + nbits; 698115395Srwatson#endif 699105696Srwatson nh_reset(hc); 700115395Srwatson} 701115395Srwatson 702105696Srwatson/* ---------------------------------------------------------------------- */ 703115395Srwatson 704105696Srwatsonstatic void nh(nh_ctx *hc, UINT8 *buf, UINT32 padded_len, 705115395Srwatson UINT32 unpadded_len, UINT8 *result) 706115395Srwatson/* All-in-one nh_update() and nh_final() equivalent. 707115395Srwatson * Assumes that padded_len is divisible by L1_PAD_BOUNDARY and result is 708105696Srwatson * well aligned 709115395Srwatson */ 710105696Srwatson{ 711105696Srwatson UINT32 nbits; 712115395Srwatson 713101099Srwatson /* Initialize the hash state */ 714105696Srwatson nbits = (unpadded_len << 3); 715105696Srwatson 716105696Srwatson ((UINT64 *)result)[0] = nbits; 717105696Srwatson#if (UMAC_OUTPUT_LEN >= 8) 718105696Srwatson ((UINT64 *)result)[1] = nbits; 719105696Srwatson#endif 720105696Srwatson#if (UMAC_OUTPUT_LEN >= 12) 721105696Srwatson ((UINT64 *)result)[2] = nbits; 722105696Srwatson#endif 723105696Srwatson#if (UMAC_OUTPUT_LEN == 16) 724105696Srwatson ((UINT64 *)result)[3] = nbits; 725105696Srwatson#endif 726105696Srwatson 727105696Srwatson nh_aux(hc->nh_key, buf, result, padded_len); 728105696Srwatson} 729105696Srwatson 730105696Srwatson/* ---------------------------------------------------------------------- */ 731105696Srwatson/* ---------------------------------------------------------------------- */ 732105696Srwatson/* ----- Begin UHASH Section -------------------------------------------- */ 733105696Srwatson/* ---------------------------------------------------------------------- */ 734101099Srwatson/* ---------------------------------------------------------------------- */ 735101099Srwatson 736101099Srwatson/* UHASH is a multi-layered algorithm. Data presented to UHASH is first 737101099Srwatson * hashed by NH. The NH output is then hashed by a polynomial-hash layer 738105696Srwatson * unless the initial data to be hashed is short. After the polynomial- 739105696Srwatson * layer, an inner-product hash is used to produce the final UHASH output. 740101099Srwatson * 741105696Srwatson * UHASH provides two interfaces, one all-at-once and another where data 742105696Srwatson * buffers are presented sequentially. In the sequential interface, the 743105696Srwatson * UHASH client calls the routine uhash_update() as many times as necessary. 744105696Srwatson * When there is no more data to be fed to UHASH, the client calls 745105696Srwatson * uhash_final() which 746105696Srwatson * calculates the UHASH output. Before beginning another UHASH calculation 747105696Srwatson * the uhash_reset() routine must be called. The all-at-once UHASH routine, 748105696Srwatson * uhash(), is equivalent to the sequence of calls uhash_update() and 749105696Srwatson * uhash_final(); however it is optimized and should be 750105696Srwatson * used whenever the sequential interface is not necessary. 751105696Srwatson * 752105696Srwatson * The routine uhash_init() initializes the uhash_ctx data structure and 753105696Srwatson * must be called once, before any other UHASH routine. 754105696Srwatson */ 755105696Srwatson 756105696Srwatson/* ---------------------------------------------------------------------- */ 757105696Srwatson/* ----- Constants and uhash_ctx ---------------------------------------- */ 758105696Srwatson/* ---------------------------------------------------------------------- */ 759105696Srwatson 760101099Srwatson/* ---------------------------------------------------------------------- */ 761101099Srwatson/* ----- Poly hash and Inner-Product hash Constants --------------------- */ 762101099Srwatson/* ---------------------------------------------------------------------- */ 763105696Srwatson 764105696Srwatson/* Primes and masks */ 765105696Srwatson#define p36 ((UINT64)0x0000000FFFFFFFFBull) /* 2^36 - 5 */ 766105696Srwatson#define p64 ((UINT64)0xFFFFFFFFFFFFFFC5ull) /* 2^64 - 59 */ 767105696Srwatson#define m36 ((UINT64)0x0000000FFFFFFFFFull) /* The low 36 of 64 bits */ 768105696Srwatson 769105696Srwatson 770101099Srwatson/* ---------------------------------------------------------------------- */ 771101099Srwatson 772101099Srwatsontypedef struct uhash_ctx { 773101099Srwatson nh_ctx hash; /* Hash context for L1 NH hash */ 774101099Srwatson UINT64 poly_key_8[STREAMS]; /* p64 poly keys */ 775107698Srwatson UINT64 poly_accum[STREAMS]; /* poly hash result */ 776107698Srwatson UINT64 ip_keys[STREAMS*4]; /* Inner-product keys */ 777101099Srwatson UINT32 ip_trans[STREAMS]; /* Inner-product translation */ 778101099Srwatson UINT32 msg_len; /* Total length of data passed */ 779101099Srwatson /* to uhash */ 780101099Srwatson} uhash_ctx; 781101099Srwatsontypedef struct uhash_ctx *uhash_ctx_t; 782101099Srwatson 783101099Srwatson/* ---------------------------------------------------------------------- */ 784101099Srwatson 785101099Srwatson 786101099Srwatson/* The polynomial hashes use Horner's rule to evaluate a polynomial one 787105606Srwatson * word at a time. As described in the specification, poly32 and poly64 788105606Srwatson * require keys from special domains. The following implementations exploit 789105606Srwatson * the special domains to avoid overflow. The results are not guaranteed to 790105606Srwatson * be within Z_p32 and Z_p64, but the Inner-Product hash implementation 791101099Srwatson * patches any errant values. 792101099Srwatson */ 793105643Srwatson 794101099Srwatsonstatic UINT64 poly64(UINT64 cur, UINT64 key, UINT64 data) 795101099Srwatson{ 796101099Srwatson UINT32 key_hi = (UINT32)(key >> 32), 797107698Srwatson key_lo = (UINT32)key, 798107698Srwatson cur_hi = (UINT32)(cur >> 32), 799101099Srwatson cur_lo = (UINT32)cur, 800101099Srwatson x_lo, 801101099Srwatson x_hi; 802101099Srwatson UINT64 X,T,res; 803105643Srwatson 804101099Srwatson X = MUL64(key_hi, cur_lo) + MUL64(cur_hi, key_lo); 805101099Srwatson x_lo = (UINT32)X; 806101099Srwatson x_hi = (UINT32)(X >> 32); 807107698Srwatson 808107698Srwatson res = (MUL64(key_hi, cur_hi) + x_hi) * 59 + MUL64(key_lo, cur_lo); 809122563Srwatson 810104535Srwatson T = ((UINT64)x_lo << 32); 811104535Srwatson res += T; 812104535Srwatson if (res < T) 813122524Srwatson res += 59; 814104535Srwatson 815104535Srwatson res += data; 816104535Srwatson if (res < data) 817104535Srwatson res += 59; 818104535Srwatson 819104535Srwatson return res; 820101099Srwatson} 821101099Srwatson 822101099Srwatson 823101099Srwatson/* Although UMAC is specified to use a ramped polynomial hash scheme, this 824101099Srwatson * implementation does not handle all ramp levels. Because we don't handle 825122524Srwatson * the ramp up to p128 modulus in this implementation, we are limited to 826101099Srwatson * 2^14 poly_hash() invocations per stream (for a total capacity of 2^24 827101099Srwatson * bytes input to UMAC per tag, ie. 16MB). 828101099Srwatson */ 829101099Srwatsonstatic void poly_hash(uhash_ctx_t hc, UINT32 data_in[]) 830101099Srwatson{ 831101099Srwatson int i; 832101099Srwatson UINT64 *data=(UINT64*)data_in; 833101099Srwatson 834101099Srwatson for (i = 0; i < STREAMS; i++) { 835101099Srwatson if ((UINT32)(data[i] >> 32) == 0xfffffffful) { 836101099Srwatson hc->poly_accum[i] = poly64(hc->poly_accum[i], 837101099Srwatson hc->poly_key_8[i], p64 - 1); 838101099Srwatson hc->poly_accum[i] = poly64(hc->poly_accum[i], 839101099Srwatson hc->poly_key_8[i], (data[i] - 59)); 840105643Srwatson } else { 841101099Srwatson hc->poly_accum[i] = poly64(hc->poly_accum[i], 842105643Srwatson hc->poly_key_8[i], data[i]); 843101099Srwatson } 844101099Srwatson } 845101099Srwatson} 846101099Srwatson 847101099Srwatson 848101099Srwatson/* ---------------------------------------------------------------------- */ 849101099Srwatson 850101099Srwatson 851101099Srwatson/* The final step in UHASH is an inner-product hash. The poly hash 852101099Srwatson * produces a result not neccesarily WORD_LEN bytes long. The inner- 853101099Srwatson * product hash breaks the polyhash output into 16-bit chunks and 854105656Srwatson * multiplies each with a 36 bit key. 855101099Srwatson */ 856101099Srwatson 857101099Srwatsonstatic UINT64 ip_aux(UINT64 t, UINT64 *ipkp, UINT64 data) 858107698Srwatson{ 859107698Srwatson t = t + ipkp[0] * (UINT64)(UINT16)(data >> 48); 860107698Srwatson t = t + ipkp[1] * (UINT64)(UINT16)(data >> 32); 861101099Srwatson t = t + ipkp[2] * (UINT64)(UINT16)(data >> 16); 862101099Srwatson t = t + ipkp[3] * (UINT64)(UINT16)(data); 863101099Srwatson 864101099Srwatson return t; 865101099Srwatson} 866101099Srwatson 867105656Srwatsonstatic UINT32 ip_reduce_p36(UINT64 t) 868101099Srwatson{ 869101099Srwatson/* Divisionless modular reduction */ 870101099Srwatson UINT64 ret; 871105988Srwatson 872105988Srwatson ret = (t & m36) + 5 * (t >> 36); 873105988Srwatson if (ret >= p36) 874101099Srwatson ret -= p36; 875101099Srwatson 876101099Srwatson /* return least significant 32 bits */ 877105988Srwatson return (UINT32)(ret); 878105988Srwatson} 879101099Srwatson 880101099Srwatson 881101099Srwatson/* If the data being hashed by UHASH is no longer than L1_KEY_LEN, then 882101099Srwatson * the polyhash stage is skipped and ip_short is applied directly to the 883101099Srwatson * NH output. 884105988Srwatson */ 885105988Srwatsonstatic void ip_short(uhash_ctx_t ahc, UINT8 *nh_res, u_char *res) 886101099Srwatson{ 887105988Srwatson UINT64 t; 888106354Smux UINT64 *nhp = (UINT64 *)nh_res; 889101099Srwatson 890105988Srwatson t = ip_aux(0,ahc->ip_keys, nhp[0]); 891105988Srwatson STORE_UINT32_BIG((UINT32 *)res+0, ip_reduce_p36(t) ^ ahc->ip_trans[0]); 892101099Srwatson#if (UMAC_OUTPUT_LEN >= 8) 893105988Srwatson t = ip_aux(0,ahc->ip_keys+4, nhp[1]); 894105988Srwatson STORE_UINT32_BIG((UINT32 *)res+1, ip_reduce_p36(t) ^ ahc->ip_trans[1]); 895105988Srwatson#endif 896105988Srwatson#if (UMAC_OUTPUT_LEN >= 12) 897105988Srwatson t = ip_aux(0,ahc->ip_keys+8, nhp[2]); 898105988Srwatson STORE_UINT32_BIG((UINT32 *)res+2, ip_reduce_p36(t) ^ ahc->ip_trans[2]); 899105988Srwatson#endif 900105988Srwatson#if (UMAC_OUTPUT_LEN == 16) 901105988Srwatson t = ip_aux(0,ahc->ip_keys+12, nhp[3]); 902105988Srwatson STORE_UINT32_BIG((UINT32 *)res+3, ip_reduce_p36(t) ^ ahc->ip_trans[3]); 903101099Srwatson#endif 904101099Srwatson} 905105988Srwatson 906105988Srwatson/* If the data being hashed by UHASH is longer than L1_KEY_LEN, then 907105988Srwatson * the polyhash stage is not skipped and ip_long is applied to the 908105988Srwatson * polyhash output. 909105988Srwatson */ 910105988Srwatsonstatic void ip_long(uhash_ctx_t ahc, u_char *res) 911105988Srwatson{ 912105988Srwatson int i; 913105988Srwatson UINT64 t; 914105988Srwatson 915105988Srwatson for (i = 0; i < STREAMS; i++) { 916105988Srwatson /* fix polyhash output not in Z_p64 */ 917105988Srwatson if (ahc->poly_accum[i] >= p64) 918101099Srwatson ahc->poly_accum[i] -= p64; 919105988Srwatson t = ip_aux(0,ahc->ip_keys+(i*4), ahc->poly_accum[i]); 920101099Srwatson STORE_UINT32_BIG((UINT32 *)res+i, 921101099Srwatson ip_reduce_p36(t) ^ ahc->ip_trans[i]); 922101099Srwatson } 923101099Srwatson} 924105988Srwatson 925105988Srwatson 926101099Srwatson/* ---------------------------------------------------------------------- */ 927101099Srwatson 928101099Srwatson/* ---------------------------------------------------------------------- */ 929101099Srwatson 930105988Srwatson/* Reset uhash context for next hash session */ 931101099Srwatsonstatic int uhash_reset(uhash_ctx_t pc) 932101099Srwatson{ 933101099Srwatson nh_reset(&pc->hash); 934101099Srwatson pc->msg_len = 0; 935105988Srwatson pc->poly_accum[0] = 1; 936105988Srwatson#if (UMAC_OUTPUT_LEN >= 8) 937105988Srwatson pc->poly_accum[1] = 1; 938105988Srwatson#endif 939105988Srwatson#if (UMAC_OUTPUT_LEN >= 12) 940105988Srwatson pc->poly_accum[2] = 1; 941105988Srwatson#endif 942105988Srwatson#if (UMAC_OUTPUT_LEN == 16) 943105988Srwatson pc->poly_accum[3] = 1; 944105988Srwatson#endif 945105988Srwatson return 1; 946105988Srwatson} 947122524Srwatson 948105988Srwatson/* ---------------------------------------------------------------------- */ 949105988Srwatson 950105988Srwatson/* Given a pointer to the internal key needed by kdf() and a uhash context, 951105988Srwatson * initialize the NH context and generate keys needed for poly and inner- 952105988Srwatson * product hashing. All keys are endian adjusted in memory so that native 953105988Srwatson * loads cause correct keys to be in registers during calculation. 954105988Srwatson */ 955105988Srwatsonstatic void uhash_init(uhash_ctx_t ahc, aes_int_key prf_key) 956105988Srwatson{ 957105988Srwatson int i; 958105988Srwatson UINT8 buf[(8*STREAMS+4)*sizeof(UINT64)]; 959105988Srwatson 960105988Srwatson /* Zero the entire uhash context */ 961105988Srwatson memset(ahc, 0, sizeof(uhash_ctx)); 962105988Srwatson 963105988Srwatson /* Initialize the L1 hash */ 964105988Srwatson nh_init(&ahc->hash, prf_key); 965105988Srwatson 966105988Srwatson /* Setup L2 hash variables */ 967105988Srwatson kdf(buf, prf_key, 2, sizeof(buf)); /* Fill buffer with index 1 key */ 968105988Srwatson for (i = 0; i < STREAMS; i++) { 969105988Srwatson /* Fill keys from the buffer, skipping bytes in the buffer not 970105988Srwatson * used by this implementation. Endian reverse the keys if on a 971105988Srwatson * little-endian computer. 972105988Srwatson */ 973105988Srwatson memcpy(ahc->poly_key_8+i, buf+24*i, 8); 974105988Srwatson endian_convert_if_le(ahc->poly_key_8+i, 8, 8); 975105988Srwatson /* Mask the 64-bit keys to their special domain */ 976105988Srwatson ahc->poly_key_8[i] &= ((UINT64)0x01ffffffu << 32) + 0x01ffffffu; 977105988Srwatson ahc->poly_accum[i] = 1; /* Our polyhash prepends a non-zero word */ 978105988Srwatson } 979105988Srwatson 980101099Srwatson /* Setup L3-1 hash variables */ 981101099Srwatson kdf(buf, prf_key, 3, sizeof(buf)); /* Fill buffer with index 2 key */ 982101099Srwatson for (i = 0; i < STREAMS; i++) 983101099Srwatson memcpy(ahc->ip_keys+4*i, buf+(8*i+4)*sizeof(UINT64), 984122875Srwatson 4*sizeof(UINT64)); 985122875Srwatson endian_convert_if_le(ahc->ip_keys, sizeof(UINT64), 986122875Srwatson sizeof(ahc->ip_keys)); 987122875Srwatson for (i = 0; i < STREAMS*4; i++) 988122875Srwatson ahc->ip_keys[i] %= p36; /* Bring into Z_p36 */ 989122875Srwatson 990122875Srwatson /* Setup L3-2 hash variables */ 991122875Srwatson /* Fill buffer with index 4 key */ 992122875Srwatson kdf(ahc->ip_trans, prf_key, 4, STREAMS * sizeof(UINT32)); 993122875Srwatson endian_convert_if_le(ahc->ip_trans, sizeof(UINT32), 994122875Srwatson STREAMS * sizeof(UINT32)); 995122875Srwatson} 996101099Srwatson 997101099Srwatson/* ---------------------------------------------------------------------- */ 998101099Srwatson 999101099Srwatson#if 0 1000101099Srwatsonstatic uhash_ctx_t uhash_alloc(u_char key[]) 1001101099Srwatson{ 1002101099Srwatson/* Allocate memory and force to a 16-byte boundary. */ 1003101099Srwatson uhash_ctx_t ctx; 1004101099Srwatson u_char bytes_to_add; 1005101099Srwatson aes_int_key prf_key; 1006101099Srwatson 1007101099Srwatson ctx = (uhash_ctx_t)malloc(sizeof(uhash_ctx)+ALLOC_BOUNDARY); 1008101099Srwatson if (ctx) { 1009101099Srwatson if (ALLOC_BOUNDARY) { 1010101099Srwatson bytes_to_add = ALLOC_BOUNDARY - 1011101099Srwatson ((ptrdiff_t)ctx & (ALLOC_BOUNDARY -1)); 1012101099Srwatson ctx = (uhash_ctx_t)((u_char *)ctx + bytes_to_add); 1013122524Srwatson *((u_char *)ctx - 1) = bytes_to_add; 1014101099Srwatson } 1015101099Srwatson aes_key_setup(key,prf_key); 1016101099Srwatson uhash_init(ctx, prf_key); 1017101099Srwatson } 1018101099Srwatson return (ctx); 1019101099Srwatson} 1020101099Srwatson#endif 1021101099Srwatson 1022101099Srwatson/* ---------------------------------------------------------------------- */ 1023101099Srwatson 1024101099Srwatson#if 0 1025122524Srwatsonstatic int uhash_free(uhash_ctx_t ctx) 1026101099Srwatson{ 1027101099Srwatson/* Free memory allocated by uhash_alloc */ 1028101099Srwatson u_char bytes_to_sub; 1029101099Srwatson 1030101099Srwatson if (ctx) { 1031101099Srwatson if (ALLOC_BOUNDARY) { 1032101099Srwatson bytes_to_sub = *((u_char *)ctx - 1); 1033101099Srwatson ctx = (uhash_ctx_t)((u_char *)ctx - bytes_to_sub); 1034101099Srwatson } 1035101099Srwatson free(ctx); 1036101099Srwatson } 1037101099Srwatson return (1); 1038101099Srwatson} 1039101099Srwatson#endif 1040101099Srwatson/* ---------------------------------------------------------------------- */ 1041101099Srwatson 1042101099Srwatsonstatic int uhash_update(uhash_ctx_t ctx, u_char *input, long len) 1043101099Srwatson/* Given len bytes of data, we parse it into L1_KEY_LEN chunks and 1044101099Srwatson * hash each one with NH, calling the polyhash on each NH output. 1045101099Srwatson */ 1046101099Srwatson{ 1047101099Srwatson UWORD bytes_hashed, bytes_remaining; 1048101099Srwatson UINT64 result_buf[STREAMS]; 1049101099Srwatson UINT8 *nh_result = (UINT8 *)&result_buf; 1050101099Srwatson 1051101099Srwatson if (ctx->msg_len + len <= L1_KEY_LEN) { 1052101099Srwatson nh_update(&ctx->hash, (UINT8 *)input, len); 1053105656Srwatson ctx->msg_len += len; 1054101099Srwatson } else { 1055101099Srwatson 1056101099Srwatson bytes_hashed = ctx->msg_len % L1_KEY_LEN; 1057101099Srwatson if (ctx->msg_len == L1_KEY_LEN) 1058101099Srwatson bytes_hashed = L1_KEY_LEN; 1059101099Srwatson 1060101099Srwatson if (bytes_hashed + len >= L1_KEY_LEN) { 1061101099Srwatson 1062101099Srwatson /* If some bytes have been passed to the hash function */ 1063101099Srwatson /* then we want to pass at most (L1_KEY_LEN - bytes_hashed) */ 1064101099Srwatson /* bytes to complete the current nh_block. */ 1065105656Srwatson if (bytes_hashed) { 1066101099Srwatson bytes_remaining = (L1_KEY_LEN - bytes_hashed); 1067101099Srwatson nh_update(&ctx->hash, (UINT8 *)input, bytes_remaining); 1068101099Srwatson nh_final(&ctx->hash, nh_result); 1069101099Srwatson ctx->msg_len += bytes_remaining; 1070101099Srwatson poly_hash(ctx,(UINT32 *)nh_result); 1071101099Srwatson len -= bytes_remaining; 1072101099Srwatson input += bytes_remaining; 1073101099Srwatson } 1074101099Srwatson 1075101099Srwatson /* Hash directly from input stream if enough bytes */ 1076101099Srwatson while (len >= L1_KEY_LEN) { 1077101099Srwatson nh(&ctx->hash, (UINT8 *)input, L1_KEY_LEN, 1078101099Srwatson L1_KEY_LEN, nh_result); 1079101099Srwatson ctx->msg_len += L1_KEY_LEN; 1080101099Srwatson len -= L1_KEY_LEN; 1081101099Srwatson input += L1_KEY_LEN; 1082101099Srwatson poly_hash(ctx,(UINT32 *)nh_result); 1083101099Srwatson } 1084101099Srwatson } 1085101099Srwatson 1086101099Srwatson /* pass remaining < L1_KEY_LEN bytes of input data to NH */ 1087101099Srwatson if (len) { 1088101099Srwatson nh_update(&ctx->hash, (UINT8 *)input, len); 1089101099Srwatson ctx->msg_len += len; 1090101099Srwatson } 1091101099Srwatson } 1092101099Srwatson 1093101099Srwatson return (1); 1094101099Srwatson} 1095101099Srwatson 1096101099Srwatson/* ---------------------------------------------------------------------- */ 1097101099Srwatson 1098101099Srwatsonstatic int uhash_final(uhash_ctx_t ctx, u_char *res) 1099101099Srwatson/* Incorporate any pending data, pad, and generate tag */ 1100101099Srwatson{ 1101101099Srwatson UINT64 result_buf[STREAMS]; 1102122524Srwatson UINT8 *nh_result = (UINT8 *)&result_buf; 1103101099Srwatson 1104101099Srwatson if (ctx->msg_len > L1_KEY_LEN) { 1105101099Srwatson if (ctx->msg_len % L1_KEY_LEN) { 1106101099Srwatson nh_final(&ctx->hash, nh_result); 1107101099Srwatson poly_hash(ctx,(UINT32 *)nh_result); 1108101099Srwatson } 1109101099Srwatson ip_long(ctx, res); 1110101099Srwatson } else { 1111121816Sbrooks nh_final(&ctx->hash, nh_result); 1112101099Srwatson ip_short(ctx,nh_result, res); 1113101099Srwatson } 1114110350Srwatson uhash_reset(ctx); 1115101099Srwatson return (1); 1116101099Srwatson} 1117101099Srwatson 1118101099Srwatson/* ---------------------------------------------------------------------- */ 1119110350Srwatson 1120101099Srwatson#if 0 1121101099Srwatsonstatic int uhash(uhash_ctx_t ahc, u_char *msg, long len, u_char *res) 1122101099Srwatson/* assumes that msg is in a writable buffer of length divisible by */ 1123101099Srwatson/* L1_PAD_BOUNDARY. Bytes beyond msg[len] may be zeroed. */ 1124110350Srwatson{ 1125101099Srwatson UINT8 nh_result[STREAMS*sizeof(UINT64)]; 1126101099Srwatson UINT32 nh_len; 1127101099Srwatson int extra_zeroes_needed; 1128110350Srwatson 1129101099Srwatson /* If the message to be hashed is no longer than L1_HASH_LEN, we skip 1130101099Srwatson * the polyhash. 1131101099Srwatson */ 1132101099Srwatson if (len <= L1_KEY_LEN) { 1133101099Srwatson if (len == 0) /* If zero length messages will not */ 1134106089Srwatson nh_len = L1_PAD_BOUNDARY; /* be seen, comment out this case */ 1135101099Srwatson else 1136101099Srwatson nh_len = ((len + (L1_PAD_BOUNDARY - 1)) & ~(L1_PAD_BOUNDARY - 1)); 1137101099Srwatson extra_zeroes_needed = nh_len - len; 1138101099Srwatson zero_pad((UINT8 *)msg + len, extra_zeroes_needed); 1139101099Srwatson nh(&ahc->hash, (UINT8 *)msg, nh_len, len, nh_result); 1140101099Srwatson ip_short(ahc,nh_result, res); 1141101099Srwatson } else { 1142101099Srwatson /* Otherwise, we hash each L1_KEY_LEN chunk with NH, passing the NH 1143101099Srwatson * output to poly_hash(). 1144101099Srwatson */ 1145121816Sbrooks do { 1146110350Srwatson nh(&ahc->hash, (UINT8 *)msg, L1_KEY_LEN, L1_KEY_LEN, nh_result); 1147101099Srwatson poly_hash(ahc,(UINT32 *)nh_result); 1148101099Srwatson len -= L1_KEY_LEN; 1149106089Srwatson msg += L1_KEY_LEN; 1150106089Srwatson } while (len >= L1_KEY_LEN); 1151106089Srwatson if (len) { 1152106089Srwatson nh_len = ((len + (L1_PAD_BOUNDARY - 1)) & ~(L1_PAD_BOUNDARY - 1)); 1153106089Srwatson extra_zeroes_needed = nh_len - len; 1154101099Srwatson zero_pad((UINT8 *)msg + len, extra_zeroes_needed); 1155101099Srwatson nh(&ahc->hash, (UINT8 *)msg, nh_len, len, nh_result); 1156101099Srwatson poly_hash(ahc,(UINT32 *)nh_result); 1157101099Srwatson } 1158101099Srwatson 1159101099Srwatson ip_long(ahc, res); 1160101099Srwatson } 1161110350Srwatson 1162110350Srwatson uhash_reset(ahc); 1163101099Srwatson return 1; 1164101099Srwatson} 1165101099Srwatson#endif 1166101099Srwatson 1167101099Srwatson/* ---------------------------------------------------------------------- */ 1168101099Srwatson/* ---------------------------------------------------------------------- */ 1169101099Srwatson/* ----- Begin UMAC Section --------------------------------------------- */ 1170101099Srwatson/* ---------------------------------------------------------------------- */ 1171101099Srwatson/* ---------------------------------------------------------------------- */ 1172101099Srwatson 1173101099Srwatson/* The UMAC interface has two interfaces, an all-at-once interface where 1174101099Srwatson * the entire message to be authenticated is passed to UMAC in one buffer, 1175101099Srwatson * and a sequential interface where the message is presented a little at a 1176101099Srwatson * time. The all-at-once is more optimaized than the sequential version and 1177101099Srwatson * should be preferred when the sequential interface is not required. 1178101099Srwatson */ 1179101099Srwatsonstruct umac_ctx { 1180101099Srwatson uhash_ctx hash; /* Hash function for message compression */ 1181101099Srwatson pdf_ctx pdf; /* PDF for hashed output */ 1182101099Srwatson void *free_ptr; /* Address to free this struct via */ 1183101099Srwatson} umac_ctx; 1184101099Srwatson 1185101099Srwatson/* ---------------------------------------------------------------------- */ 1186101099Srwatson 1187101099Srwatson#if 0 1188101099Srwatsonint umac_reset(struct umac_ctx *ctx) 1189101099Srwatson/* Reset the hash function to begin a new authentication. */ 1190101099Srwatson{ 1191101099Srwatson uhash_reset(&ctx->hash); 1192101099Srwatson return (1); 1193101099Srwatson} 1194101099Srwatson#endif 1195101099Srwatson 1196101099Srwatson/* ---------------------------------------------------------------------- */ 1197101099Srwatson 1198101099Srwatsonint umac_delete(struct umac_ctx *ctx) 1199101099Srwatson/* Deallocate the ctx structure */ 1200101099Srwatson{ 1201101099Srwatson if (ctx) { 1202101099Srwatson if (ALLOC_BOUNDARY) 1203101099Srwatson ctx = (struct umac_ctx *)ctx->free_ptr; 1204101099Srwatson xfree(ctx); 1205101099Srwatson } 1206101099Srwatson return (1); 1207101099Srwatson} 1208101099Srwatson 1209101099Srwatson/* ---------------------------------------------------------------------- */ 1210101099Srwatson 1211101099Srwatsonstruct umac_ctx *umac_new(u_char key[]) 1212105656Srwatson/* Dynamically allocate a umac_ctx struct, initialize variables, 1213105656Srwatson * generate subkeys from key. Align to 16-byte boundary. 1214105696Srwatson */ 1215105656Srwatson{ 1216105656Srwatson struct umac_ctx *ctx, *octx; 1217105656Srwatson size_t bytes_to_add; 1218105656Srwatson aes_int_key prf_key; 1219105656Srwatson 1220105656Srwatson octx = ctx = xmalloc(sizeof(*ctx) + ALLOC_BOUNDARY); 1221101099Srwatson if (ctx) { 1222101099Srwatson if (ALLOC_BOUNDARY) { 1223101099Srwatson bytes_to_add = ALLOC_BOUNDARY - 1224101099Srwatson ((ptrdiff_t)ctx & (ALLOC_BOUNDARY - 1)); 1225101099Srwatson ctx = (struct umac_ctx *)((u_char *)ctx + bytes_to_add); 1226101099Srwatson } 1227101099Srwatson ctx->free_ptr = octx; 1228101099Srwatson aes_key_setup(key,prf_key); 1229101099Srwatson pdf_init(&ctx->pdf, prf_key); 1230101099Srwatson uhash_init(&ctx->hash, prf_key); 1231105643Srwatson } 1232101099Srwatson 1233101099Srwatson return (ctx); 1234101099Srwatson} 1235101099Srwatson 1236101099Srwatson/* ---------------------------------------------------------------------- */ 1237101099Srwatson 1238101099Srwatsonint umac_final(struct umac_ctx *ctx, u_char tag[], u_char nonce[8]) 1239101099Srwatson/* Incorporate any pending data, pad, and generate tag */ 1240101099Srwatson{ 1241101099Srwatson uhash_final(&ctx->hash, (u_char *)tag); 1242101099Srwatson pdf_gen_xor(&ctx->pdf, (UINT8 *)nonce, (UINT8 *)tag); 1243101099Srwatson 1244101099Srwatson return (1); 1245101099Srwatson} 1246101099Srwatson 1247101099Srwatson/* ---------------------------------------------------------------------- */ 1248101099Srwatson 1249101099Srwatsonint umac_update(struct umac_ctx *ctx, u_char *input, long len) 1250101099Srwatson/* Given len bytes of data, we parse it into L1_KEY_LEN chunks and */ 1251101099Srwatson/* hash each one, calling the PDF on the hashed output whenever the hash- */ 1252101099Srwatson/* output buffer is full. */ 1253101099Srwatson{ 1254101099Srwatson uhash_update(&ctx->hash, input, len); 1255101099Srwatson return (1); 1256101099Srwatson} 1257101099Srwatson 1258101099Srwatson/* ---------------------------------------------------------------------- */ 1259101099Srwatson 1260101099Srwatson#if 0 1261101099Srwatsonint umac(struct umac_ctx *ctx, u_char *input, 1262101099Srwatson long len, u_char tag[], 1263101099Srwatson u_char nonce[8]) 1264101099Srwatson/* All-in-one version simply calls umac_update() and umac_final(). */ 1265101099Srwatson{ 1266101099Srwatson uhash(&ctx->hash, input, len, (u_char *)tag); 1267101099Srwatson pdf_gen_xor(&ctx->pdf, (UINT8 *)nonce, (UINT8 *)tag); 1268101099Srwatson 1269101099Srwatson return (1); 1270101099Srwatson} 1271101099Srwatson#endif 1272101099Srwatson 1273101099Srwatson/* ---------------------------------------------------------------------- */ 1274101099Srwatson/* ---------------------------------------------------------------------- */ 1275101099Srwatson/* ----- End UMAC Section ----------------------------------------------- */ 1276101099Srwatson/* ---------------------------------------------------------------------- */ 1277101099Srwatson/* ---------------------------------------------------------------------- */ 1278101099Srwatson