/src/nettle-with-libgmp/cast128.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* cast128.c  | 
2  |  |  | 
3  |  |    The CAST-128 block cipher, described in RFC 2144.  | 
4  |  |  | 
5  |  |    Copyright (C) 2001, 2014 Niels Möller  | 
6  |  |  | 
7  |  |    This file is part of GNU Nettle.  | 
8  |  |  | 
9  |  |    GNU Nettle is free software: you can redistribute it and/or  | 
10  |  |    modify it under the terms of either:  | 
11  |  |  | 
12  |  |      * the GNU Lesser General Public License as published by the Free  | 
13  |  |        Software Foundation; either version 3 of the License, or (at your  | 
14  |  |        option) any later version.  | 
15  |  |  | 
16  |  |    or  | 
17  |  |  | 
18  |  |      * the GNU General Public License as published by the Free  | 
19  |  |        Software Foundation; either version 2 of the License, or (at your  | 
20  |  |        option) any later version.  | 
21  |  |  | 
22  |  |    or both in parallel, as here.  | 
23  |  |  | 
24  |  |    GNU Nettle is distributed in the hope that it will be useful,  | 
25  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
26  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
27  |  |    General Public License for more details.  | 
28  |  |  | 
29  |  |    You should have received copies of the GNU General Public License and  | 
30  |  |    the GNU Lesser General Public License along with this program.  If  | 
31  |  |    not, see http://www.gnu.org/licenses/.  | 
32  |  | */  | 
33  |  |  | 
34  |  | /* Based on:  | 
35  |  |  *  | 
36  |  |  *  CAST-128 in C  | 
37  |  |  *  Written by Steve Reid <sreid@sea-to-sky.net>  | 
38  |  |  *  100% Public Domain - no warranty  | 
39  |  |  *  Released 1997.10.11  | 
40  |  |  */  | 
41  |  |  | 
42  |  | #if HAVE_CONFIG_H  | 
43  |  | # include "config.h"  | 
44  |  | #endif  | 
45  |  |  | 
46  |  | #include <assert.h>  | 
47  |  | #include <stdlib.h>  | 
48  |  | #include <string.h>  | 
49  |  |  | 
50  |  | #include "cast128.h"  | 
51  |  | #include "cast128_sboxes.h"  | 
52  |  |  | 
53  |  | #include "macros.h"  | 
54  |  |  | 
55  | 350  | #define CAST_SMALL_KEY 10  | 
56  |  |  | 
57  | 81.3k  | #define S1 cast_sbox1  | 
58  | 81.3k  | #define S2 cast_sbox2  | 
59  | 81.3k  | #define S3 cast_sbox3  | 
60  | 81.3k  | #define S4 cast_sbox4  | 
61  | 14.0k  | #define S5 cast_sbox5  | 
62  | 14.0k  | #define S6 cast_sbox6  | 
63  | 14.0k  | #define S7 cast_sbox7  | 
64  | 14.0k  | #define S8 cast_sbox8  | 
65  |  |  | 
66  |  | /* Macros to access 8-bit bytes out of a 32-bit word */  | 
67  | 95.3k  | #define B0(x) ( (uint8_t) (x>>24) )  | 
68  | 95.3k  | #define B1(x) ( (uint8_t) ((x>>16)&0xff) )  | 
69  | 95.3k  | #define B2(x) ( (uint8_t) ((x>>8)&0xff) )  | 
70  | 95.3k  | #define B3(x) ( (uint8_t) ((x)&0xff) )  | 
71  |  |  | 
72  |  | /* NOTE: Depends on ROTL32 supporting a zero shift count. */  | 
73  |  |  | 
74  |  | /* CAST-128 uses three different round functions */  | 
75  | 29.9k  | #define F1(l, r, i) do {         \ | 
76  | 29.9k  |     t = ctx->Km[i] + r;           \  | 
77  | 29.9k  |     t = ROTL32(ctx->Kr[i], t);          \  | 
78  | 29.9k  |     l ^= ((S1[B0(t)] ^ S2[B1(t)]) - S3[B2(t)]) + S4[B3(t)]; \  | 
79  | 29.9k  |   } while (0)  | 
80  | 25.6k  | #define F2(l, r, i) do {         \ | 
81  | 25.6k  |     t = ctx->Km[i] ^ r;           \  | 
82  | 25.6k  |     t = ROTL32( ctx->Kr[i], t);         \  | 
83  | 25.6k  |     l ^= ((S1[B0(t)] - S2[B1(t)]) + S3[B2(t)]) ^ S4[B3(t)]; \  | 
84  | 25.6k  |   } while (0)  | 
85  | 25.6k  | #define F3(l, r, i) do {         \ | 
86  | 25.6k  |     t = ctx->Km[i] - r;           \  | 
87  | 25.6k  |     t = ROTL32(ctx->Kr[i], t);          \  | 
88  | 25.6k  |     l ^= ((S1[B0(t)] + S2[B1(t)]) ^ S3[B2(t)]) - S4[B3(t)]; \  | 
89  | 25.6k  |   } while (0)  | 
90  |  |  | 
91  |  |  | 
92  |  | /***** Encryption Function *****/  | 
93  |  |  | 
94  |  | void  | 
95  |  | cast128_encrypt(const struct cast128_ctx *ctx,  | 
96  |  |     size_t length, uint8_t *dst,  | 
97  |  |     const uint8_t *src)  | 
98  | 130  | { | 
99  | 130  |   FOR_BLOCKS(length, dst, src, CAST128_BLOCK_SIZE)  | 
100  | 1.82k  |     { | 
101  | 1.82k  |       uint32_t t, l, r;  | 
102  |  |  | 
103  |  |       /* Get inblock into l,r */  | 
104  | 1.82k  |       l = READ_UINT32(src);  | 
105  | 1.82k  |       r = READ_UINT32(src+4);  | 
106  |  |  | 
107  |  |       /* Do the work */  | 
108  | 1.82k  |       F1(l, r,  0);  | 
109  | 1.82k  |       F2(r, l,  1);  | 
110  | 1.82k  |       F3(l, r,  2);  | 
111  | 1.82k  |       F1(r, l,  3);  | 
112  | 1.82k  |       F2(l, r,  4);  | 
113  | 1.82k  |       F3(r, l,  5);  | 
114  | 1.82k  |       F1(l, r,  6);  | 
115  | 1.82k  |       F2(r, l,  7);  | 
116  | 1.82k  |       F3(l, r,  8);  | 
117  | 1.82k  |       F1(r, l,  9);  | 
118  | 1.82k  |       F2(l, r, 10);  | 
119  | 1.82k  |       F3(r, l, 11);  | 
120  |  |       /* Only do full 16 rounds if key length > 80 bits */  | 
121  | 1.82k  |       if (ctx->rounds & 16) { | 
122  | 1.34k  |   F1(l, r, 12);  | 
123  | 1.34k  |   F2(r, l, 13);  | 
124  | 1.34k  |   F3(l, r, 14);  | 
125  | 1.34k  |   F1(r, l, 15);  | 
126  | 1.34k  |       }  | 
127  |  |       /* Put l,r into outblock */  | 
128  | 1.82k  |       WRITE_UINT32(dst, r);  | 
129  | 1.82k  |       WRITE_UINT32(dst + 4, l);  | 
130  | 1.82k  |     }  | 
131  | 130  | }  | 
132  |  |  | 
133  |  |  | 
134  |  | /***** Decryption Function *****/  | 
135  |  |  | 
136  |  | void  | 
137  |  | cast128_decrypt(const struct cast128_ctx *ctx,  | 
138  |  |     size_t length, uint8_t *dst,  | 
139  |  |     const uint8_t *src)  | 
140  | 220  | { | 
141  | 220  |   FOR_BLOCKS(length, dst, src, CAST128_BLOCK_SIZE)  | 
142  | 3.55k  |     { | 
143  | 3.55k  |       uint32_t t, l, r;  | 
144  |  |  | 
145  |  |       /* Get inblock into l,r */  | 
146  | 3.55k  |       r = READ_UINT32(src);  | 
147  | 3.55k  |       l = READ_UINT32(src+4);  | 
148  |  |  | 
149  |  |       /* Do the work */  | 
150  |  |       /* Only do full 16 rounds if key length > 80 bits */  | 
151  | 3.55k  |       if (ctx->rounds & 16) { | 
152  | 2.85k  |   F1(r, l, 15);  | 
153  | 2.85k  |   F3(l, r, 14);  | 
154  | 2.85k  |   F2(r, l, 13);  | 
155  | 2.85k  |   F1(l, r, 12);  | 
156  | 2.85k  |       }  | 
157  | 3.55k  |       F3(r, l, 11);  | 
158  | 3.55k  |       F2(l, r, 10);  | 
159  | 3.55k  |       F1(r, l,  9);  | 
160  | 3.55k  |       F3(l, r,  8);  | 
161  | 3.55k  |       F2(r, l,  7);  | 
162  | 3.55k  |       F1(l, r,  6);  | 
163  | 3.55k  |       F3(r, l,  5);  | 
164  | 3.55k  |       F2(l, r,  4);  | 
165  | 3.55k  |       F1(r, l,  3);  | 
166  | 3.55k  |       F3(l, r,  2);  | 
167  | 3.55k  |       F2(r, l,  1);  | 
168  | 3.55k  |       F1(l, r,  0);  | 
169  |  |  | 
170  |  |       /* Put l,r into outblock */  | 
171  | 3.55k  |       WRITE_UINT32(dst, l);  | 
172  | 3.55k  |       WRITE_UINT32(dst + 4, r);  | 
173  | 3.55k  |     }  | 
174  | 220  | }  | 
175  |  |  | 
176  |  | /***** Key Schedule *****/  | 
177  |  |  | 
178  | 5.03k  | #define SET_KM(i, k) ctx->Km[i] = (k)  | 
179  | 5.03k  | #define SET_KR(i, k) ctx->Kr[i] = (k) & 31  | 
180  |  |  | 
181  | 700  | #define EXPAND(set, full) do {           \ | 
182  | 700  |     z0 = x0 ^ S5[B1(x3)] ^ S6[B3(x3)] ^ S7[B0(x3)] ^ S8[B2(x3)] ^ S7[B0(x2)]; \  | 
183  | 700  |     z1 = x2 ^ S5[B0(z0)] ^ S6[B2(z0)] ^ S7[B1(z0)] ^ S8[B3(z0)] ^ S8[B2(x2)]; \  | 
184  | 700  |     z2 = x3 ^ S5[B3(z1)] ^ S6[B2(z1)] ^ S7[B1(z1)] ^ S8[B0(z1)] ^ S5[B1(x2)]; \  | 
185  | 700  |     z3 = x1 ^ S5[B2(z2)] ^ S6[B1(z2)] ^ S7[B3(z2)] ^ S8[B0(z2)] ^ S6[B3(x2)]; \  | 
186  | 700  |                       \  | 
187  | 700  |     set(0, S5[B0(z2)] ^ S6[B1(z2)] ^ S7[B3(z1)] ^ S8[B2(z1)] ^ S5[B2(z0)]); \  | 
188  | 700  |     set(1, S5[B2(z2)] ^ S6[B3(z2)] ^ S7[B1(z1)] ^ S8[B0(z1)] ^ S6[B2(z1)]); \  | 
189  | 700  |     set(2, S5[B0(z3)] ^ S6[B1(z3)] ^ S7[B3(z0)] ^ S8[B2(z0)] ^ S7[B1(z2)]); \  | 
190  | 700  |     set(3, S5[B2(z3)] ^ S6[B3(z3)] ^ S7[B1(z0)] ^ S8[B0(z0)] ^ S8[B0(z3)]); \  | 
191  | 700  |                       \  | 
192  | 700  |     x0 = z2 ^ S5[B1(z1)] ^ S6[B3(z1)] ^ S7[B0(z1)] ^ S8[B2(z1)] ^ S7[B0(z0)]; \  | 
193  | 700  |     x1 = z0 ^ S5[B0(x0)] ^ S6[B2(x0)] ^ S7[B1(x0)] ^ S8[B3(x0)] ^ S8[B2(z0)]; \  | 
194  | 700  |     x2 = z1 ^ S5[B3(x1)] ^ S6[B2(x1)] ^ S7[B1(x1)] ^ S8[B0(x1)] ^ S5[B1(z0)]; \  | 
195  | 700  |     x3 = z3 ^ S5[B2(x2)] ^ S6[B1(x2)] ^ S7[B3(x2)] ^ S8[B0(x2)] ^ S6[B3(z0)]; \  | 
196  | 700  |                       \  | 
197  | 700  |     set(4, S5[B3(x0)] ^ S6[B2(x0)] ^ S7[B0(x3)] ^ S8[B1(x3)] ^ S5[B0(x2)]); \  | 
198  | 700  |     set(5, S5[B1(x0)] ^ S6[B0(x0)] ^ S7[B2(x3)] ^ S8[B3(x3)] ^ S6[B1(x3)]); \  | 
199  | 700  |     set(6, S5[B3(x1)] ^ S6[B2(x1)] ^ S7[B0(x2)] ^ S8[B1(x2)] ^ S7[B3(x0)]); \  | 
200  | 700  |     set(7, S5[B1(x1)] ^ S6[B0(x1)] ^ S7[B2(x2)] ^ S8[B3(x2)] ^ S8[B3(x1)]); \  | 
201  | 700  |                       \  | 
202  | 700  |     z0 = x0 ^ S5[B1(x3)] ^ S6[B3(x3)] ^ S7[B0(x3)] ^ S8[B2(x3)] ^ S7[B0(x2)]; \  | 
203  | 700  |     z1 = x2 ^ S5[B0(z0)] ^ S6[B2(z0)] ^ S7[B1(z0)] ^ S8[B3(z0)] ^ S8[B2(x2)]; \  | 
204  | 700  |     z2 = x3 ^ S5[B3(z1)] ^ S6[B2(z1)] ^ S7[B1(z1)] ^ S8[B0(z1)] ^ S5[B1(x2)]; \  | 
205  | 700  |     z3 = x1 ^ S5[B2(z2)] ^ S6[B1(z2)] ^ S7[B3(z2)] ^ S8[B0(z2)] ^ S6[B3(x2)]; \  | 
206  | 700  |                       \  | 
207  | 700  |     set(8,  S5[B3(z0)] ^ S6[B2(z0)] ^ S7[B0(z3)] ^ S8[B1(z3)] ^ S5[B1(z2)]); \  | 
208  | 700  |     set(9,  S5[B1(z0)] ^ S6[B0(z0)] ^ S7[B2(z3)] ^ S8[B3(z3)] ^ S6[B0(z3)]); \  | 
209  | 700  |     set(10, S5[B3(z1)] ^ S6[B2(z1)] ^ S7[B0(z2)] ^ S8[B1(z2)] ^ S7[B2(z0)]); \  | 
210  | 700  |     set(11, S5[B1(z1)] ^ S6[B0(z1)] ^ S7[B2(z2)] ^ S8[B3(z2)] ^ S8[B2(z1)]); \  | 
211  | 700  |                   \  | 
212  | 700  |     x0 = z2 ^ S5[B1(z1)] ^ S6[B3(z1)] ^ S7[B0(z1)] ^ S8[B2(z1)] ^ S7[B0(z0)]; \  | 
213  | 700  |     x1 = z0 ^ S5[B0(x0)] ^ S6[B2(x0)] ^ S7[B1(x0)] ^ S8[B3(x0)] ^ S8[B2(z0)]; \  | 
214  | 700  |     x2 = z1 ^ S5[B3(x1)] ^ S6[B2(x1)] ^ S7[B1(x1)] ^ S8[B0(x1)] ^ S5[B1(z0)]; \  | 
215  | 700  |     x3 = z3 ^ S5[B2(x2)] ^ S6[B1(x2)] ^ S7[B3(x2)] ^ S8[B0(x2)] ^ S6[B3(z0)]; \  | 
216  | 700  |     if (full)               \  | 
217  | 700  |       {                 \ | 
218  | 416  |   set(12, S5[B0(x2)] ^ S6[B1(x2)] ^ S7[B3(x1)] ^ S8[B2(x1)] ^ S5[B3(x0)]); \  | 
219  | 416  |   set(13, S5[B2(x2)] ^ S6[B3(x2)] ^ S7[B1(x1)] ^ S8[B0(x1)] ^ S6[B3(x1)]); \  | 
220  | 416  |   set(14, S5[B0(x3)] ^ S6[B1(x3)] ^ S7[B3(x0)] ^ S8[B2(x0)] ^ S7[B0(x2)]); \  | 
221  | 416  |   set(15, S5[B2(x3)] ^ S6[B3(x3)] ^ S7[B1(x0)] ^ S8[B0(x0)] ^ S8[B1(x3)]); \  | 
222  | 416  |       }                  \  | 
223  | 700  | } while (0)  | 
224  |  |  | 
225  |  | void  | 
226  |  | cast5_set_key(struct cast128_ctx *ctx,  | 
227  |  |         size_t length, const uint8_t *key)  | 
228  | 350  | { | 
229  | 350  |   uint32_t x0, x1, x2, x3, z0, z1, z2, z3;  | 
230  | 350  |   uint32_t w;  | 
231  | 350  |   int full;  | 
232  |  |  | 
233  | 350  |   assert (length >= CAST5_MIN_KEY_SIZE);  | 
234  | 350  |   assert (length <= CAST5_MAX_KEY_SIZE);  | 
235  |  |  | 
236  | 350  |   full = (length > CAST_SMALL_KEY);  | 
237  |  |  | 
238  | 350  |   x0 = READ_UINT32 (key);  | 
239  |  |  | 
240  |  |   /* Read final word, possibly zero-padded. */  | 
241  | 350  |   switch (length & 3)  | 
242  | 350  |     { | 
243  | 216  |     case 0:  | 
244  | 216  |       w = READ_UINT32 (key + length - 4);  | 
245  | 216  |       break;  | 
246  | 47  |     case 3:  | 
247  | 47  |       w = READ_UINT24 (key + length - 3) << 8;  | 
248  | 47  |       break;  | 
249  | 71  |     case 2:  | 
250  | 71  |       w = READ_UINT16 (key + length - 2) << 16;  | 
251  | 71  |       break;  | 
252  | 16  |     case 1:  | 
253  | 16  |       w = (uint32_t) key[length - 1] << 24;  | 
254  | 16  |       break;  | 
255  | 350  |     }  | 
256  |  |  | 
257  | 350  |   if (length <= 8)  | 
258  | 74  |     { | 
259  | 74  |       x1 = w;  | 
260  | 74  |       x2 = x3 = 0;  | 
261  | 74  |     }  | 
262  | 276  |   else  | 
263  | 276  |     { | 
264  | 276  |       x1 = READ_UINT32 (key + 4);  | 
265  | 276  |       if (length <= 12)  | 
266  | 180  |   { | 
267  | 180  |     x2 = w;  | 
268  | 180  |     x3 = 0;  | 
269  | 180  |   }  | 
270  | 96  |       else  | 
271  | 96  |   { | 
272  | 96  |     x2 = READ_UINT32 (key + 8);  | 
273  | 96  |     x3 = w;  | 
274  | 96  |   }  | 
275  | 276  |     }  | 
276  |  |  | 
277  | 5.03k  |   EXPAND(SET_KM, full);  | 
278  | 5.03k  |   EXPAND(SET_KR, full);  | 
279  |  |  | 
280  | 350  |   ctx->rounds = full ? 16 : 12;  | 
281  | 350  | }  | 
282  |  |  | 
283  |  | void  | 
284  |  | cast128_set_key(struct cast128_ctx *ctx, const uint8_t *key)  | 
285  | 0  | { | 
286  | 0  |   cast5_set_key (ctx, CAST128_KEY_SIZE, key);  | 
287  | 0  | }  |