blob: 7663c28ba3539f230c9cc5b1bd044164f1ef561c [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{
Herbert Xu957e0fe2015-05-27 16:03:50 +080025 struct scatterlist sg[3];
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
Herbert Xu957e0fe2015-05-27 16:03:50 +080034 sg_init_table(sg, 3);
35 sg_set_buf(&sg[0], &aad[2], be16_to_cpup((__be16 *)aad));
36 sg_set_buf(&sg[1], data, data_len);
37 sg_set_buf(&sg[2], mic, mic_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070038
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070039 aead_request_set_tfm(aead_req, tfm);
Herbert Xu957e0fe2015-05-27 16:03:50 +080040 aead_request_set_crypt(aead_req, sg, sg, data_len, b_0);
41 aead_request_set_ad(aead_req, sg[0].length);
Jiri Bencf0706e82007-05-05 11:45:53 -070042
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070043 crypto_aead_encrypt(aead_req);
Jiri Bencf0706e82007-05-05 11:45:53 -070044}
45
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020046int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020047 u8 *data, size_t data_len, u8 *mic,
48 size_t mic_len)
Jiri Bencf0706e82007-05-05 11:45:53 -070049{
Herbert Xu957e0fe2015-05-27 16:03:50 +080050 struct scatterlist sg[3];
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070051 char aead_req_data[sizeof(struct aead_request) +
52 crypto_aead_reqsize(tfm)]
53 __aligned(__alignof__(struct aead_request));
54 struct aead_request *aead_req = (void *) aead_req_data;
Jiri Bencf0706e82007-05-05 11:45:53 -070055
Ronald Wahl4f031fa2014-11-06 11:52:13 +010056 if (data_len == 0)
57 return -EINVAL;
58
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070059 memset(aead_req, 0, sizeof(aead_req_data));
Jiri Bencf0706e82007-05-05 11:45:53 -070060
Herbert Xu957e0fe2015-05-27 16:03:50 +080061 sg_init_table(sg, 3);
62 sg_set_buf(&sg[0], &aad[2], be16_to_cpup((__be16 *)aad));
63 sg_set_buf(&sg[1], data, data_len);
64 sg_set_buf(&sg[2], mic, mic_len);
Jiri Bencf0706e82007-05-05 11:45:53 -070065
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070066 aead_request_set_tfm(aead_req, tfm);
Herbert Xu957e0fe2015-05-27 16:03:50 +080067 aead_request_set_crypt(aead_req, sg, sg, data_len + mic_len, b_0);
68 aead_request_set_ad(aead_req, sg[0].length);
Jiri Bencf0706e82007-05-05 11:45:53 -070069
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070070 return crypto_aead_decrypt(aead_req);
Jiri Bencf0706e82007-05-05 11:45:53 -070071}
72
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020073struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[],
74 size_t key_len,
75 size_t mic_len)
Jiri Bencf0706e82007-05-05 11:45:53 -070076{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020077 struct crypto_aead *tfm;
78 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -070079
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020080 tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
81 if (IS_ERR(tfm))
82 return tfm;
Jiri Bencf0706e82007-05-05 11:45:53 -070083
Jouni Malinen2b2ba0d2015-01-24 19:52:07 +020084 err = crypto_aead_setkey(tfm, key, key_len);
Dan Carpenter45fd6322015-03-23 17:08:14 +030085 if (err)
86 goto free_aead;
87 err = crypto_aead_setauthsize(tfm, mic_len);
88 if (err)
89 goto free_aead;
Jiri Bencf0706e82007-05-05 11:45:53 -070090
Dan Carpenter45fd6322015-03-23 17:08:14 +030091 return tfm;
92
93free_aead:
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020094 crypto_free_aead(tfm);
95 return ERR_PTR(err);
Jiri Bencf0706e82007-05-05 11:45:53 -070096}
97
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020098void ieee80211_aes_key_free(struct crypto_aead *tfm)
Jiri Bencf0706e82007-05-05 11:45:53 -070099{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +0200100 crypto_free_aead(tfm);
Jiri Bencf0706e82007-05-05 11:45:53 -0700101}