2 * AES Key Wrap Algorithm (RFC3394)
4 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
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
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
26 int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
28 u8 *a, *r, b[AES_BLOCK_SIZE];
36 /* 1) Initialize variables. */
37 os_memset(a, 0xa6, 8);
38 os_memcpy(r, plain, 8 * n);
40 ctx = aes_encrypt_init(kek, kek_len);
44 /* 2) Calculate intermediate values.
47 * B = AES(K, A | R[i])
48 * A = MSB(64, B) ^ t where t = (n*j)+i
51 for (j = 0; j <= 5; j++) {
53 for (i = 1; i <= n; i++) {
55 os_memcpy(b + 8, r, 8);
56 aes_encrypt(ctx, b, b);
63 os_memcpy(r, b + 8, 8);
67 aes_encrypt_deinit(ctx);
69 /* 3) Output the results.
71 * These are already in @cipher due to the location of temporary