Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 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> |
Emmanuel Grumbach | 4afebd6 | 2012-11-07 11:13:58 +0200 | [diff] [blame] | 13 | #include <linux/export.h> |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 14 | #include <linux/err.h> |
Johannes Berg | 0cd20a2 | 2011-07-06 22:02:14 +0200 | [diff] [blame] | 15 | #include <crypto/aes.h> |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 16 | |
| 17 | #include <net/mac80211.h> |
| 18 | #include "key.h" |
| 19 | #include "aes_cmac.h" |
| 20 | |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 21 | #define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */ |
Jouni Malinen | 56c52da | 2015-01-24 19:52:08 +0200 | [diff] [blame] | 22 | #define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */ |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 23 | #define AAD_LEN 20 |
| 24 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 25 | static const u8 zero[CMAC_TLEN_256]; |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 26 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 27 | void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad, |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 28 | const u8 *data, size_t data_len, u8 *mic) |
| 29 | { |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 30 | SHASH_DESC_ON_STACK(desc, tfm); |
| 31 | u8 out[AES_BLOCK_SIZE]; |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 32 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 33 | desc->tfm = tfm; |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 34 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 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); |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 41 | } |
| 42 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 43 | void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad, |
Jouni Malinen | 56c52da | 2015-01-24 19:52:08 +0200 | [diff] [blame] | 44 | const u8 *data, size_t data_len, u8 *mic) |
| 45 | { |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 46 | SHASH_DESC_ON_STACK(desc, tfm); |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 47 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 48 | desc->tfm = tfm; |
Jouni Malinen | 56c52da | 2015-01-24 19:52:08 +0200 | [diff] [blame] | 49 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 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); |
Jouni Malinen | 56c52da | 2015-01-24 19:52:08 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 56 | struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[], |
| 57 | size_t key_len) |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 58 | { |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 59 | struct crypto_shash *tfm; |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 60 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 61 | tfm = crypto_alloc_shash("cmac(aes)", 0, 0); |
Ben Hutchings | 1ac62ba | 2010-08-01 17:37:03 +0100 | [diff] [blame] | 62 | if (!IS_ERR(tfm)) |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 63 | crypto_shash_setkey(tfm, key, key_len); |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 64 | |
| 65 | return tfm; |
| 66 | } |
| 67 | |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 68 | void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm) |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 69 | { |
Ard Biesheuvel | 2671782 | 2017-02-06 10:49:28 +0000 | [diff] [blame] | 70 | crypto_free_shash(tfm); |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 71 | } |