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