cfb8: Fix decrypt path
[gd/nettle] / umac.h
1 /* umac.h
2
3    UMAC message authentication code (RFC-4418).
4
5    Copyright (C) 2013 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33
34 #ifndef NETTLE_UMAC_H_INCLUDED
35 #define NETTLE_UMAC_H_INCLUDED
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /* Namespace mangling */
42 #define umac32_set_key  nettle_umac32_set_key
43 #define umac64_set_key  nettle_umac64_set_key
44 #define umac96_set_key  nettle_umac96_set_key
45 #define umac128_set_key nettle_umac128_set_key
46 #define umac32_set_nonce  nettle_umac32_set_nonce
47 #define umac64_set_nonce  nettle_umac64_set_nonce
48 #define umac96_set_nonce  nettle_umac96_set_nonce
49 #define umac128_set_nonce nettle_umac128_set_nonce
50 #define umac32_update  nettle_umac32_update
51 #define umac64_update  nettle_umac64_update
52 #define umac96_update  nettle_umac96_update
53 #define umac128_update nettle_umac128_update
54 #define umac32_digest  nettle_umac32_digest
55 #define umac64_digest  nettle_umac64_digest
56 #define umac96_digest  nettle_umac96_digest
57 #define umac128_digest nettle_umac128_digest
58
59 #include "nettle-types.h"
60 #include "aes.h"
61
62 #define UMAC_KEY_SIZE AES128_KEY_SIZE
63 #define UMAC32_DIGEST_SIZE 4
64 #define UMAC64_DIGEST_SIZE 8
65 #define UMAC96_DIGEST_SIZE 12
66 #define UMAC128_DIGEST_SIZE 16
67 #define UMAC_BLOCK_SIZE 1024
68 #define UMAC_MIN_NONCE_SIZE 1
69 #define UMAC_MAX_NONCE_SIZE AES_BLOCK_SIZE
70 /* For backwards compatibility */
71 #define UMAC_DATA_SIZE UMAC_BLOCK_SIZE
72
73 /* Subkeys and state for UMAC with tag size 32*n bits. */
74 #define _UMAC_STATE(n)                                  \
75   uint32_t l1_key[UMAC_BLOCK_SIZE/4 + 4*((n)-1)];       \
76   /* Keys in 32-bit pieces, high first */               \
77   uint32_t l2_key[6*(n)];                               \
78   uint64_t l3_key1[8*(n)];                              \
79   uint32_t l3_key2[(n)];                                \
80   /* AES cipher for encrypting the nonce */             \
81   struct aes128_ctx pdf_key;                            \
82   /* The l2_state consists of 2*n uint64_t, for poly64  \
83      and poly128 hashing, followed by n additional      \
84      uint64_t used as an input buffer. */               \
85   uint64_t l2_state[3*(n)];                             \
86   /* Input to the pdf_key, zero-padded and low bits     \
87      cleared if appropriate. */                         \
88   uint8_t nonce[AES_BLOCK_SIZE];                        \
89   unsigned short nonce_length /* For incrementing */
90
91   /* Buffering */ 
92 #define _UMAC_BUFFER                                    \
93   unsigned index;                                       \
94   /* Complete blocks processed */                       \
95   uint64_t count;                                       \
96   uint8_t block[UMAC_BLOCK_SIZE]
97   
98 #define _UMAC_NONCE_CACHED 0x80
99
100 struct umac32_ctx
101 {
102   _UMAC_STATE(1);
103   /* Low bits and cache flag. */
104   unsigned short nonce_low;
105   /* Previous padding block */
106   uint32_t pad_cache[AES_BLOCK_SIZE / 4];
107   _UMAC_BUFFER;
108 };
109
110 struct umac64_ctx
111 {
112   _UMAC_STATE(2);
113   /* Low bit and cache flag. */
114   unsigned short nonce_low;
115   /* Previous padding block */
116   uint32_t pad_cache[AES_BLOCK_SIZE/4];
117   _UMAC_BUFFER;
118 };
119
120 struct umac96_ctx
121 {
122   _UMAC_STATE(3);
123   _UMAC_BUFFER;
124 };
125
126 struct umac128_ctx
127 {
128   _UMAC_STATE(4);
129   _UMAC_BUFFER;
130 };
131
132 /* The _set_key function initialize the nonce to zero. */
133 void
134 umac32_set_key (struct umac32_ctx *ctx, const uint8_t *key);
135 void
136 umac64_set_key (struct umac64_ctx *ctx, const uint8_t *key);
137 void
138 umac96_set_key (struct umac96_ctx *ctx, const uint8_t *key);
139 void
140 umac128_set_key (struct umac128_ctx *ctx, const uint8_t *key);
141
142 /* Optional, if not used, messages get incrementing nonces starting from zero. */
143 void
144 umac32_set_nonce (struct umac32_ctx *ctx,
145                   size_t nonce_length, const uint8_t *nonce);
146 void
147 umac64_set_nonce (struct umac64_ctx *ctx,
148                   size_t nonce_length, const uint8_t *nonce);
149 void
150 umac96_set_nonce (struct umac96_ctx *ctx,
151                   size_t nonce_length, const uint8_t *nonce);
152 void
153 umac128_set_nonce (struct umac128_ctx *ctx,
154                    size_t nonce_length, const uint8_t *nonce);
155
156 void
157 umac32_update (struct umac32_ctx *ctx,
158                size_t length, const uint8_t *data);
159 void
160 umac64_update (struct umac64_ctx *ctx,
161                size_t length, const uint8_t *data);
162 void
163 umac96_update (struct umac96_ctx *ctx,
164                size_t length, const uint8_t *data);
165 void
166 umac128_update (struct umac128_ctx *ctx,
167                 size_t length, const uint8_t *data);
168
169 /* The _digest functions increment the nonce */
170 void
171 umac32_digest (struct umac32_ctx *ctx,
172                size_t length, uint8_t *digest);
173 void
174 umac64_digest (struct umac64_ctx *ctx,
175                size_t length, uint8_t *digest);
176 void
177 umac96_digest (struct umac96_ctx *ctx,
178                size_t length, uint8_t *digest);
179 void
180 umac128_digest (struct umac128_ctx *ctx,
181                 size_t length, uint8_t *digest);
182
183
184 /* Internal functions */
185 #define UMAC_POLY64_BLOCKS 16384
186
187 #define UMAC_P64_OFFSET 59
188 #define UMAC_P64 (- (uint64_t) UMAC_P64_OFFSET)
189
190 #define UMAC_P128_OFFSET 159
191 #define UMAC_P128_HI (~(uint64_t) 0)
192 #define UMAC_P128_LO (-(uint64_t) UMAC_P128_OFFSET)
193
194 #ifdef __cplusplus
195 }
196 #endif
197
198 #endif /* NETTLE_UMAC_H_INCLUDED */