1 // SPDX-License-Identifier: GPL-2.0
4 * OFB: Output FeedBack mode
6 * Copyright (C) 2018 ARM Limited or its affiliates.
10 #include <crypto/algapi.h>
11 #include <crypto/internal/skcipher.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/scatterlist.h>
17 #include <linux/slab.h>
19 struct crypto_ofb_ctx
{
20 struct crypto_cipher
*child
;
24 static int crypto_ofb_setkey(struct crypto_skcipher
*parent
, const u8
*key
,
27 struct crypto_ofb_ctx
*ctx
= crypto_skcipher_ctx(parent
);
28 struct crypto_cipher
*child
= ctx
->child
;
31 crypto_cipher_clear_flags(child
, CRYPTO_TFM_REQ_MASK
);
32 crypto_cipher_set_flags(child
, crypto_skcipher_get_flags(parent
) &
34 err
= crypto_cipher_setkey(child
, key
, keylen
);
35 crypto_skcipher_set_flags(parent
, crypto_cipher_get_flags(child
) &
40 static int crypto_ofb_crypt(struct skcipher_request
*req
)
42 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
43 struct crypto_ofb_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
44 struct crypto_cipher
*cipher
= ctx
->child
;
45 const unsigned int bsize
= crypto_cipher_blocksize(cipher
);
46 struct skcipher_walk walk
;
49 err
= skcipher_walk_virt(&walk
, req
, false);
51 while (walk
.nbytes
>= bsize
) {
52 const u8
*src
= walk
.src
.virt
.addr
;
53 u8
*dst
= walk
.dst
.virt
.addr
;
54 u8
* const iv
= walk
.iv
;
55 unsigned int nbytes
= walk
.nbytes
;
58 crypto_cipher_encrypt_one(cipher
, iv
, iv
);
59 crypto_xor_cpy(dst
, src
, iv
, bsize
);
62 } while ((nbytes
-= bsize
) >= bsize
);
64 err
= skcipher_walk_done(&walk
, nbytes
);
68 crypto_cipher_encrypt_one(cipher
, walk
.iv
, walk
.iv
);
69 crypto_xor_cpy(walk
.dst
.virt
.addr
, walk
.src
.virt
.addr
, walk
.iv
,
71 err
= skcipher_walk_done(&walk
, 0);
76 static int crypto_ofb_init_tfm(struct crypto_skcipher
*tfm
)
78 struct skcipher_instance
*inst
= skcipher_alg_instance(tfm
);
79 struct crypto_spawn
*spawn
= skcipher_instance_ctx(inst
);
80 struct crypto_ofb_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
81 struct crypto_cipher
*cipher
;
83 cipher
= crypto_spawn_cipher(spawn
);
85 return PTR_ERR(cipher
);
91 static void crypto_ofb_exit_tfm(struct crypto_skcipher
*tfm
)
93 struct crypto_ofb_ctx
*ctx
= crypto_skcipher_ctx(tfm
);
95 crypto_free_cipher(ctx
->child
);
98 static void crypto_ofb_free(struct skcipher_instance
*inst
)
100 crypto_drop_skcipher(skcipher_instance_ctx(inst
));
104 static int crypto_ofb_create(struct crypto_template
*tmpl
, struct rtattr
**tb
)
106 struct skcipher_instance
*inst
;
107 struct crypto_attr_type
*algt
;
108 struct crypto_spawn
*spawn
;
109 struct crypto_alg
*alg
;
113 err
= crypto_check_attr_type(tb
, CRYPTO_ALG_TYPE_SKCIPHER
);
117 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
121 algt
= crypto_get_attr_type(tb
);
126 mask
= CRYPTO_ALG_TYPE_MASK
|
127 crypto_requires_off(algt
->type
, algt
->mask
,
128 CRYPTO_ALG_NEED_FALLBACK
);
130 alg
= crypto_get_attr_alg(tb
, CRYPTO_ALG_TYPE_CIPHER
, mask
);
135 spawn
= skcipher_instance_ctx(inst
);
136 err
= crypto_init_spawn(spawn
, alg
, skcipher_crypto_instance(inst
),
137 CRYPTO_ALG_TYPE_MASK
);
142 err
= crypto_inst_setname(skcipher_crypto_instance(inst
), "ofb", alg
);
146 /* OFB mode is a stream cipher. */
147 inst
->alg
.base
.cra_blocksize
= 1;
150 * To simplify the implementation, configure the skcipher walk to only
151 * give a partial block at the very end, never earlier.
153 inst
->alg
.chunksize
= alg
->cra_blocksize
;
155 inst
->alg
.base
.cra_priority
= alg
->cra_priority
;
156 inst
->alg
.base
.cra_alignmask
= alg
->cra_alignmask
;
158 inst
->alg
.ivsize
= alg
->cra_blocksize
;
159 inst
->alg
.min_keysize
= alg
->cra_cipher
.cia_min_keysize
;
160 inst
->alg
.max_keysize
= alg
->cra_cipher
.cia_max_keysize
;
162 inst
->alg
.base
.cra_ctxsize
= sizeof(struct crypto_ofb_ctx
);
164 inst
->alg
.init
= crypto_ofb_init_tfm
;
165 inst
->alg
.exit
= crypto_ofb_exit_tfm
;
167 inst
->alg
.setkey
= crypto_ofb_setkey
;
168 inst
->alg
.encrypt
= crypto_ofb_crypt
;
169 inst
->alg
.decrypt
= crypto_ofb_crypt
;
171 inst
->free
= crypto_ofb_free
;
173 err
= skcipher_register_instance(tmpl
, inst
);
181 crypto_drop_spawn(spawn
);
187 static struct crypto_template crypto_ofb_tmpl
= {
189 .create
= crypto_ofb_create
,
190 .module
= THIS_MODULE
,
193 static int __init
crypto_ofb_module_init(void)
195 return crypto_register_template(&crypto_ofb_tmpl
);
198 static void __exit
crypto_ofb_module_exit(void)
200 crypto_unregister_template(&crypto_ofb_tmpl
);
203 module_init(crypto_ofb_module_init
);
204 module_exit(crypto_ofb_module_exit
);
206 MODULE_LICENSE("GPL");
207 MODULE_DESCRIPTION("OFB block cipher algorithm");
208 MODULE_ALIAS_CRYPTO("ofb");