blob: 7c7df475a401693dd44cb1a6b9d68dd255b2acd5 [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];
26 struct {
27 struct aead_request req;
28 u8 priv[crypto_aead_reqsize(tfm)];
29 } aead_req;
Jiri Bencf0706e82007-05-05 11:45:53 -070030
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020031 memset(&aead_req, 0, sizeof(aead_req));
Harvey Harrison5fdae6b2008-07-02 16:30:53 -070032
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020033 sg_init_one(&pt, data, data_len);
34 sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
35 sg_init_table(ct, 2);
36 sg_set_buf(&ct[0], data, data_len);
37 sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -070038
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020039 aead_request_set_tfm(&aead_req.req, tfm);
40 aead_request_set_assoc(&aead_req.req, &assoc, assoc.length);
41 aead_request_set_crypt(&aead_req.req, &pt, ct, data_len, b_0);
Jiri Bencf0706e82007-05-05 11:45:53 -070042
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020043 crypto_aead_encrypt(&aead_req.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,
47 u8 *data, size_t data_len, u8 *mic)
Jiri Bencf0706e82007-05-05 11:45:53 -070048{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020049 struct scatterlist assoc, pt, ct[2];
50 struct {
51 struct aead_request req;
52 u8 priv[crypto_aead_reqsize(tfm)];
53 } aead_req;
Jiri Bencf0706e82007-05-05 11:45:53 -070054
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020055 memset(&aead_req, 0, sizeof(aead_req));
Jiri Bencf0706e82007-05-05 11:45:53 -070056
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020057 sg_init_one(&pt, data, data_len);
58 sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
59 sg_init_table(ct, 2);
60 sg_set_buf(&ct[0], data, data_len);
61 sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN);
Jiri Bencf0706e82007-05-05 11:45:53 -070062
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020063 aead_request_set_tfm(&aead_req.req, tfm);
64 aead_request_set_assoc(&aead_req.req, &assoc, assoc.length);
65 aead_request_set_crypt(&aead_req.req, ct, &pt,
66 data_len + IEEE80211_CCMP_MIC_LEN, b_0);
Jiri Bencf0706e82007-05-05 11:45:53 -070067
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020068 return crypto_aead_decrypt(&aead_req.req);
Jiri Bencf0706e82007-05-05 11:45:53 -070069}
70
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020071struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[])
Jiri Bencf0706e82007-05-05 11:45:53 -070072{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020073 struct crypto_aead *tfm;
74 int err;
Jiri Bencf0706e82007-05-05 11:45:53 -070075
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020076 tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
77 if (IS_ERR(tfm))
78 return tfm;
Jiri Bencf0706e82007-05-05 11:45:53 -070079
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020080 err = crypto_aead_setkey(tfm, key, WLAN_KEY_LEN_CCMP);
81 if (!err)
82 err = crypto_aead_setauthsize(tfm, IEEE80211_CCMP_MIC_LEN);
83 if (!err)
84 return tfm;
Jiri Bencf0706e82007-05-05 11:45:53 -070085
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020086 crypto_free_aead(tfm);
87 return ERR_PTR(err);
Jiri Bencf0706e82007-05-05 11:45:53 -070088}
89
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020090void ieee80211_aes_key_free(struct crypto_aead *tfm)
Jiri Bencf0706e82007-05-05 11:45:53 -070091{
Ard Biesheuvel7ec7c4a2013-10-10 09:55:20 +020092 crypto_free_aead(tfm);
Jiri Bencf0706e82007-05-05 11:45:53 -070093}