blob: b630fdecceee33e8421875ad7cf45104fd1a1b12 [file] [log] [blame]
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +01001// 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.
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +01008 */
9
10#include <crypto/algapi.h>
Ard Biesheuvel0eb76ba2020-12-11 13:27:15 +010011#include <crypto/internal/cipher.h>
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010012#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>
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010017
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080018static int crypto_ofb_crypt(struct skcipher_request *req)
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010019{
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010020 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Eric Biggers21f3ca62019-01-03 20:16:20 -080021 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080022 const unsigned int bsize = crypto_cipher_blocksize(cipher);
23 struct skcipher_walk walk;
24 int err;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010025
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080026 err = skcipher_walk_virt(&walk, req, false);
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010027
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080028 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;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010033
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080034 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);
40
41 err = skcipher_walk_done(&walk, nbytes);
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010042 }
43
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080044 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;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010051}
52
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010053static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb)
54{
55 struct skcipher_instance *inst;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010056 struct crypto_alg *alg;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010057 int err;
58
Herbert Xub3c16bf2019-12-20 13:29:40 +080059 inst = skcipher_alloc_instance_simple(tmpl, tb);
Eric Biggers21f3ca62019-01-03 20:16:20 -080060 if (IS_ERR(inst))
61 return PTR_ERR(inst);
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010062
Herbert Xub3c16bf2019-12-20 13:29:40 +080063 alg = skcipher_ialg_simple(inst);
64
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080065 /* OFB mode is a stream cipher. */
66 inst->alg.base.cra_blocksize = 1;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010067
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080068 /*
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
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080074 inst->alg.encrypt = crypto_ofb_crypt;
75 inst->alg.decrypt = crypto_ofb_crypt;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010076
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010077 err = skcipher_register_instance(tmpl, inst);
78 if (err)
Eric Biggers21f3ca62019-01-03 20:16:20 -080079 inst->free(inst);
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010080
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010081 return err;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010082}
83
84static struct crypto_template crypto_ofb_tmpl = {
85 .name = "ofb",
86 .create = crypto_ofb_create,
87 .module = THIS_MODULE,
88};
89
90static int __init crypto_ofb_module_init(void)
91{
92 return crypto_register_template(&crypto_ofb_tmpl);
93}
94
95static void __exit crypto_ofb_module_exit(void)
96{
97 crypto_unregister_template(&crypto_ofb_tmpl);
98}
99
Eric Biggersc4741b22019-04-11 21:57:42 -0700100subsys_initcall(crypto_ofb_module_init);
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +0100101module_exit(crypto_ofb_module_exit);
102
103MODULE_LICENSE("GPL");
Eric Biggers21f3ca62019-01-03 20:16:20 -0800104MODULE_DESCRIPTION("OFB block cipher mode of operation");
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +0100105MODULE_ALIAS_CRYPTO("ofb");
Ard Biesheuvel0eb76ba2020-12-11 13:27:15 +0100106MODULE_IMPORT_NS(CRYPTO_INTERNAL);