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