zram: use __bio_add_page for adding single page to bio
[linux-block.git] / crypto / echainiv.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
a10f554f
HX
2/*
3 * echainiv: Encrypted Chain IV Generator
4 *
53a5d5dd
HX
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
a10f554f
HX
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>
a10f554f
HX
14 */
15
d97de47c 16#include <crypto/internal/geniv.h>
a10f554f 17#include <crypto/scatterwalk.h>
0e8bff47 18#include <crypto/skcipher.h>
a10f554f
HX
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
a10f554f 22#include <linux/module.h>
53a5d5dd 23#include <linux/slab.h>
a10f554f
HX
24#include <linux/string.h>
25
a10f554f
HX
26static int echainiv_encrypt(struct aead_request *req)
27{
28 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
376e0d69 29 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
a10f554f 30 struct aead_request *subreq = aead_request_ctx(req);
53a5d5dd
HX
31 __be64 nseqno;
32 u64 seqno;
a10f554f 33 u8 *info;
823655c9 34 unsigned int ivsize = crypto_aead_ivsize(geniv);
a10f554f
HX
35 int err;
36
823655c9
HX
37 if (req->cryptlen < ivsize)
38 return -EINVAL;
39
376e0d69 40 aead_request_set_tfm(subreq, ctx->child);
a10f554f 41
a10f554f
HX
42 info = req->iv;
43
a10f554f 44 if (req->src != req->dst) {
8d605398 45 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
a10f554f 46
8d605398 47 skcipher_request_set_sync_tfm(nreq, ctx->sknull);
0e8bff47
HX
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);
a10f554f
HX
55 if (err)
56 return err;
57 }
58
53a5d5dd
HX
59 aead_request_set_callback(subreq, req->base.flags,
60 req->base.complete, req->base.data);
a10f554f 61 aead_request_set_crypt(subreq, req->dst, req->dst,
5499b1a7
HX
62 req->cryptlen, info);
63 aead_request_set_ad(subreq, req->assoclen);
a10f554f 64
53a5d5dd
HX
65 memcpy(&nseqno, info + ivsize - 8, 8);
66 seqno = be64_to_cpu(nseqno);
67 memset(info, 0, ivsize);
68
a10f554f 69 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
a10f554f 70
53a5d5dd
HX
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);
a10f554f
HX
83}
84
a10f554f
HX
85static int echainiv_decrypt(struct aead_request *req)
86{
87 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
376e0d69 88 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
a10f554f
HX
89 struct aead_request *subreq = aead_request_ctx(req);
90 crypto_completion_t compl;
91 void *data;
823655c9
HX
92 unsigned int ivsize = crypto_aead_ivsize(geniv);
93
5499b1a7 94 if (req->cryptlen < ivsize)
823655c9 95 return -EINVAL;
a10f554f 96
376e0d69 97 aead_request_set_tfm(subreq, ctx->child);
a10f554f
HX
98
99 compl = req->base.complete;
100 data = req->base.data;
101
a10f554f
HX
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);
374d4ad1 105 aead_request_set_ad(subreq, req->assoclen + ivsize);
a10f554f
HX
106
107 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
a10f554f
HX
108
109 return crypto_aead_decrypt(subreq);
110}
111
1e419c79
HX
112static int echainiv_aead_create(struct crypto_template *tmpl,
113 struct rtattr **tb)
a10f554f
HX
114{
115 struct aead_instance *inst;
1e419c79 116 int err;
a10f554f 117
e72b48c5 118 inst = aead_geniv_alloc(tmpl, tb);
a10f554f
HX
119
120 if (IS_ERR(inst))
1e419c79 121 return PTR_ERR(inst);
a10f554f 122
1e419c79 123 err = -EINVAL;
53a5d5dd 124 if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize)
1e419c79 125 goto free_inst;
a10f554f 126
f261c5fb 127 inst->alg.encrypt = echainiv_encrypt;
a10f554f
HX
128 inst->alg.decrypt = echainiv_decrypt;
129
376e0d69
HX
130 inst->alg.init = aead_init_geniv;
131 inst->alg.exit = aead_exit_geniv;
a10f554f 132
376e0d69 133 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
9d03aee1 134 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
a10f554f 135
1e419c79 136 err = aead_register_instance(tmpl, inst);
0f8f6d86 137 if (err) {
1e419c79 138free_inst:
0f8f6d86
EB
139 inst->free(inst);
140 }
141 return err;
a10f554f
HX
142}
143
144static struct crypto_template echainiv_tmpl = {
145 .name = "echainiv",
9fcc704d 146 .create = echainiv_aead_create,
a10f554f
HX
147 .module = THIS_MODULE,
148};
149
150static int __init echainiv_module_init(void)
151{
152 return crypto_register_template(&echainiv_tmpl);
153}
154
155static void __exit echainiv_module_exit(void)
156{
157 crypto_unregister_template(&echainiv_tmpl);
158}
159
c4741b23 160subsys_initcall(echainiv_module_init);
a10f554f
HX
161module_exit(echainiv_module_exit);
162
163MODULE_LICENSE("GPL");
164MODULE_DESCRIPTION("Encrypted Chain IV Generator");
165MODULE_ALIAS_CRYPTO("echainiv");