xref: /openbmc/linux/net/mac80211/aes_cmac.c (revision 26717828b75dd5c46e97f7f4a9b937d038bb2852)
1765cb46aSJouni Malinen /*
2765cb46aSJouni Malinen  * AES-128-CMAC with TLen 16 for IEEE 802.11w BIP
3765cb46aSJouni Malinen  * Copyright 2008, Jouni Malinen <j@w1.fi>
4765cb46aSJouni Malinen  *
5765cb46aSJouni Malinen  * This program is free software; you can redistribute it and/or modify
6765cb46aSJouni Malinen  * it under the terms of the GNU General Public License version 2 as
7765cb46aSJouni Malinen  * published by the Free Software Foundation.
8765cb46aSJouni Malinen  */
9765cb46aSJouni Malinen 
10765cb46aSJouni Malinen #include <linux/kernel.h>
11765cb46aSJouni Malinen #include <linux/types.h>
12765cb46aSJouni Malinen #include <linux/crypto.h>
134afebd63SEmmanuel Grumbach #include <linux/export.h>
14765cb46aSJouni Malinen #include <linux/err.h>
150cd20a27SJohannes Berg #include <crypto/aes.h>
16765cb46aSJouni Malinen 
17765cb46aSJouni Malinen #include <net/mac80211.h>
18765cb46aSJouni Malinen #include "key.h"
19765cb46aSJouni Malinen #include "aes_cmac.h"
20765cb46aSJouni Malinen 
21765cb46aSJouni Malinen #define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
2256c52da2SJouni Malinen #define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
23765cb46aSJouni Malinen #define AAD_LEN 20
24765cb46aSJouni Malinen 
25*26717828SArd Biesheuvel static const u8 zero[CMAC_TLEN_256];
26765cb46aSJouni Malinen 
27*26717828SArd Biesheuvel void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
28765cb46aSJouni Malinen 			const u8 *data, size_t data_len, u8 *mic)
29765cb46aSJouni Malinen {
30*26717828SArd Biesheuvel 	SHASH_DESC_ON_STACK(desc, tfm);
31*26717828SArd Biesheuvel 	u8 out[AES_BLOCK_SIZE];
32765cb46aSJouni Malinen 
33*26717828SArd Biesheuvel 	desc->tfm = tfm;
34765cb46aSJouni Malinen 
35*26717828SArd Biesheuvel 	crypto_shash_init(desc);
36*26717828SArd Biesheuvel 	crypto_shash_update(desc, aad, AAD_LEN);
37*26717828SArd Biesheuvel 	crypto_shash_update(desc, data, data_len - CMAC_TLEN);
38*26717828SArd Biesheuvel 	crypto_shash_finup(desc, zero, CMAC_TLEN, out);
39*26717828SArd Biesheuvel 
40*26717828SArd Biesheuvel 	memcpy(mic, out, CMAC_TLEN);
41765cb46aSJouni Malinen }
42765cb46aSJouni Malinen 
43*26717828SArd Biesheuvel void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
4456c52da2SJouni Malinen 			    const u8 *data, size_t data_len, u8 *mic)
4556c52da2SJouni Malinen {
46*26717828SArd Biesheuvel 	SHASH_DESC_ON_STACK(desc, tfm);
47765cb46aSJouni Malinen 
48*26717828SArd Biesheuvel 	desc->tfm = tfm;
4956c52da2SJouni Malinen 
50*26717828SArd Biesheuvel 	crypto_shash_init(desc);
51*26717828SArd Biesheuvel 	crypto_shash_update(desc, aad, AAD_LEN);
52*26717828SArd Biesheuvel 	crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
53*26717828SArd Biesheuvel 	crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
5456c52da2SJouni Malinen }
5556c52da2SJouni Malinen 
56*26717828SArd Biesheuvel struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
5756c52da2SJouni Malinen 						  size_t key_len)
58765cb46aSJouni Malinen {
59*26717828SArd Biesheuvel 	struct crypto_shash *tfm;
60765cb46aSJouni Malinen 
61*26717828SArd Biesheuvel 	tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
621ac62ba7SBen Hutchings 	if (!IS_ERR(tfm))
63*26717828SArd Biesheuvel 		crypto_shash_setkey(tfm, key, key_len);
64765cb46aSJouni Malinen 
65765cb46aSJouni Malinen 	return tfm;
66765cb46aSJouni Malinen }
67765cb46aSJouni Malinen 
68*26717828SArd Biesheuvel void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
69765cb46aSJouni Malinen {
70*26717828SArd Biesheuvel 	crypto_free_shash(tfm);
71765cb46aSJouni Malinen }
72