3 Copyright (C) 2013 Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
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.
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.
20 or both in parallel, as here.
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.
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/.
43 umac_kdf (struct aes128_ctx *aes, unsigned index, unsigned length, uint8_t *dst)
45 uint8_t block[AES_BLOCK_SIZE];
47 WRITE_UINT64 (block, (uint64_t) index);
48 for (count = 1; length >= AES_BLOCK_SIZE;
49 length -= AES_BLOCK_SIZE, dst += AES_BLOCK_SIZE, count++)
51 WRITE_UINT64 (block + 8, count);
52 aes128_encrypt (aes, AES_BLOCK_SIZE, dst, block);
56 WRITE_UINT64 (block + 8, count);
57 aes128_encrypt (aes, AES_BLOCK_SIZE, block, block);
58 memcpy (dst, block, length);
63 #define BE_SWAP32(x) x
64 #define BE_SWAP32_N(n, x)
66 #define BE_SWAP32(x) \
67 ((ROTL32(8, x) & 0x00FF00FFUL) | \
68 (ROTL32(24, x) & 0xFF00FF00UL))
69 #define BE_SWAP32_N(n, x) do { \
71 for (be_i = 0; be_i < n; be_i++) \
73 uint32_t be_x = (x)[be_i]; \
74 (x)[be_i] = BE_SWAP32 (be_x); \
80 _umac_set_key (uint32_t *l1_key, uint32_t *l2_key,
81 uint64_t *l3_key1, uint32_t *l3_key2,
82 struct aes128_ctx *aes, const uint8_t *key, unsigned n)
85 uint8_t buffer[UMAC_KEY_SIZE];
87 aes128_set_encrypt_key (aes, key);
89 size = UMAC_BLOCK_SIZE / 4 + 4*(n-1);
90 umac_kdf (aes, 1, size * sizeof(uint32_t), (uint8_t *) l1_key);
91 BE_SWAP32_N (size, l1_key);
94 umac_kdf (aes, 2, size * sizeof(uint32_t), (uint8_t *) l2_key);
95 _umac_l2_init (size, l2_key);
98 umac_kdf (aes, 3, size * sizeof(uint64_t), (uint8_t *) l3_key1);
99 _umac_l3_init (size, l3_key1);
101 /* No need to byteswap these subkeys. */
102 umac_kdf (aes, 4, n * sizeof(uint32_t), (uint8_t *) l3_key2);
104 umac_kdf (aes, 0, UMAC_KEY_SIZE, buffer);
105 aes128_set_encrypt_key (aes, buffer);