32f34c39503b958eafb7f6afd171779fc70c7a0f
[platform/upstream/nettle.git] / umac32.c
1 /* umac32.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 umac32_set_key (struct umac32_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, 1);
48
49   /* Clear nonce */
50   memset (ctx->nonce, 0, sizeof(ctx->nonce));
51   ctx->nonce_low = 0;
52   ctx->nonce_length = sizeof(ctx->nonce);
53
54   /* Initialize buffer */
55   ctx->count = ctx->index = 0;
56 }
57
58 void
59 umac32_set_nonce (struct umac32_ctx *ctx,
60                   size_t nonce_length, const uint8_t *nonce)
61 {
62   assert (nonce_length > 0);
63   assert (nonce_length <= AES_BLOCK_SIZE);
64
65   memcpy (ctx->nonce, nonce, nonce_length);
66   memset (ctx->nonce + nonce_length, 0, AES_BLOCK_SIZE - nonce_length);
67
68   ctx->nonce_low = ctx->nonce[nonce_length - 1] & 3;
69   ctx->nonce[nonce_length - 1] &= ~3;
70   ctx->nonce_length = nonce_length;
71 }
72
73 #define UMAC32_BLOCK(ctx, block) do {                                   \
74     uint64_t __umac32_y                                                 \
75       = _umac_nh (ctx->l1_key, UMAC_BLOCK_SIZE, block)                  \
76       + 8*UMAC_BLOCK_SIZE ;                                             \
77     _umac_l2 (ctx->l2_key, ctx->l2_state, 1, ctx->count++, &__umac32_y); \
78   } while (0)
79
80 void
81 umac32_update (struct umac32_ctx *ctx,
82                size_t length, const uint8_t *data)
83 {
84   MD_UPDATE (ctx, length, data, UMAC32_BLOCK, (void)0);
85 }
86
87
88 void
89 umac32_digest (struct umac32_ctx *ctx,
90                size_t length, uint8_t *digest)
91 {
92   uint32_t pad;
93
94   assert (length > 0);
95   assert (length <= 4);
96
97   if (ctx->index > 0 || ctx->count == 0)
98     {
99       /* Zero pad to multiple of 32 */
100       uint64_t y;
101       unsigned pad = (ctx->index > 0) ? 31 & - ctx->index : 32;
102       memset (ctx->block + ctx->index, 0, pad);
103
104       y = _umac_nh (ctx->l1_key, ctx->index + pad, ctx->block)
105         + 8 * ctx->index;
106       _umac_l2 (ctx->l2_key, ctx->l2_state, 1, ctx->count++, &y);
107     }
108   assert (ctx->count > 0);
109   if ( !(ctx->nonce_low & _UMAC_NONCE_CACHED))
110     {
111       aes128_encrypt (&ctx->pdf_key, AES_BLOCK_SIZE,
112                       (uint8_t *) ctx->pad_cache, ctx->nonce);
113       ctx->nonce_low |= _UMAC_NONCE_CACHED;
114     }
115
116   pad = ctx->pad_cache[ctx->nonce_low & 3];
117
118   /* Increment nonce */
119   ctx->nonce_low++;
120   if ( !(ctx->nonce_low & 3))
121     {
122       unsigned i = ctx->nonce_length - 1;
123
124       ctx->nonce_low = 0;
125       ctx->nonce[i] += 4;
126
127       if (ctx->nonce[i] == 0 && i > 0)
128         INCREMENT (i, ctx->nonce);
129     }
130
131   _umac_l2_final (ctx->l2_key, ctx->l2_state, 1, ctx->count);
132   pad ^= ctx->l3_key2[0] ^ _umac_l3 (ctx->l3_key1, ctx->l2_state);
133   memcpy (digest, &pad, length);
134
135   /* Reinitialize */
136   ctx->count = ctx->index = 0;
137 }