4 * - AES Key Wrap Algorithm (128-bit KEK) (RFC3394)
5 * - One-Key CBC MAC (OMAC1) hash with AES-128
6 * - AES-128 CTR mode encryption
7 * - AES-128 EAX mode encryption/decryption
10 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
16 * Alternatively, this software may be distributed under the terms of BSD
19 * See README and COPYING for more details.
30 #endif /* INTERNAL_AES */
33 #ifndef CONFIG_NO_AES_WRAP
36 * aes_wrap - Wrap keys with AES Key Wrap Algorithm (128-bit KEK) (RFC3394)
37 * @kek: 16-octet Key encryption key (KEK)
38 * @n: Length of the plaintext key in 64-bit units; e.g., 2 = 128-bit = 16
40 * @plain: Plaintext key to be wrapped, n * 64 bits
41 * @cipher: Wrapped key, (n + 1) * 64 bits
42 * Returns: 0 on success, -1 on failure
44 int aes_wrap(const u8 *kek, int n, const u8 *plain, u8 *cipher)
53 /* 1) Initialize variables. */
54 os_memset(a, 0xa6, 8);
55 os_memcpy(r, plain, 8 * n);
57 ctx = aes_encrypt_init(kek, 16);
61 /* 2) Calculate intermediate values.
64 * B = AES(K, A | R[i])
65 * A = MSB(64, B) ^ t where t = (n*j)+i
68 for (j = 0; j <= 5; j++) {
70 for (i = 1; i <= n; i++) {
72 os_memcpy(b + 8, r, 8);
73 aes_encrypt(ctx, b, b);
76 os_memcpy(r, b + 8, 8);
80 aes_encrypt_deinit(ctx);
82 /* 3) Output the results.
84 * These are already in @cipher due to the location of temporary
91 #endif /* CONFIG_NO_AES_WRAP */
95 * aes_unwrap - Unwrap key with AES Key Wrap Algorithm (128-bit KEK) (RFC3394)
96 * @kek: Key encryption key (KEK)
97 * @n: Length of the plaintext key in 64-bit units; e.g., 2 = 128-bit = 16
99 * @cipher: Wrapped key to be unwrapped, (n + 1) * 64 bits
100 * @plain: Plaintext key, n * 64 bits
101 * Returns: 0 on success, -1 on failure (e.g., integrity verification failed)
103 int aes_unwrap(const u8 *kek, int n, const u8 *cipher, u8 *plain)
109 /* 1) Initialize variables. */
110 os_memcpy(a, cipher, 8);
112 os_memcpy(r, cipher + 8, 8 * n);
114 ctx = aes_decrypt_init(kek, 16);
118 /* 2) Compute intermediate values.
121 * B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i
125 for (j = 5; j >= 0; j--) {
126 r = plain + (n - 1) * 8;
127 for (i = n; i >= 1; i--) {
131 os_memcpy(b + 8, r, 8);
132 aes_decrypt(ctx, b, b);
134 os_memcpy(r, b + 8, 8);
138 aes_decrypt_deinit(ctx);
140 /* 3) Output results.
142 * These are already in @plain due to the location of temporary
143 * variables. Just verify that the IV matches with the expected value.
145 for (i = 0; i < 8; i++) {
154 #define BLOCK_SIZE 16
156 #ifndef CONFIG_NO_AES_OMAC1
158 static void gf_mulx(u8 *pad)
162 carry = pad[0] & 0x80;
163 for (i = 0; i < BLOCK_SIZE - 1; i++)
164 pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
165 pad[BLOCK_SIZE - 1] <<= 1;
167 pad[BLOCK_SIZE - 1] ^= 0x87;
172 * omac1_aes_128_vector - One-Key CBC MAC (OMAC1) hash with AES-128
173 * @key: 128-bit key for the hash operation
174 * @num_elem: Number of elements in the data vector
175 * @addr: Pointers to the data areas
176 * @len: Lengths of the data blocks
177 * @mac: Buffer for MAC (128 bits, i.e., 16 bytes)
178 * Returns: 0 on success, -1 on failure
180 int omac1_aes_128_vector(const u8 *key, size_t num_elem,
181 const u8 *addr[], const size_t *len, u8 *mac)
184 u8 cbc[BLOCK_SIZE], pad[BLOCK_SIZE];
186 size_t i, e, left, total_len;
188 ctx = aes_encrypt_init(key, 16);
191 os_memset(cbc, 0, BLOCK_SIZE);
194 for (e = 0; e < num_elem; e++)
202 while (left >= BLOCK_SIZE) {
203 for (i = 0; i < BLOCK_SIZE; i++) {
211 if (left > BLOCK_SIZE)
212 aes_encrypt(ctx, cbc, cbc);
216 os_memset(pad, 0, BLOCK_SIZE);
217 aes_encrypt(ctx, pad, pad);
220 if (left || total_len == 0) {
221 for (i = 0; i < left; i++) {
233 for (i = 0; i < BLOCK_SIZE; i++)
235 aes_encrypt(ctx, pad, mac);
236 aes_encrypt_deinit(ctx);
242 * omac1_aes_128 - One-Key CBC MAC (OMAC1) hash with AES-128 (aka AES-CMAC)
243 * @key: 128-bit key for the hash operation
244 * @data: Data buffer for which a MAC is determined
245 * @data_len: Length of data buffer in bytes
246 * @mac: Buffer for MAC (128 bits, i.e., 16 bytes)
247 * Returns: 0 on success, -1 on failure
249 * This is a mode for using block cipher (AES in this case) for authentication.
250 * OMAC1 was standardized with the name CMAC by NIST in a Special Publication
253 int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
255 return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
258 #endif /* CONFIG_NO_AES_OMAC1 */
262 * aes_128_encrypt_block - Perform one AES 128-bit block operation
264 * @in: Input data (16 bytes)
265 * @out: Output of the AES block operation (16 bytes)
266 * Returns: 0 on success, -1 on failure
268 int aes_128_encrypt_block(const u8 *key, const u8 *in, u8 *out)
271 ctx = aes_encrypt_init(key, 16);
274 aes_encrypt(ctx, in, out);
275 aes_encrypt_deinit(ctx);
280 #ifndef CONFIG_NO_AES_CTR
283 * aes_128_ctr_encrypt - AES-128 CTR mode encryption
284 * @key: Key for encryption (16 bytes)
285 * @nonce: Nonce for counter mode (16 bytes)
286 * @data: Data to encrypt in-place
287 * @data_len: Length of data in bytes
288 * Returns: 0 on success, -1 on failure
290 int aes_128_ctr_encrypt(const u8 *key, const u8 *nonce,
291 u8 *data, size_t data_len)
294 size_t j, len, left = data_len;
297 u8 counter[BLOCK_SIZE], buf[BLOCK_SIZE];
299 ctx = aes_encrypt_init(key, 16);
302 os_memcpy(counter, nonce, BLOCK_SIZE);
305 aes_encrypt(ctx, counter, buf);
307 len = (left < BLOCK_SIZE) ? left : BLOCK_SIZE;
308 for (j = 0; j < len; j++)
313 for (i = BLOCK_SIZE - 1; i >= 0; i--) {
319 aes_encrypt_deinit(ctx);
323 #endif /* CONFIG_NO_AES_CTR */
326 #ifndef CONFIG_NO_AES_EAX
329 * aes_128_eax_encrypt - AES-128 EAX mode encryption
330 * @key: Key for encryption (16 bytes)
331 * @nonce: Nonce for counter mode
332 * @nonce_len: Nonce length in bytes
333 * @hdr: Header data to be authenticity protected
334 * @hdr_len: Length of the header data bytes
335 * @data: Data to encrypt in-place
336 * @data_len: Length of data in bytes
337 * @tag: 16-byte tag value
338 * Returns: 0 on success, -1 on failure
340 int aes_128_eax_encrypt(const u8 *key, const u8 *nonce, size_t nonce_len,
341 const u8 *hdr, size_t hdr_len,
342 u8 *data, size_t data_len, u8 *tag)
346 u8 nonce_mac[BLOCK_SIZE], hdr_mac[BLOCK_SIZE], data_mac[BLOCK_SIZE];
349 if (nonce_len > data_len)
353 if (hdr_len > buf_len)
357 buf = os_malloc(buf_len);
361 os_memset(buf, 0, 15);
364 os_memcpy(buf + 16, nonce, nonce_len);
365 omac1_aes_128(key, buf, 16 + nonce_len, nonce_mac);
368 os_memcpy(buf + 16, hdr, hdr_len);
369 omac1_aes_128(key, buf, 16 + hdr_len, hdr_mac);
371 aes_128_ctr_encrypt(key, nonce_mac, data, data_len);
373 os_memcpy(buf + 16, data, data_len);
374 omac1_aes_128(key, buf, 16 + data_len, data_mac);
378 for (i = 0; i < BLOCK_SIZE; i++)
379 tag[i] = nonce_mac[i] ^ data_mac[i] ^ hdr_mac[i];
386 * aes_128_eax_decrypt - AES-128 EAX mode decryption
387 * @key: Key for decryption (16 bytes)
388 * @nonce: Nonce for counter mode
389 * @nonce_len: Nonce length in bytes
390 * @hdr: Header data to be authenticity protected
391 * @hdr_len: Length of the header data bytes
392 * @data: Data to encrypt in-place
393 * @data_len: Length of data in bytes
394 * @tag: 16-byte tag value
395 * Returns: 0 on success, -1 on failure, -2 if tag does not match
397 int aes_128_eax_decrypt(const u8 *key, const u8 *nonce, size_t nonce_len,
398 const u8 *hdr, size_t hdr_len,
399 u8 *data, size_t data_len, const u8 *tag)
403 u8 nonce_mac[BLOCK_SIZE], hdr_mac[BLOCK_SIZE], data_mac[BLOCK_SIZE];
406 if (nonce_len > data_len)
410 if (hdr_len > buf_len)
414 buf = os_malloc(buf_len);
418 os_memset(buf, 0, 15);
421 os_memcpy(buf + 16, nonce, nonce_len);
422 omac1_aes_128(key, buf, 16 + nonce_len, nonce_mac);
425 os_memcpy(buf + 16, hdr, hdr_len);
426 omac1_aes_128(key, buf, 16 + hdr_len, hdr_mac);
429 os_memcpy(buf + 16, data, data_len);
430 omac1_aes_128(key, buf, 16 + data_len, data_mac);
434 for (i = 0; i < BLOCK_SIZE; i++) {
435 if (tag[i] != (nonce_mac[i] ^ data_mac[i] ^ hdr_mac[i]))
439 aes_128_ctr_encrypt(key, nonce_mac, data, data_len);
444 #endif /* CONFIG_NO_AES_EAX */
447 #ifndef CONFIG_NO_AES_CBC
450 * aes_128_cbc_encrypt - AES-128 CBC encryption
451 * @key: Encryption key
452 * @iv: Encryption IV for CBC mode (16 bytes)
453 * @data: Data to encrypt in-place
454 * @data_len: Length of data in bytes (must be divisible by 16)
455 * Returns: 0 on success, -1 on failure
457 int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
464 ctx = aes_encrypt_init(key, 16);
467 os_memcpy(cbc, iv, BLOCK_SIZE);
469 blocks = data_len / BLOCK_SIZE;
470 for (i = 0; i < blocks; i++) {
471 for (j = 0; j < BLOCK_SIZE; j++)
473 aes_encrypt(ctx, cbc, cbc);
474 os_memcpy(pos, cbc, BLOCK_SIZE);
477 aes_encrypt_deinit(ctx);
483 * aes_128_cbc_decrypt - AES-128 CBC decryption
484 * @key: Decryption key
485 * @iv: Decryption IV for CBC mode (16 bytes)
486 * @data: Data to decrypt in-place
487 * @data_len: Length of data in bytes (must be divisible by 16)
488 * Returns: 0 on success, -1 on failure
490 int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
493 u8 cbc[BLOCK_SIZE], tmp[BLOCK_SIZE];
497 ctx = aes_decrypt_init(key, 16);
500 os_memcpy(cbc, iv, BLOCK_SIZE);
502 blocks = data_len / BLOCK_SIZE;
503 for (i = 0; i < blocks; i++) {
504 os_memcpy(tmp, pos, BLOCK_SIZE);
505 aes_decrypt(ctx, pos, pos);
506 for (j = 0; j < BLOCK_SIZE; j++)
508 os_memcpy(cbc, tmp, BLOCK_SIZE);
511 aes_decrypt_deinit(ctx);
515 #endif /* CONFIG_NO_AES_CBC */