/src/nettle/umac-set-key.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* umac-set-key.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 <string.h> |
37 | | |
38 | | #include "umac.h" |
39 | | #include "umac-internal.h" |
40 | | |
41 | | #include "macros.h" |
42 | | #include "bswap-internal.h" |
43 | | |
44 | | static void |
45 | | umac_kdf (struct aes128_ctx *aes, unsigned index, unsigned length, uint8_t *dst) |
46 | 0 | { |
47 | 0 | uint8_t block[AES_BLOCK_SIZE]; |
48 | 0 | uint64_t count; |
49 | 0 | WRITE_UINT64 (block, (uint64_t) index); |
50 | 0 | for (count = 1; length >= AES_BLOCK_SIZE; |
51 | 0 | length -= AES_BLOCK_SIZE, dst += AES_BLOCK_SIZE, count++) |
52 | 0 | { |
53 | 0 | WRITE_UINT64 (block + 8, count); |
54 | 0 | aes128_encrypt (aes, AES_BLOCK_SIZE, dst, block); |
55 | 0 | } |
56 | 0 | if (length > 0) |
57 | 0 | { |
58 | 0 | WRITE_UINT64 (block + 8, count); |
59 | 0 | aes128_encrypt (aes, AES_BLOCK_SIZE, block, block); |
60 | 0 | memcpy (dst, block, length); |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | | #if WORDS_BIGENDIAN |
65 | | /* FIXME: Duplicated with blowfish-bcrypt.c. */ |
66 | | #define bswap32_if_le_n(n, x) |
67 | | #else |
68 | | static void |
69 | | bswap32_if_le_n (unsigned n, uint32_t *x) |
70 | 0 | { |
71 | 0 | unsigned i; |
72 | 0 | for (i = 0; i < n; i++) |
73 | 0 | x[i] = nettle_bswap32 (x[i]); |
74 | 0 | } |
75 | | #endif |
76 | | |
77 | | void |
78 | | _nettle_umac_set_key (uint32_t *l1_key, uint32_t *l2_key, |
79 | | uint64_t *l3_key1, uint32_t *l3_key2, |
80 | | struct aes128_ctx *aes, const uint8_t *key, unsigned n) |
81 | 0 | { |
82 | 0 | unsigned size; |
83 | 0 | uint8_t buffer[UMAC_KEY_SIZE]; |
84 | |
|
85 | 0 | aes128_set_encrypt_key (aes, key); |
86 | |
|
87 | 0 | size = UMAC_BLOCK_SIZE / 4 + 4*(n-1); |
88 | 0 | umac_kdf (aes, 1, size * sizeof(uint32_t), (uint8_t *) l1_key); |
89 | 0 | bswap32_if_le_n (size, l1_key); |
90 | |
|
91 | 0 | size = 6*n; |
92 | 0 | umac_kdf (aes, 2, size * sizeof(uint32_t), (uint8_t *) l2_key); |
93 | 0 | _nettle_umac_l2_init (size, l2_key); |
94 | |
|
95 | 0 | size = 8*n; |
96 | 0 | umac_kdf (aes, 3, size * sizeof(uint64_t), (uint8_t *) l3_key1); |
97 | 0 | _nettle_umac_l3_init (size, l3_key1); |
98 | | |
99 | | /* No need to byteswap these subkeys. */ |
100 | 0 | umac_kdf (aes, 4, n * sizeof(uint32_t), (uint8_t *) l3_key2); |
101 | |
|
102 | 0 | umac_kdf (aes, 0, UMAC_KEY_SIZE, buffer); |
103 | 0 | aes128_set_encrypt_key (aes, buffer); |
104 | 0 | } |