Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * MIPS accelerated ChaCha and XChaCha stream ciphers,
0004  * including ChaCha20 (RFC7539)
0005  *
0006  * Copyright (C) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
0007  */
0008 
0009 #include <asm/byteorder.h>
0010 #include <crypto/algapi.h>
0011 #include <crypto/internal/chacha.h>
0012 #include <crypto/internal/skcipher.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 
0016 asmlinkage void chacha_crypt_arch(u32 *state, u8 *dst, const u8 *src,
0017                   unsigned int bytes, int nrounds);
0018 EXPORT_SYMBOL(chacha_crypt_arch);
0019 
0020 asmlinkage void hchacha_block_arch(const u32 *state, u32 *stream, int nrounds);
0021 EXPORT_SYMBOL(hchacha_block_arch);
0022 
0023 void chacha_init_arch(u32 *state, const u32 *key, const u8 *iv)
0024 {
0025     chacha_init_generic(state, key, iv);
0026 }
0027 EXPORT_SYMBOL(chacha_init_arch);
0028 
0029 static int chacha_mips_stream_xor(struct skcipher_request *req,
0030                   const struct chacha_ctx *ctx, const u8 *iv)
0031 {
0032     struct skcipher_walk walk;
0033     u32 state[16];
0034     int err;
0035 
0036     err = skcipher_walk_virt(&walk, req, false);
0037 
0038     chacha_init_generic(state, ctx->key, iv);
0039 
0040     while (walk.nbytes > 0) {
0041         unsigned int nbytes = walk.nbytes;
0042 
0043         if (nbytes < walk.total)
0044             nbytes = round_down(nbytes, walk.stride);
0045 
0046         chacha_crypt(state, walk.dst.virt.addr, walk.src.virt.addr,
0047                  nbytes, ctx->nrounds);
0048         err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
0049     }
0050 
0051     return err;
0052 }
0053 
0054 static int chacha_mips(struct skcipher_request *req)
0055 {
0056     struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0057     struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
0058 
0059     return chacha_mips_stream_xor(req, ctx, req->iv);
0060 }
0061 
0062 static int xchacha_mips(struct skcipher_request *req)
0063 {
0064     struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0065     struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
0066     struct chacha_ctx subctx;
0067     u32 state[16];
0068     u8 real_iv[16];
0069 
0070     chacha_init_generic(state, ctx->key, req->iv);
0071 
0072     hchacha_block(state, subctx.key, ctx->nrounds);
0073     subctx.nrounds = ctx->nrounds;
0074 
0075     memcpy(&real_iv[0], req->iv + 24, 8);
0076     memcpy(&real_iv[8], req->iv + 16, 8);
0077     return chacha_mips_stream_xor(req, &subctx, real_iv);
0078 }
0079 
0080 static struct skcipher_alg algs[] = {
0081     {
0082         .base.cra_name      = "chacha20",
0083         .base.cra_driver_name   = "chacha20-mips",
0084         .base.cra_priority  = 200,
0085         .base.cra_blocksize = 1,
0086         .base.cra_ctxsize   = sizeof(struct chacha_ctx),
0087         .base.cra_module    = THIS_MODULE,
0088 
0089         .min_keysize        = CHACHA_KEY_SIZE,
0090         .max_keysize        = CHACHA_KEY_SIZE,
0091         .ivsize         = CHACHA_IV_SIZE,
0092         .chunksize      = CHACHA_BLOCK_SIZE,
0093         .setkey         = chacha20_setkey,
0094         .encrypt        = chacha_mips,
0095         .decrypt        = chacha_mips,
0096     }, {
0097         .base.cra_name      = "xchacha20",
0098         .base.cra_driver_name   = "xchacha20-mips",
0099         .base.cra_priority  = 200,
0100         .base.cra_blocksize = 1,
0101         .base.cra_ctxsize   = sizeof(struct chacha_ctx),
0102         .base.cra_module    = THIS_MODULE,
0103 
0104         .min_keysize        = CHACHA_KEY_SIZE,
0105         .max_keysize        = CHACHA_KEY_SIZE,
0106         .ivsize         = XCHACHA_IV_SIZE,
0107         .chunksize      = CHACHA_BLOCK_SIZE,
0108         .setkey         = chacha20_setkey,
0109         .encrypt        = xchacha_mips,
0110         .decrypt        = xchacha_mips,
0111     }, {
0112         .base.cra_name      = "xchacha12",
0113         .base.cra_driver_name   = "xchacha12-mips",
0114         .base.cra_priority  = 200,
0115         .base.cra_blocksize = 1,
0116         .base.cra_ctxsize   = sizeof(struct chacha_ctx),
0117         .base.cra_module    = THIS_MODULE,
0118 
0119         .min_keysize        = CHACHA_KEY_SIZE,
0120         .max_keysize        = CHACHA_KEY_SIZE,
0121         .ivsize         = XCHACHA_IV_SIZE,
0122         .chunksize      = CHACHA_BLOCK_SIZE,
0123         .setkey         = chacha12_setkey,
0124         .encrypt        = xchacha_mips,
0125         .decrypt        = xchacha_mips,
0126     }
0127 };
0128 
0129 static int __init chacha_simd_mod_init(void)
0130 {
0131     return IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER) ?
0132         crypto_register_skciphers(algs, ARRAY_SIZE(algs)) : 0;
0133 }
0134 
0135 static void __exit chacha_simd_mod_fini(void)
0136 {
0137     if (IS_REACHABLE(CONFIG_CRYPTO_SKCIPHER))
0138         crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
0139 }
0140 
0141 module_init(chacha_simd_mod_init);
0142 module_exit(chacha_simd_mod_fini);
0143 
0144 MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (MIPS accelerated)");
0145 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
0146 MODULE_LICENSE("GPL v2");
0147 MODULE_ALIAS_CRYPTO("chacha20");
0148 MODULE_ALIAS_CRYPTO("chacha20-mips");
0149 MODULE_ALIAS_CRYPTO("xchacha20");
0150 MODULE_ALIAS_CRYPTO("xchacha20-mips");
0151 MODULE_ALIAS_CRYPTO("xchacha12");
0152 MODULE_ALIAS_CRYPTO("xchacha12-mips");