Blame view
crypto/echainiv.c
4.08 KB
2874c5fd2 treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
a10f554fa crypto: echainiv ... |
2 3 4 |
/* * echainiv: Encrypted Chain IV Generator * |
53a5d5ddc crypto: echainiv ... |
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 crypto: echainiv ... |
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 crypto: echainiv ... |
14 |
*/ |
d97de47ca crypto: echainiv ... |
15 |
#include <crypto/internal/geniv.h> |
a10f554fa crypto: echainiv ... |
16 |
#include <crypto/scatterwalk.h> |
0e8bff47f crypto: echainiv ... |
17 |
#include <crypto/skcipher.h> |
a10f554fa crypto: echainiv ... |
18 19 20 |
#include <linux/err.h> #include <linux/init.h> #include <linux/kernel.h> |
a10f554fa crypto: echainiv ... |
21 |
#include <linux/module.h> |
53a5d5ddc crypto: echainiv ... |
22 |
#include <linux/slab.h> |
a10f554fa crypto: echainiv ... |
23 |
#include <linux/string.h> |
a10f554fa crypto: echainiv ... |
24 25 26 |
static int echainiv_encrypt(struct aead_request *req) { struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
376e0d697 crypto: echainiv ... |
27 |
struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
a10f554fa crypto: echainiv ... |
28 |
struct aead_request *subreq = aead_request_ctx(req); |
53a5d5ddc crypto: echainiv ... |
29 30 |
__be64 nseqno; u64 seqno; |
a10f554fa crypto: echainiv ... |
31 |
u8 *info; |
823655c99 crypto: echainiv ... |
32 |
unsigned int ivsize = crypto_aead_ivsize(geniv); |
a10f554fa crypto: echainiv ... |
33 |
int err; |
823655c99 crypto: echainiv ... |
34 35 |
if (req->cryptlen < ivsize) return -EINVAL; |
376e0d697 crypto: echainiv ... |
36 |
aead_request_set_tfm(subreq, ctx->child); |
a10f554fa crypto: echainiv ... |
37 |
|
a10f554fa crypto: echainiv ... |
38 |
info = req->iv; |
a10f554fa crypto: echainiv ... |
39 |
if (req->src != req->dst) { |
8d6053984 crypto: null - Re... |
40 |
SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull); |
a10f554fa crypto: echainiv ... |
41 |
|
8d6053984 crypto: null - Re... |
42 |
skcipher_request_set_sync_tfm(nreq, ctx->sknull); |
0e8bff47f crypto: echainiv ... |
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 crypto: echainiv ... |
50 51 52 |
if (err) return err; } |
53a5d5ddc crypto: echainiv ... |
53 54 |
aead_request_set_callback(subreq, req->base.flags, req->base.complete, req->base.data); |
a10f554fa crypto: echainiv ... |
55 |
aead_request_set_crypt(subreq, req->dst, req->dst, |
5499b1a73 crypto: echainiv ... |
56 57 |
req->cryptlen, info); aead_request_set_ad(subreq, req->assoclen); |
a10f554fa crypto: echainiv ... |
58 |
|
53a5d5ddc crypto: echainiv ... |
59 60 61 |
memcpy(&nseqno, info + ivsize - 8, 8); seqno = be64_to_cpu(nseqno); memset(info, 0, ivsize); |
a10f554fa crypto: echainiv ... |
62 |
scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1); |
a10f554fa crypto: echainiv ... |
63 |
|
53a5d5ddc crypto: echainiv ... |
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 crypto: echainiv ... |
76 |
} |
a10f554fa crypto: echainiv ... |
77 78 79 |
static int echainiv_decrypt(struct aead_request *req) { struct crypto_aead *geniv = crypto_aead_reqtfm(req); |
376e0d697 crypto: echainiv ... |
80 |
struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv); |
a10f554fa crypto: echainiv ... |
81 82 83 |
struct aead_request *subreq = aead_request_ctx(req); crypto_completion_t compl; void *data; |
823655c99 crypto: echainiv ... |
84 |
unsigned int ivsize = crypto_aead_ivsize(geniv); |
5499b1a73 crypto: echainiv ... |
85 |
if (req->cryptlen < ivsize) |
823655c99 crypto: echainiv ... |
86 |
return -EINVAL; |
a10f554fa crypto: echainiv ... |
87 |
|
376e0d697 crypto: echainiv ... |
88 |
aead_request_set_tfm(subreq, ctx->child); |
a10f554fa crypto: echainiv ... |
89 90 91 |
compl = req->base.complete; data = req->base.data; |
a10f554fa crypto: echainiv ... |
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 crypto: aead - Re... |
95 |
aead_request_set_ad(subreq, req->assoclen + ivsize); |
a10f554fa crypto: echainiv ... |
96 97 |
scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0); |
a10f554fa crypto: echainiv ... |
98 99 100 |
return crypto_aead_decrypt(subreq); } |
1e419c797 crypto: echainiv ... |
101 102 |
static int echainiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb) |
a10f554fa crypto: echainiv ... |
103 104 |
{ struct aead_instance *inst; |
1e419c797 crypto: echainiv ... |
105 |
int err; |
a10f554fa crypto: echainiv ... |
106 |
|
e72b48c5e crypto: geniv - r... |
107 |
inst = aead_geniv_alloc(tmpl, tb); |
a10f554fa crypto: echainiv ... |
108 109 |
if (IS_ERR(inst)) |
1e419c797 crypto: echainiv ... |
110 |
return PTR_ERR(inst); |
a10f554fa crypto: echainiv ... |
111 |
|
1e419c797 crypto: echainiv ... |
112 |
err = -EINVAL; |
53a5d5ddc crypto: echainiv ... |
113 |
if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize) |
1e419c797 crypto: echainiv ... |
114 |
goto free_inst; |
a10f554fa crypto: echainiv ... |
115 |
|
f261c5fbe crypto: echainiv ... |
116 |
inst->alg.encrypt = echainiv_encrypt; |
a10f554fa crypto: echainiv ... |
117 |
inst->alg.decrypt = echainiv_decrypt; |
376e0d697 crypto: echainiv ... |
118 119 |
inst->alg.init = aead_init_geniv; inst->alg.exit = aead_exit_geniv; |
a10f554fa crypto: echainiv ... |
120 |
|
376e0d697 crypto: echainiv ... |
121 |
inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx); |
9d03aee12 crypto: echainiv ... |
122 |
inst->alg.base.cra_ctxsize += inst->alg.ivsize; |
a10f554fa crypto: echainiv ... |
123 |
|
1e419c797 crypto: echainiv ... |
124 |
err = aead_register_instance(tmpl, inst); |
0f8f6d86d crypto: geniv - c... |
125 |
if (err) { |
1e419c797 crypto: echainiv ... |
126 |
free_inst: |
0f8f6d86d crypto: geniv - c... |
127 128 129 |
inst->free(inst); } return err; |
a10f554fa crypto: echainiv ... |
130 131 132 133 |
} static struct crypto_template echainiv_tmpl = { .name = "echainiv", |
9fcc704df crypto: echainiv ... |
134 |
.create = echainiv_aead_create, |
a10f554fa crypto: echainiv ... |
135 136 137 138 139 140 141 142 143 144 145 146 |
.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 crypto: run initc... |
147 |
subsys_initcall(echainiv_module_init); |
a10f554fa crypto: echainiv ... |
148 149 150 151 152 |
module_exit(echainiv_module_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Encrypted Chain IV Generator"); MODULE_ALIAS_CRYPTO("echainiv"); |