Linux 4.1.16
[linux/fpc-iii.git] / net / mac80211 / aes_gcm.c
blobfd278bbe1b0db49ef825a025f11488eec7014daa
1 /*
2 * Copyright 2014-2015, Qualcomm Atheros, Inc.
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 */
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>
15 #include <net/mac80211.h>
16 #include "key.h"
17 #include "aes_gcm.h"
19 void ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
20 u8 *data, size_t data_len, u8 *mic)
22 struct scatterlist assoc, pt, ct[2];
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;
29 memset(aead_req, 0, sizeof(aead_req_data));
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);
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);
41 crypto_aead_encrypt(aead_req);
44 int ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
45 u8 *data, size_t data_len, u8 *mic)
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;
53 if (data_len == 0)
54 return -EINVAL;
56 memset(aead_req, 0, sizeof(aead_req_data));
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);
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);
69 return crypto_aead_decrypt(aead_req);
72 struct crypto_aead *ieee80211_aes_gcm_key_setup_encrypt(const u8 key[],
73 size_t key_len)
75 struct crypto_aead *tfm;
76 int err;
78 tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
79 if (IS_ERR(tfm))
80 return tfm;
82 err = crypto_aead_setkey(tfm, key, key_len);
83 if (err)
84 goto free_aead;
85 err = crypto_aead_setauthsize(tfm, IEEE80211_GCMP_MIC_LEN);
86 if (err)
87 goto free_aead;
89 return tfm;
91 free_aead:
92 crypto_free_aead(tfm);
93 return ERR_PTR(err);
96 void ieee80211_aes_gcm_key_free(struct crypto_aead *tfm)
98 crypto_free_aead(tfm);