2 * Copyright 2003-2004, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
5 * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org>
7 * 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.
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/crypto.h>
15 #include <linux/err.h>
16 #include <crypto/aes.h>
18 #include <net/mac80211.h>
22 void ieee80211_aes_ccm_encrypt(struct crypto_aead
*tfm
, u8
*b_0
, u8
*aad
,
23 u8
*data
, size_t data_len
, u8
*mic
,
26 struct scatterlist assoc
, pt
, ct
[2];
28 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
;
33 memset(aead_req
, 0, sizeof(aead_req_data
));
35 sg_init_one(&pt
, data
, data_len
);
36 sg_init_one(&assoc
, &aad
[2], be16_to_cpup((__be16
*)aad
));
38 sg_set_buf(&ct
[0], data
, data_len
);
39 sg_set_buf(&ct
[1], mic
, mic_len
);
41 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
);
45 crypto_aead_encrypt(aead_req
);
48 int ieee80211_aes_ccm_decrypt(struct crypto_aead
*tfm
, u8
*b_0
, u8
*aad
,
49 u8
*data
, size_t data_len
, u8
*mic
,
52 struct scatterlist assoc
, pt
, ct
[2];
53 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
;
61 memset(aead_req
, 0, sizeof(aead_req_data
));
63 sg_init_one(&pt
, data
, data_len
);
64 sg_init_one(&assoc
, &aad
[2], be16_to_cpup((__be16
*)aad
));
66 sg_set_buf(&ct
[0], data
, data_len
);
67 sg_set_buf(&ct
[1], mic
, mic_len
);
69 aead_request_set_tfm(aead_req
, tfm
);
70 aead_request_set_assoc(aead_req
, &assoc
, assoc
.length
);
71 aead_request_set_crypt(aead_req
, ct
, &pt
, data_len
+ mic_len
, b_0
);
73 return crypto_aead_decrypt(aead_req
);
76 struct crypto_aead
*ieee80211_aes_key_setup_encrypt(const u8 key
[],
80 struct crypto_aead
*tfm
;
83 tfm
= crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC
);
87 err
= crypto_aead_setkey(tfm
, key
, key_len
);
89 err
= crypto_aead_setauthsize(tfm
, mic_len
);
93 crypto_free_aead(tfm
);
97 void ieee80211_aes_key_free(struct crypto_aead
*tfm
)
99 crypto_free_aead(tfm
);