/src/hostap/src/crypto/aes-wrap.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * AES Key Wrap Algorithm (RFC3394) |
3 | | * |
4 | | * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi> |
5 | | * |
6 | | * This software may be distributed under the terms of the BSD license. |
7 | | * See README for more details. |
8 | | */ |
9 | | |
10 | | #include "includes.h" |
11 | | |
12 | | #include "common.h" |
13 | | #include "aes.h" |
14 | | #include "aes_wrap.h" |
15 | | |
16 | | /** |
17 | | * aes_wrap - Wrap keys with AES Key Wrap Algorithm (RFC3394) |
18 | | * @kek: Key encryption key (KEK) |
19 | | * @kek_len: Length of KEK in octets |
20 | | * @n: Length of the plaintext key in 64-bit units; e.g., 2 = 128-bit = 16 |
21 | | * bytes |
22 | | * @plain: Plaintext key to be wrapped, n * 64 bits |
23 | | * @cipher: Wrapped key, (n + 1) * 64 bits |
24 | | * Returns: 0 on success, -1 on failure |
25 | | */ |
26 | | int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher) |
27 | 1.81k | { |
28 | 1.81k | u8 *a, *r, b[AES_BLOCK_SIZE]; |
29 | 1.81k | int i, j; |
30 | 1.81k | void *ctx; |
31 | 1.81k | unsigned int t; |
32 | | |
33 | 1.81k | a = cipher; |
34 | 1.81k | r = cipher + 8; |
35 | | |
36 | | /* 1) Initialize variables. */ |
37 | 1.81k | os_memset(a, 0xa6, 8); |
38 | 1.81k | os_memcpy(r, plain, 8 * n); |
39 | | |
40 | 1.81k | ctx = aes_encrypt_init(kek, kek_len); |
41 | 1.81k | if (ctx == NULL) |
42 | 0 | return -1; |
43 | | |
44 | | /* 2) Calculate intermediate values. |
45 | | * For j = 0 to 5 |
46 | | * For i=1 to n |
47 | | * B = AES(K, A | R[i]) |
48 | | * A = MSB(64, B) ^ t where t = (n*j)+i |
49 | | * R[i] = LSB(64, B) |
50 | | */ |
51 | 12.6k | for (j = 0; j <= 5; j++) { |
52 | 10.8k | r = cipher + 8; |
53 | 119k | for (i = 1; i <= n; i++) { |
54 | 108k | os_memcpy(b, a, 8); |
55 | 108k | os_memcpy(b + 8, r, 8); |
56 | 108k | aes_encrypt(ctx, b, b); |
57 | 108k | os_memcpy(a, b, 8); |
58 | 108k | t = n * j + i; |
59 | 108k | a[7] ^= t; |
60 | 108k | a[6] ^= t >> 8; |
61 | 108k | a[5] ^= t >> 16; |
62 | 108k | a[4] ^= t >> 24; |
63 | 108k | os_memcpy(r, b + 8, 8); |
64 | 108k | r += 8; |
65 | 108k | } |
66 | 10.8k | } |
67 | 1.81k | aes_encrypt_deinit(ctx); |
68 | | |
69 | | /* 3) Output the results. |
70 | | * |
71 | | * These are already in @cipher due to the location of temporary |
72 | | * variables. |
73 | | */ |
74 | | |
75 | 1.81k | return 0; |
76 | 1.81k | } |