blob: ec24378caaafaf333152e856aa0e2e920ddbb13f [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,
23 u8 *data, size_t data_len, u8 *mic)
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);
38 sg_set_buf(&ct[1], mic, IEEE80211_CCMP_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,
48 u8 *data, size_t data_len, u8 *mic)
Jiri Bencf0706e82007-05-05 11:45:53 -070049{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020050 struct scatterlist assoc, pt, ct[2];
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
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070056 memset(aead_req, 0, sizeof(aead_req_data));
Jiri Bencf0706e82007-05-05 11:45:53 -070057
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020058 sg_init_one(&pt, data, data_len);
59 sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
60 sg_init_table(ct, 2);
61 sg_set_buf(&ct[0], data, data_len);
62 sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -070063
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070064 aead_request_set_tfm(aead_req, tfm);
65 aead_request_set_assoc(aead_req, &assoc, assoc.length);
66 aead_request_set_crypt(aead_req, ct, &pt,
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020067 data_len + IEEE80211_CCMP_MIC_LEN, b_0);
Jiri Bencf0706e82007-05-05 11:45:53 -070068
Jan-Simon Möller6e1ee5d2014-03-20 23:39:32 -070069 return crypto_aead_decrypt(aead_req);
Jiri Bencf0706e82007-05-05 11:45:53 -070070}
71
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020072struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[])
Jiri Bencf0706e82007-05-05 11:45:53 -070073{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020074 struct crypto_aead *tfm;
75 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -070076
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020077 tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
78 if (IS_ERR(tfm))
79 return tfm;
Jiri Bencf0706e82007-05-05 11:45:53 -070080
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020081 err = crypto_aead_setkey(tfm, key, WLAN_KEY_LEN_CCMP);
82 if (!err)
83 err = crypto_aead_setauthsize(tfm, IEEE80211_CCMP_MIC_LEN);
84 if (!err)
85 return tfm;
Jiri Bencf0706e82007-05-05 11:45:53 -070086
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020087 crypto_free_aead(tfm);
88 return ERR_PTR(err);
Jiri Bencf0706e82007-05-05 11:45:53 -070089}
90
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020091void ieee80211_aes_key_free(struct crypto_aead *tfm)
Jiri Bencf0706e82007-05-05 11:45:53 -070092{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020093 crypto_free_aead(tfm);
Jiri Bencf0706e82007-05-05 11:45:53 -070094}