blob: 7869bb40acaa1acbe60763493a738bf32812bb34 [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>
14#include <linux/crypto.h>
15#include <linux/err.h>
Johannes Berg0cd20a22011-07-06 22:02:14 +020016#include <crypto/aes.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070017
18#include <net/mac80211.h>
Johannes Berg2c8dccc2008-04-08 15:14:40 -040019#include "key.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070020#include "aes_ccm.h"
21
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020022void ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020023 u8 *data, size_t data_len, u8 *mic,
24 size_t mic_len)
Jiri Bencf0706e82007-05-05 11:45:53 -070025{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020026 struct scatterlist assoc, pt, ct[2];
Jiri Bencf0706e82007-05-05 11:45:53 -070027
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070028 char aead_req_data[sizeof(struct aead_request) +
29 crypto_aead_reqsize(tfm)]
30 __aligned(__alignof__(struct aead_request));
31 struct aead_request *aead_req = (void *) aead_req_data;
32
33 memset(aead_req, 0, sizeof(aead_req_data));
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070034
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020035 sg_init_one(&pt, data, data_len);
36 sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
37 sg_init_table(ct, 2);
38 sg_set_buf(&ct[0], data, data_len);
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020039 sg_set_buf(&ct[1], mic, mic_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070040
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070041 aead_request_set_tfm(aead_req, tfm);
42 aead_request_set_assoc(aead_req, &assoc, assoc.length);
43 aead_request_set_crypt(aead_req, &pt, ct, data_len, b_0);
Jiri Bencf0706e82007-05-05 11:45:53 -070044
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070045 crypto_aead_encrypt(aead_req);
Jiri Bencf0706e82007-05-05 11:45:53 -070046}
47
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020048int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020049 u8 *data, size_t data_len, u8 *mic,
50 size_t mic_len)
Jiri Bencf0706e82007-05-05 11:45:53 -070051{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020052 struct scatterlist assoc, pt, ct[2];
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070053 char aead_req_data[sizeof(struct aead_request) +
54 crypto_aead_reqsize(tfm)]
55 __aligned(__alignof__(struct aead_request));
56 struct aead_request *aead_req = (void *) aead_req_data;
Jiri Bencf0706e82007-05-05 11:45:53 -070057
Ronald Wahl4f031fa2014-11-06 11:52:13 +010058 if (data_len == 0)
59 return -EINVAL;
60
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070061 memset(aead_req, 0, sizeof(aead_req_data));
Jiri Bencf0706e82007-05-05 11:45:53 -070062
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020063 sg_init_one(&pt, data, data_len);
64 sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
65 sg_init_table(ct, 2);
66 sg_set_buf(&ct[0], data, data_len);
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020067 sg_set_buf(&ct[1], mic, mic_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070068
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070069 aead_request_set_tfm(aead_req, tfm);
70 aead_request_set_assoc(aead_req, &assoc, assoc.length);
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020071 aead_request_set_crypt(aead_req, ct, &pt, data_len + mic_len, b_0);
Jiri Bencf0706e82007-05-05 11:45:53 -070072
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070073 return crypto_aead_decrypt(aead_req);
Jiri Bencf0706e82007-05-05 11:45:53 -070074}
75
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020076struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[],
77 size_t key_len,
78 size_t mic_len)
Jiri Bencf0706e82007-05-05 11:45:53 -070079{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020080 struct crypto_aead *tfm;
81 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -070082
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020083 tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
84 if (IS_ERR(tfm))
85 return tfm;
Jiri Bencf0706e82007-05-05 11:45:53 -070086
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020087 err = crypto_aead_setkey(tfm, key, key_len);
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020088 if (!err)
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020089 err = crypto_aead_setauthsize(tfm, mic_len);
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020090 if (!err)
91 return tfm;
Jiri Bencf0706e82007-05-05 11:45:53 -070092
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020093 crypto_free_aead(tfm);
94 return ERR_PTR(err);
Jiri Bencf0706e82007-05-05 11:45:53 -070095}
96
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020097void ieee80211_aes_key_free(struct crypto_aead *tfm)
Jiri Bencf0706e82007-05-05 11:45:53 -070098{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020099 crypto_free_aead(tfm);
Jiri Bencf0706e82007-05-05 11:45:53 -0700100}