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.
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <linux/err.h>
12 #include <crypto/aead.h>
14 #include <net/mac80211.h>
18 int ieee80211_aes_gcm_encrypt(struct crypto_aead
*tfm
, u8
*j_0
, u8
*aad
,
19 u8
*data
, size_t data_len
, u8
*mic
)
21 struct scatterlist sg
[3];
22 struct aead_request
*aead_req
;
23 int reqsize
= sizeof(*aead_req
) + crypto_aead_reqsize(tfm
);
26 aead_req
= kzalloc(reqsize
+ GCM_AAD_LEN
, GFP_ATOMIC
);
30 __aad
= (u8
*)aead_req
+ reqsize
;
31 memcpy(__aad
, aad
, GCM_AAD_LEN
);
34 sg_set_buf(&sg
[0], &__aad
[2], be16_to_cpup((__be16
*)__aad
));
35 sg_set_buf(&sg
[1], data
, data_len
);
36 sg_set_buf(&sg
[2], mic
, IEEE80211_GCMP_MIC_LEN
);
38 aead_request_set_tfm(aead_req
, tfm
);
39 aead_request_set_crypt(aead_req
, sg
, sg
, data_len
, j_0
);
40 aead_request_set_ad(aead_req
, sg
[0].length
);
42 crypto_aead_encrypt(aead_req
);
47 int ieee80211_aes_gcm_decrypt(struct crypto_aead
*tfm
, u8
*j_0
, u8
*aad
,
48 u8
*data
, size_t data_len
, u8
*mic
)
50 struct scatterlist sg
[3];
51 struct aead_request
*aead_req
;
52 int reqsize
= sizeof(*aead_req
) + crypto_aead_reqsize(tfm
);
59 aead_req
= kzalloc(reqsize
+ GCM_AAD_LEN
, GFP_ATOMIC
);
63 __aad
= (u8
*)aead_req
+ reqsize
;
64 memcpy(__aad
, aad
, GCM_AAD_LEN
);
67 sg_set_buf(&sg
[0], &__aad
[2], be16_to_cpup((__be16
*)__aad
));
68 sg_set_buf(&sg
[1], data
, data_len
);
69 sg_set_buf(&sg
[2], mic
, IEEE80211_GCMP_MIC_LEN
);
71 aead_request_set_tfm(aead_req
, tfm
);
72 aead_request_set_crypt(aead_req
, sg
, sg
,
73 data_len
+ IEEE80211_GCMP_MIC_LEN
, j_0
);
74 aead_request_set_ad(aead_req
, sg
[0].length
);
76 err
= crypto_aead_decrypt(aead_req
);
82 struct crypto_aead
*ieee80211_aes_gcm_key_setup_encrypt(const u8 key
[],
85 struct crypto_aead
*tfm
;
88 tfm
= crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC
);
92 err
= crypto_aead_setkey(tfm
, key
, key_len
);
95 err
= crypto_aead_setauthsize(tfm
, IEEE80211_GCMP_MIC_LEN
);
102 crypto_free_aead(tfm
);
106 void ieee80211_aes_gcm_key_free(struct crypto_aead
*tfm
)
108 crypto_free_aead(tfm
);