Commit | Line | Data |
---|---|---|
00b9cfa3 JM |
1 | /* |
2 | * Copyright 2014-2015, Qualcomm Atheros, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | */ | |
8 | ||
9 | #include <linux/kernel.h> | |
10 | #include <linux/types.h> | |
11 | #include <linux/crypto.h> | |
12 | #include <linux/err.h> | |
13 | #include <crypto/aes.h> | |
14 | ||
15 | #include <net/mac80211.h> | |
16 | #include "key.h" | |
17 | #include "aes_gcm.h" | |
18 | ||
19 | void ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad, | |
20 | u8 *data, size_t data_len, u8 *mic) | |
21 | { | |
22 | struct scatterlist assoc, pt, ct[2]; | |
23 | ||
24 | char aead_req_data[sizeof(struct aead_request) + | |
25 | crypto_aead_reqsize(tfm)] | |
26 | __aligned(__alignof__(struct aead_request)); | |
27 | struct aead_request *aead_req = (void *)aead_req_data; | |
28 | ||
29 | memset(aead_req, 0, sizeof(aead_req_data)); | |
30 | ||
31 | sg_init_one(&pt, data, data_len); | |
32 | sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad)); | |
33 | sg_init_table(ct, 2); | |
34 | sg_set_buf(&ct[0], data, data_len); | |
35 | sg_set_buf(&ct[1], mic, IEEE80211_GCMP_MIC_LEN); | |
36 | ||
37 | aead_request_set_tfm(aead_req, tfm); | |
38 | aead_request_set_assoc(aead_req, &assoc, assoc.length); | |
39 | aead_request_set_crypt(aead_req, &pt, ct, data_len, j_0); | |
40 | ||
41 | crypto_aead_encrypt(aead_req); | |
42 | } | |
43 | ||
44 | int ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad, | |
45 | u8 *data, size_t data_len, u8 *mic) | |
46 | { | |
47 | struct scatterlist assoc, pt, ct[2]; | |
48 | char aead_req_data[sizeof(struct aead_request) + | |
49 | crypto_aead_reqsize(tfm)] | |
50 | __aligned(__alignof__(struct aead_request)); | |
51 | struct aead_request *aead_req = (void *)aead_req_data; | |
52 | ||
53 | if (data_len == 0) | |
54 | return -EINVAL; | |
55 | ||
56 | memset(aead_req, 0, sizeof(aead_req_data)); | |
57 | ||
58 | sg_init_one(&pt, data, data_len); | |
59 | sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad)); | |
60 | sg_init_table(ct, 2); | |
61 | sg_set_buf(&ct[0], data, data_len); | |
62 | sg_set_buf(&ct[1], mic, IEEE80211_GCMP_MIC_LEN); | |
63 | ||
64 | aead_request_set_tfm(aead_req, tfm); | |
65 | aead_request_set_assoc(aead_req, &assoc, assoc.length); | |
66 | aead_request_set_crypt(aead_req, ct, &pt, | |
67 | data_len + IEEE80211_GCMP_MIC_LEN, j_0); | |
68 | ||
69 | return crypto_aead_decrypt(aead_req); | |
70 | } | |
71 | ||
72 | struct crypto_aead *ieee80211_aes_gcm_key_setup_encrypt(const u8 key[], | |
73 | size_t key_len) | |
74 | { | |
75 | struct crypto_aead *tfm; | |
76 | int err; | |
77 | ||
78 | tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC); | |
79 | if (IS_ERR(tfm)) | |
80 | return tfm; | |
81 | ||
82 | err = crypto_aead_setkey(tfm, key, key_len); | |
07862e13 JB |
83 | if (err) |
84 | goto free_aead; | |
85 | err = crypto_aead_setauthsize(tfm, IEEE80211_GCMP_MIC_LEN); | |
86 | if (err) | |
87 | goto free_aead; | |
88 | ||
89 | return tfm; | |
00b9cfa3 | 90 | |
07862e13 | 91 | free_aead: |
00b9cfa3 JM |
92 | crypto_free_aead(tfm); |
93 | return ERR_PTR(err); | |
94 | } | |
95 | ||
96 | void ieee80211_aes_gcm_key_free(struct crypto_aead *tfm) | |
97 | { | |
98 | crypto_free_aead(tfm); | |
99 | } |