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> |
| 13 | #include <linux/err.h> |
Johannes Berg | 0cd20a2 | 2011-07-06 22:02:14 +0200 | [diff] [blame] | 14 | #include <crypto/aes.h> |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 15 | |
| 16 | #include <net/mac80211.h> |
| 17 | #include "key.h" |
| 18 | #include "aes_cmac.h" |
| 19 | |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 20 | #define AES_CMAC_KEY_LEN 16 |
| 21 | #define CMAC_TLEN 8 /* CMAC TLen = 64 bits (8 octets) */ |
| 22 | #define AAD_LEN 20 |
| 23 | |
| 24 | |
| 25 | static void gf_mulx(u8 *pad) |
| 26 | { |
| 27 | int i, carry; |
| 28 | |
| 29 | carry = pad[0] & 0x80; |
| 30 | for (i = 0; i < AES_BLOCK_SIZE - 1; i++) |
| 31 | pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7); |
| 32 | pad[AES_BLOCK_SIZE - 1] <<= 1; |
| 33 | if (carry) |
| 34 | pad[AES_BLOCK_SIZE - 1] ^= 0x87; |
| 35 | } |
| 36 | |
| 37 | |
Johannes Berg | 75396ae | 2011-07-06 22:00:35 +0200 | [diff] [blame] | 38 | static void aes_128_cmac_vector(struct crypto_cipher *tfm, size_t num_elem, |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 39 | const u8 *addr[], const size_t *len, u8 *mac) |
| 40 | { |
Johannes Berg | d348f69 | 2012-08-19 14:51:44 +0200 | [diff] [blame] | 41 | u8 cbc[AES_BLOCK_SIZE], pad[AES_BLOCK_SIZE]; |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 42 | const u8 *pos, *end; |
| 43 | size_t i, e, left, total_len; |
| 44 | |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 45 | memset(cbc, 0, AES_BLOCK_SIZE); |
| 46 | |
| 47 | total_len = 0; |
| 48 | for (e = 0; e < num_elem; e++) |
| 49 | total_len += len[e]; |
| 50 | left = total_len; |
| 51 | |
| 52 | e = 0; |
| 53 | pos = addr[0]; |
| 54 | end = pos + len[0]; |
| 55 | |
| 56 | while (left >= AES_BLOCK_SIZE) { |
| 57 | for (i = 0; i < AES_BLOCK_SIZE; i++) { |
| 58 | cbc[i] ^= *pos++; |
| 59 | if (pos >= end) { |
| 60 | e++; |
| 61 | pos = addr[e]; |
| 62 | end = pos + len[e]; |
| 63 | } |
| 64 | } |
| 65 | if (left > AES_BLOCK_SIZE) |
| 66 | crypto_cipher_encrypt_one(tfm, cbc, cbc); |
| 67 | left -= AES_BLOCK_SIZE; |
| 68 | } |
| 69 | |
| 70 | memset(pad, 0, AES_BLOCK_SIZE); |
| 71 | crypto_cipher_encrypt_one(tfm, pad, pad); |
| 72 | gf_mulx(pad); |
| 73 | |
| 74 | if (left || total_len == 0) { |
| 75 | for (i = 0; i < left; i++) { |
| 76 | cbc[i] ^= *pos++; |
| 77 | if (pos >= end) { |
| 78 | e++; |
| 79 | pos = addr[e]; |
| 80 | end = pos + len[e]; |
| 81 | } |
| 82 | } |
| 83 | cbc[left] ^= 0x80; |
| 84 | gf_mulx(pad); |
| 85 | } |
| 86 | |
| 87 | for (i = 0; i < AES_BLOCK_SIZE; i++) |
| 88 | pad[i] ^= cbc[i]; |
| 89 | crypto_cipher_encrypt_one(tfm, pad, pad); |
| 90 | memcpy(mac, pad, CMAC_TLEN); |
| 91 | } |
| 92 | |
| 93 | |
Johannes Berg | 75396ae | 2011-07-06 22:00:35 +0200 | [diff] [blame] | 94 | void ieee80211_aes_cmac(struct crypto_cipher *tfm, const u8 *aad, |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 95 | const u8 *data, size_t data_len, u8 *mic) |
| 96 | { |
| 97 | const u8 *addr[3]; |
| 98 | size_t len[3]; |
| 99 | u8 zero[CMAC_TLEN]; |
| 100 | |
| 101 | memset(zero, 0, CMAC_TLEN); |
| 102 | addr[0] = aad; |
| 103 | len[0] = AAD_LEN; |
| 104 | addr[1] = data; |
| 105 | len[1] = data_len - CMAC_TLEN; |
| 106 | addr[2] = zero; |
| 107 | len[2] = CMAC_TLEN; |
| 108 | |
Johannes Berg | 75396ae | 2011-07-06 22:00:35 +0200 | [diff] [blame] | 109 | aes_128_cmac_vector(tfm, 3, addr, len, mic); |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
| 113 | struct crypto_cipher * ieee80211_aes_cmac_key_setup(const u8 key[]) |
| 114 | { |
| 115 | struct crypto_cipher *tfm; |
| 116 | |
| 117 | tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC); |
Ben Hutchings | 1ac62ba | 2010-08-01 17:37:03 +0100 | [diff] [blame] | 118 | if (!IS_ERR(tfm)) |
| 119 | crypto_cipher_setkey(tfm, key, AES_CMAC_KEY_LEN); |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 120 | |
| 121 | return tfm; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | void ieee80211_aes_cmac_key_free(struct crypto_cipher *tfm) |
| 126 | { |
Jesper Juhl | ffa56e54 | 2010-11-04 22:59:56 +0100 | [diff] [blame] | 127 | crypto_free_cipher(tfm); |
Jouni Malinen | 765cb46 | 2009-01-08 13:32:01 +0200 | [diff] [blame] | 128 | } |
Assaf Krauss | 5d0d04e | 2012-08-01 15:12:48 +0300 | [diff] [blame] | 129 | |
| 130 | void ieee80211_aes_cmac_calculate_k1_k2(struct ieee80211_key_conf *keyconf, |
| 131 | u8 *k1, u8 *k2) |
| 132 | { |
| 133 | u8 l[AES_BLOCK_SIZE] = {}; |
| 134 | struct ieee80211_key *key = |
| 135 | container_of(keyconf, struct ieee80211_key, conf); |
| 136 | |
| 137 | crypto_cipher_encrypt_one(key->u.aes_cmac.tfm, l, l); |
| 138 | |
| 139 | memcpy(k1, l, AES_BLOCK_SIZE); |
| 140 | gf_mulx(k1); |
| 141 | |
| 142 | memcpy(k2, k1, AES_BLOCK_SIZE); |
| 143 | gf_mulx(k2); |
| 144 | } |
| 145 | EXPORT_SYMBOL(ieee80211_aes_cmac_calculate_k1_k2); |