xref: /dflybsd-src/contrib/wpa_supplicant/src/crypto/aes-wrap.c (revision 3a84a4273475ed07d0ab1c2dfeffdfedef35d9cd)
13ff40c12SJohn Marino /*
2*a1157835SDaniel Fojt  * AES Key Wrap Algorithm (RFC3394)
33ff40c12SJohn Marino  *
43ff40c12SJohn Marino  * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
53ff40c12SJohn Marino  *
63ff40c12SJohn Marino  * This software may be distributed under the terms of the BSD license.
73ff40c12SJohn Marino  * See README for more details.
83ff40c12SJohn Marino  */
93ff40c12SJohn Marino 
103ff40c12SJohn Marino #include "includes.h"
113ff40c12SJohn Marino 
123ff40c12SJohn Marino #include "common.h"
133ff40c12SJohn Marino #include "aes.h"
143ff40c12SJohn Marino #include "aes_wrap.h"
153ff40c12SJohn Marino 
163ff40c12SJohn Marino /**
17*a1157835SDaniel Fojt  * aes_wrap - Wrap keys with AES Key Wrap Algorithm (RFC3394)
18*a1157835SDaniel Fojt  * @kek: Key encryption key (KEK)
19*a1157835SDaniel Fojt  * @kek_len: Length of KEK in octets
203ff40c12SJohn Marino  * @n: Length of the plaintext key in 64-bit units; e.g., 2 = 128-bit = 16
213ff40c12SJohn Marino  * bytes
223ff40c12SJohn Marino  * @plain: Plaintext key to be wrapped, n * 64 bits
233ff40c12SJohn Marino  * @cipher: Wrapped key, (n + 1) * 64 bits
243ff40c12SJohn Marino  * Returns: 0 on success, -1 on failure
253ff40c12SJohn Marino  */
aes_wrap(const u8 * kek,size_t kek_len,int n,const u8 * plain,u8 * cipher)26*a1157835SDaniel Fojt int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
273ff40c12SJohn Marino {
28*a1157835SDaniel Fojt 	u8 *a, *r, b[AES_BLOCK_SIZE];
293ff40c12SJohn Marino 	int i, j;
303ff40c12SJohn Marino 	void *ctx;
31*a1157835SDaniel Fojt 	unsigned int t;
323ff40c12SJohn Marino 
333ff40c12SJohn Marino 	a = cipher;
343ff40c12SJohn Marino 	r = cipher + 8;
353ff40c12SJohn Marino 
363ff40c12SJohn Marino 	/* 1) Initialize variables. */
373ff40c12SJohn Marino 	os_memset(a, 0xa6, 8);
383ff40c12SJohn Marino 	os_memcpy(r, plain, 8 * n);
393ff40c12SJohn Marino 
40*a1157835SDaniel Fojt 	ctx = aes_encrypt_init(kek, kek_len);
413ff40c12SJohn Marino 	if (ctx == NULL)
423ff40c12SJohn Marino 		return -1;
433ff40c12SJohn Marino 
443ff40c12SJohn Marino 	/* 2) Calculate intermediate values.
453ff40c12SJohn Marino 	 * For j = 0 to 5
463ff40c12SJohn Marino 	 *     For i=1 to n
473ff40c12SJohn Marino 	 *         B = AES(K, A | R[i])
483ff40c12SJohn Marino 	 *         A = MSB(64, B) ^ t where t = (n*j)+i
493ff40c12SJohn Marino 	 *         R[i] = LSB(64, B)
503ff40c12SJohn Marino 	 */
513ff40c12SJohn Marino 	for (j = 0; j <= 5; j++) {
523ff40c12SJohn Marino 		r = cipher + 8;
533ff40c12SJohn Marino 		for (i = 1; i <= n; i++) {
543ff40c12SJohn Marino 			os_memcpy(b, a, 8);
553ff40c12SJohn Marino 			os_memcpy(b + 8, r, 8);
563ff40c12SJohn Marino 			aes_encrypt(ctx, b, b);
573ff40c12SJohn Marino 			os_memcpy(a, b, 8);
58*a1157835SDaniel Fojt 			t = n * j + i;
59*a1157835SDaniel Fojt 			a[7] ^= t;
60*a1157835SDaniel Fojt 			a[6] ^= t >> 8;
61*a1157835SDaniel Fojt 			a[5] ^= t >> 16;
62*a1157835SDaniel Fojt 			a[4] ^= t >> 24;
633ff40c12SJohn Marino 			os_memcpy(r, b + 8, 8);
643ff40c12SJohn Marino 			r += 8;
653ff40c12SJohn Marino 		}
663ff40c12SJohn Marino 	}
673ff40c12SJohn Marino 	aes_encrypt_deinit(ctx);
683ff40c12SJohn Marino 
693ff40c12SJohn Marino 	/* 3) Output the results.
703ff40c12SJohn Marino 	 *
713ff40c12SJohn Marino 	 * These are already in @cipher due to the location of temporary
723ff40c12SJohn Marino 	 * variables.
733ff40c12SJohn Marino 	 */
743ff40c12SJohn Marino 
753ff40c12SJohn Marino 	return 0;
763ff40c12SJohn Marino }
77