blob: 3afe361fd27ca5ef5ac1648106fbe010520fb377 [file] [log] [blame]
Jouni Malinen00b9cfa2015-01-24 19:52:06 +02001/*
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>
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020011#include <linux/err.h>
Herbert Xud8fe0dd2015-04-22 15:06:32 +080012#include <crypto/aead.h>
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020013
14#include <net/mac80211.h>
15#include "key.h"
16#include "aes_gcm.h"
17
18void ieee80211_aes_gcm_encrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
19 u8 *data, size_t data_len, u8 *mic)
20{
Herbert Xu957e0fe2015-05-27 16:03:50 +080021 struct scatterlist sg[3];
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020022
23 char aead_req_data[sizeof(struct aead_request) +
24 crypto_aead_reqsize(tfm)]
25 __aligned(__alignof__(struct aead_request));
26 struct aead_request *aead_req = (void *)aead_req_data;
27
28 memset(aead_req, 0, sizeof(aead_req_data));
29
Herbert Xu957e0fe2015-05-27 16:03:50 +080030 sg_init_table(sg, 3);
31 sg_set_buf(&sg[0], &aad[2], be16_to_cpup((__be16 *)aad));
32 sg_set_buf(&sg[1], data, data_len);
33 sg_set_buf(&sg[2], mic, IEEE80211_GCMP_MIC_LEN);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020034
35 aead_request_set_tfm(aead_req, tfm);
Herbert Xu957e0fe2015-05-27 16:03:50 +080036 aead_request_set_crypt(aead_req, sg, sg, data_len, j_0);
37 aead_request_set_ad(aead_req, sg[0].length);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020038
39 crypto_aead_encrypt(aead_req);
40}
41
42int ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
43 u8 *data, size_t data_len, u8 *mic)
44{
Herbert Xu957e0fe2015-05-27 16:03:50 +080045 struct scatterlist sg[3];
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020046 char aead_req_data[sizeof(struct aead_request) +
47 crypto_aead_reqsize(tfm)]
48 __aligned(__alignof__(struct aead_request));
49 struct aead_request *aead_req = (void *)aead_req_data;
50
51 if (data_len == 0)
52 return -EINVAL;
53
54 memset(aead_req, 0, sizeof(aead_req_data));
55
Herbert Xu957e0fe2015-05-27 16:03:50 +080056 sg_init_table(sg, 3);
57 sg_set_buf(&sg[0], &aad[2], be16_to_cpup((__be16 *)aad));
58 sg_set_buf(&sg[1], data, data_len);
59 sg_set_buf(&sg[2], mic, IEEE80211_GCMP_MIC_LEN);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020060
61 aead_request_set_tfm(aead_req, tfm);
Herbert Xu957e0fe2015-05-27 16:03:50 +080062 aead_request_set_crypt(aead_req, sg, sg,
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020063 data_len + IEEE80211_GCMP_MIC_LEN, j_0);
Herbert Xu957e0fe2015-05-27 16:03:50 +080064 aead_request_set_ad(aead_req, sg[0].length);
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020065
66 return crypto_aead_decrypt(aead_req);
67}
68
69struct crypto_aead *ieee80211_aes_gcm_key_setup_encrypt(const u8 key[],
70 size_t key_len)
71{
72 struct crypto_aead *tfm;
73 int err;
74
75 tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
76 if (IS_ERR(tfm))
77 return tfm;
78
79 err = crypto_aead_setkey(tfm, key, key_len);
Johannes Berg07862e12015-03-23 17:08:14 +030080 if (err)
81 goto free_aead;
82 err = crypto_aead_setauthsize(tfm, IEEE80211_GCMP_MIC_LEN);
83 if (err)
84 goto free_aead;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020085
Johannes Berg07862e12015-03-23 17:08:14 +030086 return tfm;
87
88free_aead:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020089 crypto_free_aead(tfm);
90 return ERR_PTR(err);
91}
92
93void ieee80211_aes_gcm_key_free(struct crypto_aead *tfm)
94{
95 crypto_free_aead(tfm);
96}