1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2765cb46aSJouni Malinen /* 3765cb46aSJouni Malinen * AES-128-CMAC with TLen 16 for IEEE 802.11w BIP 4765cb46aSJouni Malinen * Copyright 2008, Jouni Malinen <j@w1.fi> 5765cb46aSJouni Malinen */ 6765cb46aSJouni Malinen 7765cb46aSJouni Malinen #include <linux/kernel.h> 8765cb46aSJouni Malinen #include <linux/types.h> 9765cb46aSJouni Malinen #include <linux/crypto.h> 104afebd63SEmmanuel Grumbach #include <linux/export.h> 11765cb46aSJouni Malinen #include <linux/err.h> 120cd20a27SJohannes Berg #include <crypto/aes.h> 13765cb46aSJouni Malinen 14765cb46aSJouni Malinen #include <net/mac80211.h> 15765cb46aSJouni Malinen #include "key.h" 16765cb46aSJouni Malinen #include "aes_cmac.h" 17765cb46aSJouni Malinen 18765cb46aSJouni Malinen #define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */ 1956c52da2SJouni Malinen #define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */ 20765cb46aSJouni Malinen #define AAD_LEN 20 21765cb46aSJouni Malinen 2226717828SArd Biesheuvel static const u8 zero[CMAC_TLEN_256]; 23765cb46aSJouni Malinen 2426717828SArd Biesheuvel void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad, 25765cb46aSJouni Malinen const u8 *data, size_t data_len, u8 *mic) 26765cb46aSJouni Malinen { 2726717828SArd Biesheuvel SHASH_DESC_ON_STACK(desc, tfm); 2826717828SArd Biesheuvel u8 out[AES_BLOCK_SIZE]; 29765cb46aSJouni Malinen 3026717828SArd Biesheuvel desc->tfm = tfm; 31765cb46aSJouni Malinen 3226717828SArd Biesheuvel crypto_shash_init(desc); 3326717828SArd Biesheuvel crypto_shash_update(desc, aad, AAD_LEN); 3426717828SArd Biesheuvel crypto_shash_update(desc, data, data_len - CMAC_TLEN); 3526717828SArd Biesheuvel crypto_shash_finup(desc, zero, CMAC_TLEN, out); 3626717828SArd Biesheuvel 3726717828SArd Biesheuvel memcpy(mic, out, CMAC_TLEN); 38765cb46aSJouni Malinen } 39765cb46aSJouni Malinen 4026717828SArd Biesheuvel void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad, 4156c52da2SJouni Malinen const u8 *data, size_t data_len, u8 *mic) 4256c52da2SJouni Malinen { 4326717828SArd Biesheuvel SHASH_DESC_ON_STACK(desc, tfm); 44765cb46aSJouni Malinen 4526717828SArd Biesheuvel desc->tfm = tfm; 4656c52da2SJouni Malinen 4726717828SArd Biesheuvel crypto_shash_init(desc); 4826717828SArd Biesheuvel crypto_shash_update(desc, aad, AAD_LEN); 4926717828SArd Biesheuvel crypto_shash_update(desc, data, data_len - CMAC_TLEN_256); 5026717828SArd Biesheuvel crypto_shash_finup(desc, zero, CMAC_TLEN_256, mic); 5156c52da2SJouni Malinen } 5256c52da2SJouni Malinen 5326717828SArd Biesheuvel struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[], 5456c52da2SJouni Malinen size_t key_len) 55765cb46aSJouni Malinen { 5626717828SArd Biesheuvel struct crypto_shash *tfm; 57765cb46aSJouni Malinen 5826717828SArd Biesheuvel tfm = crypto_alloc_shash("cmac(aes)", 0, 0); 591ac62ba7SBen Hutchings if (!IS_ERR(tfm)) 6026717828SArd Biesheuvel crypto_shash_setkey(tfm, key, key_len); 61765cb46aSJouni Malinen 62765cb46aSJouni Malinen return tfm; 63765cb46aSJouni Malinen } 64765cb46aSJouni Malinen 6526717828SArd Biesheuvel void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm) 66765cb46aSJouni Malinen { 6726717828SArd Biesheuvel crypto_free_shash(tfm); 68765cb46aSJouni Malinen } 69