Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 2 | /* |
| 3 | * AES-GMAC for IEEE 802.11 BIP-GMAC-128 and BIP-GMAC-256 |
| 4 | * Copyright 2015, Qualcomm Atheros, Inc. |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/types.h> |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 9 | #include <linux/err.h> |
Herbert Xu | d8fe0dd | 2015-04-22 15:06:32 +0800 | [diff] [blame] | 10 | #include <crypto/aead.h> |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 11 | #include <crypto/aes.h> |
| 12 | |
| 13 | #include <net/mac80211.h> |
| 14 | #include "key.h" |
| 15 | #include "aes_gmac.h" |
| 16 | |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 17 | int ieee80211_aes_gmac(struct crypto_aead *tfm, const u8 *aad, u8 *nonce, |
| 18 | const u8 *data, size_t data_len, u8 *mic) |
| 19 | { |
Jouni Malinen | 2d5d4b0 | 2020-02-22 15:25:45 +0200 | [diff] [blame] | 20 | struct scatterlist sg[5]; |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 21 | u8 *zero, *__aad, iv[AES_BLOCK_SIZE]; |
| 22 | struct aead_request *aead_req; |
| 23 | int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm); |
Jouni Malinen | 2d5d4b0 | 2020-02-22 15:25:45 +0200 | [diff] [blame] | 24 | const __le16 *fc; |
Daniel Phan | 58d2562 | 2021-03-09 12:41:36 -0800 | [diff] [blame] | 25 | int ret; |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 26 | |
| 27 | if (data_len < GMAC_MIC_LEN) |
| 28 | return -EINVAL; |
| 29 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 30 | aead_req = kzalloc(reqsize + GMAC_MIC_LEN + GMAC_AAD_LEN, GFP_ATOMIC); |
| 31 | if (!aead_req) |
| 32 | return -ENOMEM; |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 33 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 34 | zero = (u8 *)aead_req + reqsize; |
| 35 | __aad = zero + GMAC_MIC_LEN; |
| 36 | memcpy(__aad, aad, GMAC_AAD_LEN); |
| 37 | |
Jouni Malinen | 2d5d4b0 | 2020-02-22 15:25:45 +0200 | [diff] [blame] | 38 | fc = (const __le16 *)aad; |
| 39 | if (ieee80211_is_beacon(*fc)) { |
| 40 | /* mask Timestamp field to zero */ |
| 41 | sg_init_table(sg, 5); |
| 42 | sg_set_buf(&sg[0], __aad, GMAC_AAD_LEN); |
| 43 | sg_set_buf(&sg[1], zero, 8); |
| 44 | sg_set_buf(&sg[2], data + 8, data_len - 8 - GMAC_MIC_LEN); |
| 45 | sg_set_buf(&sg[3], zero, GMAC_MIC_LEN); |
| 46 | sg_set_buf(&sg[4], mic, GMAC_MIC_LEN); |
| 47 | } else { |
| 48 | sg_init_table(sg, 4); |
| 49 | sg_set_buf(&sg[0], __aad, GMAC_AAD_LEN); |
| 50 | sg_set_buf(&sg[1], data, data_len - GMAC_MIC_LEN); |
| 51 | sg_set_buf(&sg[2], zero, GMAC_MIC_LEN); |
| 52 | sg_set_buf(&sg[3], mic, GMAC_MIC_LEN); |
| 53 | } |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 54 | |
| 55 | memcpy(iv, nonce, GMAC_NONCE_LEN); |
| 56 | memset(iv + GMAC_NONCE_LEN, 0, sizeof(iv) - GMAC_NONCE_LEN); |
| 57 | iv[AES_BLOCK_SIZE - 1] = 0x01; |
| 58 | |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 59 | aead_request_set_tfm(aead_req, tfm); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 60 | aead_request_set_crypt(aead_req, sg, sg, 0, iv); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 61 | aead_request_set_ad(aead_req, GMAC_AAD_LEN + data_len); |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 62 | |
Daniel Phan | 58d2562 | 2021-03-09 12:41:36 -0800 | [diff] [blame] | 63 | ret = crypto_aead_encrypt(aead_req); |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 64 | kfree_sensitive(aead_req); |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 65 | |
Daniel Phan | 58d2562 | 2021-03-09 12:41:36 -0800 | [diff] [blame] | 66 | return ret; |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | struct crypto_aead *ieee80211_aes_gmac_key_setup(const u8 key[], |
| 70 | size_t key_len) |
| 71 | { |
| 72 | struct crypto_aead *tfm; |
| 73 | int err; |
| 74 | |
| 75 | tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC); |
| 76 | if (IS_ERR(tfm)) |
| 77 | return tfm; |
| 78 | |
| 79 | err = crypto_aead_setkey(tfm, key, key_len); |
| 80 | if (!err) |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 81 | err = crypto_aead_setauthsize(tfm, GMAC_MIC_LEN); |
Jouni Malinen | 82ca6ef | 2015-03-23 15:41:15 +0200 | [diff] [blame] | 82 | if (!err) |
| 83 | return tfm; |
Jouni Malinen | 8ade538 | 2015-01-24 19:52:09 +0200 | [diff] [blame] | 84 | |
| 85 | crypto_free_aead(tfm); |
| 86 | return ERR_PTR(err); |
| 87 | } |
| 88 | |
| 89 | void ieee80211_aes_gmac_key_free(struct crypto_aead *tfm) |
| 90 | { |
| 91 | crypto_free_aead(tfm); |
| 92 | } |