]>
Commit | Line | Data |
---|---|---|
e497c518 GBY |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
3 | /* | |
4 | * OFB: Output FeedBack mode | |
5 | * | |
6 | * Copyright (C) 2018 ARM Limited or its affiliates. | |
7 | * All rights reserved. | |
e497c518 GBY |
8 | */ |
9 | ||
10 | #include <crypto/algapi.h> | |
0eb76ba2 | 11 | #include <crypto/internal/cipher.h> |
e497c518 GBY |
12 | #include <crypto/internal/skcipher.h> |
13 | #include <linux/err.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/kernel.h> | |
16 | #include <linux/module.h> | |
e497c518 | 17 | |
b3e3e2db | 18 | static int crypto_ofb_crypt(struct skcipher_request *req) |
e497c518 | 19 | { |
e497c518 | 20 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
21f3ca6c | 21 | struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
b3e3e2db EB |
22 | const unsigned int bsize = crypto_cipher_blocksize(cipher); |
23 | struct skcipher_walk walk; | |
24 | int err; | |
e497c518 | 25 | |
b3e3e2db | 26 | err = skcipher_walk_virt(&walk, req, false); |
e497c518 | 27 | |
b3e3e2db EB |
28 | while (walk.nbytes >= bsize) { |
29 | const u8 *src = walk.src.virt.addr; | |
30 | u8 *dst = walk.dst.virt.addr; | |
31 | u8 * const iv = walk.iv; | |
32 | unsigned int nbytes = walk.nbytes; | |
e497c518 | 33 | |
b3e3e2db EB |
34 | do { |
35 | crypto_cipher_encrypt_one(cipher, iv, iv); | |
36 | crypto_xor_cpy(dst, src, iv, bsize); | |
37 | dst += bsize; | |
38 | src += bsize; | |
39 | } while ((nbytes -= bsize) >= bsize); | |
e497c518 | 40 | |
b3e3e2db EB |
41 | err = skcipher_walk_done(&walk, nbytes); |
42 | } | |
e497c518 | 43 | |
b3e3e2db EB |
44 | if (walk.nbytes) { |
45 | crypto_cipher_encrypt_one(cipher, walk.iv, walk.iv); | |
46 | crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, walk.iv, | |
47 | walk.nbytes); | |
48 | err = skcipher_walk_done(&walk, 0); | |
49 | } | |
50 | return err; | |
e497c518 GBY |
51 | } |
52 | ||
e497c518 GBY |
53 | static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb) |
54 | { | |
55 | struct skcipher_instance *inst; | |
e497c518 | 56 | struct crypto_alg *alg; |
e497c518 GBY |
57 | int err; |
58 | ||
b3c16bfc | 59 | inst = skcipher_alloc_instance_simple(tmpl, tb); |
21f3ca6c EB |
60 | if (IS_ERR(inst)) |
61 | return PTR_ERR(inst); | |
e497c518 | 62 | |
b3c16bfc HX |
63 | alg = skcipher_ialg_simple(inst); |
64 | ||
b3e3e2db EB |
65 | /* OFB mode is a stream cipher. */ |
66 | inst->alg.base.cra_blocksize = 1; | |
67 | ||
68 | /* | |
69 | * To simplify the implementation, configure the skcipher walk to only | |
70 | * give a partial block at the very end, never earlier. | |
71 | */ | |
72 | inst->alg.chunksize = alg->cra_blocksize; | |
73 | ||
b3e3e2db EB |
74 | inst->alg.encrypt = crypto_ofb_crypt; |
75 | inst->alg.decrypt = crypto_ofb_crypt; | |
e497c518 | 76 | |
e497c518 GBY |
77 | err = skcipher_register_instance(tmpl, inst); |
78 | if (err) | |
21f3ca6c | 79 | inst->free(inst); |
e497c518 | 80 | |
e497c518 | 81 | return err; |
e497c518 GBY |
82 | } |
83 | ||
84 | static struct crypto_template crypto_ofb_tmpl = { | |
85 | .name = "ofb", | |
86 | .create = crypto_ofb_create, | |
87 | .module = THIS_MODULE, | |
88 | }; | |
89 | ||
90 | static int __init crypto_ofb_module_init(void) | |
91 | { | |
92 | return crypto_register_template(&crypto_ofb_tmpl); | |
93 | } | |
94 | ||
95 | static void __exit crypto_ofb_module_exit(void) | |
96 | { | |
97 | crypto_unregister_template(&crypto_ofb_tmpl); | |
98 | } | |
99 | ||
c4741b23 | 100 | subsys_initcall(crypto_ofb_module_init); |
e497c518 GBY |
101 | module_exit(crypto_ofb_module_exit); |
102 | ||
103 | MODULE_LICENSE("GPL"); | |
21f3ca6c | 104 | MODULE_DESCRIPTION("OFB block cipher mode of operation"); |
e497c518 | 105 | MODULE_ALIAS_CRYPTO("ofb"); |
0eb76ba2 | 106 | MODULE_IMPORT_NS(CRYPTO_INTERNAL); |