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