Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2003-2004, Instant802 Networks, Inc. |
| 3 | * Copyright 2005-2006, Devicescape Software, Inc. |
| 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 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/crypto.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <asm/scatterlist.h> |
| 15 | |
| 16 | #include <net/mac80211.h> |
| 17 | #include "ieee80211_key.h" |
| 18 | #include "aes_ccm.h" |
| 19 | |
| 20 | |
| 21 | static void ieee80211_aes_encrypt(struct crypto_cipher *tfm, |
| 22 | const u8 pt[16], u8 ct[16]) |
| 23 | { |
| 24 | crypto_cipher_encrypt_one(tfm, ct, pt); |
| 25 | } |
| 26 | |
| 27 | |
| 28 | static inline void aes_ccm_prepare(struct crypto_cipher *tfm, u8 *b_0, u8 *aad, |
| 29 | u8 *b, u8 *s_0, u8 *a) |
| 30 | { |
| 31 | int i; |
| 32 | |
| 33 | ieee80211_aes_encrypt(tfm, b_0, b); |
| 34 | |
| 35 | /* Extra Authenticate-only data (always two AES blocks) */ |
| 36 | for (i = 0; i < AES_BLOCK_LEN; i++) |
| 37 | aad[i] ^= b[i]; |
| 38 | ieee80211_aes_encrypt(tfm, aad, b); |
| 39 | |
| 40 | aad += AES_BLOCK_LEN; |
| 41 | |
| 42 | for (i = 0; i < AES_BLOCK_LEN; i++) |
| 43 | aad[i] ^= b[i]; |
| 44 | ieee80211_aes_encrypt(tfm, aad, a); |
| 45 | |
| 46 | /* Mask out bits from auth-only-b_0 */ |
| 47 | b_0[0] &= 0x07; |
| 48 | |
| 49 | /* S_0 is used to encrypt T (= MIC) */ |
| 50 | b_0[14] = 0; |
| 51 | b_0[15] = 0; |
| 52 | ieee80211_aes_encrypt(tfm, b_0, s_0); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *scratch, |
| 57 | u8 *b_0, u8 *aad, u8 *data, size_t data_len, |
| 58 | u8 *cdata, u8 *mic) |
| 59 | { |
| 60 | int i, j, last_len, num_blocks; |
| 61 | u8 *pos, *cpos, *b, *s_0, *e; |
| 62 | |
| 63 | b = scratch; |
| 64 | s_0 = scratch + AES_BLOCK_LEN; |
| 65 | e = scratch + 2 * AES_BLOCK_LEN; |
| 66 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 67 | num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN); |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 68 | last_len = data_len % AES_BLOCK_LEN; |
| 69 | aes_ccm_prepare(tfm, b_0, aad, b, s_0, b); |
| 70 | |
| 71 | /* Process payload blocks */ |
| 72 | pos = data; |
| 73 | cpos = cdata; |
| 74 | for (j = 1; j <= num_blocks; j++) { |
| 75 | int blen = (j == num_blocks && last_len) ? |
| 76 | last_len : AES_BLOCK_LEN; |
| 77 | |
| 78 | /* Authentication followed by encryption */ |
| 79 | for (i = 0; i < blen; i++) |
| 80 | b[i] ^= pos[i]; |
| 81 | ieee80211_aes_encrypt(tfm, b, b); |
| 82 | |
| 83 | b_0[14] = (j >> 8) & 0xff; |
| 84 | b_0[15] = j & 0xff; |
| 85 | ieee80211_aes_encrypt(tfm, b_0, e); |
| 86 | for (i = 0; i < blen; i++) |
| 87 | *cpos++ = *pos++ ^ e[i]; |
| 88 | } |
| 89 | |
| 90 | for (i = 0; i < CCMP_MIC_LEN; i++) |
| 91 | mic[i] = b[i] ^ s_0[i]; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *scratch, |
| 96 | u8 *b_0, u8 *aad, u8 *cdata, size_t data_len, |
| 97 | u8 *mic, u8 *data) |
| 98 | { |
| 99 | int i, j, last_len, num_blocks; |
| 100 | u8 *pos, *cpos, *b, *s_0, *a; |
| 101 | |
| 102 | b = scratch; |
| 103 | s_0 = scratch + AES_BLOCK_LEN; |
| 104 | a = scratch + 2 * AES_BLOCK_LEN; |
| 105 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 106 | num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_LEN); |
Jiri Benc | f0706e82 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 107 | last_len = data_len % AES_BLOCK_LEN; |
| 108 | aes_ccm_prepare(tfm, b_0, aad, b, s_0, a); |
| 109 | |
| 110 | /* Process payload blocks */ |
| 111 | cpos = cdata; |
| 112 | pos = data; |
| 113 | for (j = 1; j <= num_blocks; j++) { |
| 114 | int blen = (j == num_blocks && last_len) ? |
| 115 | last_len : AES_BLOCK_LEN; |
| 116 | |
| 117 | /* Decryption followed by authentication */ |
| 118 | b_0[14] = (j >> 8) & 0xff; |
| 119 | b_0[15] = j & 0xff; |
| 120 | ieee80211_aes_encrypt(tfm, b_0, b); |
| 121 | for (i = 0; i < blen; i++) { |
| 122 | *pos = *cpos++ ^ b[i]; |
| 123 | a[i] ^= *pos++; |
| 124 | } |
| 125 | |
| 126 | ieee80211_aes_encrypt(tfm, a, a); |
| 127 | } |
| 128 | |
| 129 | for (i = 0; i < CCMP_MIC_LEN; i++) { |
| 130 | if ((mic[i] ^ s_0[i]) != a[i]) |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | struct crypto_cipher * ieee80211_aes_key_setup_encrypt(const u8 key[]) |
| 139 | { |
| 140 | struct crypto_cipher *tfm; |
| 141 | |
| 142 | tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC); |
| 143 | if (IS_ERR(tfm)) |
| 144 | return NULL; |
| 145 | |
| 146 | crypto_cipher_setkey(tfm, key, ALG_CCMP_KEY_LEN); |
| 147 | |
| 148 | return tfm; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void ieee80211_aes_key_free(struct crypto_cipher *tfm) |
| 153 | { |
| 154 | if (tfm) |
| 155 | crypto_free_cipher(tfm); |
| 156 | } |