Added CAST5 (CAST-128) cipher, Added CTR mode to twofish, unified
[silc.git] / lib / silccrypt / ciphers_def.h
1 /*
2
3   ciphers_def.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1999 - 2006 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #ifndef CIPHERS_DEF_H
21 #define CIPHERS_DEF_H
22
23 /* General definitions for algorithms */
24 typedef unsigned char u1byte;
25 typedef SilcUInt32 u4byte;
26 typedef SilcUInt32 u32;
27 typedef SilcUInt32 uint_32t;
28 typedef SilcUInt8 uint_8t;
29
30 #define rotr(x, nr) (((x) >> ((int)(nr))) | ((x) << (32 - (int)(nr))))
31 #define rotl(x, nr) (((x) << ((int)(nr))) | ((x) >> (32 - (int)(nr))))
32 #define byte(x, nr) ((x) >> (nr * 8) & 255)
33
34 /* Byte key to words */
35 #define SILC_GET_WORD_KEY(s, d, len)            \
36 do {                                            \
37   int _i;                                       \
38   for (_i = 0; _i < (len / 8) / 4; _i++)        \
39     SILC_GET32_LSB(d[_i], s + (_i * 4));        \
40 } while(0);
41
42 /* CBC mode macros (LSB). */
43
44 #define SILC_CBC_GET_IV(d, s)                   \
45 do {                                            \
46   SILC_GET32_LSB(d[0], &s[0]);                  \
47   SILC_GET32_LSB(d[1], &s[4]);                  \
48   SILC_GET32_LSB(d[2], &s[8]);                  \
49   SILC_GET32_LSB(d[3], &s[12]);                 \
50 } while(0);
51
52 #define SILC_CBC_PUT_IV(s, d)                   \
53 do {                                            \
54   SILC_PUT32_LSB(s[0], &d[0]);                  \
55   SILC_PUT32_LSB(s[1], &d[4]);                  \
56   SILC_PUT32_LSB(s[2], &d[8]);                  \
57   SILC_PUT32_LSB(s[3], &d[12]);                 \
58 } while(0);
59
60 #define SILC_CBC_ENC_PRE(d, s)                  \
61 do {                                            \
62   SILC_GET32_X_LSB(d[0], &s[0]);                \
63   SILC_GET32_X_LSB(d[1], &s[4]);                \
64   SILC_GET32_X_LSB(d[2], &s[8]);                \
65   SILC_GET32_X_LSB(d[3], &s[12]);               \
66 } while(0);
67
68 #define SILC_CBC_ENC_POST(s, d, t)              \
69 do {                                            \
70   SILC_PUT32_LSB(s[0], &d[0]);                  \
71   SILC_PUT32_LSB(s[1], &d[4]);                  \
72   SILC_PUT32_LSB(s[2], &d[8]);                  \
73   SILC_PUT32_LSB(s[3], &d[12]);                 \
74                                                 \
75   d += 16;                                      \
76   t += 16;                                      \
77 } while(0);
78
79 #define SILC_CBC_DEC_PRE(d, s)                  \
80 do {                                            \
81   SILC_GET32_LSB(d[0], &s[0]);                  \
82   SILC_GET32_LSB(d[1], &s[4]);                  \
83   SILC_GET32_LSB(d[2], &s[8]);                  \
84   SILC_GET32_LSB(d[3], &s[12]);                 \
85 } while(0);
86
87 #define SILC_CBC_DEC_POST(s, d, p, t, siv)      \
88 do {                                            \
89   s[0] ^= siv[0];                               \
90   s[1] ^= siv[1];                               \
91   s[2] ^= siv[2];                               \
92   s[3] ^= siv[3];                               \
93                                                 \
94   SILC_PUT32_LSB(s[0], &d[0]);                  \
95   SILC_PUT32_LSB(s[1], &d[4]);                  \
96   SILC_PUT32_LSB(s[2], &d[8]);                  \
97   SILC_PUT32_LSB(s[3], &d[12]);                 \
98                                                 \
99   siv[0] = t[0];                                \
100   siv[1] = t[1];                                \
101   siv[2] = t[2];                                \
102   siv[3] = t[3];                                \
103                                                 \
104   d += 16;                                      \
105   p += 16;                                      \
106 } while(0);
107
108 /* CBC mode macros (MSB). */
109
110 #define SILC_CBC_MSB_GET_IV(d, s, l)            \
111 do {                                            \
112   SILC_GET32_MSB(d[0], &s[0]);                  \
113   SILC_GET32_MSB(d[1], &s[4]);                  \
114   if (l > 8) {                                  \
115     SILC_GET32_MSB(d[2], &s[8]);                \
116     SILC_GET32_MSB(d[3], &s[12]);               \
117   }                                             \
118 } while(0);
119
120 #define SILC_CBC_MSB_PUT_IV(s, d, l)            \
121 do {                                            \
122   SILC_PUT32_MSB(s[0], &d[0]);                  \
123   SILC_PUT32_MSB(s[1], &d[4]);                  \
124   if (l > 8) {                                  \
125     SILC_PUT32_MSB(s[2], &d[8]);                \
126     SILC_PUT32_MSB(s[3], &d[12]);               \
127   }                                             \
128 } while(0);
129
130 #define SILC_CBC_MSB_ENC_PRE(d, s, l)           \
131 do {                                            \
132   SILC_GET32_X_MSB(d[0], &s[0]);                \
133   SILC_GET32_X_MSB(d[1], &s[4]);                \
134   if (l > 8) {                                  \
135     SILC_GET32_X_MSB(d[2], &s[8]);              \
136     SILC_GET32_X_MSB(d[3], &s[12]);             \
137   }                                             \
138 } while(0);
139
140 #define SILC_CBC_MSB_ENC_POST(s, d, t, l)       \
141 do {                                            \
142   SILC_PUT32_MSB(s[0], &d[0]);                  \
143   SILC_PUT32_MSB(s[1], &d[4]);                  \
144   if (l > 8) {                                  \
145     SILC_PUT32_MSB(s[2], &d[8]);                \
146     SILC_PUT32_MSB(s[3], &d[12]);               \
147   }                                             \
148                                                 \
149   d += l;                                       \
150   t += l;                                       \
151 } while(0);
152
153 #define SILC_CBC_MSB_DEC_PRE(d, s, l)           \
154 do {                                            \
155   SILC_GET32_MSB(d[0], &s[0]);                  \
156   SILC_GET32_MSB(d[1], &s[4]);                  \
157   if (l > 8) {                                  \
158     SILC_GET32_MSB(d[2], &s[8]);                \
159     SILC_GET32_MSB(d[3], &s[12]);               \
160   }                                             \
161 } while(0);
162
163 #define SILC_CBC_MSB_DEC_POST(s, d, p, t, siv, l)       \
164 do {                                                    \
165   s[0] ^= siv[0];                                       \
166   s[1] ^= siv[1];                                       \
167   if (l > 8) {                                          \
168     s[2] ^= siv[2];                                     \
169     s[3] ^= siv[3];                                     \
170   }                                                     \
171                                                         \
172   SILC_PUT32_MSB(s[0], &d[0]);                          \
173   SILC_PUT32_MSB(s[1], &d[4]);                          \
174   if (l > 8) {                                          \
175     SILC_PUT32_MSB(s[2], &d[8]);                        \
176     SILC_PUT32_MSB(s[3], &d[12]);                       \
177   }                                                     \
178                                                         \
179   siv[0] = t[0];                                        \
180   siv[1] = t[1];                                        \
181   if (l > 8) {                                          \
182     siv[2] = t[2];                                      \
183     siv[3] = t[3];                                      \
184   }                                                     \
185                                                         \
186   d += l;                                               \
187   p += l;                                               \
188 } while(0);
189
190 #endif