blob: 2ec68e3f2c552b3e925c41591042ebbade3be6dd [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>
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>
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010016
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080017static int crypto_ofb_crypt(struct skcipher_request *req)
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010018{
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010019 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
Eric Biggers21f3ca62019-01-03 20:16:20 -080020 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080021 const unsigned int bsize = crypto_cipher_blocksize(cipher);
22 struct skcipher_walk walk;
23 int err;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010024
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080025 err = skcipher_walk_virt(&walk, req, false);
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010026
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080027 while (walk.nbytes >= bsize) {
28 const u8 *src = walk.src.virt.addr;
29 u8 *dst = walk.dst.virt.addr;
30 u8 * const iv = walk.iv;
31 unsigned int nbytes = walk.nbytes;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010032
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080033 do {
34 crypto_cipher_encrypt_one(cipher, iv, iv);
35 crypto_xor_cpy(dst, src, iv, bsize);
36 dst += bsize;
37 src += bsize;
38 } while ((nbytes -= bsize) >= bsize);
39
40 err = skcipher_walk_done(&walk, nbytes);
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010041 }
42
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080043 if (walk.nbytes) {
44 crypto_cipher_encrypt_one(cipher, walk.iv, walk.iv);
45 crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, walk.iv,
46 walk.nbytes);
47 err = skcipher_walk_done(&walk, 0);
48 }
49 return err;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010050}
51
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010052static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb)
53{
54 struct skcipher_instance *inst;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010055 struct crypto_alg *alg;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010056 int err;
57
Herbert Xub3c16bf2019-12-20 13:29:40 +080058 inst = skcipher_alloc_instance_simple(tmpl, tb);
Eric Biggers21f3ca62019-01-03 20:16:20 -080059 if (IS_ERR(inst))
60 return PTR_ERR(inst);
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010061
Herbert Xub3c16bf2019-12-20 13:29:40 +080062 alg = skcipher_ialg_simple(inst);
63
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080064 /* OFB mode is a stream cipher. */
65 inst->alg.base.cra_blocksize = 1;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010066
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080067 /*
68 * To simplify the implementation, configure the skcipher walk to only
69 * give a partial block at the very end, never earlier.
70 */
71 inst->alg.chunksize = alg->cra_blocksize;
72
Eric Biggersb3e3e2d2019-01-03 20:16:12 -080073 inst->alg.encrypt = crypto_ofb_crypt;
74 inst->alg.decrypt = crypto_ofb_crypt;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010075
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010076 err = skcipher_register_instance(tmpl, inst);
77 if (err)
Eric Biggers21f3ca62019-01-03 20:16:20 -080078 inst->free(inst);
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010079
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010080 return err;
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +010081}
82
83static struct crypto_template crypto_ofb_tmpl = {
84 .name = "ofb",
85 .create = crypto_ofb_create,
86 .module = THIS_MODULE,
87};
88
89static int __init crypto_ofb_module_init(void)
90{
91 return crypto_register_template(&crypto_ofb_tmpl);
92}
93
94static void __exit crypto_ofb_module_exit(void)
95{
96 crypto_unregister_template(&crypto_ofb_tmpl);
97}
98
Eric Biggersc4741b22019-04-11 21:57:42 -070099subsys_initcall(crypto_ofb_module_init);
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +0100100module_exit(crypto_ofb_module_exit);
101
102MODULE_LICENSE("GPL");
Eric Biggers21f3ca62019-01-03 20:16:20 -0800103MODULE_DESCRIPTION("OFB block cipher mode of operation");
Gilad Ben-Yossefe497c512018-09-20 14:18:39 +0100104MODULE_ALIAS_CRYPTO("ofb");