blob: 2fb65588490c3ddbd7fe69b56ce0e7fb0a8f0312 [file] [log] [blame]
Jouni Malinen765cb462009-01-08 13:32:01 +02001/*
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 Grumbach4afebd62012-11-07 11:13:58 +020013#include <linux/export.h>
Jouni Malinen765cb462009-01-08 13:32:01 +020014#include <linux/err.h>
Johannes Berg0cd20a22011-07-06 22:02:14 +020015#include <crypto/aes.h>
Jouni Malinen765cb462009-01-08 13:32:01 +020016
17#include <net/mac80211.h>
18#include "key.h"
19#include "aes_cmac.h"
20
Jouni Malinen765cb462009-01-08 13:32:01 +020021#define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */
Jouni Malinen56c52da2015-01-24 19:52:08 +020022#define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */
Jouni Malinen765cb462009-01-08 13:32:01 +020023#define AAD_LEN 20
24
Ard Biesheuvel26717822017-02-06 10:49:28 +000025static const u8 zero[CMAC_TLEN_256];
Jouni Malinen765cb462009-01-08 13:32:01 +020026
Ard Biesheuvel26717822017-02-06 10:49:28 +000027void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad,
Jouni Malinen765cb462009-01-08 13:32:01 +020028 const u8 *data, size_t data_len, u8 *mic)
29{
Ard Biesheuvel26717822017-02-06 10:49:28 +000030 SHASH_DESC_ON_STACK(desc, tfm);
31 u8 out[AES_BLOCK_SIZE];
Jouni Malinen765cb462009-01-08 13:32:01 +020032
Ard Biesheuvel26717822017-02-06 10:49:28 +000033 desc->tfm = tfm;
Jouni Malinen765cb462009-01-08 13:32:01 +020034
Ard Biesheuvel26717822017-02-06 10:49:28 +000035 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 Malinen765cb462009-01-08 13:32:01 +020041}
42
Ard Biesheuvel26717822017-02-06 10:49:28 +000043void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad,
Jouni Malinen56c52da2015-01-24 19:52:08 +020044 const u8 *data, size_t data_len, u8 *mic)
45{
Ard Biesheuvel26717822017-02-06 10:49:28 +000046 SHASH_DESC_ON_STACK(desc, tfm);
Jouni Malinen765cb462009-01-08 13:32:01 +020047
Ard Biesheuvel26717822017-02-06 10:49:28 +000048 desc->tfm = tfm;
Jouni Malinen56c52da2015-01-24 19:52:08 +020049
Ard Biesheuvel26717822017-02-06 10:49:28 +000050 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 Malinen56c52da2015-01-24 19:52:08 +020054}
55
Ard Biesheuvel26717822017-02-06 10:49:28 +000056struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[],
57 size_t key_len)
Jouni Malinen765cb462009-01-08 13:32:01 +020058{
Ard Biesheuvel26717822017-02-06 10:49:28 +000059 struct crypto_shash *tfm;
Jouni Malinen765cb462009-01-08 13:32:01 +020060
Ard Biesheuvel26717822017-02-06 10:49:28 +000061 tfm = crypto_alloc_shash("cmac(aes)", 0, 0);
Ben Hutchings1ac62ba2010-08-01 17:37:03 +010062 if (!IS_ERR(tfm))
Ard Biesheuvel26717822017-02-06 10:49:28 +000063 crypto_shash_setkey(tfm, key, key_len);
Jouni Malinen765cb462009-01-08 13:32:01 +020064
65 return tfm;
66}
67
Ard Biesheuvel26717822017-02-06 10:49:28 +000068void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm)
Jouni Malinen765cb462009-01-08 13:32:01 +020069{
Ard Biesheuvel26717822017-02-06 10:49:28 +000070 crypto_free_shash(tfm);
Jouni Malinen765cb462009-01-08 13:32:01 +020071}