chiark / gitweb /
symm/t/chacha: Missing test from RFC8439.
[catacomb] / symm / twofish.h
1 /* -*-c-*-
2  *
3  * The Twofish block cipher
4  *
5  * (c) 2000 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
12  * Catacomb is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * Catacomb is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with Catacomb; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Notes on the Twofish block cipher ---------------------------------*
29  *
30  * Twofish was designed by Bruce Schneier, John Kelsey, Doug Whiting, David
31  * Wagner, Chris Hall and Niels Ferguson.  The algorithm is unpatented and
32  * free for anyone to use.  It was one of the five AES finalist algorithms.
33  *
34  * Twofish is a complex cipher offering various space and time tradeoffs.
35  * This implementation has a heavy key schedule and fast bulk encryption.
36  */
37
38 #ifndef CATACOMB_TWOFISH_H
39 #define CATACOMB_TWOFISH_H
40
41 #ifdef __cplusplus
42   extern "C" {
43 #endif
44
45 /*----- Header files ------------------------------------------------------*/
46
47 #include <stddef.h>
48
49 #include <mLib/bits.h>
50
51 /*----- Magical numbers ---------------------------------------------------*/
52
53 #define TWOFISH_BLKSZ 16
54 #define TWOFISH_KEYSZ 32
55 #define TWOFISH_CLASS (N, L, 128)
56
57 extern const octet twofish_keysz[];
58
59 /*----- Data structures ---------------------------------------------------*/
60
61 typedef struct twofish_ctx {
62   uint32 k[40];
63   uint32 g[4][256];
64 } twofish_ctx;
65
66 typedef struct twofish_fk {
67   uint32 t0[8], t23[8], t4[2];
68   octet t1[32];
69 } twofish_fk;
70
71 /*----- Functions provided ------------------------------------------------*/
72
73 /* --- @twofish_initfk@ --- *
74  *
75  * Arguments:   @twofish_ctx *k@ = pointer to key block to fill in
76  *              @const void *buf@ = pointer to buffer of key material
77  *              @size_t sz@ = size of key material
78  *              @const twofish_fk *fk@ = family-key information
79  *
80  * Returns:     ---
81  *
82  * Use:         Does the underlying Twofish key initialization with family
83  *              key.  Pass in a family-key structure initialized to
84  *              all-bits-zero for a standard key schedule.
85  */
86
87 extern void twofish_initfk(twofish_ctx */*k*/, const void */*buf*/,
88                            size_t /*sz*/, const twofish_fk */*fk*/);
89
90 /* --- @twofish_init@ --- *
91  *
92  * Arguments:   @twofish_ctx *k@ = pointer to key block to fill in
93  *              @const void *buf@ = pointer to buffer of key material
94  *              @size_t sz@ = size of key material
95  *
96  * Returns:     ---
97  *
98  * Use:         Initializes a Twofish key buffer.  Twofish accepts keys of up
99  *              to 256 bits in length.
100  */
101
102 extern void twofish_init(twofish_ctx */*k*/,
103                          const void */*buf*/, size_t /*sz*/);
104
105 /* --- @twofish_fkinit@ --- *
106  *
107  * Arguments:   @twofish_fk *fk@ = pointer to family key block
108  *              @const void *buf@ = pointer to buffer of key material
109  *              @size_t sz@ = size of key material
110  *
111  * Returns:     ---
112  *
113  * Use:         Initializes a family-key buffer.  This implementation allows
114  *              family keys of any size acceptable to the Twofish algorithm.
115  */
116
117 extern void twofish_fkinit(twofish_fk */*fk*/,
118                            const void */*buf*/, size_t /*sz*/);
119
120 /* --- @twofish_eblk@, @twofish_dblk@ --- *
121  *
122  * Arguments:   @const twofish_ctx *k@ = pointer to key block
123  *              @const uint32 s[4]@ = pointer to source block
124  *              @uint32 d[4]@ = pointer to destination block
125  *
126  * Returns:     ---
127  *
128  * Use:         Low-level block encryption and decryption.
129  */
130
131 extern void twofish_eblk(const twofish_ctx */*k*/,
132                          const uint32 */*s*/, uint32 */*d*/);
133
134 extern void twofish_dblk(const twofish_ctx */*k*/,
135                          const uint32 */*s*/, uint32 */*d*/);
136
137 /*----- That's all, folks -------------------------------------------------*/
138
139 #ifdef __cplusplus
140   }
141 #endif
142
143 #endif