8d72f1bf8ccb3f800d801c6bca7032366cd4420d
[platform/upstream/nettle.git] / umac96.c
1 /* umac96.c
2
3    Copyright (C) 2013 Niels Möller
4
5    This file is part of GNU Nettle.
6
7    GNU Nettle is free software: you can redistribute it and/or
8    modify it under the terms of either:
9
10      * the GNU Lesser General Public License as published by the Free
11        Software Foundation; either version 3 of the License, or (at your
12        option) any later version.
13
14    or
15
16      * the GNU General Public License as published by the Free
17        Software Foundation; either version 2 of the License, or (at your
18        option) any later version.
19
20    or both in parallel, as here.
21
22    GNU Nettle is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25    General Public License for more details.
26
27    You should have received copies of the GNU General Public License and
28    the GNU Lesser General Public License along with this program.  If
29    not, see http://www.gnu.org/licenses/.
30 */
31
32 #if HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <assert.h>
37 #include <string.h>
38
39 #include "umac.h"
40
41 #include "macros.h"
42
43 void
44 umac96_set_key (struct umac96_ctx *ctx, const uint8_t *key)
45 {
46   _umac_set_key (ctx->l1_key, ctx->l2_key, ctx->l3_key1, ctx->l3_key2,
47                  &ctx->pdf_key, key, 3);
48
49   /* Clear nonce */
50   memset (ctx->nonce, 0, sizeof(ctx->nonce));
51   ctx->nonce_length = sizeof(ctx->nonce);
52
53   /* Initialize buffer */
54   ctx->count = ctx->index = 0;
55 }
56
57 void
58 umac96_set_nonce (struct umac96_ctx *ctx,
59                   size_t nonce_length, const uint8_t *nonce)
60 {
61   assert (nonce_length > 0);
62   assert (nonce_length <= AES_BLOCK_SIZE);
63
64   memcpy (ctx->nonce, nonce, nonce_length);
65   memset (ctx->nonce + nonce_length, 0, AES_BLOCK_SIZE - nonce_length);
66
67   ctx->nonce_length = nonce_length;
68 }
69
70 #define UMAC96_BLOCK(ctx, block) do {                                   \
71     uint64_t __umac96_y[3];                                             \
72     _umac_nh_n (__umac96_y, 3, ctx->l1_key, UMAC_BLOCK_SIZE, block);    \
73     __umac96_y[0] += 8*UMAC_BLOCK_SIZE;                                 \
74     __umac96_y[1] += 8*UMAC_BLOCK_SIZE;                                 \
75     __umac96_y[2] += 8*UMAC_BLOCK_SIZE;                                 \
76     _umac_l2 (ctx->l2_key, ctx->l2_state, 3, ctx->count++, __umac96_y); \
77   } while (0)
78
79 void
80 umac96_update (struct umac96_ctx *ctx,
81                size_t length, const uint8_t *data)
82 {
83   MD_UPDATE (ctx, length, data, UMAC96_BLOCK, (void)0);
84 }
85
86
87 void
88 umac96_digest (struct umac96_ctx *ctx,
89                size_t length, uint8_t *digest)
90 {
91   uint32_t tag[4];
92   unsigned i;
93
94   assert (length > 0);
95   assert (length <= 12);
96
97   if (ctx->index > 0 || ctx->count == 0)
98     {
99       /* Zero pad to multiple of 32 */
100       uint64_t y[3];
101       unsigned pad = (ctx->index > 0) ? 31 & - ctx->index : 32;
102       memset (ctx->block + ctx->index, 0, pad);
103
104       _umac_nh_n (y, 3, ctx->l1_key, ctx->index + pad, ctx->block);
105       y[0] += 8 * ctx->index;
106       y[1] += 8 * ctx->index;
107       y[2] += 8 * ctx->index;
108       _umac_l2 (ctx->l2_key, ctx->l2_state, 3, ctx->count++, y);
109     }
110   assert (ctx->count > 0);
111
112   aes128_encrypt (&ctx->pdf_key, AES_BLOCK_SIZE,
113                   (uint8_t *) tag, ctx->nonce);
114
115   INCREMENT (ctx->nonce_length, ctx->nonce);
116
117   _umac_l2_final (ctx->l2_key, ctx->l2_state, 3, ctx->count);
118   for (i = 0; i < 3; i++)
119     tag[i] ^= ctx->l3_key2[i] ^ _umac_l3 (ctx->l3_key1 + 8*i,
120                                           ctx->l2_state + 2*i);
121
122   memcpy (digest, tag, length);
123
124   /* Reinitialize */
125   ctx->count = ctx->index = 0;
126 }