Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2003-2004, Instant802 Networks, Inc. |
| 3 | * Copyright 2005-2006, Devicescape Software, Inc. |
| 4 | * |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 5 | * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org> |
| 6 | * |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 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. |
| 10 | */ |
| 11 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 13 | #include <linux/types.h> |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 14 | #include <linux/err.h> |
Herbert Xu | d8fe0dd | 2015-04-22 15:06:32 +0800 | [diff] [blame] | 15 | #include <crypto/aead.h> |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 16 | |
| 17 | #include <net/mac80211.h> |
Johannes Berg | 2c8dccc | 2008-04-08 15:14:40 -0400 | [diff] [blame] | 18 | #include "key.h" |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 19 | #include "aes_ccm.h" |
| 20 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 21 | void ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, |
Jouni Malinen | 2b2ba0d | 2015-01-24 19:52:07 +0200 | [diff] [blame] | 22 | u8 *data, size_t data_len, u8 *mic, |
| 23 | size_t mic_len) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 24 | { |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 25 | struct scatterlist sg[3]; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 26 | |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 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; |
| 31 | |
| 32 | memset(aead_req, 0, sizeof(aead_req_data)); |
Harvey Harrison | 5fdae6b | 2008-07-02 16:30:53 -0700 | [diff] [blame] | 33 | |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 34 | 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 Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 38 | |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 39 | aead_request_set_tfm(aead_req, tfm); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 40 | aead_request_set_crypt(aead_req, sg, sg, data_len, b_0); |
| 41 | aead_request_set_ad(aead_req, sg[0].length); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 42 | |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 43 | crypto_aead_encrypt(aead_req); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 46 | int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, |
Jouni Malinen | 2b2ba0d | 2015-01-24 19:52:07 +0200 | [diff] [blame] | 47 | u8 *data, size_t data_len, u8 *mic, |
| 48 | size_t mic_len) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 49 | { |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 50 | struct scatterlist sg[3]; |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 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; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 55 | |
Ronald Wahl | 4f031fa | 2014-11-06 11:52:13 +0100 | [diff] [blame] | 56 | if (data_len == 0) |
| 57 | return -EINVAL; |
| 58 | |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 59 | memset(aead_req, 0, sizeof(aead_req_data)); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 60 | |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 61 | 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 Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 65 | |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 66 | aead_request_set_tfm(aead_req, tfm); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 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); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 69 | |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 70 | return crypto_aead_decrypt(aead_req); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jouni Malinen | 2b2ba0d | 2015-01-24 19:52:07 +0200 | [diff] [blame] | 73 | struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[], |
| 74 | size_t key_len, |
| 75 | size_t mic_len) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 76 | { |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 77 | struct crypto_aead *tfm; |
| 78 | int err; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 79 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 80 | tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC); |
| 81 | if (IS_ERR(tfm)) |
| 82 | return tfm; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 83 | |
Jouni Malinen | 2b2ba0d | 2015-01-24 19:52:07 +0200 | [diff] [blame] | 84 | err = crypto_aead_setkey(tfm, key, key_len); |
Dan Carpenter | 45fd632 | 2015-03-23 17:08:14 +0300 | [diff] [blame] | 85 | if (err) |
| 86 | goto free_aead; |
| 87 | err = crypto_aead_setauthsize(tfm, mic_len); |
| 88 | if (err) |
| 89 | goto free_aead; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 90 | |
Dan Carpenter | 45fd632 | 2015-03-23 17:08:14 +0300 | [diff] [blame] | 91 | return tfm; |
| 92 | |
| 93 | free_aead: |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 94 | crypto_free_aead(tfm); |
| 95 | return ERR_PTR(err); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 98 | void ieee80211_aes_key_free(struct crypto_aead *tfm) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 99 | { |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 100 | crypto_free_aead(tfm); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 101 | } |