3 UMAC message authentication code (RFC-4418).
5 Copyright (C) 2013 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
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.
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.
22 or both in parallel, as here.
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.
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/.
34 #ifndef NETTLE_UMAC_H_INCLUDED
35 #define NETTLE_UMAC_H_INCLUDED
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
59 #include "nettle-types.h"
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
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 */
92 #define _UMAC_BUFFER \
94 /* Complete blocks processed */ \
96 uint8_t block[UMAC_BLOCK_SIZE]
98 #define _UMAC_NONCE_CACHED 0x80
103 /* Low bits and cache flag. */
104 unsigned short nonce_low;
105 /* Previous padding block */
106 uint32_t pad_cache[AES_BLOCK_SIZE / 4];
113 /* Low bit and cache flag. */
114 unsigned short nonce_low;
115 /* Previous padding block */
116 uint32_t pad_cache[AES_BLOCK_SIZE/4];
132 /* The _set_key function initialize the nonce to zero. */
134 umac32_set_key (struct umac32_ctx *ctx, const uint8_t *key);
136 umac64_set_key (struct umac64_ctx *ctx, const uint8_t *key);
138 umac96_set_key (struct umac96_ctx *ctx, const uint8_t *key);
140 umac128_set_key (struct umac128_ctx *ctx, const uint8_t *key);
142 /* Optional, if not used, messages get incrementing nonces starting from zero. */
144 umac32_set_nonce (struct umac32_ctx *ctx,
145 size_t nonce_length, const uint8_t *nonce);
147 umac64_set_nonce (struct umac64_ctx *ctx,
148 size_t nonce_length, const uint8_t *nonce);
150 umac96_set_nonce (struct umac96_ctx *ctx,
151 size_t nonce_length, const uint8_t *nonce);
153 umac128_set_nonce (struct umac128_ctx *ctx,
154 size_t nonce_length, const uint8_t *nonce);
157 umac32_update (struct umac32_ctx *ctx,
158 size_t length, const uint8_t *data);
160 umac64_update (struct umac64_ctx *ctx,
161 size_t length, const uint8_t *data);
163 umac96_update (struct umac96_ctx *ctx,
164 size_t length, const uint8_t *data);
166 umac128_update (struct umac128_ctx *ctx,
167 size_t length, const uint8_t *data);
169 /* The _digest functions increment the nonce */
171 umac32_digest (struct umac32_ctx *ctx,
172 size_t length, uint8_t *digest);
174 umac64_digest (struct umac64_ctx *ctx,
175 size_t length, uint8_t *digest);
177 umac96_digest (struct umac96_ctx *ctx,
178 size_t length, uint8_t *digest);
180 umac128_digest (struct umac128_ctx *ctx,
181 size_t length, uint8_t *digest);
184 /* Internal functions */
185 #define UMAC_POLY64_BLOCKS 16384
187 #define UMAC_P64_OFFSET 59
188 #define UMAC_P64 (- (uint64_t) UMAC_P64_OFFSET)
190 #define UMAC_P128_OFFSET 159
191 #define UMAC_P128_HI (~(uint64_t) 0)
192 #define UMAC_P128_LO (-(uint64_t) UMAC_P128_OFFSET)
198 #endif /* NETTLE_UMAC_H_INCLUDED */