Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /*
0004  * OFB: Output FeedBack mode
0005  *
0006  * Copyright (C) 2018 ARM Limited or its affiliates.
0007  * All rights reserved.
0008  */
0009 
0010 #include <crypto/algapi.h>
0011 #include <crypto/internal/cipher.h>
0012 #include <crypto/internal/skcipher.h>
0013 #include <linux/err.h>
0014 #include <linux/init.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 
0018 static int crypto_ofb_crypt(struct skcipher_request *req)
0019 {
0020     struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0021     struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
0022     const unsigned int bsize = crypto_cipher_blocksize(cipher);
0023     struct skcipher_walk walk;
0024     int err;
0025 
0026     err = skcipher_walk_virt(&walk, req, false);
0027 
0028     while (walk.nbytes >= bsize) {
0029         const u8 *src = walk.src.virt.addr;
0030         u8 *dst = walk.dst.virt.addr;
0031         u8 * const iv = walk.iv;
0032         unsigned int nbytes = walk.nbytes;
0033 
0034         do {
0035             crypto_cipher_encrypt_one(cipher, iv, iv);
0036             crypto_xor_cpy(dst, src, iv, bsize);
0037             dst += bsize;
0038             src += bsize;
0039         } while ((nbytes -= bsize) >= bsize);
0040 
0041         err = skcipher_walk_done(&walk, nbytes);
0042     }
0043 
0044     if (walk.nbytes) {
0045         crypto_cipher_encrypt_one(cipher, walk.iv, walk.iv);
0046         crypto_xor_cpy(walk.dst.virt.addr, walk.src.virt.addr, walk.iv,
0047                    walk.nbytes);
0048         err = skcipher_walk_done(&walk, 0);
0049     }
0050     return err;
0051 }
0052 
0053 static int crypto_ofb_create(struct crypto_template *tmpl, struct rtattr **tb)
0054 {
0055     struct skcipher_instance *inst;
0056     struct crypto_alg *alg;
0057     int err;
0058 
0059     inst = skcipher_alloc_instance_simple(tmpl, tb);
0060     if (IS_ERR(inst))
0061         return PTR_ERR(inst);
0062 
0063     alg = skcipher_ialg_simple(inst);
0064 
0065     /* OFB mode is a stream cipher. */
0066     inst->alg.base.cra_blocksize = 1;
0067 
0068     /*
0069      * To simplify the implementation, configure the skcipher walk to only
0070      * give a partial block at the very end, never earlier.
0071      */
0072     inst->alg.chunksize = alg->cra_blocksize;
0073 
0074     inst->alg.encrypt = crypto_ofb_crypt;
0075     inst->alg.decrypt = crypto_ofb_crypt;
0076 
0077     err = skcipher_register_instance(tmpl, inst);
0078     if (err)
0079         inst->free(inst);
0080 
0081     return err;
0082 }
0083 
0084 static struct crypto_template crypto_ofb_tmpl = {
0085     .name = "ofb",
0086     .create = crypto_ofb_create,
0087     .module = THIS_MODULE,
0088 };
0089 
0090 static int __init crypto_ofb_module_init(void)
0091 {
0092     return crypto_register_template(&crypto_ofb_tmpl);
0093 }
0094 
0095 static void __exit crypto_ofb_module_exit(void)
0096 {
0097     crypto_unregister_template(&crypto_ofb_tmpl);
0098 }
0099 
0100 subsys_initcall(crypto_ofb_module_init);
0101 module_exit(crypto_ofb_module_exit);
0102 
0103 MODULE_LICENSE("GPL");
0104 MODULE_DESCRIPTION("OFB block cipher mode of operation");
0105 MODULE_ALIAS_CRYPTO("ofb");
0106 MODULE_IMPORT_NS(CRYPTO_INTERNAL);