Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 2 | /* |
| 3 | * echainiv: Encrypted Chain IV Generator |
| 4 | * |
Herbert Xu | 53a5d5d | 2016-09-07 18:42:08 +0800 | [diff] [blame] | 5 | * This generator generates an IV based on a sequence number by multiplying |
| 6 | * it with a salt and then encrypting it with the same key as used to encrypt |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 7 | * the plain text. This algorithm requires that the block size be equal |
| 8 | * to the IV size. It is mainly useful for CBC. |
| 9 | * |
| 10 | * This generator can only be used by algorithms where authentication |
| 11 | * is performed after encryption (i.e., authenc). |
| 12 | * |
| 13 | * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 14 | */ |
| 15 | |
Herbert Xu | d97de47 | 2015-05-27 14:37:33 +0800 | [diff] [blame] | 16 | #include <crypto/internal/geniv.h> |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 17 | #include <crypto/scatterwalk.h> |
Herbert Xu | 0e8bff4 | 2016-07-12 13:17:43 +0800 | [diff] [blame] | 18 | #include <crypto/skcipher.h> |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/kernel.h> |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 22 | #include <linux/module.h> |
Herbert Xu | 53a5d5d | 2016-09-07 18:42:08 +0800 | [diff] [blame] | 23 | #include <linux/slab.h> |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 24 | #include <linux/string.h> |
| 25 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 26 | static int echainiv_encrypt(struct aead_request *req) |
| 27 | { |
| 28 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
Herbert Xu | 376e0d6 | 2015-08-13 17:29:01 +0800 | [diff] [blame] | 29 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 30 | struct aead_request *subreq = aead_request_ctx(req); |
Herbert Xu | 53a5d5d | 2016-09-07 18:42:08 +0800 | [diff] [blame] | 31 | __be64 nseqno; |
| 32 | u64 seqno; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 33 | u8 *info; |
Herbert Xu | 823655c | 2015-05-23 15:41:54 +0800 | [diff] [blame] | 34 | unsigned int ivsize = crypto_aead_ivsize(geniv); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 35 | int err; |
| 36 | |
Herbert Xu | 823655c | 2015-05-23 15:41:54 +0800 | [diff] [blame] | 37 | if (req->cryptlen < ivsize) |
| 38 | return -EINVAL; |
| 39 | |
Herbert Xu | 376e0d6 | 2015-08-13 17:29:01 +0800 | [diff] [blame] | 40 | aead_request_set_tfm(subreq, ctx->child); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 41 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 42 | info = req->iv; |
| 43 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 44 | if (req->src != req->dst) { |
Kees Cook | 8d60539 | 2018-09-18 19:10:51 -0700 | [diff] [blame] | 45 | SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 46 | |
Kees Cook | 8d60539 | 2018-09-18 19:10:51 -0700 | [diff] [blame] | 47 | skcipher_request_set_sync_tfm(nreq, ctx->sknull); |
Herbert Xu | 0e8bff4 | 2016-07-12 13:17:43 +0800 | [diff] [blame] | 48 | skcipher_request_set_callback(nreq, req->base.flags, |
| 49 | NULL, NULL); |
| 50 | skcipher_request_set_crypt(nreq, req->src, req->dst, |
| 51 | req->assoclen + req->cryptlen, |
| 52 | NULL); |
| 53 | |
| 54 | err = crypto_skcipher_encrypt(nreq); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 55 | if (err) |
| 56 | return err; |
| 57 | } |
| 58 | |
Herbert Xu | 53a5d5d | 2016-09-07 18:42:08 +0800 | [diff] [blame] | 59 | aead_request_set_callback(subreq, req->base.flags, |
| 60 | req->base.complete, req->base.data); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 61 | aead_request_set_crypt(subreq, req->dst, req->dst, |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 62 | req->cryptlen, info); |
| 63 | aead_request_set_ad(subreq, req->assoclen); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 64 | |
Herbert Xu | 53a5d5d | 2016-09-07 18:42:08 +0800 | [diff] [blame] | 65 | memcpy(&nseqno, info + ivsize - 8, 8); |
| 66 | seqno = be64_to_cpu(nseqno); |
| 67 | memset(info, 0, ivsize); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 68 | |
Herbert Xu | 53a5d5d | 2016-09-07 18:42:08 +0800 | [diff] [blame] | 69 | scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1); |
| 70 | |
| 71 | do { |
| 72 | u64 a; |
| 73 | |
| 74 | memcpy(&a, ctx->salt + ivsize - 8, 8); |
| 75 | |
| 76 | a |= 1; |
| 77 | a *= seqno; |
| 78 | |
| 79 | memcpy(info + ivsize - 8, &a, 8); |
| 80 | } while ((ivsize -= 8)); |
| 81 | |
| 82 | return crypto_aead_encrypt(subreq); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 83 | } |
| 84 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 85 | static int echainiv_decrypt(struct aead_request *req) |
| 86 | { |
| 87 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
Herbert Xu | 376e0d6 | 2015-08-13 17:29:01 +0800 | [diff] [blame] | 88 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 89 | struct aead_request *subreq = aead_request_ctx(req); |
| 90 | crypto_completion_t compl; |
| 91 | void *data; |
Herbert Xu | 823655c | 2015-05-23 15:41:54 +0800 | [diff] [blame] | 92 | unsigned int ivsize = crypto_aead_ivsize(geniv); |
| 93 | |
Herbert Xu | 5499b1a | 2015-07-09 07:17:20 +0800 | [diff] [blame] | 94 | if (req->cryptlen < ivsize) |
Herbert Xu | 823655c | 2015-05-23 15:41:54 +0800 | [diff] [blame] | 95 | return -EINVAL; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 96 | |
Herbert Xu | 376e0d6 | 2015-08-13 17:29:01 +0800 | [diff] [blame] | 97 | aead_request_set_tfm(subreq, ctx->child); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 98 | |
| 99 | compl = req->base.complete; |
| 100 | data = req->base.data; |
| 101 | |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 102 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 103 | aead_request_set_crypt(subreq, req->src, req->dst, |
| 104 | req->cryptlen - ivsize, req->iv); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 105 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 106 | |
| 107 | scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 108 | |
| 109 | return crypto_aead_decrypt(subreq); |
| 110 | } |
| 111 | |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 112 | static int echainiv_aead_create(struct crypto_template *tmpl, |
| 113 | struct rtattr **tb) |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 114 | { |
| 115 | struct aead_instance *inst; |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 116 | int err; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 117 | |
Eric Biggers | e72b48c | 2020-07-09 23:20:36 -0700 | [diff] [blame] | 118 | inst = aead_geniv_alloc(tmpl, tb); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 119 | |
| 120 | if (IS_ERR(inst)) |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 121 | return PTR_ERR(inst); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 122 | |
Herbert Xu | d97de47 | 2015-05-27 14:37:33 +0800 | [diff] [blame] | 123 | err = -EINVAL; |
Herbert Xu | 53a5d5d | 2016-09-07 18:42:08 +0800 | [diff] [blame] | 124 | if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize) |
Herbert Xu | d97de47 | 2015-05-27 14:37:33 +0800 | [diff] [blame] | 125 | goto free_inst; |
| 126 | |
Herbert Xu | f261c5f | 2015-06-03 14:49:24 +0800 | [diff] [blame] | 127 | inst->alg.encrypt = echainiv_encrypt; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 128 | inst->alg.decrypt = echainiv_decrypt; |
| 129 | |
Herbert Xu | 376e0d6 | 2015-08-13 17:29:01 +0800 | [diff] [blame] | 130 | inst->alg.init = aead_init_geniv; |
| 131 | inst->alg.exit = aead_exit_geniv; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 132 | |
Herbert Xu | 376e0d6 | 2015-08-13 17:29:01 +0800 | [diff] [blame] | 133 | inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx); |
Herbert Xu | 9d03aee | 2015-05-27 14:37:34 +0800 | [diff] [blame] | 134 | inst->alg.base.cra_ctxsize += inst->alg.ivsize; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 135 | |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 136 | err = aead_register_instance(tmpl, inst); |
Eric Biggers | 0f8f6d8 | 2020-01-02 20:04:36 -0800 | [diff] [blame] | 137 | if (err) { |
Herbert Xu | 1e419c7 | 2015-05-23 15:41:52 +0800 | [diff] [blame] | 138 | free_inst: |
Eric Biggers | 0f8f6d8 | 2020-01-02 20:04:36 -0800 | [diff] [blame] | 139 | inst->free(inst); |
| 140 | } |
| 141 | return err; |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static struct crypto_template echainiv_tmpl = { |
| 145 | .name = "echainiv", |
Herbert Xu | 9fcc704 | 2015-06-21 19:11:50 +0800 | [diff] [blame] | 146 | .create = echainiv_aead_create, |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 147 | .module = THIS_MODULE, |
| 148 | }; |
| 149 | |
| 150 | static int __init echainiv_module_init(void) |
| 151 | { |
| 152 | return crypto_register_template(&echainiv_tmpl); |
| 153 | } |
| 154 | |
| 155 | static void __exit echainiv_module_exit(void) |
| 156 | { |
| 157 | crypto_unregister_template(&echainiv_tmpl); |
| 158 | } |
| 159 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 160 | subsys_initcall(echainiv_module_init); |
Herbert Xu | a10f554 | 2015-05-21 15:11:15 +0800 | [diff] [blame] | 161 | module_exit(echainiv_module_exit); |
| 162 | |
| 163 | MODULE_LICENSE("GPL"); |
| 164 | MODULE_DESCRIPTION("Encrypted Chain IV Generator"); |
| 165 | MODULE_ALIAS_CRYPTO("echainiv"); |