Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2003-2004, Instant802 Networks, Inc. |
| 4 | * Copyright 2005-2006, Devicescape Software, Inc. |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 5 | * Copyright 2014-2015, Qualcomm Atheros, Inc. |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 6 | * |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 7 | * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org> |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 11 | #include <linux/types.h> |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 12 | #include <linux/err.h> |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 13 | #include <linux/scatterlist.h> |
Herbert Xu | d8fe0dd | 2015-04-22 15:06:32 +0800 | [diff] [blame] | 14 | #include <crypto/aead.h> |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 15 | |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 16 | #include "aead_api.h" |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 17 | |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 18 | int aead_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, size_t aad_len, |
| 19 | u8 *data, size_t data_len, u8 *mic) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 20 | { |
Johannes Berg | 9e97964 | 2017-10-11 15:46:45 +0200 | [diff] [blame] | 21 | size_t mic_len = crypto_aead_authsize(tfm); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 22 | struct scatterlist sg[3]; |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 23 | struct aead_request *aead_req; |
| 24 | int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm); |
| 25 | u8 *__aad; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 26 | |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 27 | aead_req = kzalloc(reqsize + aad_len, GFP_ATOMIC); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 28 | if (!aead_req) |
| 29 | return -ENOMEM; |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 30 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 31 | __aad = (u8 *)aead_req + reqsize; |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 32 | memcpy(__aad, aad, aad_len); |
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); |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 35 | sg_set_buf(&sg[0], __aad, aad_len); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 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); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 44 | kzfree(aead_req); |
| 45 | |
| 46 | return 0; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 49 | int aead_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, size_t aad_len, |
| 50 | u8 *data, size_t data_len, u8 *mic) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 51 | { |
Johannes Berg | 9e97964 | 2017-10-11 15:46:45 +0200 | [diff] [blame] | 52 | size_t mic_len = crypto_aead_authsize(tfm); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 53 | struct scatterlist sg[3]; |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 54 | struct aead_request *aead_req; |
| 55 | int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm); |
| 56 | u8 *__aad; |
| 57 | int err; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 58 | |
Ronald Wahl | 4f031fa | 2014-11-06 11:52:13 +0100 | [diff] [blame] | 59 | if (data_len == 0) |
| 60 | return -EINVAL; |
| 61 | |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 62 | aead_req = kzalloc(reqsize + aad_len, GFP_ATOMIC); |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 63 | if (!aead_req) |
| 64 | return -ENOMEM; |
| 65 | |
| 66 | __aad = (u8 *)aead_req + reqsize; |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 67 | memcpy(__aad, aad, aad_len); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 68 | |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 69 | sg_init_table(sg, 3); |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 70 | sg_set_buf(&sg[0], __aad, aad_len); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 71 | sg_set_buf(&sg[1], data, data_len); |
| 72 | sg_set_buf(&sg[2], mic, mic_len); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 73 | |
Jan-Simon Möller | 6e1ee5d | 2014-03-20 23:39:32 -0700 | [diff] [blame] | 74 | aead_request_set_tfm(aead_req, tfm); |
Herbert Xu | 957e0fe | 2015-05-27 16:03:50 +0800 | [diff] [blame] | 75 | aead_request_set_crypt(aead_req, sg, sg, data_len + mic_len, b_0); |
| 76 | aead_request_set_ad(aead_req, sg[0].length); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 77 | |
Ard Biesheuvel | f4a067f | 2016-10-17 15:05:33 +0100 | [diff] [blame] | 78 | err = crypto_aead_decrypt(aead_req); |
| 79 | kzfree(aead_req); |
| 80 | |
| 81 | return err; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 84 | struct crypto_aead * |
| 85 | aead_key_setup_encrypt(const char *alg, const u8 key[], |
| 86 | size_t key_len, size_t mic_len) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 87 | { |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 88 | struct crypto_aead *tfm; |
| 89 | int err; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 90 | |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 91 | tfm = crypto_alloc_aead(alg, 0, CRYPTO_ALG_ASYNC); |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 92 | if (IS_ERR(tfm)) |
| 93 | return tfm; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 94 | |
Jouni Malinen | 2b2ba0d | 2015-01-24 19:52:07 +0200 | [diff] [blame] | 95 | err = crypto_aead_setkey(tfm, key, key_len); |
Dan Carpenter | 45fd632 | 2015-03-23 17:08:14 +0300 | [diff] [blame] | 96 | if (err) |
| 97 | goto free_aead; |
| 98 | err = crypto_aead_setauthsize(tfm, mic_len); |
| 99 | if (err) |
| 100 | goto free_aead; |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 101 | |
Dan Carpenter | 45fd632 | 2015-03-23 17:08:14 +0300 | [diff] [blame] | 102 | return tfm; |
| 103 | |
| 104 | free_aead: |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 105 | crypto_free_aead(tfm); |
| 106 | return ERR_PTR(err); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Xiang Gao | 4133da7 | 2017-10-10 22:31:49 -0400 | [diff] [blame] | 109 | void aead_key_free(struct crypto_aead *tfm) |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 110 | { |
Ard Biesheuvel | 7ec7c4a | 2013-10-10 09:55:20 +0200 | [diff] [blame] | 111 | crypto_free_aead(tfm); |
Jiri Benc | f0706e8 | 2007-05-05 11:45:53 -0700 | [diff] [blame] | 112 | } |