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

TOMOYO Linux Cross Reference
Linux/crypto/twofish_generic.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ 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.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /crypto/twofish_generic.c (Version linux-6.12-rc7) and /crypto/twofish_generic.c (Version linux-4.4.302)


  1 // SPDX-License-Identifier: GPL-2.0-or-later   << 
  2 /*                                                  1 /*
  3  * Twofish for CryptoAPI                            2  * Twofish for CryptoAPI
  4  *                                                  3  *
  5  * Originally Twofish for GPG                       4  * Originally Twofish for GPG
  6  * By Matthew Skala <mskala@ansuz.sooke.bc.ca>      5  * By Matthew Skala <mskala@ansuz.sooke.bc.ca>, July 26, 1998
  7  * 256-bit key length added March 20, 1999          6  * 256-bit key length added March 20, 1999
  8  * Some modifications to reduce the text size       7  * Some modifications to reduce the text size by Werner Koch, April, 1998
  9  * Ported to the kerneli patch by Marc Mutz <M      8  * Ported to the kerneli patch by Marc Mutz <Marc@Mutz.com>
 10  * Ported to CryptoAPI by Colin Slater <hoho@t      9  * Ported to CryptoAPI by Colin Slater <hoho@tacomeat.net>
 11  *                                                 10  *
 12  * The original author has disclaimed all copy     11  * The original author has disclaimed all copyright interest in this
 13  * code and thus put it in the public domain.      12  * code and thus put it in the public domain. The subsequent authors 
 14  * have put this under the GNU General Public      13  * have put this under the GNU General Public License.
 15  *                                                 14  *
                                                   >>  15  * This program is free software; you can redistribute it and/or modify
                                                   >>  16  * it under the terms of the GNU General Public License as published by
                                                   >>  17  * the Free Software Foundation; either version 2 of the License, or
                                                   >>  18  * (at your option) any later version.
                                                   >>  19  *
                                                   >>  20  * This program is distributed in the hope that it will be useful,
                                                   >>  21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
                                                   >>  22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                                                   >>  23  * GNU General Public License for more details.
                                                   >>  24  * 
                                                   >>  25  * You should have received a copy of the GNU General Public License
                                                   >>  26  * along with this program; if not, write to the Free Software
                                                   >>  27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
                                                   >>  28  * USA
                                                   >>  29  *
 16  * This code is a "clean room" implementation,     30  * This code is a "clean room" implementation, written from the paper
 17  * _Twofish: A 128-Bit Block Cipher_ by Bruce      31  * _Twofish: A 128-Bit Block Cipher_ by Bruce Schneier, John Kelsey,
 18  * Doug Whiting, David Wagner, Chris Hall, and     32  * Doug Whiting, David Wagner, Chris Hall, and Niels Ferguson, available
 19  * through http://www.counterpane.com/twofish.     33  * through http://www.counterpane.com/twofish.html
 20  *                                                 34  *
 21  * For background information on multiplicatio     35  * For background information on multiplication in finite fields, used for
 22  * the matrix operations in the key schedule,      36  * the matrix operations in the key schedule, see the book _Contemporary
 23  * Abstract Algebra_ by Joseph A. Gallian, esp     37  * Abstract Algebra_ by Joseph A. Gallian, especially chapter 22 in the
 24  * Third Edition.                                  38  * Third Edition.
 25  */                                                39  */
 26                                                    40 
 27 #include <linux/unaligned.h>                   !!  41 #include <asm/byteorder.h>
 28 #include <crypto/algapi.h>                     << 
 29 #include <crypto/twofish.h>                        42 #include <crypto/twofish.h>
 30 #include <linux/module.h>                          43 #include <linux/module.h>
 31 #include <linux/init.h>                            44 #include <linux/init.h>
 32 #include <linux/types.h>                           45 #include <linux/types.h>
 33 #include <linux/errno.h>                           46 #include <linux/errno.h>
                                                   >>  47 #include <linux/crypto.h>
 34 #include <linux/bitops.h>                          48 #include <linux/bitops.h>
 35                                                    49 
 36 /* Macros to compute the g() function in the e     50 /* Macros to compute the g() function in the encryption and decryption
 37  * rounds.  G1 is the straight g() function; G     51  * rounds.  G1 is the straight g() function; G2 includes the 8-bit
 38  * rotation for the high 32-bit word. */           52  * rotation for the high 32-bit word. */
 39                                                    53 
 40 #define G1(a) \                                    54 #define G1(a) \
 41      (ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a)     55      (ctx->s[0][(a) & 0xFF]) ^ (ctx->s[1][((a) >> 8) & 0xFF]) \
 42    ^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s     56    ^ (ctx->s[2][((a) >> 16) & 0xFF]) ^ (ctx->s[3][(a) >> 24])
 43                                                    57 
 44 #define G2(b) \                                    58 #define G2(b) \
 45      (ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b)     59      (ctx->s[1][(b) & 0xFF]) ^ (ctx->s[2][((b) >> 8) & 0xFF]) \
 46    ^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s     60    ^ (ctx->s[3][((b) >> 16) & 0xFF]) ^ (ctx->s[0][(b) >> 24])
 47                                                    61 
 48 /* Encryption and decryption Feistel rounds.       62 /* Encryption and decryption Feistel rounds.  Each one calls the two g()
 49  * macros, does the PHT, and performs the XOR      63  * macros, does the PHT, and performs the XOR and the appropriate bit
 50  * rotations.  The parameters are the round nu     64  * rotations.  The parameters are the round number (used to select subkeys),
 51  * and the four 32-bit chunks of the text. */      65  * and the four 32-bit chunks of the text. */
 52                                                    66 
 53 #define ENCROUND(n, a, b, c, d) \                  67 #define ENCROUND(n, a, b, c, d) \
 54    x = G1 (a); y = G2 (b); \                       68    x = G1 (a); y = G2 (b); \
 55    x += y; y += x + ctx->k[2 * (n) + 1]; \         69    x += y; y += x + ctx->k[2 * (n) + 1]; \
 56    (c) ^= x + ctx->k[2 * (n)]; \                   70    (c) ^= x + ctx->k[2 * (n)]; \
 57    (c) = ror32((c), 1); \                          71    (c) = ror32((c), 1); \
 58    (d) = rol32((d), 1) ^ y                         72    (d) = rol32((d), 1) ^ y
 59                                                    73 
 60 #define DECROUND(n, a, b, c, d) \                  74 #define DECROUND(n, a, b, c, d) \
 61    x = G1 (a); y = G2 (b); \                       75    x = G1 (a); y = G2 (b); \
 62    x += y; y += x; \                               76    x += y; y += x; \
 63    (d) ^= y + ctx->k[2 * (n) + 1]; \               77    (d) ^= y + ctx->k[2 * (n) + 1]; \
 64    (d) = ror32((d), 1); \                          78    (d) = ror32((d), 1); \
 65    (c) = rol32((c), 1); \                          79    (c) = rol32((c), 1); \
 66    (c) ^= (x + ctx->k[2 * (n)])                    80    (c) ^= (x + ctx->k[2 * (n)])
 67                                                    81 
 68 /* Encryption and decryption cycles; each one      82 /* Encryption and decryption cycles; each one is simply two Feistel rounds
 69  * with the 32-bit chunks re-ordered to simula     83  * with the 32-bit chunks re-ordered to simulate the "swap" */
 70                                                    84 
 71 #define ENCCYCLE(n) \                              85 #define ENCCYCLE(n) \
 72    ENCROUND (2 * (n), a, b, c, d); \               86    ENCROUND (2 * (n), a, b, c, d); \
 73    ENCROUND (2 * (n) + 1, c, d, a, b)              87    ENCROUND (2 * (n) + 1, c, d, a, b)
 74                                                    88 
 75 #define DECCYCLE(n) \                              89 #define DECCYCLE(n) \
 76    DECROUND (2 * (n) + 1, c, d, a, b); \           90    DECROUND (2 * (n) + 1, c, d, a, b); \
 77    DECROUND (2 * (n), a, b, c, d)                  91    DECROUND (2 * (n), a, b, c, d)
 78                                                    92 
 79 /* Macros to convert the input and output byte     93 /* Macros to convert the input and output bytes into 32-bit words,
 80  * and simultaneously perform the whitening st     94  * and simultaneously perform the whitening step.  INPACK packs word
 81  * number n into the variable named by x, usin     95  * number n into the variable named by x, using whitening subkey number m.
 82  * OUTUNPACK unpacks word number n from the va     96  * OUTUNPACK unpacks word number n from the variable named by x, using
 83  * whitening subkey number m. */                   97  * whitening subkey number m. */
 84                                                    98 
 85 #define INPACK(n, x, m) \                          99 #define INPACK(n, x, m) \
 86    x = get_unaligned_le32(in + (n) * 4) ^ ctx- !! 100    x = le32_to_cpu(src[n]) ^ ctx->w[m]
 87                                                   101 
 88 #define OUTUNPACK(n, x, m) \                      102 #define OUTUNPACK(n, x, m) \
 89    x ^= ctx->w[m]; \                              103    x ^= ctx->w[m]; \
 90    put_unaligned_le32(x, out + (n) * 4)        !! 104    dst[n] = cpu_to_le32(x)
 91                                                   105 
 92                                                   106 
 93                                                   107 
 94 /* Encrypt one block.  in and out may be the s    108 /* Encrypt one block.  in and out may be the same. */
 95 static void twofish_encrypt(struct crypto_tfm     109 static void twofish_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
 96 {                                                 110 {
 97         struct twofish_ctx *ctx = crypto_tfm_c    111         struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
                                                   >> 112         const __le32 *src = (const __le32 *)in;
                                                   >> 113         __le32 *dst = (__le32 *)out;
 98                                                   114 
 99         /* The four 32-bit chunks of the text.    115         /* The four 32-bit chunks of the text. */
100         u32 a, b, c, d;                           116         u32 a, b, c, d;
101                                                   117         
102         /* Temporaries used by the round funct    118         /* Temporaries used by the round function. */
103         u32 x, y;                                 119         u32 x, y;
104                                                   120 
105         /* Input whitening and packing. */        121         /* Input whitening and packing. */
106         INPACK (0, a, 0);                         122         INPACK (0, a, 0);
107         INPACK (1, b, 1);                         123         INPACK (1, b, 1);
108         INPACK (2, c, 2);                         124         INPACK (2, c, 2);
109         INPACK (3, d, 3);                         125         INPACK (3, d, 3);
110                                                   126         
111         /* Encryption Feistel cycles. */          127         /* Encryption Feistel cycles. */
112         ENCCYCLE (0);                             128         ENCCYCLE (0);
113         ENCCYCLE (1);                             129         ENCCYCLE (1);
114         ENCCYCLE (2);                             130         ENCCYCLE (2);
115         ENCCYCLE (3);                             131         ENCCYCLE (3);
116         ENCCYCLE (4);                             132         ENCCYCLE (4);
117         ENCCYCLE (5);                             133         ENCCYCLE (5);
118         ENCCYCLE (6);                             134         ENCCYCLE (6);
119         ENCCYCLE (7);                             135         ENCCYCLE (7);
120                                                   136         
121         /* Output whitening and unpacking. */     137         /* Output whitening and unpacking. */
122         OUTUNPACK (0, c, 4);                      138         OUTUNPACK (0, c, 4);
123         OUTUNPACK (1, d, 5);                      139         OUTUNPACK (1, d, 5);
124         OUTUNPACK (2, a, 6);                      140         OUTUNPACK (2, a, 6);
125         OUTUNPACK (3, b, 7);                      141         OUTUNPACK (3, b, 7);
126                                                   142         
127 }                                                 143 }
128                                                   144 
129 /* Decrypt one block.  in and out may be the s    145 /* Decrypt one block.  in and out may be the same. */
130 static void twofish_decrypt(struct crypto_tfm     146 static void twofish_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
131 {                                                 147 {
132         struct twofish_ctx *ctx = crypto_tfm_c    148         struct twofish_ctx *ctx = crypto_tfm_ctx(tfm);
                                                   >> 149         const __le32 *src = (const __le32 *)in;
                                                   >> 150         __le32 *dst = (__le32 *)out;
133                                                   151   
134         /* The four 32-bit chunks of the text.    152         /* The four 32-bit chunks of the text. */
135         u32 a, b, c, d;                           153         u32 a, b, c, d;
136                                                   154         
137         /* Temporaries used by the round funct    155         /* Temporaries used by the round function. */
138         u32 x, y;                                 156         u32 x, y;
139                                                   157         
140         /* Input whitening and packing. */        158         /* Input whitening and packing. */
141         INPACK (0, c, 4);                         159         INPACK (0, c, 4);
142         INPACK (1, d, 5);                         160         INPACK (1, d, 5);
143         INPACK (2, a, 6);                         161         INPACK (2, a, 6);
144         INPACK (3, b, 7);                         162         INPACK (3, b, 7);
145                                                   163         
146         /* Encryption Feistel cycles. */          164         /* Encryption Feistel cycles. */
147         DECCYCLE (7);                             165         DECCYCLE (7);
148         DECCYCLE (6);                             166         DECCYCLE (6);
149         DECCYCLE (5);                             167         DECCYCLE (5);
150         DECCYCLE (4);                             168         DECCYCLE (4);
151         DECCYCLE (3);                             169         DECCYCLE (3);
152         DECCYCLE (2);                             170         DECCYCLE (2);
153         DECCYCLE (1);                             171         DECCYCLE (1);
154         DECCYCLE (0);                             172         DECCYCLE (0);
155                                                   173 
156         /* Output whitening and unpacking. */     174         /* Output whitening and unpacking. */
157         OUTUNPACK (0, a, 0);                      175         OUTUNPACK (0, a, 0);
158         OUTUNPACK (1, b, 1);                      176         OUTUNPACK (1, b, 1);
159         OUTUNPACK (2, c, 2);                      177         OUTUNPACK (2, c, 2);
160         OUTUNPACK (3, d, 3);                      178         OUTUNPACK (3, d, 3);
161                                                   179 
162 }                                                 180 }
163                                                   181 
164 static struct crypto_alg alg = {                  182 static struct crypto_alg alg = {
165         .cra_name           =   "twofish",        183         .cra_name           =   "twofish",
166         .cra_driver_name    =   "twofish-gener    184         .cra_driver_name    =   "twofish-generic",
167         .cra_priority       =   100,              185         .cra_priority       =   100,
168         .cra_flags          =   CRYPTO_ALG_TYP    186         .cra_flags          =   CRYPTO_ALG_TYPE_CIPHER,
169         .cra_blocksize      =   TF_BLOCK_SIZE,    187         .cra_blocksize      =   TF_BLOCK_SIZE,
170         .cra_ctxsize        =   sizeof(struct     188         .cra_ctxsize        =   sizeof(struct twofish_ctx),
                                                   >> 189         .cra_alignmask      =   3,
171         .cra_module         =   THIS_MODULE,      190         .cra_module         =   THIS_MODULE,
172         .cra_u              =   { .cipher = {     191         .cra_u              =   { .cipher = {
173         .cia_min_keysize    =   TF_MIN_KEY_SIZ    192         .cia_min_keysize    =   TF_MIN_KEY_SIZE,
174         .cia_max_keysize    =   TF_MAX_KEY_SIZ    193         .cia_max_keysize    =   TF_MAX_KEY_SIZE,
175         .cia_setkey         =   twofish_setkey    194         .cia_setkey         =   twofish_setkey,
176         .cia_encrypt        =   twofish_encryp    195         .cia_encrypt        =   twofish_encrypt,
177         .cia_decrypt        =   twofish_decryp    196         .cia_decrypt        =   twofish_decrypt } }
178 };                                                197 };
179                                                   198 
180 static int __init twofish_mod_init(void)          199 static int __init twofish_mod_init(void)
181 {                                                 200 {
182         return crypto_register_alg(&alg);         201         return crypto_register_alg(&alg);
183 }                                                 202 }
184                                                   203 
185 static void __exit twofish_mod_fini(void)         204 static void __exit twofish_mod_fini(void)
186 {                                                 205 {
187         crypto_unregister_alg(&alg);              206         crypto_unregister_alg(&alg);
188 }                                                 207 }
189                                                   208 
190 subsys_initcall(twofish_mod_init);             !! 209 module_init(twofish_mod_init);
191 module_exit(twofish_mod_fini);                    210 module_exit(twofish_mod_fini);
192                                                   211 
193 MODULE_LICENSE("GPL");                            212 MODULE_LICENSE("GPL");
194 MODULE_DESCRIPTION ("Twofish Cipher Algorithm"    213 MODULE_DESCRIPTION ("Twofish Cipher Algorithm");
195 MODULE_ALIAS_CRYPTO("twofish");                   214 MODULE_ALIAS_CRYPTO("twofish");
196 MODULE_ALIAS_CRYPTO("twofish-generic");           215 MODULE_ALIAS_CRYPTO("twofish-generic");
197                                                   216 

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

kernel.org | git.kernel.org | LWN.net | Project Home | 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.

sflogo.php