~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/crypto/chacha20_generic.c

Version: ~ [ linux-5.8-rc4 ] ~ [ linux-5.7.7 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.50 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.131 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.187 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.229 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.229 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * ChaCha20 256-bit cipher algorithm, RFC7539
  3  *
  4  * Copyright (C) 2015 Martin Willi
  5  *
  6  * This program is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU General Public License as published by
  8  * the Free Software Foundation; either version 2 of the License, or
  9  * (at your option) any later version.
 10  */
 11 
 12 #include <asm/unaligned.h>
 13 #include <crypto/algapi.h>
 14 #include <crypto/chacha20.h>
 15 #include <crypto/internal/skcipher.h>
 16 #include <linux/module.h>
 17 
 18 static void chacha20_docrypt(u32 *state, u8 *dst, const u8 *src,
 19                              unsigned int bytes)
 20 {
 21         u32 stream[CHACHA20_BLOCK_WORDS];
 22 
 23         if (dst != src)
 24                 memcpy(dst, src, bytes);
 25 
 26         while (bytes >= CHACHA20_BLOCK_SIZE) {
 27                 chacha20_block(state, stream);
 28                 crypto_xor(dst, (const u8 *)stream, CHACHA20_BLOCK_SIZE);
 29                 bytes -= CHACHA20_BLOCK_SIZE;
 30                 dst += CHACHA20_BLOCK_SIZE;
 31         }
 32         if (bytes) {
 33                 chacha20_block(state, stream);
 34                 crypto_xor(dst, (const u8 *)stream, bytes);
 35         }
 36 }
 37 
 38 void crypto_chacha20_init(u32 *state, struct chacha20_ctx *ctx, u8 *iv)
 39 {
 40         state[0]  = 0x61707865; /* "expa" */
 41         state[1]  = 0x3320646e; /* "nd 3" */
 42         state[2]  = 0x79622d32; /* "2-by" */
 43         state[3]  = 0x6b206574; /* "te k" */
 44         state[4]  = ctx->key[0];
 45         state[5]  = ctx->key[1];
 46         state[6]  = ctx->key[2];
 47         state[7]  = ctx->key[3];
 48         state[8]  = ctx->key[4];
 49         state[9]  = ctx->key[5];
 50         state[10] = ctx->key[6];
 51         state[11] = ctx->key[7];
 52         state[12] = get_unaligned_le32(iv +  0);
 53         state[13] = get_unaligned_le32(iv +  4);
 54         state[14] = get_unaligned_le32(iv +  8);
 55         state[15] = get_unaligned_le32(iv + 12);
 56 }
 57 EXPORT_SYMBOL_GPL(crypto_chacha20_init);
 58 
 59 int crypto_chacha20_setkey(struct crypto_skcipher *tfm, const u8 *key,
 60                            unsigned int keysize)
 61 {
 62         struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
 63         int i;
 64 
 65         if (keysize != CHACHA20_KEY_SIZE)
 66                 return -EINVAL;
 67 
 68         for (i = 0; i < ARRAY_SIZE(ctx->key); i++)
 69                 ctx->key[i] = get_unaligned_le32(key + i * sizeof(u32));
 70 
 71         return 0;
 72 }
 73 EXPORT_SYMBOL_GPL(crypto_chacha20_setkey);
 74 
 75 int crypto_chacha20_crypt(struct skcipher_request *req)
 76 {
 77         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
 78         struct chacha20_ctx *ctx = crypto_skcipher_ctx(tfm);
 79         struct skcipher_walk walk;
 80         u32 state[16];
 81         int err;
 82 
 83         err = skcipher_walk_virt(&walk, req, true);
 84 
 85         crypto_chacha20_init(state, ctx, walk.iv);
 86 
 87         while (walk.nbytes > 0) {
 88                 unsigned int nbytes = walk.nbytes;
 89 
 90                 if (nbytes < walk.total)
 91                         nbytes = round_down(nbytes, walk.stride);
 92 
 93                 chacha20_docrypt(state, walk.dst.virt.addr, walk.src.virt.addr,
 94                                  nbytes);
 95                 err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
 96         }
 97 
 98         return err;
 99 }
100 EXPORT_SYMBOL_GPL(crypto_chacha20_crypt);
101 
102 static struct skcipher_alg alg = {
103         .base.cra_name          = "chacha20",
104         .base.cra_driver_name   = "chacha20-generic",
105         .base.cra_priority      = 100,
106         .base.cra_blocksize     = 1,
107         .base.cra_ctxsize       = sizeof(struct chacha20_ctx),
108         .base.cra_module        = THIS_MODULE,
109 
110         .min_keysize            = CHACHA20_KEY_SIZE,
111         .max_keysize            = CHACHA20_KEY_SIZE,
112         .ivsize                 = CHACHA20_IV_SIZE,
113         .chunksize              = CHACHA20_BLOCK_SIZE,
114         .setkey                 = crypto_chacha20_setkey,
115         .encrypt                = crypto_chacha20_crypt,
116         .decrypt                = crypto_chacha20_crypt,
117 };
118 
119 static int __init chacha20_generic_mod_init(void)
120 {
121         return crypto_register_skcipher(&alg);
122 }
123 
124 static void __exit chacha20_generic_mod_fini(void)
125 {
126         crypto_unregister_skcipher(&alg);
127 }
128 
129 module_init(chacha20_generic_mod_init);
130 module_exit(chacha20_generic_mod_fini);
131 
132 MODULE_LICENSE("GPL");
133 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
134 MODULE_DESCRIPTION("chacha20 cipher algorithm");
135 MODULE_ALIAS_CRYPTO("chacha20");
136 MODULE_ALIAS_CRYPTO("chacha20-generic");
137 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp