blob: 363ad1c1dc0cfeeeb254e7d38fff689a12252a40 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Jouni Malinen8ade5382015-01-24 19:52:09 +02002/*
3 * AES-GMAC for IEEE 802.11 BIP-GMAC-128 and BIP-GMAC-256
4 * Copyright 2015, Qualcomm Atheros, Inc.
Jouni Malinen8ade5382015-01-24 19:52:09 +02005 */
6
7#include <linux/kernel.h>
8#include <linux/types.h>
Jouni Malinen8ade5382015-01-24 19:52:09 +02009#include <linux/err.h>
Herbert Xud8fe0dd2015-04-22 15:06:32 +080010#include <crypto/aead.h>
Jouni Malinen8ade5382015-01-24 19:52:09 +020011#include <crypto/aes.h>
12
13#include <net/mac80211.h>
14#include "key.h"
15#include "aes_gmac.h"
16
Jouni Malinen8ade5382015-01-24 19:52:09 +020017int ieee80211_aes_gmac(struct crypto_aead *tfm, const u8 *aad, u8 *nonce,
18 const u8 *data, size_t data_len, u8 *mic)
19{
Herbert Xu957e0fe2015-05-27 16:03:50 +080020 struct scatterlist sg[4];
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010021 u8 *zero, *__aad, iv[AES_BLOCK_SIZE];
22 struct aead_request *aead_req;
23 int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
Jouni Malinen8ade5382015-01-24 19:52:09 +020024
25 if (data_len < GMAC_MIC_LEN)
26 return -EINVAL;
27
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010028 aead_req = kzalloc(reqsize + GMAC_MIC_LEN + GMAC_AAD_LEN, GFP_ATOMIC);
29 if (!aead_req)
30 return -ENOMEM;
Jouni Malinen8ade5382015-01-24 19:52:09 +020031
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010032 zero = (u8 *)aead_req + reqsize;
33 __aad = zero + GMAC_MIC_LEN;
34 memcpy(__aad, aad, GMAC_AAD_LEN);
35
Herbert Xu957e0fe2015-05-27 16:03:50 +080036 sg_init_table(sg, 4);
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010037 sg_set_buf(&sg[0], __aad, GMAC_AAD_LEN);
Jouni Malinen8ade5382015-01-24 19:52:09 +020038 sg_set_buf(&sg[1], data, data_len - GMAC_MIC_LEN);
39 sg_set_buf(&sg[2], zero, GMAC_MIC_LEN);
Herbert Xu957e0fe2015-05-27 16:03:50 +080040 sg_set_buf(&sg[3], mic, GMAC_MIC_LEN);
Jouni Malinen8ade5382015-01-24 19:52:09 +020041
42 memcpy(iv, nonce, GMAC_NONCE_LEN);
43 memset(iv + GMAC_NONCE_LEN, 0, sizeof(iv) - GMAC_NONCE_LEN);
44 iv[AES_BLOCK_SIZE - 1] = 0x01;
45
Jouni Malinen8ade5382015-01-24 19:52:09 +020046 aead_request_set_tfm(aead_req, tfm);
Herbert Xu957e0fe2015-05-27 16:03:50 +080047 aead_request_set_crypt(aead_req, sg, sg, 0, iv);
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010048 aead_request_set_ad(aead_req, GMAC_AAD_LEN + data_len);
Jouni Malinen8ade5382015-01-24 19:52:09 +020049
50 crypto_aead_encrypt(aead_req);
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010051 kzfree(aead_req);
Jouni Malinen8ade5382015-01-24 19:52:09 +020052
53 return 0;
54}
55
56struct crypto_aead *ieee80211_aes_gmac_key_setup(const u8 key[],
57 size_t key_len)
58{
59 struct crypto_aead *tfm;
60 int err;
61
62 tfm = crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
63 if (IS_ERR(tfm))
64 return tfm;
65
66 err = crypto_aead_setkey(tfm, key, key_len);
67 if (!err)
Jouni Malinen8ade5382015-01-24 19:52:09 +020068 err = crypto_aead_setauthsize(tfm, GMAC_MIC_LEN);
Jouni Malinen82ca6ef2015-03-23 15:41:15 +020069 if (!err)
70 return tfm;
Jouni Malinen8ade5382015-01-24 19:52:09 +020071
72 crypto_free_aead(tfm);
73 return ERR_PTR(err);
74}
75
76void ieee80211_aes_gmac_key_free(struct crypto_aead *tfm)
77{
78 crypto_free_aead(tfm);
79}