Commit | Line | Data |
---|---|---|
f0706e82 JB |
1 | /* |
2 | * Copyright 2003-2004, Instant802 Networks, Inc. | |
3 | * Copyright 2005-2006, Devicescape Software, Inc. | |
4 | * | |
7ec7c4a9 AB |
5 | * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org> |
6 | * | |
f0706e82 JB |
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 | ||
172589cc | 12 | #include <linux/kernel.h> |
f0706e82 JB |
13 | #include <linux/types.h> |
14 | #include <linux/crypto.h> | |
15 | #include <linux/err.h> | |
0cd20a27 | 16 | #include <crypto/aes.h> |
f0706e82 JB |
17 | |
18 | #include <net/mac80211.h> | |
2c8dccc7 | 19 | #include "key.h" |
f0706e82 JB |
20 | #include "aes_ccm.h" |
21 | ||
7ec7c4a9 AB |
22 | void ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, |
23 | u8 *data, size_t data_len, u8 *mic) | |
f0706e82 | 24 | { |
7ec7c4a9 | 25 | struct scatterlist assoc, pt, ct[2]; |
f0706e82 | 26 | |
6e1ee5d2 JSM |
27 | char aead_req_data[sizeof(struct aead_request) + |
28 | crypto_aead_reqsize(tfm)] | |
29 | __aligned(__alignof__(struct aead_request)); | |
30 | struct aead_request *aead_req = (void *) aead_req_data; | |
31 | ||
32 | memset(aead_req, 0, sizeof(aead_req_data)); | |
f0706e82 | 33 | |
7ec7c4a9 AB |
34 | sg_init_one(&pt, data, data_len); |
35 | sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad)); | |
36 | sg_init_table(ct, 2); | |
37 | sg_set_buf(&ct[0], data, data_len); | |
38 | sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN); | |
f0706e82 | 39 | |
6e1ee5d2 JSM |
40 | aead_request_set_tfm(aead_req, tfm); |
41 | aead_request_set_assoc(aead_req, &assoc, assoc.length); | |
42 | aead_request_set_crypt(aead_req, &pt, ct, data_len, b_0); | |
f0706e82 | 43 | |
6e1ee5d2 | 44 | crypto_aead_encrypt(aead_req); |
f0706e82 JB |
45 | } |
46 | ||
7ec7c4a9 AB |
47 | int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, |
48 | u8 *data, size_t data_len, u8 *mic) | |
f0706e82 | 49 | { |
7ec7c4a9 | 50 | struct scatterlist assoc, pt, ct[2]; |
6e1ee5d2 JSM |
51 | char aead_req_data[sizeof(struct aead_request) + |
52 | crypto_aead_reqsize(tfm)] | |
53 | __aligned(__alignof__(struct aead_request)); | |
54 | struct aead_request *aead_req = (void *) aead_req_data; | |
7ec7c4a9 | 55 | |
4f031fa9 RW |
56 | if (data_len == 0) |
57 | return -EINVAL; | |
58 | ||
6e1ee5d2 | 59 | memset(aead_req, 0, sizeof(aead_req_data)); |
7ec7c4a9 AB |
60 | |
61 | sg_init_one(&pt, data, data_len); | |
62 | sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad)); | |
63 | sg_init_table(ct, 2); | |
64 | sg_set_buf(&ct[0], data, data_len); | |
65 | sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN); | |
66 | ||
6e1ee5d2 JSM |
67 | aead_request_set_tfm(aead_req, tfm); |
68 | aead_request_set_assoc(aead_req, &assoc, assoc.length); | |
69 | aead_request_set_crypt(aead_req, ct, &pt, | |
7ec7c4a9 AB |
70 | data_len + IEEE80211_CCMP_MIC_LEN, b_0); |
71 | ||
6e1ee5d2 | 72 | return crypto_aead_decrypt(aead_req); |
f0706e82 JB |
73 | } |
74 | ||
7ec7c4a9 | 75 | struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[]) |
f0706e82 | 76 | { |
7ec7c4a9 AB |
77 | struct crypto_aead *tfm; |
78 | int err; | |
f0706e82 | 79 | |
7ec7c4a9 AB |
80 | tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC); |
81 | if (IS_ERR(tfm)) | |
82 | return tfm; | |
f0706e82 | 83 | |
7ec7c4a9 AB |
84 | err = crypto_aead_setkey(tfm, key, WLAN_KEY_LEN_CCMP); |
85 | if (!err) | |
86 | err = crypto_aead_setauthsize(tfm, IEEE80211_CCMP_MIC_LEN); | |
87 | if (!err) | |
88 | return tfm; | |
f0706e82 | 89 | |
7ec7c4a9 AB |
90 | crypto_free_aead(tfm); |
91 | return ERR_PTR(err); | |
f0706e82 JB |
92 | } |
93 | ||
7ec7c4a9 | 94 | void ieee80211_aes_key_free(struct crypto_aead *tfm) |
f0706e82 | 95 | { |
7ec7c4a9 | 96 | crypto_free_aead(tfm); |
f0706e82 | 97 | } |