xref: /openbmc/linux/net/mac80211/aes_cmac.c (revision 26717828)
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 
2526717828SArd Biesheuvel static const u8 zero[CMAC_TLEN_256];
26765cb46aSJouni Malinen 
2726717828SArd 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 {
3026717828SArd Biesheuvel 	SHASH_DESC_ON_STACK(desc, tfm);
3126717828SArd Biesheuvel 	u8 out[AES_BLOCK_SIZE];
32765cb46aSJouni Malinen 
3326717828SArd Biesheuvel 	desc->tfm = tfm;
34765cb46aSJouni Malinen 
3526717828SArd Biesheuvel 	crypto_shash_init(desc);
3626717828SArd Biesheuvel 	crypto_shash_update(desc, aad, AAD_LEN);
3726717828SArd Biesheuvel 	crypto_shash_update(desc, data, data_len - CMAC_TLEN);
3826717828SArd Biesheuvel 	crypto_shash_finup(desc, zero, CMAC_TLEN, out);
3926717828SArd Biesheuvel 
4026717828SArd Biesheuvel 	memcpy(mic, out, CMAC_TLEN);
41765cb46aSJouni Malinen }
42765cb46aSJouni Malinen 
4326717828SArd 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 {
4626717828SArd Biesheuvel 	SHASH_DESC_ON_STACK(desc, tfm);
47765cb46aSJouni Malinen 
4826717828SArd Biesheuvel 	desc->tfm = tfm;
4956c52da2SJouni Malinen 
5026717828SArd Biesheuvel 	crypto_shash_init(desc);
5126717828SArd Biesheuvel 	crypto_shash_update(desc, aad, AAD_LEN);
5226717828SArd Biesheuvel 	crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
5326717828SArd Biesheuvel 	crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
5456c52da2SJouni Malinen }
5556c52da2SJouni Malinen 
5626717828SArd Biesheuvel struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
5756c52da2SJouni Malinen 						  size_t key_len)
58765cb46aSJouni Malinen {
5926717828SArd Biesheuvel 	struct crypto_shash *tfm;
60765cb46aSJouni Malinen 
6126717828SArd Biesheuvel 	tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
621ac62ba7SBen Hutchings 	if (!IS_ERR(tfm))
6326717828SArd Biesheuvel 		crypto_shash_setkey(tfm, key, key_len);
64765cb46aSJouni Malinen 
65765cb46aSJouni Malinen 	return tfm;
66765cb46aSJouni Malinen }
67765cb46aSJouni Malinen 
6826717828SArd Biesheuvel void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
69765cb46aSJouni Malinen {
7026717828SArd Biesheuvel 	crypto_free_shash(tfm);
71765cb46aSJouni Malinen }
72