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 | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 21 | int ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, |
| 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]; |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 26 | struct aead_request *aead_req; |
| 27 | int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm); |
| 28 | u8 *__aad; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 29 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 30 | aead_req = kzalloc(reqsize + CCM_AAD_LEN, GFP_ATOMIC); |
| 31 | if (!aead_req) |
| 32 | return -ENOMEM; |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 33 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 34 | __aad = (u8 *)aead_req + reqsize; |
| 35 | memcpy(__aad, aad, CCM_AAD_LEN); |
Harvey Harrison | 5fdae6b | 2008-07-02 16:30:53 -0700 | [diff] [blame] | 36 | |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 37 | sg_init_table(sg, 3); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 38 | sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad)); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 39 | sg_set_buf(&sg[1], data, data_len); |
| 40 | sg_set_buf(&sg[2], mic, mic_len); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 41 | |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 42 | aead_request_set_tfm(aead_req, tfm); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 43 | aead_request_set_crypt(aead_req, sg, sg, data_len, b_0); |
| 44 | aead_request_set_ad(aead_req, sg[0].length); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 45 | |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 46 | crypto_aead_encrypt(aead_req); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 47 | kzfree(aead_req); |
| 48 | |
| 49 | return 0; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 52 | 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] | 53 | u8 *data, size_t data_len, u8 *mic, |
| 54 | size_t mic_len) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 55 | { |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 56 | struct scatterlist sg[3]; |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 57 | struct aead_request *aead_req; |
| 58 | int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm); |
| 59 | u8 *__aad; |
| 60 | int err; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 61 | |
Ronald Wahl | 4f031fa | 2014-11-06 11:52:13 +0100 | [diff] [blame] | 62 | if (data_len == 0) |
| 63 | return -EINVAL; |
| 64 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 65 | aead_req = kzalloc(reqsize + CCM_AAD_LEN, GFP_ATOMIC); |
| 66 | if (!aead_req) |
| 67 | return -ENOMEM; |
| 68 | |
| 69 | __aad = (u8 *)aead_req + reqsize; |
| 70 | memcpy(__aad, aad, CCM_AAD_LEN); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 71 | |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 72 | sg_init_table(sg, 3); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 73 | sg_set_buf(&sg[0], &__aad[2], be16_to_cpup((__be16 *)__aad)); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 74 | sg_set_buf(&sg[1], data, data_len); |
| 75 | sg_set_buf(&sg[2], mic, mic_len); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 76 | |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 77 | aead_request_set_tfm(aead_req, tfm); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 78 | aead_request_set_crypt(aead_req, sg, sg, data_len + mic_len, b_0); |
| 79 | aead_request_set_ad(aead_req, sg[0].length); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 80 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 81 | err = crypto_aead_decrypt(aead_req); |
| 82 | kzfree(aead_req); |
| 83 | |
| 84 | return err; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jouni Malinen | 2b2ba0d | 2015-01-24 19:52:07 +0200 | [diff] [blame] | 87 | struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[], |
| 88 | size_t key_len, |
| 89 | size_t mic_len) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 90 | { |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 91 | struct crypto_aead *tfm; |
| 92 | int err; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 93 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 94 | tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC); |
| 95 | if (IS_ERR(tfm)) |
| 96 | return tfm; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 97 | |
Jouni Malinen | 2b2ba0d | 2015-01-24 19:52:07 +0200 | [diff] [blame] | 98 | err = crypto_aead_setkey(tfm, key, key_len); |
Dan Carpenter | 45fd632 | 2015-03-23 17:08:14 +0300 | [diff] [blame] | 99 | if (err) |
| 100 | goto free_aead; |
| 101 | err = crypto_aead_setauthsize(tfm, mic_len); |
| 102 | if (err) |
| 103 | goto free_aead; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 104 | |
Dan Carpenter | 45fd632 | 2015-03-23 17:08:14 +0300 | [diff] [blame] | 105 | return tfm; |
| 106 | |
| 107 | free_aead: |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 108 | crypto_free_aead(tfm); |
| 109 | return ERR_PTR(err); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 112 | void ieee80211_aes_key_free(struct crypto_aead *tfm) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 113 | { |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 114 | crypto_free_aead(tfm); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 115 | } |