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/err.h>
15 #include <crypto/aead.h>
17 #include <net/mac80211.h>
21 void ieee80211_aes_ccm_encrypt(struct crypto_aead
*tfm
, u8
*b_0
, u8
*aad
,
22 u8
*data
, size_t data_len
, u8
*mic
,
25 struct scatterlist sg
[3];
27 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
;
32 memset(aead_req
, 0, sizeof(aead_req_data
));
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
);
39 aead_request_set_tfm(aead_req
, tfm
);
40 aead_request_set_crypt(aead_req
, sg
, sg
, data_len
, b_0
);
41 aead_request_set_ad(aead_req
, sg
[0].length
);
43 crypto_aead_encrypt(aead_req
);
46 int ieee80211_aes_ccm_decrypt(struct crypto_aead
*tfm
, u8
*b_0
, u8
*aad
,
47 u8
*data
, size_t data_len
, u8
*mic
,
50 struct scatterlist sg
[3];
51 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
;
59 memset(aead_req
, 0, sizeof(aead_req_data
));
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
);
66 aead_request_set_tfm(aead_req
, tfm
);
67 aead_request_set_crypt(aead_req
, sg
, sg
, data_len
+ mic_len
, b_0
);
68 aead_request_set_ad(aead_req
, sg
[0].length
);
70 return crypto_aead_decrypt(aead_req
);
73 struct crypto_aead
*ieee80211_aes_key_setup_encrypt(const u8 key
[],
77 struct crypto_aead
*tfm
;
80 tfm
= crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC
);
84 err
= crypto_aead_setkey(tfm
, key
, key_len
);
87 err
= crypto_aead_setauthsize(tfm
, mic_len
);
94 crypto_free_aead(tfm
);
98 void ieee80211_aes_key_free(struct crypto_aead
*tfm
)
100 crypto_free_aead(tfm
);