Jiri Benc | f0706e8 | 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 | * |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 5 | * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org> |
| 6 | * |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 13 | #include <linux/types.h> |
| 14 | #include <linux/crypto.h> |
| 15 | #include <linux/err.h> |
Johannes Berg | 0cd20a2 | 2011-07-06 22:02:14 +0200 | [diff] [blame] | 16 | #include <crypto/aes.h> |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 17 | |
| 18 | #include <net/mac80211.h> |
Johannes Berg | 2c8dccc | 2008-04-08 15:14:40 -0400 | [diff] [blame] | 19 | #include "key.h" |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 20 | #include "aes_ccm.h" |
| 21 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 22 | void ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, |
| 23 | u8 *data, size_t data_len, u8 *mic) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 24 | { |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 25 | struct scatterlist assoc, pt, ct[2]; |
| 26 | struct { |
| 27 | struct aead_request req; |
| 28 | u8 priv[crypto_aead_reqsize(tfm)]; |
| 29 | } aead_req; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 30 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 31 | memset(&aead_req, 0, sizeof(aead_req)); |
Harvey Harrison | 5fdae6b | 2008-07-02 16:30:53 -0700 | [diff] [blame] | 32 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 33 | sg_init_one(&pt, data, data_len); |
| 34 | sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad)); |
| 35 | sg_init_table(ct, 2); |
| 36 | sg_set_buf(&ct[0], data, data_len); |
| 37 | sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 38 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 39 | aead_request_set_tfm(&aead_req.req, tfm); |
| 40 | aead_request_set_assoc(&aead_req.req, &assoc, assoc.length); |
| 41 | aead_request_set_crypt(&aead_req.req, &pt, ct, data_len, b_0); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 42 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 43 | crypto_aead_encrypt(&aead_req.req); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 46 | int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, |
| 47 | u8 *data, size_t data_len, u8 *mic) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 48 | { |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 49 | struct scatterlist assoc, pt, ct[2]; |
| 50 | struct { |
| 51 | struct aead_request req; |
| 52 | u8 priv[crypto_aead_reqsize(tfm)]; |
| 53 | } aead_req; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 54 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 55 | memset(&aead_req, 0, sizeof(aead_req)); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 56 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 57 | sg_init_one(&pt, data, data_len); |
| 58 | sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad)); |
| 59 | sg_init_table(ct, 2); |
| 60 | sg_set_buf(&ct[0], data, data_len); |
| 61 | sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 62 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 63 | aead_request_set_tfm(&aead_req.req, tfm); |
| 64 | aead_request_set_assoc(&aead_req.req, &assoc, assoc.length); |
| 65 | aead_request_set_crypt(&aead_req.req, ct, &pt, |
| 66 | data_len + IEEE80211_CCMP_MIC_LEN, b_0); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 67 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 68 | return crypto_aead_decrypt(&aead_req.req); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 71 | struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[]) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 72 | { |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 73 | struct crypto_aead *tfm; |
| 74 | int err; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 75 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 76 | tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC); |
| 77 | if (IS_ERR(tfm)) |
| 78 | return tfm; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 79 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 80 | err = crypto_aead_setkey(tfm, key, WLAN_KEY_LEN_CCMP); |
| 81 | if (!err) |
| 82 | err = crypto_aead_setauthsize(tfm, IEEE80211_CCMP_MIC_LEN); |
| 83 | if (!err) |
| 84 | return tfm; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 85 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 86 | crypto_free_aead(tfm); |
| 87 | return ERR_PTR(err); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 90 | void ieee80211_aes_key_free(struct crypto_aead *tfm) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 91 | { |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 92 | crypto_free_aead(tfm); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 93 | } |