Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * ChaCha and XChaCha stream ciphers, including ChaCha20 (RFC7539)
0004  *
0005  * Copyright (C) 2015 Martin Willi
0006  * Copyright (C) 2018 Google LLC
0007  */
0008 
0009 #include <asm/unaligned.h>
0010 #include <crypto/algapi.h>
0011 #include <crypto/internal/chacha.h>
0012 #include <crypto/internal/skcipher.h>
0013 #include <linux/module.h>
0014 
0015 static int chacha_stream_xor(struct skcipher_request *req,
0016                  const struct chacha_ctx *ctx, const u8 *iv)
0017 {
0018     struct skcipher_walk walk;
0019     u32 state[16];
0020     int err;
0021 
0022     err = skcipher_walk_virt(&walk, req, false);
0023 
0024     chacha_init_generic(state, ctx->key, iv);
0025 
0026     while (walk.nbytes > 0) {
0027         unsigned int nbytes = walk.nbytes;
0028 
0029         if (nbytes < walk.total)
0030             nbytes = round_down(nbytes, CHACHA_BLOCK_SIZE);
0031 
0032         chacha_crypt_generic(state, walk.dst.virt.addr,
0033                      walk.src.virt.addr, nbytes, ctx->nrounds);
0034         err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
0035     }
0036 
0037     return err;
0038 }
0039 
0040 static int crypto_chacha_crypt(struct skcipher_request *req)
0041 {
0042     struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0043     struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
0044 
0045     return chacha_stream_xor(req, ctx, req->iv);
0046 }
0047 
0048 static int crypto_xchacha_crypt(struct skcipher_request *req)
0049 {
0050     struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0051     struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
0052     struct chacha_ctx subctx;
0053     u32 state[16];
0054     u8 real_iv[16];
0055 
0056     /* Compute the subkey given the original key and first 128 nonce bits */
0057     chacha_init_generic(state, ctx->key, req->iv);
0058     hchacha_block_generic(state, subctx.key, ctx->nrounds);
0059     subctx.nrounds = ctx->nrounds;
0060 
0061     /* Build the real IV */
0062     memcpy(&real_iv[0], req->iv + 24, 8); /* stream position */
0063     memcpy(&real_iv[8], req->iv + 16, 8); /* remaining 64 nonce bits */
0064 
0065     /* Generate the stream and XOR it with the data */
0066     return chacha_stream_xor(req, &subctx, real_iv);
0067 }
0068 
0069 static struct skcipher_alg algs[] = {
0070     {
0071         .base.cra_name      = "chacha20",
0072         .base.cra_driver_name   = "chacha20-generic",
0073         .base.cra_priority  = 100,
0074         .base.cra_blocksize = 1,
0075         .base.cra_ctxsize   = sizeof(struct chacha_ctx),
0076         .base.cra_module    = THIS_MODULE,
0077 
0078         .min_keysize        = CHACHA_KEY_SIZE,
0079         .max_keysize        = CHACHA_KEY_SIZE,
0080         .ivsize         = CHACHA_IV_SIZE,
0081         .chunksize      = CHACHA_BLOCK_SIZE,
0082         .setkey         = chacha20_setkey,
0083         .encrypt        = crypto_chacha_crypt,
0084         .decrypt        = crypto_chacha_crypt,
0085     }, {
0086         .base.cra_name      = "xchacha20",
0087         .base.cra_driver_name   = "xchacha20-generic",
0088         .base.cra_priority  = 100,
0089         .base.cra_blocksize = 1,
0090         .base.cra_ctxsize   = sizeof(struct chacha_ctx),
0091         .base.cra_module    = THIS_MODULE,
0092 
0093         .min_keysize        = CHACHA_KEY_SIZE,
0094         .max_keysize        = CHACHA_KEY_SIZE,
0095         .ivsize         = XCHACHA_IV_SIZE,
0096         .chunksize      = CHACHA_BLOCK_SIZE,
0097         .setkey         = chacha20_setkey,
0098         .encrypt        = crypto_xchacha_crypt,
0099         .decrypt        = crypto_xchacha_crypt,
0100     }, {
0101         .base.cra_name      = "xchacha12",
0102         .base.cra_driver_name   = "xchacha12-generic",
0103         .base.cra_priority  = 100,
0104         .base.cra_blocksize = 1,
0105         .base.cra_ctxsize   = sizeof(struct chacha_ctx),
0106         .base.cra_module    = THIS_MODULE,
0107 
0108         .min_keysize        = CHACHA_KEY_SIZE,
0109         .max_keysize        = CHACHA_KEY_SIZE,
0110         .ivsize         = XCHACHA_IV_SIZE,
0111         .chunksize      = CHACHA_BLOCK_SIZE,
0112         .setkey         = chacha12_setkey,
0113         .encrypt        = crypto_xchacha_crypt,
0114         .decrypt        = crypto_xchacha_crypt,
0115     }
0116 };
0117 
0118 static int __init chacha_generic_mod_init(void)
0119 {
0120     return crypto_register_skciphers(algs, ARRAY_SIZE(algs));
0121 }
0122 
0123 static void __exit chacha_generic_mod_fini(void)
0124 {
0125     crypto_unregister_skciphers(algs, ARRAY_SIZE(algs));
0126 }
0127 
0128 subsys_initcall(chacha_generic_mod_init);
0129 module_exit(chacha_generic_mod_fini);
0130 
0131 MODULE_LICENSE("GPL");
0132 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
0133 MODULE_DESCRIPTION("ChaCha and XChaCha stream ciphers (generic)");
0134 MODULE_ALIAS_CRYPTO("chacha20");
0135 MODULE_ALIAS_CRYPTO("chacha20-generic");
0136 MODULE_ALIAS_CRYPTO("xchacha20");
0137 MODULE_ALIAS_CRYPTO("xchacha20-generic");
0138 MODULE_ALIAS_CRYPTO("xchacha12");
0139 MODULE_ALIAS_CRYPTO("xchacha12-generic");