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