aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-kernel/cryptodev/sdk_patches/0092-Support-skcipher-in-addition-to-ablkcipher-API.patch
blob: 4a82955e56b3716d30ad84a5dc79eb4b0a7f476d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
From e41e73551c886366d741b5e401a3c4c661aa3020 Mon Sep 17 00:00:00 2001
From: Michael Weiser <michael.weiser@gmx.de>
Date: Fri, 5 Aug 2016 17:26:27 +0200
Subject: [PATCH 092/104] Support skcipher in addition to ablkcipher API

The ablkcipher API is being phased out[1]. The unified skcipher API
seems to have made its entry with 4.3.[3, 4] By what can be seen from
migration patches[1.ff.], it's a drop-in replacement.

Also, deallocators such as crypto_free_skcipher() are NULL-safe now[2].

Add a new header cipherapi.h to aid migration from ablkcipher to skcipher and
retain support for old kernels. Make it decide which API to use and provide
appropriate function calls and type definitions. Since the ablkcipher and
skcipher APIs are so similar, those are mainly defines for corresponding
pseudo-functions in namespace cryptodev_ derived directly from their API
counterparts.

Compiles and works (i.e. checks pass) with Debian testing 4.6.4 kernel
as well as 4.8-rc2+ Linus git tree as of today. (Both require a fix for
changed page access API[5].)

[1] https://www.spinics.net/lists/linux-crypto/msg18133.html
[2] https://www.spinics.net/lists/linux-crypto/msg18154.html, line 120
[3] https://www.spinics.net/lists/linux-crypto/msg16373.html
[4] https://www.spinics.net/lists/linux-crypto/msg16294.html
[5] https://github.com/cryptodev-linux/cryptodev-linux/pull/14
---
 cipherapi.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 cryptlib.c  | 40 ++++++++++++++++++----------------------
 cryptlib.h  |  6 ++++--
 ioctl.c     |  4 ++--
 4 files changed, 84 insertions(+), 26 deletions(-)
 create mode 100644 cipherapi.h

diff --git a/cipherapi.h b/cipherapi.h
new file mode 100644
index 0000000..07d9923
--- /dev/null
+++ b/cipherapi.h
@@ -0,0 +1,60 @@
+#ifndef CIPHERAPI_H
+# define CIPHERAPI_H
+
+#include <linux/version.h>
+
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0))
+# include <linux/crypto.h>
+
+typedef struct ablkcipher_alg cryptodev_blkcipher_alg_t;
+typedef struct crypto_ablkcipher cryptodev_crypto_blkcipher_t;
+typedef struct ablkcipher_request cryptodev_blkcipher_request_t;
+
+# define cryptodev_crypto_alloc_blkcipher crypto_alloc_ablkcipher
+# define cryptodev_crypto_blkcipher_alg crypto_ablkcipher_alg
+# define cryptodev_crypto_blkcipher_blocksize crypto_ablkcipher_blocksize
+# define cryptodev_crypto_blkcipher_ivsize crypto_ablkcipher_ivsize
+# define cryptodev_crypto_blkcipher_alignmask crypto_ablkcipher_alignmask
+# define cryptodev_crypto_blkcipher_setkey crypto_ablkcipher_setkey
+
+static inline void cryptodev_crypto_free_blkcipher(cryptodev_crypto_blkcipher_t *c) {
+	if (c)
+		crypto_free_ablkcipher(c);
+}
+
+# define cryptodev_blkcipher_request_alloc ablkcipher_request_alloc
+# define cryptodev_blkcipher_request_set_callback ablkcipher_request_set_callback
+
+static inline void cryptodev_blkcipher_request_free(cryptodev_blkcipher_request_t *r) {
+	if (r)
+		ablkcipher_request_free(r);
+}
+
+# define cryptodev_blkcipher_request_set_crypt ablkcipher_request_set_crypt
+# define cryptodev_crypto_blkcipher_encrypt crypto_ablkcipher_encrypt
+# define cryptodev_crypto_blkcipher_decrypt crypto_ablkcipher_decrypt
+# define cryptodev_crypto_blkcipher_tfm crypto_ablkcipher_tfm
+#else
+#include <crypto/skcipher.h>
+
+typedef struct skcipher_alg cryptodev_blkcipher_alg_t;
+typedef struct crypto_skcipher cryptodev_crypto_blkcipher_t;
+typedef struct skcipher_request cryptodev_blkcipher_request_t;
+
+# define cryptodev_crypto_alloc_blkcipher crypto_alloc_skcipher
+# define cryptodev_crypto_blkcipher_alg crypto_skcipher_alg
+# define cryptodev_crypto_blkcipher_blocksize crypto_skcipher_blocksize
+# define cryptodev_crypto_blkcipher_ivsize crypto_skcipher_ivsize
+# define cryptodev_crypto_blkcipher_alignmask crypto_skcipher_alignmask
+# define cryptodev_crypto_blkcipher_setkey crypto_skcipher_setkey
+# define cryptodev_crypto_free_blkcipher crypto_free_skcipher
+# define cryptodev_blkcipher_request_alloc skcipher_request_alloc
+# define cryptodev_blkcipher_request_set_callback skcipher_request_set_callback
+# define cryptodev_blkcipher_request_free skcipher_request_free
+# define cryptodev_blkcipher_request_set_crypt skcipher_request_set_crypt
+# define cryptodev_crypto_blkcipher_encrypt crypto_skcipher_encrypt
+# define cryptodev_crypto_blkcipher_decrypt crypto_skcipher_decrypt
+# define cryptodev_crypto_blkcipher_tfm crypto_skcipher_tfm
+#endif
+
+#endif
diff --git a/cryptlib.c b/cryptlib.c
index 5d1a5a9..558d4b8 100644
--- a/cryptlib.c
+++ b/cryptlib.c
@@ -24,7 +24,6 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 
-#include <linux/crypto.h>
 #include <linux/mm.h>
 #include <linux/highmem.h>
 #include <linux/ioctl.h>
@@ -38,6 +37,7 @@
 #include <linux/rtnetlink.h>
 #include <crypto/authenc.h>
 #include "cryptodev_int.h"
+#include "cipherapi.h"
 
 
 static void cryptodev_complete(struct crypto_async_request *req, int err)
@@ -129,15 +129,15 @@ int cryptodev_cipher_init(struct cipher_data *out, const char *alg_name,
 	int ret;
 
 	if (aead == 0) {
-		struct ablkcipher_alg *alg;
+		cryptodev_blkcipher_alg_t *alg;
 
-		out->async.s = crypto_alloc_ablkcipher(alg_name, 0, 0);
+		out->async.s = cryptodev_crypto_alloc_blkcipher(alg_name, 0, 0);
 		if (unlikely(IS_ERR(out->async.s))) {
 			ddebug(1, "Failed to load cipher %s", alg_name);
 				return -EINVAL;
 		}
 
-		alg = crypto_ablkcipher_alg(out->async.s);
+		alg = cryptodev_crypto_blkcipher_alg(out->async.s);
 		if (alg != NULL) {
 			/* Was correct key length supplied? */
 			if (alg->max_keysize > 0 &&
@@ -150,11 +150,11 @@ int cryptodev_cipher_init(struct cipher_data *out, const char *alg_name,
 			}
 		}
 
-		out->blocksize = crypto_ablkcipher_blocksize(out->async.s);
-		out->ivsize = crypto_ablkcipher_ivsize(out->async.s);
-		out->alignmask = crypto_ablkcipher_alignmask(out->async.s);
+		out->blocksize = cryptodev_crypto_blkcipher_blocksize(out->async.s);
+		out->ivsize = cryptodev_crypto_blkcipher_ivsize(out->async.s);
+		out->alignmask = cryptodev_crypto_blkcipher_alignmask(out->async.s);
 
-		ret = crypto_ablkcipher_setkey(out->async.s, keyp, keylen);
+		ret = cryptodev_crypto_blkcipher_setkey(out->async.s, keyp, keylen);
 	} else {
 		out->async.as = crypto_alloc_aead(alg_name, 0, 0);
 		if (unlikely(IS_ERR(out->async.as))) {
@@ -181,14 +181,14 @@ int cryptodev_cipher_init(struct cipher_data *out, const char *alg_name,
 	init_completion(&out->async.result.completion);
 
 	if (aead == 0) {
-		out->async.request = ablkcipher_request_alloc(out->async.s, GFP_KERNEL);
+		out->async.request = cryptodev_blkcipher_request_alloc(out->async.s, GFP_KERNEL);
 		if (unlikely(!out->async.request)) {
 			derr(1, "error allocating async crypto request");
 			ret = -ENOMEM;
 			goto error;
 		}
 
-		ablkcipher_request_set_callback(out->async.request, 0,
+		cryptodev_blkcipher_request_set_callback(out->async.request, 0,
 					cryptodev_complete, &out->async.result);
 	} else {
 		out->async.arequest = aead_request_alloc(out->async.as, GFP_KERNEL);
@@ -206,10 +206,8 @@ int cryptodev_cipher_init(struct cipher_data *out, const char *alg_name,
 	return 0;
 error:
 	if (aead == 0) {
-		if (out->async.request)
-			ablkcipher_request_free(out->async.request);
-		if (out->async.s)
-			crypto_free_ablkcipher(out->async.s);
+		cryptodev_blkcipher_request_free(out->async.request);
+		cryptodev_crypto_free_blkcipher(out->async.s);
 	} else {
 		if (out->async.arequest)
 			aead_request_free(out->async.arequest);
@@ -224,10 +222,8 @@ void cryptodev_cipher_deinit(struct cipher_data *cdata)
 {
 	if (cdata->init) {
 		if (cdata->aead == 0) {
-			if (cdata->async.request)
-				ablkcipher_request_free(cdata->async.request);
-			if (cdata->async.s)
-				crypto_free_ablkcipher(cdata->async.s);
+			cryptodev_blkcipher_request_free(cdata->async.request);
+			cryptodev_crypto_free_blkcipher(cdata->async.s);
 		} else {
 			if (cdata->async.arequest)
 				aead_request_free(cdata->async.arequest);
@@ -274,10 +270,10 @@ ssize_t cryptodev_cipher_encrypt(struct cipher_data *cdata,
 	reinit_completion(&cdata->async.result.completion);
 
 	if (cdata->aead == 0) {
-		ablkcipher_request_set_crypt(cdata->async.request,
+		cryptodev_blkcipher_request_set_crypt(cdata->async.request,
 			(struct scatterlist *)src, dst,
 			len, cdata->async.iv);
-		ret = crypto_ablkcipher_encrypt(cdata->async.request);
+		ret = cryptodev_crypto_blkcipher_encrypt(cdata->async.request);
 	} else {
 		aead_request_set_crypt(cdata->async.arequest,
 			(struct scatterlist *)src, dst,
@@ -296,10 +292,10 @@ ssize_t cryptodev_cipher_decrypt(struct cipher_data *cdata,
 
 	reinit_completion(&cdata->async.result.completion);
 	if (cdata->aead == 0) {
-		ablkcipher_request_set_crypt(cdata->async.request,
+		cryptodev_blkcipher_request_set_crypt(cdata->async.request,
 			(struct scatterlist *)src, dst,
 			len, cdata->async.iv);
-		ret = crypto_ablkcipher_decrypt(cdata->async.request);
+		ret = cryptodev_crypto_blkcipher_decrypt(cdata->async.request);
 	} else {
 		aead_request_set_crypt(cdata->async.arequest,
 			(struct scatterlist *)src, dst,
diff --git a/cryptlib.h b/cryptlib.h
index d8e8046..8200a1d 100644
--- a/cryptlib.h
+++ b/cryptlib.h
@@ -11,6 +11,8 @@ struct cryptodev_result {
 	int err;
 };
 
+#include "cipherapi.h"
+
 struct cipher_data {
 	int init; /* 0 uninitialized */
 	int blocksize;
@@ -20,8 +22,8 @@ struct cipher_data {
 	int alignmask;
 	struct {
 		/* block ciphers */
-		struct crypto_ablkcipher *s;
-		struct ablkcipher_request *request;
+		cryptodev_crypto_blkcipher_t *s;
+		cryptodev_blkcipher_request_t *request;
 
 		/* AEAD ciphers */
 		struct crypto_aead *as;
diff --git a/ioctl.c b/ioctl.c
index 2e2bdeb..e3b8af1 100644
--- a/ioctl.c
+++ b/ioctl.c
@@ -35,7 +35,6 @@
  */
 
 #include <crypto/hash.h>
-#include <linux/crypto.h>
 #include <linux/mm.h>
 #include <linux/highmem.h>
 #include <linux/ioctl.h>
@@ -54,6 +53,7 @@
 #include "cryptodev_int.h"
 #include "zc.h"
 #include "version.h"
+#include "cipherapi.h"
 
 MODULE_AUTHOR("Nikos Mavrogiannopoulos <nmav@gnutls.org>");
 MODULE_DESCRIPTION("CryptoDev driver");
@@ -1052,7 +1052,7 @@ static int get_session_info(struct fcrypt *fcr, struct session_info_op *siop)
 
 	if (ses_ptr->cdata.init) {
 		if (ses_ptr->cdata.aead == 0)
-			tfm = crypto_ablkcipher_tfm(ses_ptr->cdata.async.s);
+			tfm = cryptodev_crypto_blkcipher_tfm(ses_ptr->cdata.async.s);
 		else
 			tfm = crypto_aead_tfm(ses_ptr->cdata.async.as);
 		tfm_info_to_alg_info(&siop->cipher_info, tfm);
-- 
2.10.2