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
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
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.
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>
20 crypto/Kconfig | 20 ++
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
31 @@ -342,6 +342,26 @@ config CRYPTO_ECHAINIV
32 a sequence number xored with a salt. This is the default
36 + tristate "TLS support"
38 + select CRYPTO_BLKCIPHER
39 + select CRYPTO_MANAGER
42 + select CRYPTO_AUTHENC
44 + Support for TLS 1.0 record encryption and decryption
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.
52 + User-space applications (OpenSSL, GnuTLS) can offload TLS 1.0
53 + operations through AF_ALG or cryptodev interfaces
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
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)");
75 + ret += tcrypt_test("tls10(hmac(sha1),cbc(aes))");
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 {
86 +struct tls_test_suite {
88 + struct tls_testvec *vecs;
93 struct akcipher_test_suite {
94 const struct akcipher_testvec *vecs;
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;
104 @@ -2294,6 +2302,227 @@ static int test_aead(const char *driver,
108 +static int __test_tls(struct crypto_aead *tfm, int enc,
109 + struct tls_testvec *template, unsigned int tcount,
110 + const bool diff_dst)
112 + const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
113 + unsigned int i, k, authsize;
115 + struct aead_request *req;
116 + struct scatterlist *sg;
117 + struct scatterlist *sgout;
119 + struct crypto_wait wait;
125 + char *xbuf[XBUFSIZE];
126 + char *xoutbuf[XBUFSIZE];
127 + char *axbuf[XBUFSIZE];
130 + if (testmgr_alloc_buf(xbuf))
133 + if (diff_dst && testmgr_alloc_buf(xoutbuf))
136 + if (testmgr_alloc_buf(axbuf))
139 + iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
143 + key = kzalloc(MAX_KEYLEN, GFP_KERNEL);
147 + sg = kmalloc(sizeof(*sg) * 8 * (diff_dst ? 2 : 1), GFP_KERNEL);
153 + d = diff_dst ? "-ddst" : "";
154 + e = enc ? "encryption" : "decryption";
156 + crypto_init_wait(&wait);
158 + req = aead_request_alloc(tfm, GFP_KERNEL);
160 + pr_err("alg: tls%s: Failed to allocate request for %s\n",
165 + aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
166 + crypto_req_done, &wait);
168 + for (i = 0; i < tcount; i++) {
173 + if (WARN_ON(template[i].ilen > PAGE_SIZE ||
174 + template[i].alen > PAGE_SIZE))
177 + memcpy(assoc, template[i].assoc, template[i].alen);
178 + memcpy(input, template[i].input, template[i].ilen);
180 + if (template[i].iv)
181 + memcpy(iv, template[i].iv, MAX_IVLEN);
183 + memset(iv, 0, MAX_IVLEN);
185 + crypto_aead_clear_flags(tfm, ~0);
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);
193 + memcpy(key, template[i].key, template[i].klen);
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));
204 + ret = crypto_aead_setauthsize(tfm, authsize);
206 + pr_err("alg: aead%s: Failed to set authsize to %u on test %d for %s\n",
207 + d, authsize, i, algo);
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));
219 + sg_init_table(sgout, k + 1);
220 + sg_set_buf(&sgout[0], assoc, template[i].alen);
222 + output = xoutbuf[0];
223 + sg_set_buf(&sgout[k], output,
224 + (enc ? template[i].rlen : template[i].ilen));
227 + aead_request_set_crypt(req, sg, (diff_dst) ? sgout : sg,
228 + template[i].ilen, iv);
230 + aead_request_set_ad(req, template[i].alen);
232 + ret = crypto_wait_req(enc ? crypto_aead_encrypt(req)
233 + : crypto_aead_decrypt(req), &wait);
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",
241 + /* so really, we got a bad message */
247 + /* verification failure was expected */
248 + if (template[i].novrfy)
252 + pr_err("alg: tls%s: %s failed on test %d for %s: ret=%d\n",
253 + d, e, i, algo, -ret);
258 + if (memcmp(q, template[i].result, template[i].rlen)) {
259 + pr_err("alg: tls%s: Test %d failed on %s for %s\n",
261 + hexdump(q, template[i].rlen);
262 + pr_err("should be:\n");
263 + hexdump(template[i].result, template[i].rlen);
270 + aead_request_free(req);
278 + testmgr_free_buf(axbuf);
281 + testmgr_free_buf(xoutbuf);
283 + testmgr_free_buf(xbuf);
288 +static int test_tls(struct crypto_aead *tfm, int enc,
289 + struct tls_testvec *template, unsigned int tcount)
292 + /* test 'dst == src' case */
293 + ret = __test_tls(tfm, enc, template, tcount, false);
296 + /* test 'dst != src' case */
297 + return __test_tls(tfm, enc, template, tcount, true);
300 +static int alg_test_tls(const struct alg_test_desc *desc, const char *driver,
301 + u32 type, u32 mask)
303 + struct crypto_aead *tfm;
306 + tfm = crypto_alloc_aead(driver, type, mask);
308 + pr_err("alg: aead: Failed to load transform for %s: %ld\n",
309 + driver, PTR_ERR(tfm));
310 + return PTR_ERR(tfm);
313 + if (desc->suite.tls.enc.vecs) {
314 + err = test_tls(tfm, ENCRYPT, desc->suite.tls.enc.vecs,
315 + desc->suite.tls.enc.count);
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);
325 + crypto_free_aead(tfm);
329 static int alg_test_aead(const struct alg_test_desc *desc, const char *driver,
332 @@ -5012,6 +5241,15 @@ static const struct alg_test_desc alg_te
333 .hash = __VECS(tgr192_tv_template)
336 + .alg = "tls10(hmac(sha1),cbc(aes))",
337 + .test = alg_test_tls,
340 + .enc = __VECS(tls_enc_tv_template),
341 + .dec = __VECS(tls_dec_tv_template)
345 .alg = "vmac64(aes)",
346 .test = alg_test_hash,
348 --- a/crypto/testmgr.h
349 +++ b/crypto/testmgr.h
351 #define _CRYPTO_TESTMGR_H
353 #include <linux/oid_registry.h>
354 +#include <linux/netlink.h>
356 +#define MAX_DIGEST_SIZE 64
359 +#define MAX_KEYLEN 160
363 @@ -140,6 +145,20 @@ struct drbg_testvec {
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 */
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];
388 + * TLS1.0 synthetic test vectors
390 +static struct tls_testvec tls_enc_tv_template[] = {
392 +#ifdef __LITTLE_ENDIAN
393 + .key = "\x08\x00" /* rta length */
394 + "\x01\x00" /* rta type */
396 + .key = "\x00\x08" /* rta length */
397 + "\x00\x01" /* rta type */
399 + "\x00\x00\x00\x10" /* enc key length */
400 + "authenticationkey20benckeyis16_bytes",
401 + .klen = 8 + 20 + 16,
402 + .iv = "iv0123456789abcd",
403 + .input = "Single block msg",
405 + .assoc = "\x00\x01\x02\x03\x04\x05\x06\x07"
406 + "\x00\x03\x01\x00\x10",
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,
416 +#ifdef __LITTLE_ENDIAN
417 + .key = "\x08\x00" /* rta length */
418 + "\x01\x00" /* rta type */
420 + .key = "\x00\x08" /* rta length */
421 + "\x00\x01" /* rta type */
423 + "\x00\x00\x00\x10" /* enc key length */
424 + "authenticationkey20benckeyis16_bytes",
425 + .klen = 8 + 20 + 16,
426 + .iv = "iv0123456789abcd",
429 + .assoc = "\x00\x01\x02\x03\x04\x05\x06\x07"
430 + "\x00\x03\x01\x00\x00",
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",
438 +#ifdef __LITTLE_ENDIAN
439 + .key = "\x08\x00" /* rta length */
440 + "\x01\x00" /* rta type */
442 + .key = "\x00\x08" /* rta length */
443 + "\x00\x01" /* rta type */
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",
457 + .assoc = "\x00\x01\x02\x03\x04\x05\x06\x07"
458 + "\x00\x03\x01\x01\x1d",
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,
491 +static struct tls_testvec tls_dec_tv_template[] = {
493 +#ifdef __LITTLE_ENDIAN
494 + .key = "\x08\x00" /* rta length */
495 + "\x01\x00" /* rta type */
497 + .key = "\x00\x08" /* rta length */
498 + "\x00\x01" /* rta type */
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",
514 + .result = "Single block msg",
517 +#ifdef __LITTLE_ENDIAN
518 + .key = "\x08\x00" /* rta length */
519 + "\x01\x00" /* rta type */
521 + .key = "\x00\x08" /* rta length */
522 + "\x00\x01" /* rta type */
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",
533 + .assoc = "\x00\x01\x02\x03\x04\x05\x06\x07"
534 + "\x00\x03\x01\x00\x20",
539 +#ifdef __LITTLE_ENDIAN
540 + .key = "\x08\x00" /* rta length */
541 + "\x01\x00" /* rta type */
543 + .key = "\x00\x08" /* rta length */
544 + "\x00\x01" /* rta type */
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",
578 + .ilen = 285 + 20 + 15,
579 + .assoc = "\x00\x01\x02\x03\x04\x05\x06\x07"
580 + "\x00\x03\x01\x01\x40",
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",
593 * RSA test vectors. Borrowed from openSSL.
595 static const struct akcipher_testvec rsa_tv_template[] = {
600 + * Copyright 2013 Freescale Semiconductor, Inc.
601 + * Copyright 2017 NXP Semiconductor, Inc.
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.
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>
621 +struct tls_instance_ctx {
622 + struct crypto_ahash_spawn auth;
623 + struct crypto_skcipher_spawn enc;
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;
633 +struct tls_request_ctx {
635 + * cryptlen holds the payload length in the case of encryption or
636 + * payload_len + icv_len + padding_len in case of decryption
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];
647 + struct completion completion;
651 +static void tls_async_op_done(struct crypto_async_request *req, int err)
653 + struct async_op *areq = req->data;
655 + if (err == -EINPROGRESS)
659 + complete(&areq->completion);
662 +static int crypto_tls_setkey(struct crypto_aead *tls, const u8 *key,
663 + unsigned int keylen)
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;
671 + if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
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);
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);
695 + crypto_aead_set_flags(tls, CRYPTO_TFM_RES_BAD_KEY_LEN);
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
706 +static int crypto_tls_genicv(u8 *hash, struct scatterlist *src,
707 + unsigned int srclen, struct aead_request *req)
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;
717 + /* Bail out if the request assoc len is 0 */
718 + if (!req->assoclen)
721 + init_completion(&ahash_op.completion);
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);
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);
736 + err = ahash_op.err;
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
748 +static int crypto_tls_gen_padicv(u8 *hash, unsigned int *phashlen,
749 + struct aead_request *req)
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;
759 + err = crypto_tls_genicv(hash, req->src, icvlen, req);
763 + /* add padding after digest */
764 + padlen = block_size - (srclen % block_size);
765 + memset(hash + hash_size, padlen - 1, padlen);
767 + *phashlen = hash_size + padlen;
772 +static int crypto_tls_copy_data(struct aead_request *req,
773 + struct scatterlist *src,
774 + struct scatterlist *dst,
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);
781 + skcipher_request_set_sync_tfm(skreq, ctx->null);
782 + skcipher_request_set_callback(skreq, aead_request_flags(req),
784 + skcipher_request_set_crypt(skreq, src, dst, len, NULL);
786 + return crypto_skcipher_encrypt(skreq);
789 +static int crypto_tls_encrypt(struct aead_request *req)
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;
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.
809 + hash = (u8 *)ALIGN((unsigned long)hash +
810 + crypto_ahash_alignmask(ctx->auth),
811 + crypto_ahash_alignmask(ctx->auth) + 1);
814 + * STEP 1: create ICV together with necessary padding
816 + err = crypto_tls_gen_padicv(hash, &phashlen, req);
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.
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
832 + if (req->cryptlen) {
833 + src = scatterwalk_ffwd(tmp, req->src, req->assoclen);
835 + sg_init_table(cipher, 2);
836 + sg_set_buf(cipher + 1, hash, phashlen);
838 + if (sg_is_last(src)) {
839 + sg_set_page(cipher, sg_page(src), req->cryptlen,
843 + unsigned int rem_len = req->cryptlen;
845 + for (sg = src; rem_len > sg->length; sg = sg_next(sg))
846 + rem_len -= min(rem_len, sg->length);
848 + sg_set_page(cipher, sg_page(sg), rem_len, sg->offset);
849 + sg_chain(sg, 1, cipher);
852 + sg_init_one(cipher, hash, phashlen);
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.
860 + if (req->src != req->dst) {
861 + err = crypto_tls_copy_data(req, req->src, req->dst,
866 + dst = scatterwalk_ffwd(treq_ctx->dst, req->dst, req->assoclen);
869 + * STEP 3: encrypt the frame and return the result
871 + cryptlen = req->cryptlen + phashlen;
874 + * The hash and the cipher are applied at different times and their
875 + * requests can use the same memory space without interference
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);
883 + * Apply the cipher transform. The result will be in req->dst when the
884 + * asynchronuous call terminates
886 + err = crypto_skcipher_encrypt(skreq);
891 +static int crypto_tls_decrypt(struct aead_request *req)
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;
903 + u8 padding[255]; /* padding can be 0-255 bytes */
906 + u8 *ihash, *hash = treq_ctx->tail;
911 + struct async_op ciph_op;
914 + * Rule out bad packets. The input packet length must be at least one
915 + * byte more than the hash_size
917 + if (cryptlen <= hash_size || cryptlen % block_size)
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.
927 + src = scatterwalk_ffwd(tmp, req->src, req->assoclen);
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);
945 + * Step 2 - Verify padding
946 + * Retrieve the last byte of the payload; this is the padding size.
949 + scatterwalk_map_and_copy(&pad_size, dst, cryptlen, 1, 0);
951 + /* RFC recommendation for invalid padding size. */
952 + if (cryptlen < pad_size + hash_size) {
956 + cryptlen -= pad_size;
957 + scatterwalk_map_and_copy(padding, dst, cryptlen, pad_size, 0);
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)
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
969 + hash = (u8 *)ALIGN((unsigned long)hash +
970 + crypto_ahash_alignmask(ctx->auth),
971 + crypto_ahash_alignmask(ctx->auth) + 1);
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.
977 + len_field = sg_virt(req->src) + req->assoclen - 2;
978 + cryptlen -= hash_size;
979 + *len_field = htons(cryptlen);
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);
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);
988 + err = memcmp(hash, ihash, hash_size) ? -EBADMSG : 0;
990 + if (req->src != req->dst) {
991 + err = crypto_tls_copy_data(req, req->src, req->dst, cryptlen +
997 + /* return the first found error */
1002 + aead_request_complete(req, err);
1006 +static int crypto_tls_init_tfm(struct crypto_aead *tfm)
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;
1016 + auth = crypto_spawn_ahash(&ictx->auth);
1018 + return PTR_ERR(auth);
1020 + enc = crypto_spawn_skcipher(&ictx->enc);
1021 + err = PTR_ERR(enc);
1023 + goto err_free_ahash;
1025 + null = crypto_get_default_null_skcipher();
1026 + err = PTR_ERR(null);
1028 + goto err_free_skcipher;
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
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));
1046 + crypto_aead_set_reqsize(tfm,
1047 + sizeof(struct tls_request_ctx) +
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)));
1058 + crypto_free_skcipher(enc);
1060 + crypto_free_ahash(auth);
1064 +static void crypto_tls_exit_tfm(struct crypto_aead *tfm)
1066 + struct crypto_tls_ctx *ctx = crypto_aead_ctx(tfm);
1068 + crypto_free_ahash(ctx->auth);
1069 + crypto_free_skcipher(ctx->enc);
1070 + crypto_put_default_null_skcipher();
1073 +static void crypto_tls_free(struct aead_instance *inst)
1075 + struct tls_instance_ctx *ctx = aead_instance_ctx(inst);
1077 + crypto_drop_skcipher(&ctx->enc);
1078 + crypto_drop_ahash(&ctx->auth);
1082 +static int crypto_tls_create(struct crypto_template *tmpl, struct rtattr **tb)
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;
1093 + algt = crypto_get_attr_type(tb);
1095 + return PTR_ERR(algt);
1097 + if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & algt->mask)
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));
1104 + return PTR_ERR(auth);
1106 + auth_base = &auth->base;
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;
1113 + inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
1116 + goto out_put_auth;
1118 + ctx = aead_instance_ctx(inst);
1120 + err = crypto_init_ahash_spawn(&ctx->auth, auth,
1121 + aead_crypto_instance(inst));
1123 + goto err_free_inst;
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,
1130 + goto err_drop_auth;
1132 + enc = crypto_spawn_skcipher_alg(&ctx->enc);
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;
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;
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);
1154 + inst->alg.ivsize = crypto_skcipher_alg_ivsize(enc);
1155 + inst->alg.chunksize = crypto_skcipher_alg_chunksize(enc);
1156 + inst->alg.maxauthsize = auth->digestsize;
1158 + inst->alg.init = crypto_tls_init_tfm;
1159 + inst->alg.exit = crypto_tls_exit_tfm;
1161 + inst->alg.setkey = crypto_tls_setkey;
1162 + inst->alg.encrypt = crypto_tls_encrypt;
1163 + inst->alg.decrypt = crypto_tls_decrypt;
1165 + inst->free = crypto_tls_free;
1167 + err = aead_register_instance(tmpl, inst);
1169 + goto err_drop_enc;
1172 + crypto_mod_put(auth_base);
1176 + crypto_drop_skcipher(&ctx->enc);
1178 + crypto_drop_ahash(&ctx->auth);
1185 +static struct crypto_template crypto_tls_tmpl = {
1187 + .create = crypto_tls_create,
1188 + .module = THIS_MODULE,
1191 +static int __init crypto_tls_module_init(void)
1193 + return crypto_register_template(&crypto_tls_tmpl);
1196 +static void __exit crypto_tls_module_exit(void)
1198 + crypto_unregister_template(&crypto_tls_tmpl);
1201 +module_init(crypto_tls_module_init);
1202 +module_exit(crypto_tls_module_exit);
1204 +MODULE_LICENSE("GPL");
1205 +MODULE_DESCRIPTION("TLS 1.0 record encryption");