ad557fa823d2382b15bc18b228e6bed54606aaa6
[openwrt/staging/blocktrron.git] /
1 From 223db480c54de8ec47b3b0b4c6066b58342a5ad4 Mon Sep 17 00:00:00 2001
2 From: Radu Alexe <radu.alexe@nxp.com>
3 Date: Wed, 3 May 2017 16:17:13 +0300
4 Subject: [PATCH] crypto: add support for TLS 1.0 record encryption
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This patch adds kernel support for encryption/decryption of TLS 1.0
10 records using block ciphers. Implementation is similar to authenc in the
11 sense that the base algorithms (AES, SHA1) are combined in a template to
12 produce TLS encapsulation frames. The composite algorithm will be called
13 "tls10(hmac(<digest>),cbc(<cipher>))". The cipher and hmac keys are
14 wrapped in the same format used by authenc.c.
15
16 Signed-off-by: Radu Alexe <radu.alexe@nxp.com>
17 Signed-off-by: Cristian Stoica <cristian.stoica@nxp.com>
18 Signed-off-by: Horia Geantă <horia.geanta@nxp.com>
19 ---
20 crypto/Kconfig | 20 ++
21 crypto/Makefile | 1 +
22 crypto/tcrypt.c | 3 +
23 crypto/testmgr.c | 238 ++++++++++++++++++++++
24 crypto/testmgr.h | 224 ++++++++++++++++++++
25 crypto/tls.c | 607 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
26 6 files changed, 1093 insertions(+)
27 create mode 100644 crypto/tls.c
28
29 --- a/crypto/Kconfig
30 +++ b/crypto/Kconfig
31 @@ -342,6 +342,26 @@ config CRYPTO_ECHAINIV
32 a sequence number xored with a salt. This is the default
33 algorithm for CBC.
34
35 +config CRYPTO_TLS
36 + tristate "TLS support"
37 + select CRYPTO_AEAD
38 + select CRYPTO_BLKCIPHER
39 + select CRYPTO_MANAGER
40 + select CRYPTO_HASH
41 + select CRYPTO_NULL
42 + select CRYPTO_AUTHENC
43 + help
44 + Support for TLS 1.0 record encryption and decryption
45 +
46 + This module adds support for encryption/decryption of TLS 1.0 frames
47 + using blockcipher algorithms. The name of the resulting algorithm is
48 + "tls10(hmac(<digest>),cbc(<cipher>))". By default, the generic base
49 + algorithms are used (e.g. aes-generic, sha1-generic), but hardware
50 + accelerated versions will be used automatically if available.
51 +
52 + User-space applications (OpenSSL, GnuTLS) can offload TLS 1.0
53 + operations through AF_ALG or cryptodev interfaces
54 +
55 comment "Block modes"
56
57 config CRYPTO_CBC
58 --- a/crypto/Makefile
59 +++ b/crypto/Makefile
60 @@ -143,6 +143,7 @@ obj-$(CONFIG_CRYPTO_CRC32) += crc32_gene
61 obj-$(CONFIG_CRYPTO_CRCT10DIF) += crct10dif_common.o crct10dif_generic.o
62 obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.o authencesn.o
63 obj-$(CONFIG_CRYPTO_LZO) += lzo.o lzo-rle.o
64 +obj-$(CONFIG_CRYPTO_TLS) += tls.o
65 obj-$(CONFIG_CRYPTO_LZ4) += lz4.o
66 obj-$(CONFIG_CRYPTO_LZ4HC) += lz4hc.o
67 obj-$(CONFIG_CRYPTO_XXHASH) += xxhash_generic.o
68 --- a/crypto/tcrypt.c
69 +++ b/crypto/tcrypt.c
70 @@ -2049,6 +2049,9 @@ static int do_test(const char *alg, u32
71 ret += tcrypt_test("cbc(sm4)");
72 ret += tcrypt_test("ctr(sm4)");
73 break;
74 + case 192:
75 + ret += tcrypt_test("tls10(hmac(sha1),cbc(aes))");
76 + break;
77 case 200:
78 test_cipher_speed("ecb(aes)", ENCRYPT, sec, NULL, 0,
79 speed_template_16_24_32);
80 --- a/crypto/testmgr.c
81 +++ b/crypto/testmgr.c
82 @@ -111,6 +111,13 @@ struct drbg_test_suite {
83 unsigned int count;
84 };
85
86 +struct tls_test_suite {
87 + struct {
88 + struct tls_testvec *vecs;
89 + unsigned int count;
90 + } enc, dec;
91 +};
92 +
93 struct akcipher_test_suite {
94 const struct akcipher_testvec *vecs;
95 unsigned int count;
96 @@ -135,6 +142,7 @@ struct alg_test_desc {
97 struct hash_test_suite hash;
98 struct cprng_test_suite cprng;
99 struct drbg_test_suite drbg;
100 + struct tls_test_suite tls;
101 struct akcipher_test_suite akcipher;
102 struct kpp_test_suite kpp;
103 } suite;
104 @@ -2294,6 +2302,227 @@ static int test_aead(const char *driver,
105 return 0;
106 }
107
108 +static int __test_tls(struct crypto_aead *tfm, int enc,
109 + struct tls_testvec *template, unsigned int tcount,
110 + const bool diff_dst)
111 +{
112 + const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
113 + unsigned int i, k, authsize;
114 + char *q;
115 + struct aead_request *req;
116 + struct scatterlist *sg;
117 + struct scatterlist *sgout;
118 + const char *e, *d;
119 + struct crypto_wait wait;
120 + void *input;
121 + void *output;
122 + void *assoc;
123 + char *iv;
124 + char *key;
125 + char *xbuf[XBUFSIZE];
126 + char *xoutbuf[XBUFSIZE];
127 + char *axbuf[XBUFSIZE];
128 + int ret = -ENOMEM;
129 +
130 + if (testmgr_alloc_buf(xbuf))
131 + goto out_noxbuf;
132 +
133 + if (diff_dst && testmgr_alloc_buf(xoutbuf))
134 + goto out_nooutbuf;
135 +
136 + if (testmgr_alloc_buf(axbuf))
137 + goto out_noaxbuf;
138 +
139 + iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
140 + if (!iv)
141 + goto out_noiv;
142 +
143 + key = kzalloc(MAX_KEYLEN, GFP_KERNEL);
144 + if (!key)
145 + goto out_nokey;
146 +
147 + sg = kmalloc(sizeof(*sg) * 8 * (diff_dst ? 2 : 1), GFP_KERNEL);
148 + if (!sg)
149 + goto out_nosg;
150 +
151 + sgout = sg + 8;
152 +
153 + d = diff_dst ? "-ddst" : "";
154 + e = enc ? "encryption" : "decryption";
155 +
156 + crypto_init_wait(&wait);
157 +
158 + req = aead_request_alloc(tfm, GFP_KERNEL);
159 + if (!req) {
160 + pr_err("alg: tls%s: Failed to allocate request for %s\n",
161 + d, algo);
162 + goto out;
163 + }
164 +
165 + aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
166 + crypto_req_done, &wait);
167 +
168 + for (i = 0; i < tcount; i++) {
169 + input = xbuf[0];
170 + assoc = axbuf[0];
171 +
172 + ret = -EINVAL;
173 + if (WARN_ON(template[i].ilen > PAGE_SIZE ||
174 + template[i].alen > PAGE_SIZE))
175 + goto out;
176 +
177 + memcpy(assoc, template[i].assoc, template[i].alen);
178 + memcpy(input, template[i].input, template[i].ilen);
179 +
180 + if (template[i].iv)
181 + memcpy(iv, template[i].iv, MAX_IVLEN);
182 + else
183 + memset(iv, 0, MAX_IVLEN);
184 +
185 + crypto_aead_clear_flags(tfm, ~0);
186 +
187 + if (template[i].klen > MAX_KEYLEN) {
188 + pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
189 + d, i, algo, template[i].klen, MAX_KEYLEN);
190 + ret = -EINVAL;
191 + goto out;
192 + }
193 + memcpy(key, template[i].key, template[i].klen);
194 +
195 + ret = crypto_aead_setkey(tfm, key, template[i].klen);
196 + if (!ret == template[i].fail) {
197 + pr_err("alg: tls%s: setkey failed on test %d for %s: flags=%x\n",
198 + d, i, algo, crypto_aead_get_flags(tfm));
199 + goto out;
200 + } else if (ret)
201 + continue;
202 +
203 + authsize = 20;
204 + ret = crypto_aead_setauthsize(tfm, authsize);
205 + if (ret) {
206 + pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
207 + d, authsize, i, algo);
208 + goto out;
209 + }
210 +
211 + k = !!template[i].alen;
212 + sg_init_table(sg, k + 1);
213 + sg_set_buf(&sg[0], assoc, template[i].alen);
214 + sg_set_buf(&sg[k], input, (enc ? template[i].rlen :
215 + template[i].ilen));
216 + output = input;
217 +
218 + if (diff_dst) {
219 + sg_init_table(sgout, k + 1);
220 + sg_set_buf(&sgout[0], assoc, template[i].alen);
221 +
222 + output = xoutbuf[0];
223 + sg_set_buf(&sgout[k], output,
224 + (enc ? template[i].rlen : template[i].ilen));
225 + }
226 +
227 + aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
228 + template[i].ilen, iv);
229 +
230 + aead_request_set_ad(req, template[i].alen);
231 +
232 + ret = crypto_wait_req(enc ? crypto_aead_encrypt(req)
233 + : crypto_aead_decrypt(req), &wait);
234 +
235 + switch (ret) {
236 + case 0:
237 + if (template[i].novrfy) {
238 + /* verification was supposed to fail */
239 + pr_err("alg: tls%s: %s failed on test %d for %s: ret was 0, expected -EBADMSG\n",
240 + d, e, i, algo);
241 + /* so really, we got a bad message */
242 + ret = -EBADMSG;
243 + goto out;
244 + }
245 + break;
246 + case -EBADMSG:
247 + /* verification failure was expected */
248 + if (template[i].novrfy)
249 + continue;
250 + /* fall through */
251 + default:
252 + pr_err("alg: tls%s: %s failed on test %d for %s: ret=%d\n",
253 + d, e, i, algo, -ret);
254 + goto out;
255 + }
256 +
257 + q = output;
258 + if (memcmp(q, template[i].result, template[i].rlen)) {
259 + pr_err("alg: tls%s: Test %d failed on %s for %s\n",
260 + d, i, e, algo);
261 + hexdump(q, template[i].rlen);
262 + pr_err("should be:\n");
263 + hexdump(template[i].result, template[i].rlen);
264 + ret = -EINVAL;
265 + goto out;
266 + }
267 + }
268 +
269 +out:
270 + aead_request_free(req);
271 +
272 + kfree(sg);
273 +out_nosg:
274 + kfree(key);
275 +out_nokey:
276 + kfree(iv);
277 +out_noiv:
278 + testmgr_free_buf(axbuf);
279 +out_noaxbuf:
280 + if (diff_dst)
281 + testmgr_free_buf(xoutbuf);
282 +out_nooutbuf:
283 + testmgr_free_buf(xbuf);
284 +out_noxbuf:
285 + return ret;
286 +}
287 +
288 +static int test_tls(struct crypto_aead *tfm, int enc,
289 + struct tls_testvec *template, unsigned int tcount)
290 +{
291 + int ret;
292 + /* test 'dst == src' case */
293 + ret = __test_tls(tfm, enc, template, tcount, false);
294 + if (ret)
295 + return ret;
296 + /* test 'dst != src' case */
297 + return __test_tls(tfm, enc, template, tcount, true);
298 +}
299 +
300 +static int alg_test_tls(const struct alg_test_desc *desc, const char *driver,
301 + u32 type, u32 mask)
302 +{
303 + struct crypto_aead *tfm;
304 + int err = 0;
305 +
306 + tfm = crypto_alloc_aead(driver, type, mask);
307 + if (IS_ERR(tfm)) {
308 + pr_err("alg: aead: Failed to load transform for %s: %ld\n",
309 + driver, PTR_ERR(tfm));
310 + return PTR_ERR(tfm);
311 + }
312 +
313 + if (desc->suite.tls.enc.vecs) {
314 + err = test_tls(tfm, ENCRYPT, desc->suite.tls.enc.vecs,
315 + desc->suite.tls.enc.count);
316 + if (err)
317 + goto out;
318 + }
319 +
320 + if (!err && desc->suite.tls.dec.vecs)
321 + err = test_tls(tfm, DECRYPT, desc->suite.tls.dec.vecs,
322 + desc->suite.tls.dec.count);
323 +
324 +out:
325 + crypto_free_aead(tfm);
326 + return err;
327 +}
328 +
329 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
330 u32 type, u32 mask)
331 {
332 @@ -5012,6 +5241,15 @@ static const struct alg_test_desc alg_te
333 .hash = __VECS(tgr192_tv_template)
334 }
335 }, {
336 + .alg = "tls10(hmac(sha1),cbc(aes))",
337 + .test = alg_test_tls,
338 + .suite = {
339 + .tls = {
340 + .enc = __VECS(tls_enc_tv_template),
341 + .dec = __VECS(tls_dec_tv_template)
342 + }
343 + }
344 + }, {
345 .alg = "vmac64(aes)",
346 .test = alg_test_hash,
347 .suite = {
348 --- a/crypto/testmgr.h
349 +++ b/crypto/testmgr.h
350 @@ -21,7 +21,12 @@
351 #define _CRYPTO_TESTMGR_H
352
353 #include <linux/oid_registry.h>
354 +#include <linux/netlink.h>
355
356 +#define MAX_DIGEST_SIZE 64
357 +#define MAX_TAP 8
358 +
359 +#define MAX_KEYLEN 160
360 #define MAX_IVLEN 32
361
362 /*
363 @@ -140,6 +145,20 @@ struct drbg_testvec {
364 size_t expectedlen;
365 };
366
367 +struct tls_testvec {
368 + char *key; /* wrapped keys for encryption and authentication */
369 + char *iv; /* initialization vector */
370 + char *input; /* input data */
371 + char *assoc; /* associated data: seq num, type, version, input len */
372 + char *result; /* result data */
373 + unsigned char fail; /* the test failure is expected */
374 + unsigned char novrfy; /* dec verification failure expected */
375 + unsigned char klen; /* key length */
376 + unsigned short ilen; /* input data length */
377 + unsigned short alen; /* associated data length */
378 + unsigned short rlen; /* result length */
379 +};
380 +
381 struct akcipher_testvec {
382 const unsigned char *key;
383 const unsigned char *params;
384 @@ -171,6 +190,211 @@ struct kpp_testvec {
385 static const char zeroed_string[48];
386
387 /*
388 + * TLS1.0 synthetic test vectors
389 + */
390 +static struct tls_testvec tls_enc_tv_template[] = {
391 + {
392 +#ifdef __LITTLE_ENDIAN
393 + .key = "\x08\x00" /* rta length */
394 + "\x01\x00" /* rta type */
395 +#else
396 + .key = "\x00\x08" /* rta length */
397 + "\x00\x01" /* rta type */
398 +#endif
399 + "\x00\x00\x00\x10" /* enc key length */
400 + "authenticationkey20benckeyis16_bytes",
401 + .klen = 8 + 20 + 16,
402 + .iv = "iv0123456789abcd",
403 + .input = "Single block msg",
404 + .ilen = 16,
405 + .assoc = "\x00\x01\x02\x03\x04\x05\x06\x07"
406 + "\x00\x03\x01\x00\x10",
407 + .alen = 13,
408 + .result = "\xd5\xac\xb\xd2\xac\xad\x3f\xb1"
409 + "\x59\x79\x1e\x91\x5f\x52\x14\x9c"
410 + "\xc0\x75\xd8\x4c\x97\x0f\x07\x73"
411 + "\xdc\x89\x47\x49\x49\xcb\x30\x6b"
412 + "\x1b\x45\x23\xa1\xd0\x51\xcf\x02"
413 + "\x2e\xa8\x5d\xa0\xfe\xca\x82\x61",
414 + .rlen = 16 + 20 + 12,
415 + }, {
416 +#ifdef __LITTLE_ENDIAN
417 + .key = "\x08\x00" /* rta length */
418 + "\x01\x00" /* rta type */
419 +#else
420 + .key = "\x00\x08" /* rta length */
421 + "\x00\x01" /* rta type */
422 +#endif
423 + "\x00\x00\x00\x10" /* enc key length */
424 + "authenticationkey20benckeyis16_bytes",
425 + .klen = 8 + 20 + 16,
426 + .iv = "iv0123456789abcd",
427 + .input = "",
428 + .ilen = 0,
429 + .assoc = "\x00\x01\x02\x03\x04\x05\x06\x07"
430 + "\x00\x03\x01\x00\x00",
431 + .alen = 13,
432 + .result = "\x58\x2a\x11\xc\x86\x8e\x4b\x67"
433 + "\x2d\x16\x26\x1a\xac\x4b\xe2\x1a"
434 + "\xe9\x6a\xcc\x4d\x6f\x79\x8a\x45"
435 + "\x1f\x4e\x27\xf2\xa7\x59\xb4\x5a",
436 + .rlen = 20 + 12,
437 + }, {
438 +#ifdef __LITTLE_ENDIAN
439 + .key = "\x08\x00" /* rta length */
440 + "\x01\x00" /* rta type */
441 +#else
442 + .key = "\x00\x08" /* rta length */
443 + "\x00\x01" /* rta type */
444 +#endif
445 + "\x00\x00\x00\x10" /* enc key length */
446 + "authenticationkey20benckeyis16_bytes",
447 + .klen = 8 + 20 + 16,
448 + .iv = "iv0123456789abcd",
449 + .input = "285 bytes plaintext285 bytes plaintext285 bytes"
450 + " plaintext285 bytes plaintext285 bytes plaintext285"
451 + " bytes plaintext285 bytes plaintext285 bytes"
452 + " plaintext285 bytes plaintext285 bytes plaintext285"
453 + " bytes plaintext285 bytes plaintext285 bytes"
454 + " plaintext285 bytes plaintext285 bytes plaintext285"
455 + " bytes plaintext285 bytes plaintext",
456 + .ilen = 285,
457 + .assoc = "\x00\x01\x02\x03\x04\x05\x06\x07"
458 + "\x00\x03\x01\x01\x1d",
459 + .alen = 13,
460 + .result = "\x80\x23\x82\x44\x14\x2a\x1d\x94\xc\xc2\x1d\xd"
461 + "\x3a\x32\x89\x4c\x57\x30\xa8\x89\x76\x46\xcc\x90"
462 + "\x1d\x88\xb8\xa6\x1a\x58\xe\x2d\xeb\x2c\xc7\x3a"
463 + "\x52\x4e\xdb\xb3\x1e\x83\x11\xf5\x3c\xce\x6e\x94"
464 + "\xd3\x26\x6a\x9a\xd\xbd\xc7\x98\xb9\xb3\x3a\x51"
465 + "\x1e\x4\x84\x8a\x8f\x54\x9a\x51\x69\x9c\xce\x31"
466 + "\x8d\x5d\x8b\xee\x5f\x70\xc\xc9\xb8\x50\x54\xf8"
467 + "\xb2\x4a\x7a\xcd\xeb\x7a\x82\x81\xc6\x41\xc8\x50"
468 + "\x91\x8d\xc8\xed\xcd\x40\x8f\x55\xd1\xec\xc9\xac"
469 + "\x15\x18\xf9\x20\xa0\xed\x18\xa1\xe3\x56\xe3\x14"
470 + "\xe5\xe8\x66\x63\x20\xed\xe4\x62\x9d\xa3\xa4\x1d"
471 + "\x81\x89\x18\xf2\x36\xae\xc8\x8a\x2b\xbc\xc3\xb8"
472 + "\x80\xf\x97\x21\x36\x39\x8\x84\x23\x18\x9e\x9c"
473 + "\x72\x32\x75\x2d\x2e\xf9\x60\xb\xe8\xcc\xd9\x74"
474 + "\x4\x1b\x8e\x99\xc1\x94\xee\xd0\xac\x4e\xfc\x7e"
475 + "\xf1\x96\xb3\xe7\x14\xb8\xf2\xc\x25\x97\x82\x6b"
476 + "\xbd\x0\x65\xab\x5c\xe3\x16\xfb\x68\xef\xea\x9d"
477 + "\xff\x44\x1d\x2a\x44\xf5\xc8\x56\x77\xb7\xbf\x13"
478 + "\xc8\x54\xdb\x92\xfe\x16\x4c\xbe\x18\xe9\xb\x8d"
479 + "\xb\xd4\x43\x58\x43\xaa\xf4\x3\x80\x97\x62\xd5"
480 + "\xdf\x3c\x28\xaa\xee\x48\x4b\x55\x41\x1b\x31\x2"
481 + "\xbe\xa0\x1c\xbd\xb7\x22\x2a\xe5\x53\x72\x73\x20"
482 + "\x44\x4f\xe6\x1\x2b\x34\x33\x11\x7d\xfb\x10\xc1"
483 + "\x66\x7c\xa6\xf4\x48\x36\x5e\x2\xda\x41\x4b\x3e"
484 + "\xe7\x80\x17\x17\xce\xf1\x3e\x6a\x8e\x26\xf3\xb7"
485 + "\x2b\x85\xd\x31\x8d\xba\x6c\x22\xb4\x28\x55\x7e"
486 + "\x2a\x9e\x26\xf1\x3d\x21\xac\x65",
487 + .rlen = 285 + 20 + 15,
488 + }
489 +};
490 +
491 +static struct tls_testvec tls_dec_tv_template[] = {
492 + {
493 +#ifdef __LITTLE_ENDIAN
494 + .key = "\x08\x00" /* rta length */
495 + "\x01\x00" /* rta type */
496 +#else
497 + .key = "\x00\x08" /* rta length */
498 + "\x00\x01" /* rta type */
499 +#endif
500 + "\x00\x00\x00\x10" /* enc key length */
501 + "authenticationkey20benckeyis16_bytes",
502 + .klen = 8 + 20 + 16,
503 + .iv = "iv0123456789abcd",
504 + .input = "\xd5\xac\xb\xd2\xac\xad\x3f\xb1"
505 + "\x59\x79\x1e\x91\x5f\x52\x14\x9c"
506 + "\xc0\x75\xd8\x4c\x97\x0f\x07\x73"
507 + "\xdc\x89\x47\x49\x49\xcb\x30\x6b"
508 + "\x1b\x45\x23\xa1\xd0\x51\xcf\x02"
509 + "\x2e\xa8\x5d\xa0\xfe\xca\x82\x61",
510 + .ilen = 16 + 20 + 12,
511 + .assoc = "\x00\x01\x02\x03\x04\x05\x06\x07"
512 + "\x00\x03\x01\x00\x30",
513 + .alen = 13,
514 + .result = "Single block msg",
515 + .rlen = 16,
516 + }, {
517 +#ifdef __LITTLE_ENDIAN
518 + .key = "\x08\x00" /* rta length */
519 + "\x01\x00" /* rta type */
520 +#else
521 + .key = "\x00\x08" /* rta length */
522 + "\x00\x01" /* rta type */
523 +#endif
524 + "\x00\x00\x00\x10" /* enc key length */
525 + "authenticationkey20benckeyis16_bytes",
526 + .klen = 8 + 20 + 16,
527 + .iv = "iv0123456789abcd",
528 + .input = "\x58\x2a\x11\xc\x86\x8e\x4b\x67"
529 + "\x2d\x16\x26\x1a\xac\x4b\xe2\x1a"
530 + "\xe9\x6a\xcc\x4d\x6f\x79\x8a\x45"
531 + "\x1f\x4e\x27\xf2\xa7\x59\xb4\x5a",
532 + .ilen = 20 + 12,
533 + .assoc = "\x00\x01\x02\x03\x04\x05\x06\x07"
534 + "\x00\x03\x01\x00\x20",
535 + .alen = 13,
536 + .result = "",
537 + .rlen = 0,
538 + }, {
539 +#ifdef __LITTLE_ENDIAN
540 + .key = "\x08\x00" /* rta length */
541 + "\x01\x00" /* rta type */
542 +#else
543 + .key = "\x00\x08" /* rta length */
544 + "\x00\x01" /* rta type */
545 +#endif
546 + "\x00\x00\x00\x10" /* enc key length */
547 + "authenticationkey20benckeyis16_bytes",
548 + .klen = 8 + 20 + 16,
549 + .iv = "iv0123456789abcd",
550 + .input = "\x80\x23\x82\x44\x14\x2a\x1d\x94\xc\xc2\x1d\xd"
551 + "\x3a\x32\x89\x4c\x57\x30\xa8\x89\x76\x46\xcc\x90"
552 + "\x1d\x88\xb8\xa6\x1a\x58\xe\x2d\xeb\x2c\xc7\x3a"
553 + "\x52\x4e\xdb\xb3\x1e\x83\x11\xf5\x3c\xce\x6e\x94"
554 + "\xd3\x26\x6a\x9a\xd\xbd\xc7\x98\xb9\xb3\x3a\x51"
555 + "\x1e\x4\x84\x8a\x8f\x54\x9a\x51\x69\x9c\xce\x31"
556 + "\x8d\x5d\x8b\xee\x5f\x70\xc\xc9\xb8\x50\x54\xf8"
557 + "\xb2\x4a\x7a\xcd\xeb\x7a\x82\x81\xc6\x41\xc8\x50"
558 + "\x91\x8d\xc8\xed\xcd\x40\x8f\x55\xd1\xec\xc9\xac"
559 + "\x15\x18\xf9\x20\xa0\xed\x18\xa1\xe3\x56\xe3\x14"
560 + "\xe5\xe8\x66\x63\x20\xed\xe4\x62\x9d\xa3\xa4\x1d"
561 + "\x81\x89\x18\xf2\x36\xae\xc8\x8a\x2b\xbc\xc3\xb8"
562 + "\x80\xf\x97\x21\x36\x39\x8\x84\x23\x18\x9e\x9c"
563 + "\x72\x32\x75\x2d\x2e\xf9\x60\xb\xe8\xcc\xd9\x74"
564 + "\x4\x1b\x8e\x99\xc1\x94\xee\xd0\xac\x4e\xfc\x7e"
565 + "\xf1\x96\xb3\xe7\x14\xb8\xf2\xc\x25\x97\x82\x6b"
566 + "\xbd\x0\x65\xab\x5c\xe3\x16\xfb\x68\xef\xea\x9d"
567 + "\xff\x44\x1d\x2a\x44\xf5\xc8\x56\x77\xb7\xbf\x13"
568 + "\xc8\x54\xdb\x92\xfe\x16\x4c\xbe\x18\xe9\xb\x8d"
569 + "\xb\xd4\x43\x58\x43\xaa\xf4\x3\x80\x97\x62\xd5"
570 + "\xdf\x3c\x28\xaa\xee\x48\x4b\x55\x41\x1b\x31\x2"
571 + "\xbe\xa0\x1c\xbd\xb7\x22\x2a\xe5\x53\x72\x73\x20"
572 + "\x44\x4f\xe6\x1\x2b\x34\x33\x11\x7d\xfb\x10\xc1"
573 + "\x66\x7c\xa6\xf4\x48\x36\x5e\x2\xda\x41\x4b\x3e"
574 + "\xe7\x80\x17\x17\xce\xf1\x3e\x6a\x8e\x26\xf3\xb7"
575 + "\x2b\x85\xd\x31\x8d\xba\x6c\x22\xb4\x28\x55\x7e"
576 + "\x2a\x9e\x26\xf1\x3d\x21\xac\x65",
577 +
578 + .ilen = 285 + 20 + 15,
579 + .assoc = "\x00\x01\x02\x03\x04\x05\x06\x07"
580 + "\x00\x03\x01\x01\x40",
581 + .alen = 13,
582 + .result = "285 bytes plaintext285 bytes plaintext285 bytes"
583 + " plaintext285 bytes plaintext285 bytes plaintext285"
584 + " bytes plaintext285 bytes plaintext285 bytes"
585 + " plaintext285 bytes plaintext285 bytes plaintext285"
586 + " bytes plaintext285 bytes plaintext285 bytes"
587 + " plaintext285 bytes plaintext285 bytes plaintext",
588 + .rlen = 285,
589 + }
590 +};
591 +
592 +/*
593 * RSA test vectors. Borrowed from openSSL.
594 */
595 static const struct akcipher_testvec rsa_tv_template[] = {
596 --- /dev/null
597 +++ b/crypto/tls.c
598 @@ -0,0 +1,607 @@
599 +/*
600 + * Copyright 2013 Freescale Semiconductor, Inc.
601 + * Copyright 2017 NXP Semiconductor, Inc.
602 + *
603 + * This program is free software; you can redistribute it and/or modify it
604 + * under the terms of the GNU General Public License as published by the Free
605 + * Software Foundation; either version 2 of the License, or (at your option)
606 + * any later version.
607 + *
608 + */
609 +
610 +#include <crypto/internal/aead.h>
611 +#include <crypto/internal/hash.h>
612 +#include <crypto/internal/skcipher.h>
613 +#include <crypto/authenc.h>
614 +#include <crypto/null.h>
615 +#include <crypto/scatterwalk.h>
616 +#include <linux/err.h>
617 +#include <linux/init.h>
618 +#include <linux/module.h>
619 +#include <linux/rtnetlink.h>
620 +
621 +struct tls_instance_ctx {
622 + struct crypto_ahash_spawn auth;
623 + struct crypto_skcipher_spawn enc;
624 +};
625 +
626 +struct crypto_tls_ctx {
627 + unsigned int reqoff;
628 + struct crypto_ahash *auth;
629 + struct crypto_skcipher *enc;
630 + struct crypto_sync_skcipher *null;
631 +};
632 +
633 +struct tls_request_ctx {
634 + /*
635 + * cryptlen holds the payload length in the case of encryption or
636 + * payload_len + icv_len + padding_len in case of decryption
637 + */
638 + unsigned int cryptlen;
639 + /* working space for partial results */
640 + struct scatterlist tmp[2];
641 + struct scatterlist cipher[2];
642 + struct scatterlist dst[2];
643 + char tail[];
644 +};
645 +
646 +struct async_op {
647 + struct completion completion;
648 + int err;
649 +};
650 +
651 +static void tls_async_op_done(struct crypto_async_request *req, int err)
652 +{
653 + struct async_op *areq = req->data;
654 +
655 + if (err == -EINPROGRESS)
656 + return;
657 +
658 + areq->err = err;
659 + complete(&areq->completion);
660 +}
661 +
662 +static int crypto_tls_setkey(struct crypto_aead *tls, const u8 *key,
663 + unsigned int keylen)
664 +{
665 + struct crypto_tls_ctx *ctx = crypto_aead_ctx(tls);
666 + struct crypto_ahash *auth = ctx->auth;
667 + struct crypto_skcipher *enc = ctx->enc;
668 + struct crypto_authenc_keys keys;
669 + int err = -EINVAL;
670 +
671 + if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
672 + goto badkey;
673 +
674 + crypto_ahash_clear_flags(auth, CRYPTO_TFM_REQ_MASK);
675 + crypto_ahash_set_flags(auth, crypto_aead_get_flags(tls) &
676 + CRYPTO_TFM_REQ_MASK);
677 + err = crypto_ahash_setkey(auth, keys.authkey, keys.authkeylen);
678 + crypto_aead_set_flags(tls, crypto_ahash_get_flags(auth) &
679 + CRYPTO_TFM_RES_MASK);
680 +
681 + if (err)
682 + goto out;
683 +
684 + crypto_skcipher_clear_flags(enc, CRYPTO_TFM_REQ_MASK);
685 + crypto_skcipher_set_flags(enc, crypto_aead_get_flags(tls) &
686 + CRYPTO_TFM_REQ_MASK);
687 + err = crypto_skcipher_setkey(enc, keys.enckey, keys.enckeylen);
688 + crypto_aead_set_flags(tls, crypto_skcipher_get_flags(enc) &
689 + CRYPTO_TFM_RES_MASK);
690 +
691 +out:
692 + return err;
693 +
694 +badkey:
695 + crypto_aead_set_flags(tls, CRYPTO_TFM_RES_BAD_KEY_LEN);
696 + goto out;
697 +}
698 +
699 +/**
700 + * crypto_tls_genicv - Calculate hmac digest for a TLS record
701 + * @hash: (output) buffer to save the digest into
702 + * @src: (input) scatterlist with the assoc and payload data
703 + * @srclen: (input) size of the source buffer (assoclen + cryptlen)
704 + * @req: (input) aead request
705 + **/
706 +static int crypto_tls_genicv(u8 *hash, struct scatterlist *src,
707 + unsigned int srclen, struct aead_request *req)
708 +{
709 + struct crypto_aead *tls = crypto_aead_reqtfm(req);
710 + struct crypto_tls_ctx *ctx = crypto_aead_ctx(tls);
711 + struct tls_request_ctx *treq_ctx = aead_request_ctx(req);
712 + struct async_op ahash_op;
713 + struct ahash_request *ahreq = (void *)(treq_ctx->tail + ctx->reqoff);
714 + unsigned int flags = CRYPTO_TFM_REQ_MAY_SLEEP;
715 + int err = -EBADMSG;
716 +
717 + /* Bail out if the request assoc len is 0 */
718 + if (!req->assoclen)
719 + return err;
720 +
721 + init_completion(&ahash_op.completion);
722 +
723 + /* the hash transform to be executed comes from the original request */
724 + ahash_request_set_tfm(ahreq, ctx->auth);
725 + /* prepare the hash request with input data and result pointer */
726 + ahash_request_set_crypt(ahreq, src, hash, srclen);
727 + /* set the notifier for when the async hash function returns */
728 + ahash_request_set_callback(ahreq, aead_request_flags(req) & flags,
729 + tls_async_op_done, &ahash_op);
730 +
731 + /* Calculate the digest on the given data. The result is put in hash */
732 + err = crypto_ahash_digest(ahreq);
733 + if (err == -EINPROGRESS) {
734 + err = wait_for_completion_interruptible(&ahash_op.completion);
735 + if (!err)
736 + err = ahash_op.err;
737 + }
738 +
739 + return err;
740 +}
741 +
742 +/**
743 + * crypto_tls_gen_padicv - Calculate and pad hmac digest for a TLS record
744 + * @hash: (output) buffer to save the digest and padding into
745 + * @phashlen: (output) the size of digest + padding
746 + * @req: (input) aead request
747 + **/
748 +static int crypto_tls_gen_padicv(u8 *hash, unsigned int *phashlen,
749 + struct aead_request *req)
750 +{
751 + struct crypto_aead *tls = crypto_aead_reqtfm(req);
752 + unsigned int hash_size = crypto_aead_authsize(tls);
753 + unsigned int block_size = crypto_aead_blocksize(tls);
754 + unsigned int srclen = req->cryptlen + hash_size;
755 + unsigned int icvlen = req->cryptlen + req->assoclen;
756 + unsigned int padlen;
757 + int err;
758 +
759 + err = crypto_tls_genicv(hash, req->src, icvlen, req);
760 + if (err)
761 + goto out;
762 +
763 + /* add padding after digest */
764 + padlen = block_size - (srclen % block_size);
765 + memset(hash + hash_size, padlen - 1, padlen);
766 +
767 + *phashlen = hash_size + padlen;
768 +out:
769 + return err;
770 +}
771 +
772 +static int crypto_tls_copy_data(struct aead_request *req,
773 + struct scatterlist *src,
774 + struct scatterlist *dst,
775 + unsigned int len)
776 +{
777 + struct crypto_aead *tls = crypto_aead_reqtfm(req);
778 + struct crypto_tls_ctx *ctx = crypto_aead_ctx(tls);
779 + SYNC_SKCIPHER_REQUEST_ON_STACK(skreq, ctx->null);
780 +
781 + skcipher_request_set_sync_tfm(skreq, ctx->null);
782 + skcipher_request_set_callback(skreq, aead_request_flags(req),
783 + NULL, NULL);
784 + skcipher_request_set_crypt(skreq, src, dst, len, NULL);
785 +
786 + return crypto_skcipher_encrypt(skreq);
787 +}
788 +
789 +static int crypto_tls_encrypt(struct aead_request *req)
790 +{
791 + struct crypto_aead *tls = crypto_aead_reqtfm(req);
792 + struct crypto_tls_ctx *ctx = crypto_aead_ctx(tls);
793 + struct tls_request_ctx *treq_ctx = aead_request_ctx(req);
794 + struct skcipher_request *skreq;
795 + struct scatterlist *cipher = treq_ctx->cipher;
796 + struct scatterlist *tmp = treq_ctx->tmp;
797 + struct scatterlist *sg, *src, *dst;
798 + unsigned int cryptlen, phashlen;
799 + u8 *hash = treq_ctx->tail;
800 + int err;
801 +
802 + /*
803 + * The hash result is saved at the beginning of the tls request ctx
804 + * and is aligned as required by the hash transform. Enough space was
805 + * allocated in crypto_tls_init_tfm to accommodate the difference. The
806 + * requests themselves start later at treq_ctx->tail + ctx->reqoff so
807 + * the result is not overwritten by the second (cipher) request.
808 + */
809 + hash = (u8 *)ALIGN((unsigned long)hash +
810 + crypto_ahash_alignmask(ctx->auth),
811 + crypto_ahash_alignmask(ctx->auth) + 1);
812 +
813 + /*
814 + * STEP 1: create ICV together with necessary padding
815 + */
816 + err = crypto_tls_gen_padicv(hash, &phashlen, req);
817 + if (err)
818 + return err;
819 +
820 + /*
821 + * STEP 2: Hash and padding are combined with the payload
822 + * depending on the form it arrives. Scatter tables must have at least
823 + * one page of data before chaining with another table and can't have
824 + * an empty data page. The following code addresses these requirements.
825 + *
826 + * If the payload is empty, only the hash is encrypted, otherwise the
827 + * payload scatterlist is merged with the hash. A special merging case
828 + * is when the payload has only one page of data. In that case the
829 + * payload page is moved to another scatterlist and prepared there for
830 + * encryption.
831 + */
832 + if (req->cryptlen) {
833 + src = scatterwalk_ffwd(tmp, req->src, req->assoclen);
834 +
835 + sg_init_table(cipher, 2);
836 + sg_set_buf(cipher + 1, hash, phashlen);
837 +
838 + if (sg_is_last(src)) {
839 + sg_set_page(cipher, sg_page(src), req->cryptlen,
840 + src->offset);
841 + src = cipher;
842 + } else {
843 + unsigned int rem_len = req->cryptlen;
844 +
845 + for (sg = src; rem_len > sg->length; sg = sg_next(sg))
846 + rem_len -= min(rem_len, sg->length);
847 +
848 + sg_set_page(cipher, sg_page(sg), rem_len, sg->offset);
849 + sg_chain(sg, 1, cipher);
850 + }
851 + } else {
852 + sg_init_one(cipher, hash, phashlen);
853 + src = cipher;
854 + }
855 +
856 + /**
857 + * If src != dst copy the associated data from source to destination.
858 + * In both cases fast-forward passed the associated data in the dest.
859 + */
860 + if (req->src != req->dst) {
861 + err = crypto_tls_copy_data(req, req->src, req->dst,
862 + req->assoclen);
863 + if (err)
864 + return err;
865 + }
866 + dst = scatterwalk_ffwd(treq_ctx->dst, req->dst, req->assoclen);
867 +
868 + /*
869 + * STEP 3: encrypt the frame and return the result
870 + */
871 + cryptlen = req->cryptlen + phashlen;
872 +
873 + /*
874 + * The hash and the cipher are applied at different times and their
875 + * requests can use the same memory space without interference
876 + */
877 + skreq = (void *)(treq_ctx->tail + ctx->reqoff);
878 + skcipher_request_set_tfm(skreq, ctx->enc);
879 + skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
880 + skcipher_request_set_callback(skreq, aead_request_flags(req),
881 + req->base.complete, req->base.data);
882 + /*
883 + * Apply the cipher transform. The result will be in req->dst when the
884 + * asynchronuous call terminates
885 + */
886 + err = crypto_skcipher_encrypt(skreq);
887 +
888 + return err;
889 +}
890 +
891 +static int crypto_tls_decrypt(struct aead_request *req)
892 +{
893 + struct crypto_aead *tls = crypto_aead_reqtfm(req);
894 + struct crypto_tls_ctx *ctx = crypto_aead_ctx(tls);
895 + struct tls_request_ctx *treq_ctx = aead_request_ctx(req);
896 + unsigned int cryptlen = req->cryptlen;
897 + unsigned int hash_size = crypto_aead_authsize(tls);
898 + unsigned int block_size = crypto_aead_blocksize(tls);
899 + struct skcipher_request *skreq = (void *)(treq_ctx->tail + ctx->reqoff);
900 + struct scatterlist *tmp = treq_ctx->tmp;
901 + struct scatterlist *src, *dst;
902 +
903 + u8 padding[255]; /* padding can be 0-255 bytes */
904 + u8 pad_size;
905 + u16 *len_field;
906 + u8 *ihash, *hash = treq_ctx->tail;
907 +
908 + int paderr = 0;
909 + int err = -EINVAL;
910 + int i;
911 + struct async_op ciph_op;
912 +
913 + /*
914 + * Rule out bad packets. The input packet length must be at least one
915 + * byte more than the hash_size
916 + */
917 + if (cryptlen <= hash_size || cryptlen % block_size)
918 + goto out;
919 +
920 + /*
921 + * Step 1 - Decrypt the source. Fast-forward past the associated data
922 + * to the encrypted data. The result will be overwritten in place so
923 + * that the decrypted data will be adjacent to the associated data. The
924 + * last step (computing the hash) will have it's input data already
925 + * prepared and ready to be accessed at req->src.
926 + */
927 + src = scatterwalk_ffwd(tmp, req->src, req->assoclen);
928 + dst = src;
929 +
930 + init_completion(&ciph_op.completion);
931 + skcipher_request_set_tfm(skreq, ctx->enc);
932 + skcipher_request_set_callback(skreq, aead_request_flags(req),
933 + tls_async_op_done, &ciph_op);
934 + skcipher_request_set_crypt(skreq, src, dst, cryptlen, req->iv);
935 + err = crypto_skcipher_decrypt(skreq);
936 + if (err == -EINPROGRESS) {
937 + err = wait_for_completion_interruptible(&ciph_op.completion);
938 + if (!err)
939 + err = ciph_op.err;
940 + }
941 + if (err)
942 + goto out;
943 +
944 + /*
945 + * Step 2 - Verify padding
946 + * Retrieve the last byte of the payload; this is the padding size.
947 + */
948 + cryptlen -= 1;
949 + scatterwalk_map_and_copy(&pad_size, dst, cryptlen, 1, 0);
950 +
951 + /* RFC recommendation for invalid padding size. */
952 + if (cryptlen < pad_size + hash_size) {
953 + pad_size = 0;
954 + paderr = -EBADMSG;
955 + }
956 + cryptlen -= pad_size;
957 + scatterwalk_map_and_copy(padding, dst, cryptlen, pad_size, 0);
958 +
959 + /* Padding content must be equal with pad_size. We verify it all */
960 + for (i = 0; i < pad_size; i++)
961 + if (padding[i] != pad_size)
962 + paderr = -EBADMSG;
963 +
964 + /*
965 + * Step 3 - Verify hash
966 + * Align the digest result as required by the hash transform. Enough
967 + * space was allocated in crypto_tls_init_tfm
968 + */
969 + hash = (u8 *)ALIGN((unsigned long)hash +
970 + crypto_ahash_alignmask(ctx->auth),
971 + crypto_ahash_alignmask(ctx->auth) + 1);
972 + /*
973 + * Two bytes at the end of the associated data make the length field.
974 + * It must be updated with the length of the cleartext message before
975 + * the hash is calculated.
976 + */
977 + len_field = sg_virt(req->src) + req->assoclen - 2;
978 + cryptlen -= hash_size;
979 + *len_field = htons(cryptlen);
980 +
981 + /* This is the hash from the decrypted packet. Save it for later */
982 + ihash = hash + hash_size;
983 + scatterwalk_map_and_copy(ihash, dst, cryptlen, hash_size, 0);
984 +
985 + /* Now compute and compare our ICV with the one from the packet */
986 + err = crypto_tls_genicv(hash, req->src, cryptlen + req->assoclen, req);
987 + if (!err)
988 + err = memcmp(hash, ihash, hash_size) ? -EBADMSG : 0;
989 +
990 + if (req->src != req->dst) {
991 + err = crypto_tls_copy_data(req, req->src, req->dst, cryptlen +
992 + req->assoclen);
993 + if (err)
994 + goto out;
995 + }
996 +
997 + /* return the first found error */
998 + if (paderr)
999 + err = paderr;
1000 +
1001 +out:
1002 + aead_request_complete(req, err);
1003 + return err;
1004 +}
1005 +
1006 +static int crypto_tls_init_tfm(struct crypto_aead *tfm)
1007 +{
1008 + struct aead_instance *inst = aead_alg_instance(tfm);
1009 + struct tls_instance_ctx *ictx = aead_instance_ctx(inst);
1010 + struct crypto_tls_ctx *ctx = crypto_aead_ctx(tfm);
1011 + struct crypto_ahash *auth;
1012 + struct crypto_skcipher *enc;
1013 + struct crypto_sync_skcipher *null;
1014 + int err;
1015 +
1016 + auth = crypto_spawn_ahash(&ictx->auth);
1017 + if (IS_ERR(auth))
1018 + return PTR_ERR(auth);
1019 +
1020 + enc = crypto_spawn_skcipher(&ictx->enc);
1021 + err = PTR_ERR(enc);
1022 + if (IS_ERR(enc))
1023 + goto err_free_ahash;
1024 +
1025 + null = crypto_get_default_null_skcipher();
1026 + err = PTR_ERR(null);
1027 + if (IS_ERR(null))
1028 + goto err_free_skcipher;
1029 +
1030 + ctx->auth = auth;
1031 + ctx->enc = enc;
1032 + ctx->null = null;
1033 +
1034 + /*
1035 + * Allow enough space for two digests. The two digests will be compared
1036 + * during the decryption phase. One will come from the decrypted packet
1037 + * and the other will be calculated. For encryption, one digest is
1038 + * padded (up to a cipher blocksize) and chained with the payload
1039 + */
1040 + ctx->reqoff = ALIGN(crypto_ahash_digestsize(auth) +
1041 + crypto_ahash_alignmask(auth),
1042 + crypto_ahash_alignmask(auth) + 1) +
1043 + max(crypto_ahash_digestsize(auth),
1044 + crypto_skcipher_blocksize(enc));
1045 +
1046 + crypto_aead_set_reqsize(tfm,
1047 + sizeof(struct tls_request_ctx) +
1048 + ctx->reqoff +
1049 + max_t(unsigned int,
1050 + crypto_ahash_reqsize(auth) +
1051 + sizeof(struct ahash_request),
1052 + crypto_skcipher_reqsize(enc) +
1053 + sizeof(struct skcipher_request)));
1054 +
1055 + return 0;
1056 +
1057 +err_free_skcipher:
1058 + crypto_free_skcipher(enc);
1059 +err_free_ahash:
1060 + crypto_free_ahash(auth);
1061 + return err;
1062 +}
1063 +
1064 +static void crypto_tls_exit_tfm(struct crypto_aead *tfm)
1065 +{
1066 + struct crypto_tls_ctx *ctx = crypto_aead_ctx(tfm);
1067 +
1068 + crypto_free_ahash(ctx->auth);
1069 + crypto_free_skcipher(ctx->enc);
1070 + crypto_put_default_null_skcipher();
1071 +}
1072 +
1073 +static void crypto_tls_free(struct aead_instance *inst)
1074 +{
1075 + struct tls_instance_ctx *ctx = aead_instance_ctx(inst);
1076 +
1077 + crypto_drop_skcipher(&ctx->enc);
1078 + crypto_drop_ahash(&ctx->auth);
1079 + kfree(inst);
1080 +}
1081 +
1082 +static int crypto_tls_create(struct crypto_template *tmpl, struct rtattr **tb)
1083 +{
1084 + struct crypto_attr_type *algt;
1085 + struct aead_instance *inst;
1086 + struct hash_alg_common *auth;
1087 + struct crypto_alg *auth_base;
1088 + struct skcipher_alg *enc;
1089 + struct tls_instance_ctx *ctx;
1090 + const char *enc_name;
1091 + int err;
1092 +
1093 + algt = crypto_get_attr_type(tb);
1094 + if (IS_ERR(algt))
1095 + return PTR_ERR(algt);
1096 +
1097 + if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
1098 + return -EINVAL;
1099 +
1100 + auth = ahash_attr_alg(tb[1], CRYPTO_ALG_TYPE_HASH,
1101 + CRYPTO_ALG_TYPE_AHASH_MASK |
1102 + crypto_requires_sync(algt->type, algt->mask));
1103 + if (IS_ERR(auth))
1104 + return PTR_ERR(auth);
1105 +
1106 + auth_base = &auth->base;
1107 +
1108 + enc_name = crypto_attr_alg_name(tb[2]);
1109 + err = PTR_ERR(enc_name);
1110 + if (IS_ERR(enc_name))
1111 + goto out_put_auth;
1112 +
1113 + inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
1114 + err = -ENOMEM;
1115 + if (!inst)
1116 + goto out_put_auth;
1117 +
1118 + ctx = aead_instance_ctx(inst);
1119 +
1120 + err = crypto_init_ahash_spawn(&ctx->auth, auth,
1121 + aead_crypto_instance(inst));
1122 + if (err)
1123 + goto err_free_inst;
1124 +
1125 + crypto_set_skcipher_spawn(&ctx->enc, aead_crypto_instance(inst));
1126 + err = crypto_grab_skcipher(&ctx->enc, enc_name, 0,
1127 + crypto_requires_sync(algt->type,
1128 + algt->mask));
1129 + if (err)
1130 + goto err_drop_auth;
1131 +
1132 + enc = crypto_spawn_skcipher_alg(&ctx->enc);
1133 +
1134 + err = -ENAMETOOLONG;
1135 + if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
1136 + "tls10(%s,%s)", auth_base->cra_name,
1137 + enc->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
1138 + goto err_drop_enc;
1139 +
1140 + if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
1141 + "tls10(%s,%s)", auth_base->cra_driver_name,
1142 + enc->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
1143 + goto err_drop_enc;
1144 +
1145 + inst->alg.base.cra_flags = (auth_base->cra_flags |
1146 + enc->base.cra_flags) & CRYPTO_ALG_ASYNC;
1147 + inst->alg.base.cra_priority = enc->base.cra_priority * 10 +
1148 + auth_base->cra_priority;
1149 + inst->alg.base.cra_blocksize = enc->base.cra_blocksize;
1150 + inst->alg.base.cra_alignmask = auth_base->cra_alignmask |
1151 + enc->base.cra_alignmask;
1152 + inst->alg.base.cra_ctxsize = sizeof(struct crypto_tls_ctx);
1153 +
1154 + inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
1155 + inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
1156 + inst->alg.maxauthsize = auth->digestsize;
1157 +
1158 + inst->alg.init = crypto_tls_init_tfm;
1159 + inst->alg.exit = crypto_tls_exit_tfm;
1160 +
1161 + inst->alg.setkey = crypto_tls_setkey;
1162 + inst->alg.encrypt = crypto_tls_encrypt;
1163 + inst->alg.decrypt = crypto_tls_decrypt;
1164 +
1165 + inst->free = crypto_tls_free;
1166 +
1167 + err = aead_register_instance(tmpl, inst);
1168 + if (err)
1169 + goto err_drop_enc;
1170 +
1171 +out:
1172 + crypto_mod_put(auth_base);
1173 + return err;
1174 +
1175 +err_drop_enc:
1176 + crypto_drop_skcipher(&ctx->enc);
1177 +err_drop_auth:
1178 + crypto_drop_ahash(&ctx->auth);
1179 +err_free_inst:
1180 + kfree(inst);
1181 +out_put_auth:
1182 + goto out;
1183 +}
1184 +
1185 +static struct crypto_template crypto_tls_tmpl = {
1186 + .name = "tls10",
1187 + .create = crypto_tls_create,
1188 + .module = THIS_MODULE,
1189 +};
1190 +
1191 +static int __init crypto_tls_module_init(void)
1192 +{
1193 + return crypto_register_template(&crypto_tls_tmpl);
1194 +}
1195 +
1196 +static void __exit crypto_tls_module_exit(void)
1197 +{
1198 + crypto_unregister_template(&crypto_tls_tmpl);
1199 +}
1200 +
1201 +module_init(crypto_tls_module_init);
1202 +module_exit(crypto_tls_module_exit);
1203 +
1204 +MODULE_LICENSE("GPL");
1205 +MODULE_DESCRIPTION("TLS 1.0 record encryption");