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 int bsize
= crypto_cfb_bsize(tfm
);
57 const unsigned long alignmask
= crypto_skcipher_alignmask(tfm
);
58 u8 tmp
[bsize
+ alignmask
];
59 u8
*stream
= PTR_ALIGN(tmp
+ 0, alignmask
+ 1);
60 u8
*src
= walk
->src
.virt
.addr
;
61 u8
*dst
= walk
->dst
.virt
.addr
;
63 unsigned int nbytes
= walk
->nbytes
;
65 crypto_cfb_encrypt_one(tfm
, iv
, stream
);
66 crypto_xor_cpy(dst
, stream
, src
, nbytes
);
69 static int crypto_cfb_encrypt_segment(struct skcipher_walk
*walk
,
70 struct crypto_skcipher
*tfm
)
72 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
73 unsigned int nbytes
= walk
->nbytes
;
74 u8
*src
= walk
->src
.virt
.addr
;
75 u8
*dst
= walk
->dst
.virt
.addr
;
79 crypto_cfb_encrypt_one(tfm
, iv
, dst
);
80 crypto_xor(dst
, src
, bsize
);
81 memcpy(iv
, dst
, bsize
);
85 } while ((nbytes
-= bsize
) >= bsize
);
90 static int crypto_cfb_encrypt_inplace(struct skcipher_walk
*walk
,
91 struct crypto_skcipher
*tfm
)
93 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
94 unsigned int nbytes
= walk
->nbytes
;
95 u8
*src
= walk
->src
.virt
.addr
;
100 crypto_cfb_encrypt_one(tfm
, iv
, tmp
);
101 crypto_xor(src
, tmp
, bsize
);
105 } while ((nbytes
-= bsize
) >= bsize
);
107 memcpy(walk
->iv
, iv
, bsize
);
112 static int crypto_cfb_encrypt(struct skcipher_request
*req
)
114 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
115 struct skcipher_walk walk
;
116 unsigned int bsize
= crypto_cfb_bsize(tfm
);
119 err
= skcipher_walk_virt(&walk
, req
, false);
121 while (walk
.nbytes
>= bsize
) {
122 if (walk
.src
.virt
.addr
== walk
.dst
.virt
.addr
)
123 err
= crypto_cfb_encrypt_inplace(&walk
, tfm
);
125 err
= crypto_cfb_encrypt_segment(&walk
, tfm
);
126 err
= skcipher_walk_done(&walk
, err
);
130 crypto_cfb_final(&walk
, tfm
);
131 err
= skcipher_walk_done(&walk
, 0);
137 static int crypto_cfb_decrypt_segment(struct skcipher_walk
*walk
,
138 struct crypto_skcipher
*tfm
)
140 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
141 unsigned int nbytes
= walk
->nbytes
;
142 u8
*src
= walk
->src
.virt
.addr
;
143 u8
*dst
= walk
->dst
.virt
.addr
;
147 crypto_cfb_encrypt_one(tfm
, iv
, dst
);
148 crypto_xor(dst
, iv
, bsize
);
153 } while ((nbytes
-= bsize
) >= bsize
);
155 memcpy(walk
->iv
, iv
, bsize
);
160 static int crypto_cfb_decrypt_inplace(struct skcipher_walk
*walk
,
161 struct crypto_skcipher
*tfm
)
163 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
164 unsigned int nbytes
= walk
->nbytes
;
165 u8
*src
= walk
->src
.virt
.addr
;
170 crypto_cfb_encrypt_one(tfm
, iv
, tmp
);
171 memcpy(iv
, src
, bsize
);
172 crypto_xor(src
, tmp
, bsize
);
174 } while ((nbytes
-= bsize
) >= bsize
);
176 memcpy(walk
->iv
, iv
, bsize
);
181 static int crypto_cfb_decrypt_blocks(struct skcipher_walk
*walk
,
182 struct crypto_skcipher
*tfm
)
184 if (walk
->src
.virt
.addr
== walk
->dst
.virt
.addr
)
185 return crypto_cfb_decrypt_inplace(walk
, tfm
);
187 return crypto_cfb_decrypt_segment(walk
, tfm
);
190 static int crypto_cfb_setkey(struct crypto_skcipher
*parent
, const u8
*key
,
193 struct crypto_cfb_ctx
*ctx
= crypto_skcipher_ctx(parent
);
194 struct crypto_cipher
*child
= ctx
->child
;
197 crypto_cipher_clear_flags(child
, CRYPTO_TFM_REQ_MASK
);
198 crypto_cipher_set_flags(child
, crypto_skcipher_get_flags(parent
) &
199 CRYPTO_TFM_REQ_MASK
);
200 err
= crypto_cipher_setkey(child
, key
, keylen
);
201 crypto_skcipher_set_flags(parent
, crypto_cipher_get_flags(child
) &
202 CRYPTO_TFM_RES_MASK
);
206 static int crypto_cfb_decrypt(struct skcipher_request
*req
)
208 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
209 struct skcipher_walk walk
;
210 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
213 err
= skcipher_walk_virt(&walk
, req
, false);
215 while (walk
.nbytes
>= bsize
) {
216 err
= crypto_cfb_decrypt_blocks(&walk
, tfm
);
217 err
= skcipher_walk_done(&walk
, err
);
221 crypto_cfb_final(&walk
, tfm
);
222 err
= skcipher_walk_done(&walk
, 0);
228 static int crypto_cfb_init_tfm(struct crypto_skcipher
*tfm
)
230 struct skcipher_instance
*inst
= skcipher_alg_instance(tfm
);
231 struct crypto_spawn
*spawn
= skcipher_instance_ctx(inst
);
232 struct crypto_cfb_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
233 struct crypto_cipher
*cipher
;
235 cipher
= crypto_spawn_cipher(spawn
);
237 return PTR_ERR(cipher
);
243 static void crypto_cfb_exit_tfm(struct crypto_skcipher
*tfm
)
245 struct crypto_cfb_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
247 crypto_free_cipher(ctx
->child
);
250 static void crypto_cfb_free(struct skcipher_instance
*inst
)
252 crypto_drop_skcipher(skcipher_instance_ctx(inst
));
256 static int crypto_cfb_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
258 struct skcipher_instance
*inst
;
259 struct crypto_attr_type
*algt
;
260 struct crypto_spawn
*spawn
;
261 struct crypto_alg
*alg
;
265 err
= crypto_check_attr_type(tb
, CRYPTO_ALG_TYPE_SKCIPHER
);
269 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
273 algt
= crypto_get_attr_type(tb
);
278 mask
= CRYPTO_ALG_TYPE_MASK
|
279 crypto_requires_off(algt
->type
, algt
->mask
,
280 CRYPTO_ALG_NEED_FALLBACK
);
282 alg
= crypto_get_attr_alg(tb
, CRYPTO_ALG_TYPE_CIPHER
, mask
);
287 spawn
= skcipher_instance_ctx(inst
);
288 err
= crypto_init_spawn(spawn
, alg
, skcipher_crypto_instance(inst
),
289 CRYPTO_ALG_TYPE_MASK
);
294 err
= crypto_inst_setname(skcipher_crypto_instance(inst
), "cfb", alg
);
298 inst
->alg
.base
.cra_priority
= alg
->cra_priority
;
299 /* we're a stream cipher independend of the crypto cra_blocksize */
300 inst
->alg
.base
.cra_blocksize
= 1;
301 inst
->alg
.base
.cra_alignmask
= alg
->cra_alignmask
;
303 inst
->alg
.ivsize
= alg
->cra_blocksize
;
304 inst
->alg
.min_keysize
= alg
->cra_cipher
.cia_min_keysize
;
305 inst
->alg
.max_keysize
= alg
->cra_cipher
.cia_max_keysize
;
307 inst
->alg
.base
.cra_ctxsize
= sizeof(struct crypto_cfb_ctx
);
309 inst
->alg
.init
= crypto_cfb_init_tfm
;
310 inst
->alg
.exit
= crypto_cfb_exit_tfm
;
312 inst
->alg
.setkey
= crypto_cfb_setkey
;
313 inst
->alg
.encrypt
= crypto_cfb_encrypt
;
314 inst
->alg
.decrypt
= crypto_cfb_decrypt
;
316 inst
->free
= crypto_cfb_free
;
318 err
= skcipher_register_instance(tmpl
, inst
);
326 crypto_drop_spawn(spawn
);
332 static struct crypto_template crypto_cfb_tmpl
= {
334 .create
= crypto_cfb_create
,
335 .module
= THIS_MODULE
,
338 static int __init
crypto_cfb_module_init(void)
340 return crypto_register_template(&crypto_cfb_tmpl
);
343 static void __exit
crypto_cfb_module_exit(void)
345 crypto_unregister_template(&crypto_cfb_tmpl
);
348 module_init(crypto_cfb_module_init
);
349 module_exit(crypto_cfb_module_exit
);
351 MODULE_LICENSE("GPL");
352 MODULE_DESCRIPTION("CFB block cipher algorithm");
353 MODULE_ALIAS_CRYPTO("cfb");