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_128_eax_encrypt - AES-128 EAX mode encryption
18 * @key: Key for encryption (16 bytes)
19 * @nonce: Nonce for counter mode
20 * @nonce_len: Nonce length in bytes
21 * @hdr: Header data to be authenticity protected
22 * @hdr_len: Length of the header data bytes
23 * @data: Data to encrypt in-place
24 * @data_len: Length of data in bytes
25 * @tag: 16-byte tag value
26 * Returns: 0 on success, -1 on failure
28 int aes_128_eax_encrypt(const u8 *key, const u8 *nonce, size_t nonce_len,
29 const u8 *hdr, size_t hdr_len,
30 u8 *data, size_t data_len, u8 *tag)
34 u8 nonce_mac[AES_BLOCK_SIZE], hdr_mac[AES_BLOCK_SIZE],
35 data_mac[AES_BLOCK_SIZE];
38 if (nonce_len > data_len)
42 if (hdr_len > buf_len)
46 buf = os_malloc(buf_len);
50 os_memset(buf, 0, 15);
53 os_memcpy(buf + 16, nonce, nonce_len);
54 if (omac1_aes_128(key, buf, 16 + nonce_len, nonce_mac))
58 os_memcpy(buf + 16, hdr, hdr_len);
59 if (omac1_aes_128(key, buf, 16 + hdr_len, hdr_mac))
62 if (aes_128_ctr_encrypt(key, nonce_mac, data, data_len))
65 os_memcpy(buf + 16, data, data_len);
66 if (omac1_aes_128(key, buf, 16 + data_len, data_mac))
69 for (i = 0; i < AES_BLOCK_SIZE; i++)
70 tag[i] = nonce_mac[i] ^ data_mac[i] ^ hdr_mac[i];
81 * aes_128_eax_decrypt - AES-128 EAX mode decryption
82 * @key: Key for decryption (16 bytes)
83 * @nonce: Nonce for counter mode
84 * @nonce_len: Nonce length in bytes
85 * @hdr: Header data to be authenticity protected
86 * @hdr_len: Length of the header data bytes
87 * @data: Data to encrypt in-place
88 * @data_len: Length of data in bytes
89 * @tag: 16-byte tag value
90 * Returns: 0 on success, -1 on failure, -2 if tag does not match
92 int aes_128_eax_decrypt(const u8 *key, const u8 *nonce, size_t nonce_len,
93 const u8 *hdr, size_t hdr_len,
94 u8 *data, size_t data_len, const u8 *tag)
98 u8 nonce_mac[AES_BLOCK_SIZE], hdr_mac[AES_BLOCK_SIZE],
99 data_mac[AES_BLOCK_SIZE];
102 if (nonce_len > data_len)
106 if (hdr_len > buf_len)
110 buf = os_malloc(buf_len);
114 os_memset(buf, 0, 15);
117 os_memcpy(buf + 16, nonce, nonce_len);
118 if (omac1_aes_128(key, buf, 16 + nonce_len, nonce_mac)) {
124 os_memcpy(buf + 16, hdr, hdr_len);
125 if (omac1_aes_128(key, buf, 16 + hdr_len, hdr_mac)) {
131 os_memcpy(buf + 16, data, data_len);
132 if (omac1_aes_128(key, buf, 16 + data_len, data_mac)) {
139 for (i = 0; i < AES_BLOCK_SIZE; i++) {
140 if (tag[i] != (nonce_mac[i] ^ data_mac[i] ^ hdr_mac[i]))
144 return aes_128_ctr_encrypt(key, nonce_mac, data, data_len);