1 //SPDX-License-Identifier: GPL-2.0
3 * CFB: Cipher FeedBack mode
5 * Copyright (c) 2018 James.Bottomley@HansenPartnership.com
7 * CFB is a stream cipher mode which is layered on to a block
8 * encryption scheme. It works very much like a one time pad where
9 * the pad is generated initially from the encrypted IV and then
10 * subsequently from the encrypted previous block of ciphertext. The
11 * pad is XOR'd into the plain text to get the final ciphertext.
13 * The scheme of CFB is best described by wikipedia:
15 * https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#CFB
17 * Note that since the pad for both encryption and decryption is
18 * generated by an encryption operation, CFB never uses the block
19 * decryption function.
22 #include <crypto/algapi.h>
23 #include <crypto/internal/skcipher.h>
24 #include <linux/err.h>
25 #include <linux/init.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/string.h>
30 #include <linux/types.h>
32 struct crypto_cfb_ctx
{
33 struct crypto_cipher
*child
;
36 static unsigned int crypto_cfb_bsize(struct crypto_skcipher
*tfm
)
38 struct crypto_cfb_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
39 struct crypto_cipher
*child
= ctx
->child
;
41 return crypto_cipher_blocksize(child
);
44 static void crypto_cfb_encrypt_one(struct crypto_skcipher
*tfm
,
45 const u8
*src
, u8
*dst
)
47 struct crypto_cfb_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
49 crypto_cipher_encrypt_one(ctx
->child
, dst
, src
);
52 /* final encrypt and decrypt is the same */
53 static void crypto_cfb_final(struct skcipher_walk
*walk
,
54 struct crypto_skcipher
*tfm
)
56 const unsigned long alignmask
= crypto_skcipher_alignmask(tfm
);
57 u8 tmp
[MAX_CIPHER_BLOCKSIZE
+ MAX_CIPHER_ALIGNMASK
];
58 u8
*stream
= PTR_ALIGN(tmp
+ 0, alignmask
+ 1);
59 u8
*src
= walk
->src
.virt
.addr
;
60 u8
*dst
= walk
->dst
.virt
.addr
;
62 unsigned int nbytes
= walk
->nbytes
;
64 crypto_cfb_encrypt_one(tfm
, iv
, stream
);
65 crypto_xor_cpy(dst
, stream
, src
, nbytes
);
68 static int crypto_cfb_encrypt_segment(struct skcipher_walk
*walk
,
69 struct crypto_skcipher
*tfm
)
71 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
72 unsigned int nbytes
= walk
->nbytes
;
73 u8
*src
= walk
->src
.virt
.addr
;
74 u8
*dst
= walk
->dst
.virt
.addr
;
78 crypto_cfb_encrypt_one(tfm
, iv
, dst
);
79 crypto_xor(dst
, src
, bsize
);
80 memcpy(iv
, dst
, bsize
);
84 } while ((nbytes
-= bsize
) >= bsize
);
89 static int crypto_cfb_encrypt_inplace(struct skcipher_walk
*walk
,
90 struct crypto_skcipher
*tfm
)
92 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
93 unsigned int nbytes
= walk
->nbytes
;
94 u8
*src
= walk
->src
.virt
.addr
;
96 u8 tmp
[MAX_CIPHER_BLOCKSIZE
];
99 crypto_cfb_encrypt_one(tfm
, iv
, tmp
);
100 crypto_xor(src
, tmp
, bsize
);
104 } while ((nbytes
-= bsize
) >= bsize
);
106 memcpy(walk
->iv
, iv
, bsize
);
111 static int crypto_cfb_encrypt(struct skcipher_request
*req
)
113 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
114 struct skcipher_walk walk
;
115 unsigned int bsize
= crypto_cfb_bsize(tfm
);
118 err
= skcipher_walk_virt(&walk
, req
, false);
120 while (walk
.nbytes
>= bsize
) {
121 if (walk
.src
.virt
.addr
== walk
.dst
.virt
.addr
)
122 err
= crypto_cfb_encrypt_inplace(&walk
, tfm
);
124 err
= crypto_cfb_encrypt_segment(&walk
, tfm
);
125 err
= skcipher_walk_done(&walk
, err
);
129 crypto_cfb_final(&walk
, tfm
);
130 err
= skcipher_walk_done(&walk
, 0);
136 static int crypto_cfb_decrypt_segment(struct skcipher_walk
*walk
,
137 struct crypto_skcipher
*tfm
)
139 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
140 unsigned int nbytes
= walk
->nbytes
;
141 u8
*src
= walk
->src
.virt
.addr
;
142 u8
*dst
= walk
->dst
.virt
.addr
;
146 crypto_cfb_encrypt_one(tfm
, iv
, dst
);
147 crypto_xor(dst
, iv
, bsize
);
152 } while ((nbytes
-= bsize
) >= bsize
);
154 memcpy(walk
->iv
, iv
, bsize
);
159 static int crypto_cfb_decrypt_inplace(struct skcipher_walk
*walk
,
160 struct crypto_skcipher
*tfm
)
162 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
163 unsigned int nbytes
= walk
->nbytes
;
164 u8
*src
= walk
->src
.virt
.addr
;
166 u8 tmp
[MAX_CIPHER_BLOCKSIZE
];
169 crypto_cfb_encrypt_one(tfm
, iv
, tmp
);
170 memcpy(iv
, src
, bsize
);
171 crypto_xor(src
, tmp
, bsize
);
173 } while ((nbytes
-= bsize
) >= bsize
);
175 memcpy(walk
->iv
, iv
, bsize
);
180 static int crypto_cfb_decrypt_blocks(struct skcipher_walk
*walk
,
181 struct crypto_skcipher
*tfm
)
183 if (walk
->src
.virt
.addr
== walk
->dst
.virt
.addr
)
184 return crypto_cfb_decrypt_inplace(walk
, tfm
);
186 return crypto_cfb_decrypt_segment(walk
, tfm
);
189 static int crypto_cfb_setkey(struct crypto_skcipher
*parent
, const u8
*key
,
192 struct crypto_cfb_ctx
*ctx
= crypto_skcipher_ctx(parent
);
193 struct crypto_cipher
*child
= ctx
->child
;
196 crypto_cipher_clear_flags(child
, CRYPTO_TFM_REQ_MASK
);
197 crypto_cipher_set_flags(child
, crypto_skcipher_get_flags(parent
) &
198 CRYPTO_TFM_REQ_MASK
);
199 err
= crypto_cipher_setkey(child
, key
, keylen
);
200 crypto_skcipher_set_flags(parent
, crypto_cipher_get_flags(child
) &
201 CRYPTO_TFM_RES_MASK
);
205 static int crypto_cfb_decrypt(struct skcipher_request
*req
)
207 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
208 struct skcipher_walk walk
;
209 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
212 err
= skcipher_walk_virt(&walk
, req
, false);
214 while (walk
.nbytes
>= bsize
) {
215 err
= crypto_cfb_decrypt_blocks(&walk
, tfm
);
216 err
= skcipher_walk_done(&walk
, err
);
220 crypto_cfb_final(&walk
, tfm
);
221 err
= skcipher_walk_done(&walk
, 0);
227 static int crypto_cfb_init_tfm(struct crypto_skcipher
*tfm
)
229 struct skcipher_instance
*inst
= skcipher_alg_instance(tfm
);
230 struct crypto_spawn
*spawn
= skcipher_instance_ctx(inst
);
231 struct crypto_cfb_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
232 struct crypto_cipher
*cipher
;
234 cipher
= crypto_spawn_cipher(spawn
);
236 return PTR_ERR(cipher
);
242 static void crypto_cfb_exit_tfm(struct crypto_skcipher
*tfm
)
244 struct crypto_cfb_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
246 crypto_free_cipher(ctx
->child
);
249 static void crypto_cfb_free(struct skcipher_instance
*inst
)
251 crypto_drop_skcipher(skcipher_instance_ctx(inst
));
255 static int crypto_cfb_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
257 struct skcipher_instance
*inst
;
258 struct crypto_attr_type
*algt
;
259 struct crypto_spawn
*spawn
;
260 struct crypto_alg
*alg
;
264 err
= crypto_check_attr_type(tb
, CRYPTO_ALG_TYPE_SKCIPHER
);
268 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
272 algt
= crypto_get_attr_type(tb
);
277 mask
= CRYPTO_ALG_TYPE_MASK
|
278 crypto_requires_off(algt
->type
, algt
->mask
,
279 CRYPTO_ALG_NEED_FALLBACK
);
281 alg
= crypto_get_attr_alg(tb
, CRYPTO_ALG_TYPE_CIPHER
, mask
);
286 spawn
= skcipher_instance_ctx(inst
);
287 err
= crypto_init_spawn(spawn
, alg
, skcipher_crypto_instance(inst
),
288 CRYPTO_ALG_TYPE_MASK
);
293 err
= crypto_inst_setname(skcipher_crypto_instance(inst
), "cfb", alg
);
297 inst
->alg
.base
.cra_priority
= alg
->cra_priority
;
298 /* we're a stream cipher independend of the crypto cra_blocksize */
299 inst
->alg
.base
.cra_blocksize
= 1;
300 inst
->alg
.base
.cra_alignmask
= alg
->cra_alignmask
;
302 inst
->alg
.ivsize
= alg
->cra_blocksize
;
303 inst
->alg
.min_keysize
= alg
->cra_cipher
.cia_min_keysize
;
304 inst
->alg
.max_keysize
= alg
->cra_cipher
.cia_max_keysize
;
306 inst
->alg
.base
.cra_ctxsize
= sizeof(struct crypto_cfb_ctx
);
308 inst
->alg
.init
= crypto_cfb_init_tfm
;
309 inst
->alg
.exit
= crypto_cfb_exit_tfm
;
311 inst
->alg
.setkey
= crypto_cfb_setkey
;
312 inst
->alg
.encrypt
= crypto_cfb_encrypt
;
313 inst
->alg
.decrypt
= crypto_cfb_decrypt
;
315 inst
->free
= crypto_cfb_free
;
317 err
= skcipher_register_instance(tmpl
, inst
);
325 crypto_drop_spawn(spawn
);
331 static struct crypto_template crypto_cfb_tmpl
= {
333 .create
= crypto_cfb_create
,
334 .module
= THIS_MODULE
,
337 static int __init
crypto_cfb_module_init(void)
339 return crypto_register_template(&crypto_cfb_tmpl
);
342 static void __exit
crypto_cfb_module_exit(void)
344 crypto_unregister_template(&crypto_cfb_tmpl
);
347 module_init(crypto_cfb_module_init
);
348 module_exit(crypto_cfb_module_exit
);
350 MODULE_LICENSE("GPL");
351 MODULE_DESCRIPTION("CFB block cipher algorithm");
352 MODULE_ALIAS_CRYPTO("cfb");