Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 2 | /* |
| 3 | * seqiv: Sequence Number IV Generator |
| 4 | * |
| 5 | * This generator generates an IV based on a sequence number by xoring it |
| 6 | * with a salt. This algorithm is mainly useful for CTR and similar modes. |
| 7 | * |
| 8 | * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 9 | */ |
| 10 | |
Herbert Xu | 661cfd0 | 2015-05-27 14:37:37 +0800 | [diff] [blame] | 11 | #include <crypto/internal/geniv.h> |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 12 | #include <crypto/scatterwalk.h> |
Herbert Xu | 3a01d0e | 2016-07-12 13:17:50 +0800 | [diff] [blame] | 13 | #include <crypto/skcipher.h> |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 14 | #include <linux/err.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 19 | #include <linux/string.h> |
| 20 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 21 | static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err) |
| 22 | { |
| 23 | struct aead_request *subreq = aead_request_ctx(req); |
| 24 | struct crypto_aead *geniv; |
| 25 | |
| 26 | if (err == -EINPROGRESS) |
| 27 | return; |
| 28 | |
| 29 | if (err) |
| 30 | goto out; |
| 31 | |
| 32 | geniv = crypto_aead_reqtfm(req); |
| 33 | memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv)); |
| 34 | |
| 35 | out: |
Waiman Long | 453431a | 2020-08-06 23:18:13 -0700 | [diff] [blame] | 36 | kfree_sensitive(subreq->iv); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static void seqiv_aead_encrypt_complete(struct crypto_async_request *base, |
| 40 | int err) |
| 41 | { |
| 42 | struct aead_request *req = base->data; |
| 43 | |
| 44 | seqiv_aead_encrypt_complete2(req, err); |
| 45 | aead_request_complete(req, err); |
| 46 | } |
| 47 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 48 | static int seqiv_aead_encrypt(struct aead_request *req) |
| 49 | { |
| 50 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 51 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 52 | struct aead_request *subreq = aead_request_ctx(req); |
| 53 | crypto_completion_t compl; |
| 54 | void *data; |
| 55 | u8 *info; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 56 | unsigned int ivsize = 8; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 57 | int err; |
| 58 | |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 59 | if (req->cryptlen < ivsize) |
| 60 | return -EINVAL; |
| 61 | |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 62 | aead_request_set_tfm(subreq, ctx->child); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 63 | |
| 64 | compl = req->base.complete; |
| 65 | data = req->base.data; |
| 66 | info = req->iv; |
| 67 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 68 | if (req->src != req->dst) { |
Kees Cook | 8d60539 | 2018-09-18 19:10:51 -0700 | [diff] [blame] | 69 | SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 70 | |
Kees Cook | 8d60539 | 2018-09-18 19:10:51 -0700 | [diff] [blame] | 71 | skcipher_request_set_sync_tfm(nreq, ctx->sknull); |
Herbert Xu | ef22871 | 2016-07-12 13:17:45 +0800 | [diff] [blame] | 72 | skcipher_request_set_callback(nreq, req->base.flags, |
| 73 | NULL, NULL); |
| 74 | skcipher_request_set_crypt(nreq, req->src, req->dst, |
| 75 | req->assoclen + req->cryptlen, |
| 76 | NULL); |
| 77 | |
| 78 | err = crypto_skcipher_encrypt(nreq); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 79 | if (err) |
| 80 | return err; |
| 81 | } |
| 82 | |
| 83 | if (unlikely(!IS_ALIGNED((unsigned long)info, |
| 84 | crypto_aead_alignmask(geniv) + 1))) { |
YueHaibing | 7e33d4d | 2019-01-21 07:57:00 +0000 | [diff] [blame] | 85 | info = kmemdup(req->iv, ivsize, req->base.flags & |
| 86 | CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : |
| 87 | GFP_ATOMIC); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 88 | if (!info) |
| 89 | return -ENOMEM; |
| 90 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 91 | compl = seqiv_aead_encrypt_complete; |
| 92 | data = req; |
| 93 | } |
| 94 | |
| 95 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 96 | aead_request_set_crypt(subreq, req->dst, req->dst, |
| 97 | req->cryptlen - ivsize, info); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 98 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 99 | |
| 100 | crypto_xor(info, ctx->salt, ivsize); |
| 101 | scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1); |
| 102 | |
| 103 | err = crypto_aead_encrypt(subreq); |
| 104 | if (unlikely(info != req->iv)) |
| 105 | seqiv_aead_encrypt_complete2(req, err); |
| 106 | return err; |
| 107 | } |
| 108 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 109 | static int seqiv_aead_decrypt(struct aead_request *req) |
| 110 | { |
| 111 | struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 112 | struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 113 | struct aead_request *subreq = aead_request_ctx(req); |
| 114 | crypto_completion_t compl; |
| 115 | void *data; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 116 | unsigned int ivsize = 8; |
| 117 | |
| 118 | if (req->cryptlen < ivsize + crypto_aead_authsize(geniv)) |
| 119 | return -EINVAL; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 120 | |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 121 | aead_request_set_tfm(subreq, ctx->child); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 122 | |
| 123 | compl = req->base.complete; |
| 124 | data = req->base.data; |
| 125 | |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 126 | aead_request_set_callback(subreq, req->base.flags, compl, data); |
| 127 | aead_request_set_crypt(subreq, req->src, req->dst, |
| 128 | req->cryptlen - ivsize, req->iv); |
Herbert Xu | 374d4ad | 2015-05-23 15:41:57 +0800 | [diff] [blame] | 129 | aead_request_set_ad(subreq, req->assoclen + ivsize); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 130 | |
| 131 | scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0); |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 132 | |
| 133 | return crypto_aead_decrypt(subreq); |
| 134 | } |
| 135 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 136 | static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb) |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 137 | { |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 138 | struct aead_instance *inst; |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 139 | int err; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 140 | |
Eric Biggers | e72b48c | 2020-07-09 23:20:36 -0700 | [diff] [blame] | 141 | inst = aead_geniv_alloc(tmpl, tb); |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 142 | |
| 143 | if (IS_ERR(inst)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 144 | return PTR_ERR(inst); |
| 145 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 146 | err = -EINVAL; |
Herbert Xu | dd04446 | 2015-05-23 15:41:56 +0800 | [diff] [blame] | 147 | if (inst->alg.ivsize != sizeof(u64)) |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 148 | goto free_inst; |
Herbert Xu | c0ecf89 | 2015-01-16 19:51:20 +1100 | [diff] [blame] | 149 | |
Herbert Xu | b7dcfab | 2015-06-03 14:49:27 +0800 | [diff] [blame] | 150 | inst->alg.encrypt = seqiv_aead_encrypt; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 151 | inst->alg.decrypt = seqiv_aead_decrypt; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 152 | |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 153 | inst->alg.init = aead_init_geniv; |
| 154 | inst->alg.exit = aead_exit_geniv; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 155 | |
Herbert Xu | 659e7f5 | 2015-08-13 17:28:59 +0800 | [diff] [blame] | 156 | inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx); |
Herbert Xu | 5964f26 | 2015-07-09 07:17:22 +0800 | [diff] [blame] | 157 | inst->alg.base.cra_ctxsize += inst->alg.ivsize; |
Herbert Xu | 856e3f40 | 2015-05-21 15:11:13 +0800 | [diff] [blame] | 158 | |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 159 | err = aead_register_instance(tmpl, inst); |
Eric Biggers | 0f8f6d8 | 2020-01-02 20:04:36 -0800 | [diff] [blame] | 160 | if (err) { |
Herbert Xu | 0677157 | 2015-05-23 15:41:51 +0800 | [diff] [blame] | 161 | free_inst: |
Eric Biggers | 0f8f6d8 | 2020-01-02 20:04:36 -0800 | [diff] [blame] | 162 | inst->free(inst); |
| 163 | } |
| 164 | return err; |
Herbert Xu | 14df4d8 | 2007-12-12 12:27:26 +0800 | [diff] [blame] | 165 | } |
| 166 | |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 167 | static struct crypto_template seqiv_tmpl = { |
| 168 | .name = "seqiv", |
Eric Biggers | 4688111 | 2020-07-09 23:20:37 -0700 | [diff] [blame] | 169 | .create = seqiv_aead_create, |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 170 | .module = THIS_MODULE, |
| 171 | }; |
| 172 | |
| 173 | static int __init seqiv_module_init(void) |
| 174 | { |
Herbert Xu | 8a2cd1c | 2015-08-13 17:28:53 +0800 | [diff] [blame] | 175 | return crypto_register_template(&seqiv_tmpl); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static void __exit seqiv_module_exit(void) |
| 179 | { |
| 180 | crypto_unregister_template(&seqiv_tmpl); |
| 181 | } |
| 182 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 183 | subsys_initcall(seqiv_module_init); |
Herbert Xu | 0a27032 | 2007-11-30 21:38:37 +1100 | [diff] [blame] | 184 | module_exit(seqiv_module_exit); |
| 185 | |
| 186 | MODULE_LICENSE("GPL"); |
| 187 | MODULE_DESCRIPTION("Sequence Number IV Generator"); |
Kees Cook | 4943ba1 | 2014-11-24 16:32:38 -0800 | [diff] [blame] | 188 | MODULE_ALIAS_CRYPTO("seqiv"); |