blob: 3f2fad615d265e65c7b024b096002d243e7a0486 [file] [log] [blame]
Herbert Xu0a270322007-11-30 21:38:37 +11001/*
2 * seqiv: Sequence Number IV Generator
3 *
4 * This generator generates an IV based on a sequence number by xoring it
5 * with a salt. This algorithm is mainly useful for CTR and similar modes.
6 *
7 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 */
15
Herbert Xu661cfd02015-05-27 14:37:37 +080016#include <crypto/internal/geniv.h>
Herbert Xu856e3f402015-05-21 15:11:13 +080017#include <crypto/scatterwalk.h>
Herbert Xu3a01d0e2016-07-12 13:17:50 +080018#include <crypto/skcipher.h>
Herbert Xu0a270322007-11-30 21:38:37 +110019#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Herbert Xu0a270322007-11-30 21:38:37 +110024#include <linux/string.h>
25
Herbert Xu06771572015-05-23 15:41:51 +080026static void seqiv_free(struct crypto_instance *inst);
27
Herbert Xu856e3f402015-05-21 15:11:13 +080028static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
29{
30 struct aead_request *subreq = aead_request_ctx(req);
31 struct crypto_aead *geniv;
32
33 if (err == -EINPROGRESS)
34 return;
35
36 if (err)
37 goto out;
38
39 geniv = crypto_aead_reqtfm(req);
40 memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
41
42out:
43 kzfree(subreq->iv);
44}
45
46static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
47 int err)
48{
49 struct aead_request *req = base->data;
50
51 seqiv_aead_encrypt_complete2(req, err);
52 aead_request_complete(req, err);
53}
54
Herbert Xu856e3f402015-05-21 15:11:13 +080055static int seqiv_aead_encrypt(struct aead_request *req)
56{
57 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
Herbert Xu659e7f52015-08-13 17:28:59 +080058 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xu856e3f402015-05-21 15:11:13 +080059 struct aead_request *subreq = aead_request_ctx(req);
60 crypto_completion_t compl;
61 void *data;
62 u8 *info;
Herbert Xudd044462015-05-23 15:41:56 +080063 unsigned int ivsize = 8;
Herbert Xu856e3f402015-05-21 15:11:13 +080064 int err;
65
Herbert Xudd044462015-05-23 15:41:56 +080066 if (req->cryptlen < ivsize)
67 return -EINVAL;
68
Herbert Xu659e7f52015-08-13 17:28:59 +080069 aead_request_set_tfm(subreq, ctx->child);
Herbert Xu856e3f402015-05-21 15:11:13 +080070
71 compl = req->base.complete;
72 data = req->base.data;
73 info = req->iv;
74
Herbert Xu856e3f402015-05-21 15:11:13 +080075 if (req->src != req->dst) {
Kees Cook8d605392018-09-18 19:10:51 -070076 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
Herbert Xu856e3f402015-05-21 15:11:13 +080077
Kees Cook8d605392018-09-18 19:10:51 -070078 skcipher_request_set_sync_tfm(nreq, ctx->sknull);
Herbert Xuef228712016-07-12 13:17:45 +080079 skcipher_request_set_callback(nreq, req->base.flags,
80 NULL, NULL);
81 skcipher_request_set_crypt(nreq, req->src, req->dst,
82 req->assoclen + req->cryptlen,
83 NULL);
84
85 err = crypto_skcipher_encrypt(nreq);
Herbert Xu856e3f402015-05-21 15:11:13 +080086 if (err)
87 return err;
88 }
89
90 if (unlikely(!IS_ALIGNED((unsigned long)info,
91 crypto_aead_alignmask(geniv) + 1))) {
YueHaibing7e33d4d2019-01-21 07:57:00 +000092 info = kmemdup(req->iv, ivsize, req->base.flags &
93 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
94 GFP_ATOMIC);
Herbert Xu856e3f402015-05-21 15:11:13 +080095 if (!info)
96 return -ENOMEM;
97
Herbert Xu856e3f402015-05-21 15:11:13 +080098 compl = seqiv_aead_encrypt_complete;
99 data = req;
100 }
101
102 aead_request_set_callback(subreq, req->base.flags, compl, data);
103 aead_request_set_crypt(subreq, req->dst, req->dst,
104 req->cryptlen - ivsize, info);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800105 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800106
107 crypto_xor(info, ctx->salt, ivsize);
108 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
109
110 err = crypto_aead_encrypt(subreq);
111 if (unlikely(info != req->iv))
112 seqiv_aead_encrypt_complete2(req, err);
113 return err;
114}
115
Herbert Xu856e3f402015-05-21 15:11:13 +0800116static int seqiv_aead_decrypt(struct aead_request *req)
117{
118 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
Herbert Xu659e7f52015-08-13 17:28:59 +0800119 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xu856e3f402015-05-21 15:11:13 +0800120 struct aead_request *subreq = aead_request_ctx(req);
121 crypto_completion_t compl;
122 void *data;
Herbert Xudd044462015-05-23 15:41:56 +0800123 unsigned int ivsize = 8;
124
125 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
126 return -EINVAL;
Herbert Xu856e3f402015-05-21 15:11:13 +0800127
Herbert Xu659e7f52015-08-13 17:28:59 +0800128 aead_request_set_tfm(subreq, ctx->child);
Herbert Xu856e3f402015-05-21 15:11:13 +0800129
130 compl = req->base.complete;
131 data = req->base.data;
132
Herbert Xu856e3f402015-05-21 15:11:13 +0800133 aead_request_set_callback(subreq, req->base.flags, compl, data);
134 aead_request_set_crypt(subreq, req->src, req->dst,
135 req->cryptlen - ivsize, req->iv);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800136 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800137
138 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
Herbert Xu856e3f402015-05-21 15:11:13 +0800139
140 return crypto_aead_decrypt(subreq);
141}
142
Herbert Xu06771572015-05-23 15:41:51 +0800143static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu14df4d82007-12-12 12:27:26 +0800144{
Herbert Xu856e3f402015-05-21 15:11:13 +0800145 struct aead_instance *inst;
Herbert Xu06771572015-05-23 15:41:51 +0800146 int err;
Herbert Xu14df4d82007-12-12 12:27:26 +0800147
Herbert Xu06771572015-05-23 15:41:51 +0800148 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
Herbert Xu14df4d82007-12-12 12:27:26 +0800149
150 if (IS_ERR(inst))
Herbert Xu06771572015-05-23 15:41:51 +0800151 return PTR_ERR(inst);
152
Herbert Xu06771572015-05-23 15:41:51 +0800153 err = -EINVAL;
Herbert Xudd044462015-05-23 15:41:56 +0800154 if (inst->alg.ivsize != sizeof(u64))
Herbert Xu06771572015-05-23 15:41:51 +0800155 goto free_inst;
Herbert Xuc0ecf892015-01-16 19:51:20 +1100156
Herbert Xub7dcfab2015-06-03 14:49:27 +0800157 inst->alg.encrypt = seqiv_aead_encrypt;
Herbert Xu856e3f402015-05-21 15:11:13 +0800158 inst->alg.decrypt = seqiv_aead_decrypt;
Herbert Xu14df4d82007-12-12 12:27:26 +0800159
Herbert Xu659e7f52015-08-13 17:28:59 +0800160 inst->alg.init = aead_init_geniv;
161 inst->alg.exit = aead_exit_geniv;
Herbert Xu856e3f402015-05-21 15:11:13 +0800162
Herbert Xu659e7f52015-08-13 17:28:59 +0800163 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
Herbert Xu5964f262015-07-09 07:17:22 +0800164 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
Herbert Xu856e3f402015-05-21 15:11:13 +0800165
Herbert Xu06771572015-05-23 15:41:51 +0800166 err = aead_register_instance(tmpl, inst);
167 if (err)
168 goto free_inst;
169
Herbert Xu14df4d82007-12-12 12:27:26 +0800170out:
Herbert Xu06771572015-05-23 15:41:51 +0800171 return err;
172
173free_inst:
174 aead_geniv_free(inst);
175 goto out;
Herbert Xu14df4d82007-12-12 12:27:26 +0800176}
177
Herbert Xu06771572015-05-23 15:41:51 +0800178static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu14df4d82007-12-12 12:27:26 +0800179{
180 struct crypto_attr_type *algt;
Herbert Xu14df4d82007-12-12 12:27:26 +0800181
182 algt = crypto_get_attr_type(tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800183 if (IS_ERR(algt))
Herbert Xu06771572015-05-23 15:41:51 +0800184 return PTR_ERR(algt);
Herbert Xu14df4d82007-12-12 12:27:26 +0800185
186 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
Herbert Xu3a01d0e2016-07-12 13:17:50 +0800187 return -EINVAL;
Herbert Xu14df4d82007-12-12 12:27:26 +0800188
Herbert Xu3a01d0e2016-07-12 13:17:50 +0800189 return seqiv_aead_create(tmpl, tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800190}
191
192static void seqiv_free(struct crypto_instance *inst)
193{
Herbert Xu3a01d0e2016-07-12 13:17:50 +0800194 aead_geniv_free(aead_instance(inst));
Herbert Xu14df4d82007-12-12 12:27:26 +0800195}
196
Herbert Xu0a270322007-11-30 21:38:37 +1100197static struct crypto_template seqiv_tmpl = {
198 .name = "seqiv",
Herbert Xu06771572015-05-23 15:41:51 +0800199 .create = seqiv_create,
Herbert Xu14df4d82007-12-12 12:27:26 +0800200 .free = seqiv_free,
Herbert Xu0a270322007-11-30 21:38:37 +1100201 .module = THIS_MODULE,
202};
203
204static int __init seqiv_module_init(void)
205{
Herbert Xu8a2cd1c2015-08-13 17:28:53 +0800206 return crypto_register_template(&seqiv_tmpl);
Herbert Xu0a270322007-11-30 21:38:37 +1100207}
208
209static void __exit seqiv_module_exit(void)
210{
211 crypto_unregister_template(&seqiv_tmpl);
212}
213
Eric Biggersc4741b22019-04-11 21:57:42 -0700214subsys_initcall(seqiv_module_init);
Herbert Xu0a270322007-11-30 21:38:37 +1100215module_exit(seqiv_module_exit);
216
217MODULE_LICENSE("GPL");
218MODULE_DESCRIPTION("Sequence Number IV Generator");
Kees Cook4943ba12014-11-24 16:32:38 -0800219MODULE_ALIAS_CRYPTO("seqiv");