Commit | Line | Data |
---|---|---|
f0706e82 JB |
1 | /* |
2 | * Copyright 2003-2004, Instant802 Networks, Inc. | |
3 | * Copyright 2005-2006, Devicescape Software, Inc. | |
4133da73 | 4 | * Copyright 2014-2015, Qualcomm Atheros, Inc. |
f0706e82 | 5 | * |
7ec7c4a9 AB |
6 | * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org> |
7 | * | |
f0706e82 JB |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | ||
172589cc | 13 | #include <linux/kernel.h> |
f0706e82 | 14 | #include <linux/types.h> |
f0706e82 | 15 | #include <linux/err.h> |
4133da73 | 16 | #include <linux/scatterlist.h> |
d8fe0ddd | 17 | #include <crypto/aead.h> |
f0706e82 | 18 | |
4133da73 | 19 | #include "aead_api.h" |
f0706e82 | 20 | |
4133da73 XG |
21 | int aead_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, size_t aad_len, |
22 | u8 *data, size_t data_len, u8 *mic) | |
f0706e82 | 23 | { |
9e97964d | 24 | size_t mic_len = crypto_aead_authsize(tfm); |
957e0fe6 | 25 | struct scatterlist sg[3]; |
f4a067f9 AB |
26 | struct aead_request *aead_req; |
27 | int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm); | |
28 | u8 *__aad; | |
f0706e82 | 29 | |
4133da73 | 30 | aead_req = kzalloc(reqsize + aad_len, GFP_ATOMIC); |
f4a067f9 AB |
31 | if (!aead_req) |
32 | return -ENOMEM; | |
6e1ee5d2 | 33 | |
f4a067f9 | 34 | __aad = (u8 *)aead_req + reqsize; |
4133da73 | 35 | memcpy(__aad, aad, aad_len); |
f0706e82 | 36 | |
957e0fe6 | 37 | sg_init_table(sg, 3); |
4133da73 | 38 | sg_set_buf(&sg[0], __aad, aad_len); |
957e0fe6 HX |
39 | sg_set_buf(&sg[1], data, data_len); |
40 | sg_set_buf(&sg[2], mic, mic_len); | |
f0706e82 | 41 | |
6e1ee5d2 | 42 | aead_request_set_tfm(aead_req, tfm); |
957e0fe6 HX |
43 | aead_request_set_crypt(aead_req, sg, sg, data_len, b_0); |
44 | aead_request_set_ad(aead_req, sg[0].length); | |
f0706e82 | 45 | |
6e1ee5d2 | 46 | crypto_aead_encrypt(aead_req); |
f4a067f9 AB |
47 | kzfree(aead_req); |
48 | ||
49 | return 0; | |
f0706e82 JB |
50 | } |
51 | ||
4133da73 XG |
52 | int aead_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad, size_t aad_len, |
53 | u8 *data, size_t data_len, u8 *mic) | |
f0706e82 | 54 | { |
9e97964d | 55 | size_t mic_len = crypto_aead_authsize(tfm); |
957e0fe6 | 56 | struct scatterlist sg[3]; |
f4a067f9 AB |
57 | struct aead_request *aead_req; |
58 | int reqsize = sizeof(*aead_req) + crypto_aead_reqsize(tfm); | |
59 | u8 *__aad; | |
60 | int err; | |
7ec7c4a9 | 61 | |
4f031fa9 RW |
62 | if (data_len == 0) |
63 | return -EINVAL; | |
64 | ||
4133da73 | 65 | aead_req = kzalloc(reqsize + aad_len, GFP_ATOMIC); |
f4a067f9 AB |
66 | if (!aead_req) |
67 | return -ENOMEM; | |
68 | ||
69 | __aad = (u8 *)aead_req + reqsize; | |
4133da73 | 70 | memcpy(__aad, aad, aad_len); |
7ec7c4a9 | 71 | |
957e0fe6 | 72 | sg_init_table(sg, 3); |
4133da73 | 73 | sg_set_buf(&sg[0], __aad, aad_len); |
957e0fe6 HX |
74 | sg_set_buf(&sg[1], data, data_len); |
75 | sg_set_buf(&sg[2], mic, mic_len); | |
7ec7c4a9 | 76 | |
6e1ee5d2 | 77 | aead_request_set_tfm(aead_req, tfm); |
957e0fe6 HX |
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); | |
7ec7c4a9 | 80 | |
f4a067f9 AB |
81 | err = crypto_aead_decrypt(aead_req); |
82 | kzfree(aead_req); | |
83 | ||
84 | return err; | |
f0706e82 JB |
85 | } |
86 | ||
4133da73 XG |
87 | struct crypto_aead * |
88 | aead_key_setup_encrypt(const char *alg, const u8 key[], | |
89 | size_t key_len, size_t mic_len) | |
f0706e82 | 90 | { |
7ec7c4a9 AB |
91 | struct crypto_aead *tfm; |
92 | int err; | |
f0706e82 | 93 | |
4133da73 | 94 | tfm = crypto_alloc_aead(alg, 0, CRYPTO_ALG_ASYNC); |
7ec7c4a9 AB |
95 | if (IS_ERR(tfm)) |
96 | return tfm; | |
f0706e82 | 97 | |
2b2ba0db | 98 | err = crypto_aead_setkey(tfm, key, key_len); |
45fd6329 DC |
99 | if (err) |
100 | goto free_aead; | |
101 | err = crypto_aead_setauthsize(tfm, mic_len); | |
102 | if (err) | |
103 | goto free_aead; | |
104 | ||
105 | return tfm; | |
f0706e82 | 106 | |
45fd6329 | 107 | free_aead: |
7ec7c4a9 AB |
108 | crypto_free_aead(tfm); |
109 | return ERR_PTR(err); | |
f0706e82 JB |
110 | } |
111 | ||
4133da73 | 112 | void aead_key_free(struct crypto_aead *tfm) |
f0706e82 | 113 | { |
7ec7c4a9 | 114 | crypto_free_aead(tfm); |
f0706e82 | 115 | } |