2 * One-key CBC MAC (OMAC1) hash with AES-128
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.
16 static void gf_mulx(u8 *pad)
20 carry = pad[0] & 0x80;
21 for (i = 0; i < AES_BLOCK_SIZE - 1; i++)
22 pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7);
23 pad[AES_BLOCK_SIZE - 1] <<= 1;
25 pad[AES_BLOCK_SIZE - 1] ^= 0x87;
30 * omac1_aes_128_vector - One-Key CBC MAC (OMAC1) hash with AES-128
31 * @key: 128-bit key for the hash operation
32 * @num_elem: Number of elements in the data vector
33 * @addr: Pointers to the data areas
34 * @len: Lengths of the data blocks
35 * @mac: Buffer for MAC (128 bits, i.e., 16 bytes)
36 * Returns: 0 on success, -1 on failure
38 * This is a mode for using block cipher (AES in this case) for authentication.
39 * OMAC1 was standardized with the name CMAC by NIST in a Special Publication
42 int omac1_aes_128_vector(const u8 *key, size_t num_elem,
43 const u8 *addr[], const size_t *len, u8 *mac)
46 u8 cbc[AES_BLOCK_SIZE], pad[AES_BLOCK_SIZE];
48 size_t i, e, left, total_len;
50 ctx = aes_encrypt_init(key, 16);
53 os_memset(cbc, 0, AES_BLOCK_SIZE);
56 for (e = 0; e < num_elem; e++)
64 while (left >= AES_BLOCK_SIZE) {
65 for (i = 0; i < AES_BLOCK_SIZE; i++) {
73 if (left > AES_BLOCK_SIZE)
74 aes_encrypt(ctx, cbc, cbc);
75 left -= AES_BLOCK_SIZE;
78 os_memset(pad, 0, AES_BLOCK_SIZE);
79 aes_encrypt(ctx, pad, pad);
82 if (left || total_len == 0) {
83 for (i = 0; i < left; i++) {
95 for (i = 0; i < AES_BLOCK_SIZE; i++)
97 aes_encrypt(ctx, pad, mac);
98 aes_encrypt_deinit(ctx);
104 * omac1_aes_128 - One-Key CBC MAC (OMAC1) hash with AES-128 (aka AES-CMAC)
105 * @key: 128-bit key for the hash operation
106 * @data: Data buffer for which a MAC is determined
107 * @data_len: Length of data buffer in bytes
108 * @mac: Buffer for MAC (128 bits, i.e., 16 bytes)
109 * Returns: 0 on success, -1 on failure
111 * This is a mode for using block cipher (AES in this case) for authentication.
112 * OMAC1 was standardized with the name CMAC by NIST in a Special Publication
115 int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
117 return omac1_aes_128_vector(key, 1, &data, &data_len, mac);