Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 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. |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 8 | */ |
| 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-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 16 | |
Eric Biggers | b3e3e2d | 2019-01-03 20:16:12 -0800 | [diff] [blame] | 17 | static int crypto_ofb_crypt(struct skcipher_request *req) |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 18 | { |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 19 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
Eric Biggers | 21f3ca6 | 2019-01-03 20:16:20 -0800 | [diff] [blame] | 20 | struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); |
Eric Biggers | b3e3e2d | 2019-01-03 20:16:12 -0800 | [diff] [blame] | 21 | const unsigned int bsize = crypto_cipher_blocksize(cipher); |
| 22 | struct skcipher_walk walk; |
| 23 | int err; |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 24 | |
Eric Biggers | b3e3e2d | 2019-01-03 20:16:12 -0800 | [diff] [blame] | 25 | err = skcipher_walk_virt(&walk, req, false); |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 26 | |
Eric Biggers | b3e3e2d | 2019-01-03 20:16:12 -0800 | [diff] [blame] | 27 | 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-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 32 | |
Eric Biggers | b3e3e2d | 2019-01-03 20:16:12 -0800 | [diff] [blame] | 33 | 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-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Eric Biggers | b3e3e2d | 2019-01-03 20:16:12 -0800 | [diff] [blame] | 43 | 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-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 52 | static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb) |
| 53 | { |
| 54 | struct skcipher_instance *inst; |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 55 | struct crypto_alg *alg; |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 56 | int err; |
| 57 | |
Eric Biggers | 21f3ca6 | 2019-01-03 20:16:20 -0800 | [diff] [blame] | 58 | inst = skcipher_alloc_instance_simple(tmpl, tb, &alg); |
| 59 | if (IS_ERR(inst)) |
| 60 | return PTR_ERR(inst); |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 61 | |
Eric Biggers | b3e3e2d | 2019-01-03 20:16:12 -0800 | [diff] [blame] | 62 | /* OFB mode is a stream cipher. */ |
| 63 | inst->alg.base.cra_blocksize = 1; |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 64 | |
Eric Biggers | b3e3e2d | 2019-01-03 20:16:12 -0800 | [diff] [blame] | 65 | /* |
| 66 | * To simplify the implementation, configure the skcipher walk to only |
| 67 | * give a partial block at the very end, never earlier. |
| 68 | */ |
| 69 | inst->alg.chunksize = alg->cra_blocksize; |
| 70 | |
Eric Biggers | b3e3e2d | 2019-01-03 20:16:12 -0800 | [diff] [blame] | 71 | inst->alg.encrypt = crypto_ofb_crypt; |
| 72 | inst->alg.decrypt = crypto_ofb_crypt; |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 73 | |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 74 | err = skcipher_register_instance(tmpl, inst); |
| 75 | if (err) |
Eric Biggers | 21f3ca6 | 2019-01-03 20:16:20 -0800 | [diff] [blame] | 76 | inst->free(inst); |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 77 | |
Eric Biggers | 21f3ca6 | 2019-01-03 20:16:20 -0800 | [diff] [blame] | 78 | crypto_mod_put(alg); |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 79 | return err; |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static struct crypto_template crypto_ofb_tmpl = { |
| 83 | .name = "ofb", |
| 84 | .create = crypto_ofb_create, |
| 85 | .module = THIS_MODULE, |
| 86 | }; |
| 87 | |
| 88 | static int __init crypto_ofb_module_init(void) |
| 89 | { |
| 90 | return crypto_register_template(&crypto_ofb_tmpl); |
| 91 | } |
| 92 | |
| 93 | static void __exit crypto_ofb_module_exit(void) |
| 94 | { |
| 95 | crypto_unregister_template(&crypto_ofb_tmpl); |
| 96 | } |
| 97 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 98 | subsys_initcall(crypto_ofb_module_init); |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 99 | module_exit(crypto_ofb_module_exit); |
| 100 | |
| 101 | MODULE_LICENSE("GPL"); |
Eric Biggers | 21f3ca6 | 2019-01-03 20:16:20 -0800 | [diff] [blame] | 102 | MODULE_DESCRIPTION("OFB block cipher mode of operation"); |
Gilad Ben-Yossef | e497c51 | 2018-09-20 14:18:39 +0100 | [diff] [blame] | 103 | MODULE_ALIAS_CRYPTO("ofb"); |