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
);
84 } while ((nbytes
-= bsize
) >= bsize
);
86 memcpy(walk
->iv
, iv
, bsize
);
91 static int crypto_cfb_encrypt_inplace(struct skcipher_walk
*walk
,
92 struct crypto_skcipher
*tfm
)
94 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
95 unsigned int nbytes
= walk
->nbytes
;
96 u8
*src
= walk
->src
.virt
.addr
;
98 u8 tmp
[MAX_CIPHER_BLOCKSIZE
];
101 crypto_cfb_encrypt_one(tfm
, iv
, tmp
);
102 crypto_xor(src
, tmp
, bsize
);
106 } while ((nbytes
-= bsize
) >= bsize
);
108 memcpy(walk
->iv
, iv
, bsize
);
113 static int crypto_cfb_encrypt(struct skcipher_request
*req
)
115 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
116 struct skcipher_walk walk
;
117 unsigned int bsize
= crypto_cfb_bsize(tfm
);
120 err
= skcipher_walk_virt(&walk
, req
, false);
122 while (walk
.nbytes
>= bsize
) {
123 if (walk
.src
.virt
.addr
== walk
.dst
.virt
.addr
)
124 err
= crypto_cfb_encrypt_inplace(&walk
, tfm
);
126 err
= crypto_cfb_encrypt_segment(&walk
, tfm
);
127 err
= skcipher_walk_done(&walk
, err
);
131 crypto_cfb_final(&walk
, tfm
);
132 err
= skcipher_walk_done(&walk
, 0);
138 static int crypto_cfb_decrypt_segment(struct skcipher_walk
*walk
,
139 struct crypto_skcipher
*tfm
)
141 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
142 unsigned int nbytes
= walk
->nbytes
;
143 u8
*src
= walk
->src
.virt
.addr
;
144 u8
*dst
= walk
->dst
.virt
.addr
;
148 crypto_cfb_encrypt_one(tfm
, iv
, dst
);
149 crypto_xor(dst
, src
, bsize
);
154 } while ((nbytes
-= bsize
) >= bsize
);
156 memcpy(walk
->iv
, iv
, bsize
);
161 static int crypto_cfb_decrypt_inplace(struct skcipher_walk
*walk
,
162 struct crypto_skcipher
*tfm
)
164 const unsigned int bsize
= crypto_cfb_bsize(tfm
);
165 unsigned int nbytes
= walk
->nbytes
;
166 u8
*src
= walk
->src
.virt
.addr
;
167 u8
* const iv
= walk
->iv
;
168 u8 tmp
[MAX_CIPHER_BLOCKSIZE
];
171 crypto_cfb_encrypt_one(tfm
, iv
, tmp
);
172 memcpy(iv
, src
, bsize
);
173 crypto_xor(src
, tmp
, bsize
);
175 } while ((nbytes
-= bsize
) >= 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
);
292 err
= crypto_inst_setname(skcipher_crypto_instance(inst
), "cfb", alg
);
296 inst
->alg
.base
.cra_priority
= alg
->cra_priority
;
297 /* we're a stream cipher independend of the crypto cra_blocksize */
298 inst
->alg
.base
.cra_blocksize
= 1;
299 inst
->alg
.base
.cra_alignmask
= alg
->cra_alignmask
;
302 * To simplify the implementation, configure the skcipher walk to only
303 * give a partial block at the very end, never earlier.
305 inst
->alg
.chunksize
= alg
->cra_blocksize
;
307 inst
->alg
.ivsize
= alg
->cra_blocksize
;
308 inst
->alg
.min_keysize
= alg
->cra_cipher
.cia_min_keysize
;
309 inst
->alg
.max_keysize
= alg
->cra_cipher
.cia_max_keysize
;
311 inst
->alg
.base
.cra_ctxsize
= sizeof(struct crypto_cfb_ctx
);
313 inst
->alg
.init
= crypto_cfb_init_tfm
;
314 inst
->alg
.exit
= crypto_cfb_exit_tfm
;
316 inst
->alg
.setkey
= crypto_cfb_setkey
;
317 inst
->alg
.encrypt
= crypto_cfb_encrypt
;
318 inst
->alg
.decrypt
= crypto_cfb_decrypt
;
320 inst
->free
= crypto_cfb_free
;
322 err
= skcipher_register_instance(tmpl
, inst
);
331 crypto_drop_spawn(spawn
);
339 static struct crypto_template crypto_cfb_tmpl
= {
341 .create
= crypto_cfb_create
,
342 .module
= THIS_MODULE
,
345 static int __init
crypto_cfb_module_init(void)
347 return crypto_register_template(&crypto_cfb_tmpl
);
350 static void __exit
crypto_cfb_module_exit(void)
352 crypto_unregister_template(&crypto_cfb_tmpl
);
355 module_init(crypto_cfb_module_init
);
356 module_exit(crypto_cfb_module_exit
);
358 MODULE_LICENSE("GPL");
359 MODULE_DESCRIPTION("CFB block cipher algorithm");
360 MODULE_ALIAS_CRYPTO("cfb");