1 /*
2  * AES-128-CMAC with TLen 16 for IEEE 802.11w BIP
3  * Copyright 2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/crypto.h>
13 #include <linux/export.h>
14 #include <linux/err.h>
15 #include <crypto/aes.h>
16 
17 #include <net/mac80211.h>
18 #include "key.h"
19 #include "aes_cmac.h"
20 
21 #define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
22 #define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
23 #define AAD_LEN 20
24 
25 static const u8 zero[CMAC_TLEN_256];
26 
ieee80211_aes_cmac(struct crypto_shash * tfm,const u8 * aad,const u8 * data,size_t data_len,u8 * mic)27 void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
28 			const u8 *data, size_t data_len, u8 *mic)
29 {
30 	SHASH_DESC_ON_STACK(desc, tfm);
31 	u8 out[AES_BLOCK_SIZE];
32 
33 	desc->tfm = tfm;
34 
35 	crypto_shash_init(desc);
36 	crypto_shash_update(desc, aad, AAD_LEN);
37 	crypto_shash_update(desc, data, data_len - CMAC_TLEN);
38 	crypto_shash_finup(desc, zero, CMAC_TLEN, out);
39 
40 	memcpy(mic, out, CMAC_TLEN);
41 }
42 
ieee80211_aes_cmac_256(struct crypto_shash * tfm,const u8 * aad,const u8 * data,size_t data_len,u8 * mic)43 void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
44 			    const u8 *data, size_t data_len, u8 *mic)
45 {
46 	SHASH_DESC_ON_STACK(desc, tfm);
47 
48 	desc->tfm = tfm;
49 
50 	crypto_shash_init(desc);
51 	crypto_shash_update(desc, aad, AAD_LEN);
52 	crypto_shash_update(desc, data, data_len - CMAC_TLEN_256);
53 	crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic);
54 }
55 
ieee80211_aes_cmac_key_setup(const u8 key[],size_t key_len)56 struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
57 						  size_t key_len)
58 {
59 	struct crypto_shash *tfm;
60 
61 	tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
62 	if (!IS_ERR(tfm))
63 		crypto_shash_setkey(tfm, key, key_len);
64 
65 	return tfm;
66 }
67 
ieee80211_aes_cmac_key_free(struct crypto_shash * tfm)68 void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
69 {
70 	crypto_free_shash(tfm);
71 }
72