umac.c 45 KB

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