blob: a4e0d59a40dd52b90f2f54e230600a1633f06831 [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * Copyright 2003-2004, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 *
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +02005 * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org>
6 *
Jiri Bencf0706e82007-05-05 11:45:53 -07007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Ilpo Järvinen172589c2007-08-28 15:50:33 -070012#include <linux/kernel.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070013#include <linux/types.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070014#include <linux/err.h>
Herbert Xud8fe0dd2015-04-22 15:06:32 +080015#include <crypto/aead.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070016
17#include <net/mac80211.h>
Johannes Berg2c8dccc2008-04-08 15:14:40 -040018#include "key.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070019#include "aes_ccm.h"
20
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010021int ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
22 u8 *data, size_t data_len, u8 *mic,
23 size_t mic_len)
Jiri Bencf0706e82007-05-05 11:45:53 -070024{
Herbert Xu957e0fe2015-05-27 16:03:50 +080025 struct scatterlist sg[3];
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010026 struct aead_request *aead_req;
27 int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
28 u8 *__aad;
Jiri Bencf0706e82007-05-05 11:45:53 -070029
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010030 aead_req = kzalloc(reqsize + CCM_AAD_LEN, GFP_ATOMIC);
31 if (!aead_req)
32 return -ENOMEM;
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070033
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010034 __aad = (u8 *)aead_req + reqsize;
35 memcpy(__aad, aad, CCM_AAD_LEN);
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070036
Herbert Xu957e0fe2015-05-27 16:03:50 +080037 sg_init_table(sg, 3);
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010038 sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad));
Herbert Xu957e0fe2015-05-27 16:03:50 +080039 sg_set_buf(&sg[1], data, data_len);
40 sg_set_buf(&sg[2], mic, mic_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070041
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070042 aead_request_set_tfm(aead_req, tfm);
Herbert Xu957e0fe2015-05-27 16:03:50 +080043 aead_request_set_crypt(aead_req, sg, sg, data_len, b_0);
44 aead_request_set_ad(aead_req, sg[0].length);
Jiri Bencf0706e82007-05-05 11:45:53 -070045
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070046 crypto_aead_encrypt(aead_req);
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010047 kzfree(aead_req);
48
49 return 0;
Jiri Bencf0706e82007-05-05 11:45:53 -070050}
51
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020052int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020053 u8 *data, size_t data_len, u8 *mic,
54 size_t mic_len)
Jiri Bencf0706e82007-05-05 11:45:53 -070055{
Herbert Xu957e0fe2015-05-27 16:03:50 +080056 struct scatterlist sg[3];
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010057 struct aead_request *aead_req;
58 int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm);
59 u8 *__aad;
60 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -070061
Ronald Wahl4f031fa2014-11-06 11:52:13 +010062 if (data_len == 0)
63 return -EINVAL;
64
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010065 aead_req = kzalloc(reqsize + CCM_AAD_LEN, GFP_ATOMIC);
66 if (!aead_req)
67 return -ENOMEM;
68
69 __aad = (u8 *)aead_req + reqsize;
70 memcpy(__aad, aad, CCM_AAD_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -070071
Herbert Xu957e0fe2015-05-27 16:03:50 +080072 sg_init_table(sg, 3);
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010073 sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad));
Herbert Xu957e0fe2015-05-27 16:03:50 +080074 sg_set_buf(&sg[1], data, data_len);
75 sg_set_buf(&sg[2], mic, mic_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070076
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070077 aead_request_set_tfm(aead_req, tfm);
Herbert Xu957e0fe2015-05-27 16:03:50 +080078 aead_request_set_crypt(aead_req, sg, sg, data_len + mic_len, b_0);
79 aead_request_set_ad(aead_req, sg[0].length);
Jiri Bencf0706e82007-05-05 11:45:53 -070080
Ard Biesheuvelf4a067f2016-10-17 15:05:33 +010081 err = crypto_aead_decrypt(aead_req);
82 kzfree(aead_req);
83
84 return err;
Jiri Bencf0706e82007-05-05 11:45:53 -070085}
86
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020087struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[],
88 size_t key_len,
89 size_t mic_len)
Jiri Bencf0706e82007-05-05 11:45:53 -070090{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020091 struct crypto_aead *tfm;
92 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -070093
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020094 tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
95 if (IS_ERR(tfm))
96 return tfm;
Jiri Bencf0706e82007-05-05 11:45:53 -070097
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020098 err = crypto_aead_setkey(tfm, key, key_len);
Dan Carpenter45fd6322015-03-23 17:08:14 +030099 if (err)
100 goto free_aead;
101 err = crypto_aead_setauthsize(tfm, mic_len);
102 if (err)
103 goto free_aead;
Jiri Bencf0706e82007-05-05 11:45:53 -0700104
Dan Carpenter45fd6322015-03-23 17:08:14 +0300105 return tfm;
106
107free_aead:
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +0200108 crypto_free_aead(tfm);
109 return ERR_PTR(err);
Jiri Bencf0706e82007-05-05 11:45:53 -0700110}
111
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +0200112void ieee80211_aes_key_free(struct crypto_aead *tfm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700113{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +0200114 crypto_free_aead(tfm);
Jiri Bencf0706e82007-05-05 11:45:53 -0700115}