18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci/* 48c2ecf20Sopenharmony_ci * OFB: Output FeedBack mode 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 2018 ARM Limited or its affiliates. 78c2ecf20Sopenharmony_ci * All rights reserved. 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <crypto/algapi.h> 118c2ecf20Sopenharmony_ci#include <crypto/internal/skcipher.h> 128c2ecf20Sopenharmony_ci#include <linux/err.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/kernel.h> 158c2ecf20Sopenharmony_ci#include <linux/module.h> 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cistatic int crypto_ofb_crypt(struct skcipher_request *req) 188c2ecf20Sopenharmony_ci{ 198c2ecf20Sopenharmony_ci struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 208c2ecf20Sopenharmony_ci struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); 218c2ecf20Sopenharmony_ci const unsigned int bsize = crypto_cipher_blocksize(cipher); 228c2ecf20Sopenharmony_ci struct skcipher_walk walk; 238c2ecf20Sopenharmony_ci int err; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci err = skcipher_walk_virt(&walk, req, false); 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci while (walk.nbytes >= bsize) { 288c2ecf20Sopenharmony_ci const u8 *src = walk.src.virt.addr; 298c2ecf20Sopenharmony_ci u8 *dst = walk.dst.virt.addr; 308c2ecf20Sopenharmony_ci u8 * const iv = walk.iv; 318c2ecf20Sopenharmony_ci unsigned int nbytes = walk.nbytes; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci do { 348c2ecf20Sopenharmony_ci crypto_cipher_encrypt_one(cipher, iv, iv); 358c2ecf20Sopenharmony_ci crypto_xor_cpy(dst, src, iv, bsize); 368c2ecf20Sopenharmony_ci dst += bsize; 378c2ecf20Sopenharmony_ci src += bsize; 388c2ecf20Sopenharmony_ci } while ((nbytes -= bsize) >= bsize); 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci err = skcipher_walk_done(&walk, nbytes); 418c2ecf20Sopenharmony_ci } 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci if (walk.nbytes) { 448c2ecf20Sopenharmony_ci crypto_cipher_encrypt_one(cipher, walk.iv, walk.iv); 458c2ecf20Sopenharmony_ci crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, walk.iv, 468c2ecf20Sopenharmony_ci walk.nbytes); 478c2ecf20Sopenharmony_ci err = skcipher_walk_done(&walk, 0); 488c2ecf20Sopenharmony_ci } 498c2ecf20Sopenharmony_ci return err; 508c2ecf20Sopenharmony_ci} 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci struct skcipher_instance *inst; 558c2ecf20Sopenharmony_ci struct crypto_alg *alg; 568c2ecf20Sopenharmony_ci int err; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci inst = skcipher_alloc_instance_simple(tmpl, tb); 598c2ecf20Sopenharmony_ci if (IS_ERR(inst)) 608c2ecf20Sopenharmony_ci return PTR_ERR(inst); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci alg = skcipher_ialg_simple(inst); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci /* OFB mode is a stream cipher. */ 658c2ecf20Sopenharmony_ci inst->alg.base.cra_blocksize = 1; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci /* 688c2ecf20Sopenharmony_ci * To simplify the implementation, configure the skcipher walk to only 698c2ecf20Sopenharmony_ci * give a partial block at the very end, never earlier. 708c2ecf20Sopenharmony_ci */ 718c2ecf20Sopenharmony_ci inst->alg.chunksize = alg->cra_blocksize; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci inst->alg.encrypt = crypto_ofb_crypt; 748c2ecf20Sopenharmony_ci inst->alg.decrypt = crypto_ofb_crypt; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci err = skcipher_register_instance(tmpl, inst); 778c2ecf20Sopenharmony_ci if (err) 788c2ecf20Sopenharmony_ci inst->free(inst); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci return err; 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistatic struct crypto_template crypto_ofb_tmpl = { 848c2ecf20Sopenharmony_ci .name = "ofb", 858c2ecf20Sopenharmony_ci .create = crypto_ofb_create, 868c2ecf20Sopenharmony_ci .module = THIS_MODULE, 878c2ecf20Sopenharmony_ci}; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cistatic int __init crypto_ofb_module_init(void) 908c2ecf20Sopenharmony_ci{ 918c2ecf20Sopenharmony_ci return crypto_register_template(&crypto_ofb_tmpl); 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_cistatic void __exit crypto_ofb_module_exit(void) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci crypto_unregister_template(&crypto_ofb_tmpl); 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cisubsys_initcall(crypto_ofb_module_init); 1008c2ecf20Sopenharmony_cimodule_exit(crypto_ofb_module_exit); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1038c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("OFB block cipher mode of operation"); 1048c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("ofb"); 105