blob: b91c9d7bf6652eca1bd3b659e39ce87bb81ed21d [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{
21 struct scatterlist assoc, pt, ct[2];
22
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
30 sg_init_one(&pt, data, data_len);
31 sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
32 sg_init_table(ct, 2);
33 sg_set_buf(&ct[0], data, data_len);
34 sg_set_buf(&ct[1], mic, IEEE80211_GCMP_MIC_LEN);
35
36 aead_request_set_tfm(aead_req, tfm);
37 aead_request_set_assoc(aead_req, &assoc, assoc.length);
38 aead_request_set_crypt(aead_req, &pt, ct, data_len, j_0);
39
40 crypto_aead_encrypt(aead_req);
41}
42
43int ieee80211_aes_gcm_decrypt(struct crypto_aead *tfm, u8 *j_0, u8 *aad,
44 u8 *data, size_t data_len, u8 *mic)
45{
46 struct scatterlist assoc, pt, ct[2];
47 char aead_req_data[sizeof(struct aead_request) +
48 crypto_aead_reqsize(tfm)]
49 __aligned(__alignof__(struct aead_request));
50 struct aead_request *aead_req = (void *)aead_req_data;
51
52 if (data_len == 0)
53 return -EINVAL;
54
55 memset(aead_req, 0, sizeof(aead_req_data));
56
57 sg_init_one(&pt, data, data_len);
58 sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
59 sg_init_table(ct, 2);
60 sg_set_buf(&ct[0], data, data_len);
61 sg_set_buf(&ct[1], mic, IEEE80211_GCMP_MIC_LEN);
62
63 aead_request_set_tfm(aead_req, tfm);
64 aead_request_set_assoc(aead_req, &assoc, assoc.length);
65 aead_request_set_crypt(aead_req, ct, &pt,
66 data_len + IEEE80211_GCMP_MIC_LEN, j_0);
67
68 return crypto_aead_decrypt(aead_req);
69}
70
71struct crypto_aead *ieee80211_aes_gcm_key_setup_encrypt(const u8 key[],
72 size_t key_len)
73{
74 struct crypto_aead *tfm;
75 int err;
76
77 tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
78 if (IS_ERR(tfm))
79 return tfm;
80
81 err = crypto_aead_setkey(tfm, key, key_len);
Johannes Berg07862e12015-03-23 17:08:14 +030082 if (err)
83 goto free_aead;
84 err = crypto_aead_setauthsize(tfm, IEEE80211_GCMP_MIC_LEN);
85 if (err)
86 goto free_aead;
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020087
Johannes Berg07862e12015-03-23 17:08:14 +030088 return tfm;
89
90free_aead:
Jouni Malinen00b9cfa2015-01-24 19:52:06 +020091 crypto_free_aead(tfm);
92 return ERR_PTR(err);
93}
94
95void ieee80211_aes_gcm_key_free(struct crypto_aead *tfm)
96{
97 crypto_free_aead(tfm);
98}