Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 2 | /* |
| 3 | * AES-128-CMAC with TLen 16 for IEEE 802.11w BIP |
| 4 | * Copyright 2008, Jouni Malinen <j@w1.fi> |
Johannes Berg | 8de8570 | 2021-04-09 12:40:28 +0300 | [diff] [blame] | 5 | * Copyright (C) 2020 Intel Corporation |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/crypto.h> |
Emmanuel Grumbach | 4afebd6 | 2012-11-07 11:13:58 +0200 | [diff] [blame] | 11 | #include <linux/export.h> |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 12 | #include <linux/err.h> |
Johannes Berg | 0cd20a2 | 2011-07-06 22:02:14 +0200 | [diff] [blame] | 13 | #include <crypto/aes.h> |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 14 | |
| 15 | #include <net/mac80211.h> |
| 16 | #include "key.h" |
| 17 | #include "aes_cmac.h" |
| 18 | |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 19 | #define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */ |
Jouni Malinen | 56c52da | 2015-01-24 19:52:08 +0200 | [diff] [blame] | 20 | #define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */ |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 21 | #define AAD_LEN 20 |
| 22 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 23 | static const u8 zero[CMAC_TLEN_256]; |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 24 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 25 | void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad, |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 26 | const u8 *data, size_t data_len, u8 *mic) |
| 27 | { |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 28 | SHASH_DESC_ON_STACK(desc, tfm); |
| 29 | u8 out[AES_BLOCK_SIZE]; |
Jouni Malinen | 2d5d4b0 | 2020-02-22 15:25:45 +0200 | [diff] [blame] | 30 | const __le16 *fc; |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 31 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 32 | desc->tfm = tfm; |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 33 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 34 | crypto_shash_init(desc); |
| 35 | crypto_shash_update(desc, aad, AAD_LEN); |
Jouni Malinen | 2d5d4b0 | 2020-02-22 15:25:45 +0200 | [diff] [blame] | 36 | fc = (const __le16 *)aad; |
| 37 | if (ieee80211_is_beacon(*fc)) { |
| 38 | /* mask Timestamp field to zero */ |
| 39 | crypto_shash_update(desc, zero, 8); |
| 40 | crypto_shash_update(desc, data + 8, data_len - 8 - CMAC_TLEN); |
| 41 | } else { |
| 42 | crypto_shash_update(desc, data, data_len - CMAC_TLEN); |
| 43 | } |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 44 | crypto_shash_finup(desc, zero, CMAC_TLEN, out); |
| 45 | |
| 46 | memcpy(mic, out, CMAC_TLEN); |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 49 | void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad, |
Jouni Malinen | 56c52da | 2015-01-24 19:52:08 +0200 | [diff] [blame] | 50 | const u8 *data, size_t data_len, u8 *mic) |
| 51 | { |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 52 | SHASH_DESC_ON_STACK(desc, tfm); |
Jouni Malinen | 2d5d4b0 | 2020-02-22 15:25:45 +0200 | [diff] [blame] | 53 | const __le16 *fc; |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 54 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 55 | desc->tfm = tfm; |
Jouni Malinen | 56c52da | 2015-01-24 19:52:08 +0200 | [diff] [blame] | 56 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 57 | crypto_shash_init(desc); |
| 58 | crypto_shash_update(desc, aad, AAD_LEN); |
Jouni Malinen | 2d5d4b0 | 2020-02-22 15:25:45 +0200 | [diff] [blame] | 59 | fc = (const __le16 *)aad; |
| 60 | if (ieee80211_is_beacon(*fc)) { |
| 61 | /* mask Timestamp field to zero */ |
| 62 | crypto_shash_update(desc, zero, 8); |
| 63 | crypto_shash_update(desc, data + 8, |
| 64 | data_len - 8 - CMAC_TLEN_256); |
| 65 | } else { |
| 66 | crypto_shash_update(desc, data, data_len - CMAC_TLEN_256); |
| 67 | } |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 68 | crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic); |
Jouni Malinen | 56c52da | 2015-01-24 19:52:08 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 71 | struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[], |
| 72 | size_t key_len) |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 73 | { |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 74 | struct crypto_shash *tfm; |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 75 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 76 | tfm = crypto_alloc_shash("cmac(aes)", 0, 0); |
Johannes Berg | 8de8570 | 2021-04-09 12:40:28 +0300 | [diff] [blame] | 77 | if (!IS_ERR(tfm)) { |
| 78 | int err = crypto_shash_setkey(tfm, key, key_len); |
| 79 | |
| 80 | if (err) { |
| 81 | crypto_free_shash(tfm); |
| 82 | return ERR_PTR(err); |
| 83 | } |
| 84 | } |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 85 | |
| 86 | return tfm; |
| 87 | } |
| 88 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 89 | void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm) |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 90 | { |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 91 | crypto_free_shash(tfm); |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 92 | } |