xref: /openbmc/linux/net/mac80211/aes_gmac.c (revision 818b26588994d9d95743fca0a427f08ec6c1c41d)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28ade538bSJouni Malinen /*
38ade538bSJouni Malinen  * AES-GMAC for IEEE 802.11 BIP-GMAC-128 and BIP-GMAC-256
48ade538bSJouni Malinen  * Copyright 2015, Qualcomm Atheros, Inc.
58ade538bSJouni Malinen  */
68ade538bSJouni Malinen 
78ade538bSJouni Malinen #include <linux/kernel.h>
88ade538bSJouni Malinen #include <linux/types.h>
98ade538bSJouni Malinen #include <linux/err.h>
10d8fe0dddSHerbert Xu #include <crypto/aead.h>
118ade538bSJouni Malinen #include <crypto/aes.h>
128ade538bSJouni Malinen 
138ade538bSJouni Malinen #include <net/mac80211.h>
148ade538bSJouni Malinen #include "key.h"
158ade538bSJouni Malinen #include "aes_gmac.h"
168ade538bSJouni Malinen 
ieee80211_aes_gmac(struct crypto_aead * tfm,const u8 * aad,u8 * nonce,const u8 * data,size_t data_len,u8 * mic)178ade538bSJouni Malinen int ieee80211_aes_gmac(struct crypto_aead *tfm, const u8 *aad, u8 *nonce,
188ade538bSJouni Malinen 		       const u8 *data, size_t data_len, u8 *mic)
198ade538bSJouni Malinen {
202d5d4b0aSJouni Malinen 	struct scatterlist sg[5];
21f4a067f9SArd Biesheuvel 	u8 *zero, *__aad, iv[AES_BLOCK_SIZE];
22f4a067f9SArd Biesheuvel 	struct aead_request *aead_req;
23f4a067f9SArd Biesheuvel 	int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
242d5d4b0aSJouni Malinen 	const __le16 *fc;
25*58d25626SDaniel Phan 	int ret;
268ade538bSJouni Malinen 
278ade538bSJouni Malinen 	if (data_len < GMAC_MIC_LEN)
288ade538bSJouni Malinen 		return -EINVAL;
298ade538bSJouni Malinen 
30f4a067f9SArd Biesheuvel 	aead_req = kzalloc(reqsize + GMAC_MIC_LEN + GMAC_AAD_LEN, GFP_ATOMIC);
31f4a067f9SArd Biesheuvel 	if (!aead_req)
32f4a067f9SArd Biesheuvel 		return -ENOMEM;
338ade538bSJouni Malinen 
34f4a067f9SArd Biesheuvel 	zero = (u8 *)aead_req + reqsize;
35f4a067f9SArd Biesheuvel 	__aad = zero + GMAC_MIC_LEN;
36f4a067f9SArd Biesheuvel 	memcpy(__aad, aad, GMAC_AAD_LEN);
37f4a067f9SArd Biesheuvel 
382d5d4b0aSJouni Malinen 	fc = (const __le16 *)aad;
392d5d4b0aSJouni Malinen 	if (ieee80211_is_beacon(*fc)) {
402d5d4b0aSJouni Malinen 		/* mask Timestamp field to zero */
412d5d4b0aSJouni Malinen 		sg_init_table(sg, 5);
422d5d4b0aSJouni Malinen 		sg_set_buf(&sg[0], __aad, GMAC_AAD_LEN);
432d5d4b0aSJouni Malinen 		sg_set_buf(&sg[1], zero, 8);
442d5d4b0aSJouni Malinen 		sg_set_buf(&sg[2], data + 8, data_len - 8 - GMAC_MIC_LEN);
452d5d4b0aSJouni Malinen 		sg_set_buf(&sg[3], zero, GMAC_MIC_LEN);
462d5d4b0aSJouni Malinen 		sg_set_buf(&sg[4], mic, GMAC_MIC_LEN);
472d5d4b0aSJouni Malinen 	} else {
48957e0fe6SHerbert Xu 		sg_init_table(sg, 4);
49f4a067f9SArd Biesheuvel 		sg_set_buf(&sg[0], __aad, GMAC_AAD_LEN);
508ade538bSJouni Malinen 		sg_set_buf(&sg[1], data, data_len - GMAC_MIC_LEN);
518ade538bSJouni Malinen 		sg_set_buf(&sg[2], zero, GMAC_MIC_LEN);
52957e0fe6SHerbert Xu 		sg_set_buf(&sg[3], mic, GMAC_MIC_LEN);
532d5d4b0aSJouni Malinen 	}
548ade538bSJouni Malinen 
558ade538bSJouni Malinen 	memcpy(iv, nonce, GMAC_NONCE_LEN);
568ade538bSJouni Malinen 	memset(iv + GMAC_NONCE_LEN, 0, sizeof(iv) - GMAC_NONCE_LEN);
578ade538bSJouni Malinen 	iv[AES_BLOCK_SIZE - 1] = 0x01;
588ade538bSJouni Malinen 
598ade538bSJouni Malinen 	aead_request_set_tfm(aead_req, tfm);
60957e0fe6SHerbert Xu 	aead_request_set_crypt(aead_req, sg, sg, 0, iv);
61f4a067f9SArd Biesheuvel 	aead_request_set_ad(aead_req, GMAC_AAD_LEN + data_len);
628ade538bSJouni Malinen 
63*58d25626SDaniel Phan 	ret = crypto_aead_encrypt(aead_req);
64453431a5SWaiman Long 	kfree_sensitive(aead_req);
658ade538bSJouni Malinen 
66*58d25626SDaniel Phan 	return ret;
678ade538bSJouni Malinen }
688ade538bSJouni Malinen 
ieee80211_aes_gmac_key_setup(const u8 key[],size_t key_len)698ade538bSJouni Malinen struct crypto_aead *ieee80211_aes_gmac_key_setup(const u8 key[],
708ade538bSJouni Malinen 						 size_t key_len)
718ade538bSJouni Malinen {
728ade538bSJouni Malinen 	struct crypto_aead *tfm;
738ade538bSJouni Malinen 	int err;
748ade538bSJouni Malinen 
758ade538bSJouni Malinen 	tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
768ade538bSJouni Malinen 	if (IS_ERR(tfm))
778ade538bSJouni Malinen 		return tfm;
788ade538bSJouni Malinen 
798ade538bSJouni Malinen 	err = crypto_aead_setkey(tfm, key, key_len);
808ade538bSJouni Malinen 	if (!err)
818ade538bSJouni Malinen 		err = crypto_aead_setauthsize(tfm, GMAC_MIC_LEN);
8282ca6ef6SJouni Malinen 	if (!err)
8382ca6ef6SJouni Malinen 		return tfm;
848ade538bSJouni Malinen 
858ade538bSJouni Malinen 	crypto_free_aead(tfm);
868ade538bSJouni Malinen 	return ERR_PTR(err);
878ade538bSJouni Malinen }
888ade538bSJouni Malinen 
ieee80211_aes_gmac_key_free(struct crypto_aead * tfm)898ade538bSJouni Malinen void ieee80211_aes_gmac_key_free(struct crypto_aead *tfm)
908ade538bSJouni Malinen {
918ade538bSJouni Malinen 	crypto_free_aead(tfm);
928ade538bSJouni Malinen }
93