2 * AES Key Wrap Algorithm (128-bit KEK) (RFC3394)
4 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
23 * aes_wrap - Wrap keys with AES Key Wrap Algorithm (128-bit KEK) (RFC3394)
24 * @kek: 16-octet Key encryption key (KEK)
25 * @n: Length of the plaintext key in 64-bit units; e.g., 2 = 128-bit = 16
27 * @plain: Plaintext key to be wrapped, n * 64 bits
28 * @cipher: Wrapped key, (n + 1) * 64 bits
29 * Returns: 0 on success, -1 on failure
31 int aes_wrap(const u8 *kek, int n, const u8 *plain, u8 *cipher)
40 /* 1) Initialize variables. */
41 os_memset(a, 0xa6, 8);
42 os_memcpy(r, plain, 8 * n);
44 ctx = aes_encrypt_init(kek, 16);
48 /* 2) Calculate intermediate values.
51 * B = AES(K, A | R[i])
52 * A = MSB(64, B) ^ t where t = (n*j)+i
55 for (j = 0; j <= 5; j++) {
57 for (i = 1; i <= n; i++) {
59 os_memcpy(b + 8, r, 8);
60 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