blob: 70d53dab6508023c93a549ae7718661d455bab8e [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 Biesheuvel7ec7c4a2013-10-10 09:55:20 +020021void ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020022 u8 *data, size_t data_len, u8 *mic,
23 size_t mic_len)
Jiri Bencf0706e82007-05-05 11:45:53 -070024{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020025 struct scatterlist assoc, pt, ct[2];
Jiri Bencf0706e82007-05-05 11:45:53 -070026
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070027 char aead_req_data[sizeof(struct aead_request) +
28 crypto_aead_reqsize(tfm)]
29 __aligned(__alignof__(struct aead_request));
30 struct aead_request *aead_req = (void *) aead_req_data;
31
32 memset(aead_req, 0, sizeof(aead_req_data));
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070033
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020034 sg_init_one(&pt, data, data_len);
35 sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
36 sg_init_table(ct, 2);
37 sg_set_buf(&ct[0], data, data_len);
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020038 sg_set_buf(&ct[1], mic, mic_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070039
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070040 aead_request_set_tfm(aead_req, tfm);
41 aead_request_set_assoc(aead_req, &assoc, assoc.length);
42 aead_request_set_crypt(aead_req, &pt, ct, data_len, b_0);
Jiri Bencf0706e82007-05-05 11:45:53 -070043
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070044 crypto_aead_encrypt(aead_req);
Jiri Bencf0706e82007-05-05 11:45:53 -070045}
46
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020047int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020048 u8 *data, size_t data_len, u8 *mic,
49 size_t mic_len)
Jiri Bencf0706e82007-05-05 11:45:53 -070050{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020051 struct scatterlist assoc, pt, ct[2];
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070052 char aead_req_data[sizeof(struct aead_request) +
53 crypto_aead_reqsize(tfm)]
54 __aligned(__alignof__(struct aead_request));
55 struct aead_request *aead_req = (void *) aead_req_data;
Jiri Bencf0706e82007-05-05 11:45:53 -070056
Ronald Wahl4f031fa2014-11-06 11:52:13 +010057 if (data_len == 0)
58 return -EINVAL;
59
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070060 memset(aead_req, 0, sizeof(aead_req_data));
Jiri Bencf0706e82007-05-05 11:45:53 -070061
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020062 sg_init_one(&pt, data, data_len);
63 sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
64 sg_init_table(ct, 2);
65 sg_set_buf(&ct[0], data, data_len);
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020066 sg_set_buf(&ct[1], mic, mic_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070067
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070068 aead_request_set_tfm(aead_req, tfm);
69 aead_request_set_assoc(aead_req, &assoc, assoc.length);
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020070 aead_request_set_crypt(aead_req, ct, &pt, data_len + mic_len, b_0);
Jiri Bencf0706e82007-05-05 11:45:53 -070071
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070072 return crypto_aead_decrypt(aead_req);
Jiri Bencf0706e82007-05-05 11:45:53 -070073}
74
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020075struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[],
76 size_t key_len,
77 size_t mic_len)
Jiri Bencf0706e82007-05-05 11:45:53 -070078{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020079 struct crypto_aead *tfm;
80 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -070081
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020082 tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
83 if (IS_ERR(tfm))
84 return tfm;
Jiri Bencf0706e82007-05-05 11:45:53 -070085
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020086 err = crypto_aead_setkey(tfm, key, key_len);
Dan Carpenter45fd6322015-03-23 17:08:14 +030087 if (err)
88 goto free_aead;
89 err = crypto_aead_setauthsize(tfm, mic_len);
90 if (err)
91 goto free_aead;
Jiri Bencf0706e82007-05-05 11:45:53 -070092
Dan Carpenter45fd6322015-03-23 17:08:14 +030093 return tfm;
94
95free_aead:
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020096 crypto_free_aead(tfm);
97 return ERR_PTR(err);
Jiri Bencf0706e82007-05-05 11:45:53 -070098}
99
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +0200100void ieee80211_aes_key_free(struct crypto_aead *tfm)
Jiri Bencf0706e82007-05-05 11:45:53 -0700101{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +0200102 crypto_free_aead(tfm);
Jiri Bencf0706e82007-05-05 11:45:53 -0700103}