Commit | Line | Data |
---|---|---|
765cb46a JM |
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> | |
4afebd63 | 13 | #include <linux/export.h> |
765cb46a | 14 | #include <linux/err.h> |
0cd20a27 | 15 | #include <crypto/aes.h> |
765cb46a JM |
16 | |
17 | #include <net/mac80211.h> | |
18 | #include "key.h" | |
19 | #include "aes_cmac.h" | |
20 | ||
765cb46a | 21 | #define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */ |
56c52da2 | 22 | #define CMAC_TLEN_256 16 /* CMAC TLen = 128 bits (16 octets) */ |
765cb46a JM |
23 | #define AAD_LEN 20 |
24 | ||
26717828 | 25 | static const u8 zero[CMAC_TLEN_256]; |
765cb46a | 26 | |
26717828 | 27 | void ieee80211_aes_cmac(struct crypto_shash *tfm, const u8 *aad, |
765cb46a JM |
28 | const u8 *data, size_t data_len, u8 *mic) |
29 | { | |
26717828 AB |
30 | SHASH_DESC_ON_STACK(desc, tfm); |
31 | u8 out[AES_BLOCK_SIZE]; | |
765cb46a | 32 | |
26717828 | 33 | desc->tfm = tfm; |
765cb46a | 34 | |
26717828 AB |
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); | |
765cb46a JM |
41 | } |
42 | ||
26717828 | 43 | void ieee80211_aes_cmac_256(struct crypto_shash *tfm, const u8 *aad, |
56c52da2 JM |
44 | const u8 *data, size_t data_len, u8 *mic) |
45 | { | |
26717828 | 46 | SHASH_DESC_ON_STACK(desc, tfm); |
56c52da2 | 47 | |
26717828 | 48 | desc->tfm = tfm; |
56c52da2 | 49 | |
26717828 AB |
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); | |
56c52da2 | 54 | } |
765cb46a | 55 | |
26717828 AB |
56 | struct crypto_shash *ieee80211_aes_cmac_key_setup(const u8 key[], |
57 | size_t key_len) | |
765cb46a | 58 | { |
26717828 | 59 | struct crypto_shash *tfm; |
765cb46a | 60 | |
26717828 | 61 | tfm = crypto_alloc_shash("cmac(aes)", 0, 0); |
1ac62ba7 | 62 | if (!IS_ERR(tfm)) |
26717828 | 63 | crypto_shash_setkey(tfm, key, key_len); |
765cb46a JM |
64 | |
65 | return tfm; | |
66 | } | |
67 | ||
26717828 | 68 | void ieee80211_aes_cmac_key_free(struct crypto_shash *tfm) |
765cb46a | 69 | { |
26717828 | 70 | crypto_free_shash(tfm); |
765cb46a | 71 | } |