Blame view
crypto/pcbc.c
4.7 KB
2874c5fd2
|
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
91652be5d
|
2 3 4 5 6 7 8 9 |
/* * PCBC: Propagating Cipher Block Chaining mode * * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com) * * Derived from cbc.c * - Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> |
91652be5d
|
10 |
*/ |
6650c4de6
|
11 |
#include <crypto/algapi.h> |
043a44001
|
12 |
#include <crypto/internal/skcipher.h> |
91652be5d
|
13 14 15 16 |
#include <linux/err.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/module.h> |
91652be5d
|
17 |
|
043a44001
|
18 19 |
static int crypto_pcbc_encrypt_segment(struct skcipher_request *req, struct skcipher_walk *walk, |
d0b9007a2
|
20 |
struct crypto_cipher *tfm) |
91652be5d
|
21 |
{ |
91652be5d
|
22 23 24 25 |
int bsize = crypto_cipher_blocksize(tfm); unsigned int nbytes = walk->nbytes; u8 *src = walk->src.virt.addr; u8 *dst = walk->dst.virt.addr; |
251b7aea3
|
26 |
u8 * const iv = walk->iv; |
91652be5d
|
27 28 |
do { |
d0b9007a2
|
29 |
crypto_xor(iv, src, bsize); |
043a44001
|
30 |
crypto_cipher_encrypt_one(tfm, dst, iv); |
45fe93dff
|
31 |
crypto_xor_cpy(iv, dst, src, bsize); |
91652be5d
|
32 33 34 35 36 37 38 |
src += bsize; dst += bsize; } while ((nbytes -= bsize) >= bsize); return nbytes; } |
043a44001
|
39 40 |
static int crypto_pcbc_encrypt_inplace(struct skcipher_request *req, struct skcipher_walk *walk, |
d0b9007a2
|
41 |
struct crypto_cipher *tfm) |
91652be5d
|
42 |
{ |
91652be5d
|
43 44 45 |
int bsize = crypto_cipher_blocksize(tfm); unsigned int nbytes = walk->nbytes; u8 *src = walk->src.virt.addr; |
251b7aea3
|
46 |
u8 * const iv = walk->iv; |
6650c4de6
|
47 |
u8 tmpbuf[MAX_CIPHER_BLOCKSIZE]; |
91652be5d
|
48 49 50 |
do { memcpy(tmpbuf, src, bsize); |
d0b9007a2
|
51 |
crypto_xor(iv, src, bsize); |
043a44001
|
52 |
crypto_cipher_encrypt_one(tfm, src, iv); |
45fe93dff
|
53 |
crypto_xor_cpy(iv, tmpbuf, src, bsize); |
91652be5d
|
54 55 56 |
src += bsize; } while ((nbytes -= bsize) >= bsize); |
91652be5d
|
57 58 |
return nbytes; } |
043a44001
|
59 |
static int crypto_pcbc_encrypt(struct skcipher_request *req) |
91652be5d
|
60 |
{ |
043a44001
|
61 |
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
0be487ba2
|
62 |
struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
043a44001
|
63 64 |
struct skcipher_walk walk; unsigned int nbytes; |
91652be5d
|
65 |
int err; |
043a44001
|
66 |
err = skcipher_walk_virt(&walk, req, false); |
91652be5d
|
67 68 69 |
while ((nbytes = walk.nbytes)) { if (walk.src.virt.addr == walk.dst.virt.addr) |
043a44001
|
70 |
nbytes = crypto_pcbc_encrypt_inplace(req, &walk, |
0be487ba2
|
71 |
cipher); |
91652be5d
|
72 |
else |
043a44001
|
73 |
nbytes = crypto_pcbc_encrypt_segment(req, &walk, |
0be487ba2
|
74 |
cipher); |
043a44001
|
75 |
err = skcipher_walk_done(&walk, nbytes); |
91652be5d
|
76 77 78 79 |
} return err; } |
043a44001
|
80 81 |
static int crypto_pcbc_decrypt_segment(struct skcipher_request *req, struct skcipher_walk *walk, |
d0b9007a2
|
82 |
struct crypto_cipher *tfm) |
91652be5d
|
83 |
{ |
91652be5d
|
84 85 86 87 |
int bsize = crypto_cipher_blocksize(tfm); unsigned int nbytes = walk->nbytes; u8 *src = walk->src.virt.addr; u8 *dst = walk->dst.virt.addr; |
251b7aea3
|
88 |
u8 * const iv = walk->iv; |
91652be5d
|
89 90 |
do { |
043a44001
|
91 |
crypto_cipher_decrypt_one(tfm, dst, src); |
d0b9007a2
|
92 |
crypto_xor(dst, iv, bsize); |
45fe93dff
|
93 |
crypto_xor_cpy(iv, dst, src, bsize); |
91652be5d
|
94 95 96 97 |
src += bsize; dst += bsize; } while ((nbytes -= bsize) >= bsize); |
91652be5d
|
98 99 |
return nbytes; } |
043a44001
|
100 101 |
static int crypto_pcbc_decrypt_inplace(struct skcipher_request *req, struct skcipher_walk *walk, |
d0b9007a2
|
102 |
struct crypto_cipher *tfm) |
91652be5d
|
103 |
{ |
91652be5d
|
104 105 106 |
int bsize = crypto_cipher_blocksize(tfm); unsigned int nbytes = walk->nbytes; u8 *src = walk->src.virt.addr; |
251b7aea3
|
107 |
u8 * const iv = walk->iv; |
6650c4de6
|
108 |
u8 tmpbuf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(u32)); |
91652be5d
|
109 110 111 |
do { memcpy(tmpbuf, src, bsize); |
043a44001
|
112 |
crypto_cipher_decrypt_one(tfm, src, src); |
d0b9007a2
|
113 |
crypto_xor(src, iv, bsize); |
45fe93dff
|
114 |
crypto_xor_cpy(iv, src, tmpbuf, bsize); |
91652be5d
|
115 116 117 |
src += bsize; } while ((nbytes -= bsize) >= bsize); |
91652be5d
|
118 119 |
return nbytes; } |
043a44001
|
120 |
static int crypto_pcbc_decrypt(struct skcipher_request *req) |
91652be5d
|
121 |
{ |
043a44001
|
122 |
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
0be487ba2
|
123 |
struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
043a44001
|
124 125 |
struct skcipher_walk walk; unsigned int nbytes; |
91652be5d
|
126 |
int err; |
043a44001
|
127 |
err = skcipher_walk_virt(&walk, req, false); |
91652be5d
|
128 129 130 |
while ((nbytes = walk.nbytes)) { if (walk.src.virt.addr == walk.dst.virt.addr) |
043a44001
|
131 |
nbytes = crypto_pcbc_decrypt_inplace(req, &walk, |
0be487ba2
|
132 |
cipher); |
91652be5d
|
133 |
else |
043a44001
|
134 |
nbytes = crypto_pcbc_decrypt_segment(req, &walk, |
0be487ba2
|
135 |
cipher); |
043a44001
|
136 |
err = skcipher_walk_done(&walk, nbytes); |
91652be5d
|
137 138 139 140 |
} return err; } |
043a44001
|
141 |
static int crypto_pcbc_create(struct crypto_template *tmpl, struct rtattr **tb) |
91652be5d
|
142 |
{ |
043a44001
|
143 |
struct skcipher_instance *inst; |
91652be5d
|
144 |
struct crypto_alg *alg; |
ebc610e5b
|
145 |
int err; |
0be487ba2
|
146 147 148 |
inst = skcipher_alloc_instance_simple(tmpl, tb, &alg); if (IS_ERR(inst)) return PTR_ERR(inst); |
91652be5d
|
149 |
|
043a44001
|
150 151 |
inst->alg.encrypt = crypto_pcbc_encrypt; inst->alg.decrypt = crypto_pcbc_decrypt; |
91652be5d
|
152 |
|
043a44001
|
153 154 |
err = skcipher_register_instance(tmpl, inst); if (err) |
0be487ba2
|
155 |
inst->free(inst); |
e5bde04cc
|
156 |
crypto_mod_put(alg); |
043a44001
|
157 |
return err; |
91652be5d
|
158 159 160 161 |
} static struct crypto_template crypto_pcbc_tmpl = { .name = "pcbc", |
043a44001
|
162 |
.create = crypto_pcbc_create, |
91652be5d
|
163 164 165 166 167 168 169 170 171 172 173 174 |
.module = THIS_MODULE, }; static int __init crypto_pcbc_module_init(void) { return crypto_register_template(&crypto_pcbc_tmpl); } static void __exit crypto_pcbc_module_exit(void) { crypto_unregister_template(&crypto_pcbc_tmpl); } |
c4741b230
|
175 |
subsys_initcall(crypto_pcbc_module_init); |
91652be5d
|
176 177 178 |
module_exit(crypto_pcbc_module_exit); MODULE_LICENSE("GPL"); |
0be487ba2
|
179 |
MODULE_DESCRIPTION("PCBC block cipher mode of operation"); |
4943ba16b
|
180 |
MODULE_ALIAS_CRYPTO("pcbc"); |