summaryrefslogtreecommitdiff
path: root/umac.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2013-07-25 11:55:20 +1000
committerDamien Miller <djm@mindrot.org>2013-07-25 11:55:20 +1000
commitc331dbd22297ab9bf351abee659893d139c9f28a (patch)
tree66280e4f64ca225081b1c5f79695eb8adb46509f /umac.c
parentc8669a8cd24952b3f16a44eac63d2b6ce8a6343a (diff)
- djm@cvs.openbsd.org 2013/07/22 05:00:17
[umac.c] make MAC key, data to be hashed and nonce for final hash const; checked with -Wcast-qual
Diffstat (limited to 'umac.c')
-rw-r--r--umac.c62
1 files changed, 31 insertions, 31 deletions
diff --git a/umac.c b/umac.c
index 60514a24f..99416a510 100644
--- a/umac.c
+++ b/umac.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: umac.c,v 1.6 2013/07/20 01:43:46 djm Exp $ */ 1/* $OpenBSD: umac.c,v 1.7 2013/07/22 05:00:17 djm Exp $ */
2/* ----------------------------------------------------------------------- 2/* -----------------------------------------------------------------------
3 * 3 *
4 * umac.c -- C Implementation UMAC Message Authentication 4 * umac.c -- C Implementation UMAC Message Authentication
@@ -132,13 +132,13 @@ typedef unsigned int UWORD; /* Register */
132/* ---------------------------------------------------------------------- */ 132/* ---------------------------------------------------------------------- */
133 133
134#if HAVE_SWAP32 134#if HAVE_SWAP32
135#define LOAD_UINT32_REVERSED(p) (swap32(*(UINT32 *)(p))) 135#define LOAD_UINT32_REVERSED(p) (swap32(*(const UINT32 *)(p)))
136#define STORE_UINT32_REVERSED(p,v) (*(UINT32 *)(p) = swap32(v)) 136#define STORE_UINT32_REVERSED(p,v) (*(UINT32 *)(p) = swap32(v))
137#else /* HAVE_SWAP32 */ 137#else /* HAVE_SWAP32 */
138 138
139static UINT32 LOAD_UINT32_REVERSED(void *ptr) 139static UINT32 LOAD_UINT32_REVERSED(const void *ptr)
140{ 140{
141 UINT32 temp = *(UINT32 *)ptr; 141 UINT32 temp = *(const UINT32 *)ptr;
142 temp = (temp >> 24) | ((temp & 0x00FF0000) >> 8 ) 142 temp = (temp >> 24) | ((temp & 0x00FF0000) >> 8 )
143 | ((temp & 0x0000FF00) << 8 ) | (temp << 24); 143 | ((temp & 0x0000FF00) << 8 ) | (temp << 24);
144 return (UINT32)temp; 144 return (UINT32)temp;
@@ -159,7 +159,7 @@ static void STORE_UINT32_REVERSED(void *ptr, UINT32 x)
159 */ 159 */
160 160
161#if (__LITTLE_ENDIAN__) 161#if (__LITTLE_ENDIAN__)
162#define LOAD_UINT32_LITTLE(ptr) (*(UINT32 *)(ptr)) 162#define LOAD_UINT32_LITTLE(ptr) (*(const UINT32 *)(ptr))
163#define STORE_UINT32_BIG(ptr,x) STORE_UINT32_REVERSED(ptr,x) 163#define STORE_UINT32_BIG(ptr,x) STORE_UINT32_REVERSED(ptr,x)
164#else 164#else
165#define LOAD_UINT32_LITTLE(ptr) LOAD_UINT32_REVERSED(ptr) 165#define LOAD_UINT32_LITTLE(ptr) LOAD_UINT32_REVERSED(ptr)
@@ -184,7 +184,7 @@ typedef AES_KEY aes_int_key[1];
184#define aes_encryption(in,out,int_key) \ 184#define aes_encryption(in,out,int_key) \
185 AES_encrypt((u_char *)(in),(u_char *)(out),(AES_KEY *)int_key) 185 AES_encrypt((u_char *)(in),(u_char *)(out),(AES_KEY *)int_key)
186#define aes_key_setup(key,int_key) \ 186#define aes_key_setup(key,int_key) \
187 AES_set_encrypt_key((u_char *)(key),UMAC_KEY_LEN*8,int_key) 187 AES_set_encrypt_key((const u_char *)(key),UMAC_KEY_LEN*8,int_key)
188 188
189/* The user-supplied UMAC key is stretched using AES in a counter 189/* The user-supplied UMAC key is stretched using AES in a counter
190 * mode to supply all random bits needed by UMAC. The kdf function takes 190 * mode to supply all random bits needed by UMAC. The kdf function takes
@@ -240,7 +240,7 @@ static void pdf_init(pdf_ctx *pc, aes_int_key prf_key)
240 aes_encryption(pc->nonce, pc->cache, pc->prf_key); 240 aes_encryption(pc->nonce, pc->cache, pc->prf_key);
241} 241}
242 242
243static void pdf_gen_xor(pdf_ctx *pc, UINT8 nonce[8], UINT8 buf[8]) 243static void pdf_gen_xor(pdf_ctx *pc, const UINT8 nonce[8], UINT8 buf[8])
244{ 244{
245 /* 'ndx' indicates that we'll be using the 0th or 1st eight bytes 245 /* 'ndx' indicates that we'll be using the 0th or 1st eight bytes
246 * of the AES output. If last time around we returned the ndx-1st 246 * of the AES output. If last time around we returned the ndx-1st
@@ -261,13 +261,13 @@ static void pdf_gen_xor(pdf_ctx *pc, UINT8 nonce[8], UINT8 buf[8])
261#if LOW_BIT_MASK != 0 261#if LOW_BIT_MASK != 0
262 int ndx = nonce[7] & LOW_BIT_MASK; 262 int ndx = nonce[7] & LOW_BIT_MASK;
263#endif 263#endif
264 *(UINT32 *)t.tmp_nonce_lo = ((UINT32 *)nonce)[1]; 264 *(UINT32 *)t.tmp_nonce_lo = ((const UINT32 *)nonce)[1];
265 t.tmp_nonce_lo[3] &= ~LOW_BIT_MASK; /* zero last bit */ 265 t.tmp_nonce_lo[3] &= ~LOW_BIT_MASK; /* zero last bit */
266 266
267 if ( (((UINT32 *)t.tmp_nonce_lo)[0] != ((UINT32 *)pc->nonce)[1]) || 267 if ( (((UINT32 *)t.tmp_nonce_lo)[0] != ((UINT32 *)pc->nonce)[1]) ||
268 (((UINT32 *)nonce)[0] != ((UINT32 *)pc->nonce)[0]) ) 268 (((const UINT32 *)nonce)[0] != ((UINT32 *)pc->nonce)[0]) )
269 { 269 {
270 ((UINT32 *)pc->nonce)[0] = ((UINT32 *)nonce)[0]; 270 ((UINT32 *)pc->nonce)[0] = ((const UINT32 *)nonce)[0];
271 ((UINT32 *)pc->nonce)[1] = ((UINT32 *)t.tmp_nonce_lo)[0]; 271 ((UINT32 *)pc->nonce)[1] = ((UINT32 *)t.tmp_nonce_lo)[0];
272 aes_encryption(pc->nonce, pc->cache, pc->prf_key); 272 aes_encryption(pc->nonce, pc->cache, pc->prf_key);
273 } 273 }
@@ -335,7 +335,7 @@ typedef struct {
335 335
336#if (UMAC_OUTPUT_LEN == 4) 336#if (UMAC_OUTPUT_LEN == 4)
337 337
338static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen) 338static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
339/* NH hashing primitive. Previous (partial) hash result is loaded and 339/* NH hashing primitive. Previous (partial) hash result is loaded and
340* then stored via hp pointer. The length of the data pointed at by "dp", 340* then stored via hp pointer. The length of the data pointed at by "dp",
341* "dlen", is guaranteed to be divisible by L1_PAD_BOUNDARY (32). Key 341* "dlen", is guaranteed to be divisible by L1_PAD_BOUNDARY (32). Key
@@ -345,7 +345,7 @@ static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen)
345 UINT64 h; 345 UINT64 h;
346 UWORD c = dlen / 32; 346 UWORD c = dlen / 32;
347 UINT32 *k = (UINT32 *)kp; 347 UINT32 *k = (UINT32 *)kp;
348 UINT32 *d = (UINT32 *)dp; 348 const UINT32 *d = (const UINT32 *)dp;
349 UINT32 d0,d1,d2,d3,d4,d5,d6,d7; 349 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
350 UINT32 k0,k1,k2,k3,k4,k5,k6,k7; 350 UINT32 k0,k1,k2,k3,k4,k5,k6,k7;
351 351
@@ -370,7 +370,7 @@ static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen)
370 370
371#elif (UMAC_OUTPUT_LEN == 8) 371#elif (UMAC_OUTPUT_LEN == 8)
372 372
373static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen) 373static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
374/* Same as previous nh_aux, but two streams are handled in one pass, 374/* Same as previous nh_aux, but two streams are handled in one pass,
375 * reading and writing 16 bytes of hash-state per call. 375 * reading and writing 16 bytes of hash-state per call.
376 */ 376 */
@@ -378,7 +378,7 @@ static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen)
378 UINT64 h1,h2; 378 UINT64 h1,h2;
379 UWORD c = dlen / 32; 379 UWORD c = dlen / 32;
380 UINT32 *k = (UINT32 *)kp; 380 UINT32 *k = (UINT32 *)kp;
381 UINT32 *d = (UINT32 *)dp; 381 const UINT32 *d = (const UINT32 *)dp;
382 UINT32 d0,d1,d2,d3,d4,d5,d6,d7; 382 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
383 UINT32 k0,k1,k2,k3,k4,k5,k6,k7, 383 UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
384 k8,k9,k10,k11; 384 k8,k9,k10,k11;
@@ -417,7 +417,7 @@ static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen)
417 417
418#elif (UMAC_OUTPUT_LEN == 12) 418#elif (UMAC_OUTPUT_LEN == 12)
419 419
420static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen) 420static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
421/* Same as previous nh_aux, but two streams are handled in one pass, 421/* Same as previous nh_aux, but two streams are handled in one pass,
422 * reading and writing 24 bytes of hash-state per call. 422 * reading and writing 24 bytes of hash-state per call.
423*/ 423*/
@@ -425,7 +425,7 @@ static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen)
425 UINT64 h1,h2,h3; 425 UINT64 h1,h2,h3;
426 UWORD c = dlen / 32; 426 UWORD c = dlen / 32;
427 UINT32 *k = (UINT32 *)kp; 427 UINT32 *k = (UINT32 *)kp;
428 UINT32 *d = (UINT32 *)dp; 428 const UINT32 *d = (const UINT32 *)dp;
429 UINT32 d0,d1,d2,d3,d4,d5,d6,d7; 429 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
430 UINT32 k0,k1,k2,k3,k4,k5,k6,k7, 430 UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
431 k8,k9,k10,k11,k12,k13,k14,k15; 431 k8,k9,k10,k11,k12,k13,k14,k15;
@@ -472,7 +472,7 @@ static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen)
472 472
473#elif (UMAC_OUTPUT_LEN == 16) 473#elif (UMAC_OUTPUT_LEN == 16)
474 474
475static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen) 475static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
476/* Same as previous nh_aux, but two streams are handled in one pass, 476/* Same as previous nh_aux, but two streams are handled in one pass,
477 * reading and writing 24 bytes of hash-state per call. 477 * reading and writing 24 bytes of hash-state per call.
478*/ 478*/
@@ -480,7 +480,7 @@ static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen)
480 UINT64 h1,h2,h3,h4; 480 UINT64 h1,h2,h3,h4;
481 UWORD c = dlen / 32; 481 UWORD c = dlen / 32;
482 UINT32 *k = (UINT32 *)kp; 482 UINT32 *k = (UINT32 *)kp;
483 UINT32 *d = (UINT32 *)dp; 483 const UINT32 *d = (const UINT32 *)dp;
484 UINT32 d0,d1,d2,d3,d4,d5,d6,d7; 484 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
485 UINT32 k0,k1,k2,k3,k4,k5,k6,k7, 485 UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
486 k8,k9,k10,k11,k12,k13,k14,k15, 486 k8,k9,k10,k11,k12,k13,k14,k15,
@@ -541,7 +541,7 @@ static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen)
541 541
542/* ---------------------------------------------------------------------- */ 542/* ---------------------------------------------------------------------- */
543 543
544static void nh_transform(nh_ctx *hc, UINT8 *buf, UINT32 nbytes) 544static void nh_transform(nh_ctx *hc, const UINT8 *buf, UINT32 nbytes)
545/* This function is a wrapper for the primitive NH hash functions. It takes 545/* This function is a wrapper for the primitive NH hash functions. It takes
546 * as argument "hc" the current hash context and a buffer which must be a 546 * as argument "hc" the current hash context and a buffer which must be a
547 * multiple of L1_PAD_BOUNDARY. The key passed to nh_aux is offset 547 * multiple of L1_PAD_BOUNDARY. The key passed to nh_aux is offset
@@ -616,7 +616,7 @@ static void nh_init(nh_ctx *hc, aes_int_key prf_key)
616 616
617/* ---------------------------------------------------------------------- */ 617/* ---------------------------------------------------------------------- */
618 618
619static void nh_update(nh_ctx *hc, UINT8 *buf, UINT32 nbytes) 619static void nh_update(nh_ctx *hc, const UINT8 *buf, UINT32 nbytes)
620/* Incorporate nbytes of data into a nh_ctx, buffer whatever is not an */ 620/* Incorporate nbytes of data into a nh_ctx, buffer whatever is not an */
621/* even multiple of HASH_BUF_BYTES. */ 621/* even multiple of HASH_BUF_BYTES. */
622{ 622{
@@ -711,7 +711,7 @@ static void nh_final(nh_ctx *hc, UINT8 *result)
711 711
712/* ---------------------------------------------------------------------- */ 712/* ---------------------------------------------------------------------- */
713 713
714static void nh(nh_ctx *hc, UINT8 *buf, UINT32 padded_len, 714static void nh(nh_ctx *hc, const UINT8 *buf, UINT32 padded_len,
715 UINT32 unpadded_len, UINT8 *result) 715 UINT32 unpadded_len, UINT8 *result)
716/* All-in-one nh_update() and nh_final() equivalent. 716/* All-in-one nh_update() and nh_final() equivalent.
717 * Assumes that padded_len is divisible by L1_PAD_BOUNDARY and result is 717 * Assumes that padded_len is divisible by L1_PAD_BOUNDARY and result is
@@ -1049,7 +1049,7 @@ static int uhash_free(uhash_ctx_t ctx)
1049#endif 1049#endif
1050/* ---------------------------------------------------------------------- */ 1050/* ---------------------------------------------------------------------- */
1051 1051
1052static int uhash_update(uhash_ctx_t ctx, u_char *input, long len) 1052static int uhash_update(uhash_ctx_t ctx, const u_char *input, long len)
1053/* Given len bytes of data, we parse it into L1_KEY_LEN chunks and 1053/* Given len bytes of data, we parse it into L1_KEY_LEN chunks and
1054 * hash each one with NH, calling the polyhash on each NH output. 1054 * hash each one with NH, calling the polyhash on each NH output.
1055 */ 1055 */
@@ -1059,7 +1059,7 @@ static int uhash_update(uhash_ctx_t ctx, u_char *input, long len)
1059 UINT8 *nh_result = (UINT8 *)&result_buf; 1059 UINT8 *nh_result = (UINT8 *)&result_buf;
1060 1060
1061 if (ctx->msg_len + len <= L1_KEY_LEN) { 1061 if (ctx->msg_len + len <= L1_KEY_LEN) {
1062 nh_update(&ctx->hash, (UINT8 *)input, len); 1062 nh_update(&ctx->hash, (const UINT8 *)input, len);
1063 ctx->msg_len += len; 1063 ctx->msg_len += len;
1064 } else { 1064 } else {
1065 1065
@@ -1074,7 +1074,7 @@ static int uhash_update(uhash_ctx_t ctx, u_char *input, long len)
1074 /* bytes to complete the current nh_block. */ 1074 /* bytes to complete the current nh_block. */
1075 if (bytes_hashed) { 1075 if (bytes_hashed) {
1076 bytes_remaining = (L1_KEY_LEN - bytes_hashed); 1076 bytes_remaining = (L1_KEY_LEN - bytes_hashed);
1077 nh_update(&ctx->hash, (UINT8 *)input, bytes_remaining); 1077 nh_update(&ctx->hash, (const UINT8 *)input, bytes_remaining);
1078 nh_final(&ctx->hash, nh_result); 1078 nh_final(&ctx->hash, nh_result);
1079 ctx->msg_len += bytes_remaining; 1079 ctx->msg_len += bytes_remaining;
1080 poly_hash(ctx,(UINT32 *)nh_result); 1080 poly_hash(ctx,(UINT32 *)nh_result);
@@ -1084,7 +1084,7 @@ static int uhash_update(uhash_ctx_t ctx, u_char *input, long len)
1084 1084
1085 /* Hash directly from input stream if enough bytes */ 1085 /* Hash directly from input stream if enough bytes */
1086 while (len >= L1_KEY_LEN) { 1086 while (len >= L1_KEY_LEN) {
1087 nh(&ctx->hash, (UINT8 *)input, L1_KEY_LEN, 1087 nh(&ctx->hash, (const UINT8 *)input, L1_KEY_LEN,
1088 L1_KEY_LEN, nh_result); 1088 L1_KEY_LEN, nh_result);
1089 ctx->msg_len += L1_KEY_LEN; 1089 ctx->msg_len += L1_KEY_LEN;
1090 len -= L1_KEY_LEN; 1090 len -= L1_KEY_LEN;
@@ -1095,7 +1095,7 @@ static int uhash_update(uhash_ctx_t ctx, u_char *input, long len)
1095 1095
1096 /* pass remaining < L1_KEY_LEN bytes of input data to NH */ 1096 /* pass remaining < L1_KEY_LEN bytes of input data to NH */
1097 if (len) { 1097 if (len) {
1098 nh_update(&ctx->hash, (UINT8 *)input, len); 1098 nh_update(&ctx->hash, (const UINT8 *)input, len);
1099 ctx->msg_len += len; 1099 ctx->msg_len += len;
1100 } 1100 }
1101 } 1101 }
@@ -1218,7 +1218,7 @@ int umac_delete(struct umac_ctx *ctx)
1218 1218
1219/* ---------------------------------------------------------------------- */ 1219/* ---------------------------------------------------------------------- */
1220 1220
1221struct umac_ctx *umac_new(u_char key[]) 1221struct umac_ctx *umac_new(const u_char key[])
1222/* Dynamically allocate a umac_ctx struct, initialize variables, 1222/* Dynamically allocate a umac_ctx struct, initialize variables,
1223 * generate subkeys from key. Align to 16-byte boundary. 1223 * generate subkeys from key. Align to 16-byte boundary.
1224 */ 1224 */
@@ -1235,7 +1235,7 @@ struct umac_ctx *umac_new(u_char key[])
1235 ctx = (struct umac_ctx *)((u_char *)ctx + bytes_to_add); 1235 ctx = (struct umac_ctx *)((u_char *)ctx + bytes_to_add);
1236 } 1236 }
1237 ctx->free_ptr = octx; 1237 ctx->free_ptr = octx;
1238 aes_key_setup(key,prf_key); 1238 aes_key_setup(key, prf_key);
1239 pdf_init(&ctx->pdf, prf_key); 1239 pdf_init(&ctx->pdf, prf_key);
1240 uhash_init(&ctx->hash, prf_key); 1240 uhash_init(&ctx->hash, prf_key);
1241 } 1241 }
@@ -1245,18 +1245,18 @@ struct umac_ctx *umac_new(u_char key[])
1245 1245
1246/* ---------------------------------------------------------------------- */ 1246/* ---------------------------------------------------------------------- */
1247 1247
1248int umac_final(struct umac_ctx *ctx, u_char tag[], u_char nonce[8]) 1248int umac_final(struct umac_ctx *ctx, u_char tag[], const u_char nonce[8])
1249/* Incorporate any pending data, pad, and generate tag */ 1249/* Incorporate any pending data, pad, and generate tag */
1250{ 1250{
1251 uhash_final(&ctx->hash, (u_char *)tag); 1251 uhash_final(&ctx->hash, (u_char *)tag);
1252 pdf_gen_xor(&ctx->pdf, (UINT8 *)nonce, (UINT8 *)tag); 1252 pdf_gen_xor(&ctx->pdf, (const UINT8 *)nonce, (UINT8 *)tag);
1253 1253
1254 return (1); 1254 return (1);
1255} 1255}
1256 1256
1257/* ---------------------------------------------------------------------- */ 1257/* ---------------------------------------------------------------------- */
1258 1258
1259int umac_update(struct umac_ctx *ctx, u_char *input, long len) 1259int umac_update(struct umac_ctx *ctx, const u_char *input, long len)
1260/* Given len bytes of data, we parse it into L1_KEY_LEN chunks and */ 1260/* Given len bytes of data, we parse it into L1_KEY_LEN chunks and */
1261/* hash each one, calling the PDF on the hashed output whenever the hash- */ 1261/* hash each one, calling the PDF on the hashed output whenever the hash- */
1262/* output buffer is full. */ 1262/* output buffer is full. */