File: 3-way.c

package info (click to toggle)
libmcrypt 2.5.8-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,060 kB
  • sloc: ansic: 15,868; sh: 8,579; makefile: 196
file content (351 lines) | stat: -rw-r--r-- 7,451 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/********************************************************************\
*                                                                    *
* C specification of the threeway block cipher                       *
*                                                                    *
* found in ftp://ftp.funet.fi/pub/crypt/                             *
\********************************************************************/

/* modified in order to use the libmcrypt API by Nikos Mavroyanopoulos 
 * All modifications are placed under the license of libmcrypt.
 */

/* $Id: 3-way.c,v 1.12 2003/01/19 17:48:27 nmav Exp $ */

#include <libdefs.h>

#include <mcrypt_modules.h>
#include "3-way.h"

#define   STRT_E   0x0b0b	/* round constant of first encryption round */
#define   STRT_D   0xb1b1	/* round constant of first decryption round */
#define     NMBR       11	/* number of rounds is 11                   */

#define _mcrypt_set_key threeway_LTX__mcrypt_set_key
#define _mcrypt_encrypt threeway_LTX__mcrypt_encrypt
#define _mcrypt_decrypt threeway_LTX__mcrypt_decrypt
#define _mcrypt_get_size threeway_LTX__mcrypt_get_size
#define _mcrypt_get_block_size threeway_LTX__mcrypt_get_block_size
#define _is_block_algorithm threeway_LTX__is_block_algorithm
#define _mcrypt_get_key_size threeway_LTX__mcrypt_get_key_size
#define _mcrypt_get_supported_key_sizes threeway_LTX__mcrypt_get_supported_key_sizes
#define _mcrypt_get_algorithms_name threeway_LTX__mcrypt_get_algorithms_name
#define _mcrypt_self_test threeway_LTX__mcrypt_self_test
#define _mcrypt_algorithm_version threeway_LTX__mcrypt_algorithm_version

WIN32DLL_DEFINE
    int _mcrypt_set_key(word32 * k, word32 * input_key, int len)
{
	k[0] = 0;
	k[2] = 0;
	k[1] = 0;
	memmove(k, input_key, len);
	return 0;
}


void mu(word32 * a)
{				/* inverts the order of the bits of a */
	int i;
	word32 b[3];

	b[0] = b[1] = b[2] = 0;
	for (i = 0; i < 32; i++) {
		b[0] <<= 1;
		b[1] <<= 1;
		b[2] <<= 1;
		if (a[0] & 1)
			b[2] |= 1;
		if (a[1] & 1)
			b[1] |= 1;
		if (a[2] & 1)
			b[0] |= 1;
		a[0] >>= 1;
		a[1] >>= 1;
		a[2] >>= 1;
	}

	a[0] = b[0];
	a[1] = b[1];
	a[2] = b[2];
}

void gamma3w(word32 * a)
{				/* the nonlinear step */
	word32 b[3];

	b[0] = a[0] ^ (a[1] | (~a[2]));
	b[1] = a[1] ^ (a[2] | (~a[0]));
	b[2] = a[2] ^ (a[0] | (~a[1]));

	a[0] = b[0];
	a[1] = b[1];
	a[2] = b[2];
}


void theta(word32 * a)
{				/* the linear step */
	word32 b[3];

	b[0] =
	    a[0] ^ (a[0] >> 16) ^ (a[1] << 16) ^ (a[1] >> 16) ^ (a[2] <<
								 16) ^
	    (a[1] >> 24) ^ (a[2] << 8) ^ (a[2] >> 8) ^ (a[0] << 24) ^ (a[2]
								       >>
								       16)
	    ^ (a[0] << 16) ^ (a[2] >> 24) ^ (a[0] << 8);
	b[1] =
	    a[1] ^ (a[1] >> 16) ^ (a[2] << 16) ^ (a[2] >> 16) ^ (a[0] <<
								 16) ^
	    (a[2] >> 24) ^ (a[0] << 8) ^ (a[0] >> 8) ^ (a[1] << 24) ^ (a[0]
								       >>
								       16)
	    ^ (a[1] << 16) ^ (a[0] >> 24) ^ (a[1] << 8);
	b[2] =
	    a[2] ^ (a[2] >> 16) ^ (a[0] << 16) ^ (a[0] >> 16) ^ (a[1] <<
								 16) ^
	    (a[0] >> 24) ^ (a[1] << 8) ^ (a[1] >> 8) ^ (a[2] << 24) ^ (a[1]
								       >>
								       16)
	    ^ (a[2] << 16) ^ (a[1] >> 24) ^ (a[2] << 8);

	a[0] = b[0];
	a[1] = b[1];
	a[2] = b[2];
}

void pi_1(word32 * a)
{
	a[0] = (a[0] >> 10) ^ (a[0] << 22);
	a[2] = (a[2] << 1) ^ (a[2] >> 31);
}

void pi_2(word32 * a)
{
	a[0] = (a[0] << 1) ^ (a[0] >> 31);
	a[2] = (a[2] >> 10) ^ (a[2] << 22);
}

void rho(word32 * a)
{				/* the round function       */
	theta(a);
	pi_1(a);
	gamma3w(a);
	pi_2(a);
}

void rndcon_gen(word32 strt, word32 * rtab)
{				/* generates the round constants */
	int i;

	for (i = 0; i <= NMBR; i++) {
		rtab[i] = strt;
		strt <<= 1;
		if (strt & 0x10000)
			strt ^= 0x11011;
	}
}

WIN32DLL_DEFINE void _mcrypt_encrypt(word32 * tk, word32 * ta)
{
	int i;
	word32 rcon[NMBR + 1];
	word32 a[3], k[3];

/* Added to make it compatible with bigendian machines
 * --nikos
 */

#ifndef WORDS_BIGENDIAN
	a[0] = byteswap32(ta[0]);
	a[1] = byteswap32(ta[1]);
	a[2] = byteswap32(ta[2]);
	k[0] = byteswap32(tk[0]);
	k[1] = byteswap32(tk[1]);
	k[2] = byteswap32(tk[2]);
#else
	a[0] = ta[0];
	a[1] = ta[1];
	a[2] = ta[2];
	k[0] = (tk[0]);
	k[1] = (tk[1]);
	k[2] = (tk[2]);
#endif

	rndcon_gen(STRT_E, rcon);
	for (i = 0; i < NMBR; i++) {
		a[0] ^= k[0] ^ (rcon[i] << 16);
		a[1] ^= k[1];
		a[2] ^= k[2] ^ rcon[i];
		rho(a);
	}
	a[0] ^= k[0] ^ (rcon[NMBR] << 16);
	a[1] ^= k[1];
	a[2] ^= k[2] ^ rcon[NMBR];
	theta(a);

#ifndef WORDS_BIGENDIAN
	ta[0] = byteswap32(a[0]);
	ta[1] = byteswap32(a[1]);
	ta[2] = byteswap32(a[2]);
#else
	ta[0] = a[0];
	ta[1] = a[1];
	ta[2] = a[2];
#endif

}


WIN32DLL_DEFINE void _mcrypt_decrypt(word32 * k, word32 * ta)
{
	int i;
	word32 ki[3];		/* the `inverse' key             */
	word32 rcon[NMBR + 1];	/* the `inverse' round constants */
	word32 a[3];

#ifndef WORDS_BIGENDIAN
	a[0] = byteswap32(ta[0]);
	a[1] = byteswap32(ta[1]);
	a[2] = byteswap32(ta[2]);
	ki[0] = byteswap32(k[0]);
	ki[1] = byteswap32(k[1]);
	ki[2] = byteswap32(k[2]);
#else
	a[0] = ta[0];
	a[1] = ta[1];
	a[2] = ta[2];
	ki[0] = k[0];
	ki[1] = k[1];
	ki[2] = k[2];
#endif

	theta(ki);
	mu(ki);

	rndcon_gen(STRT_D, rcon);

	mu(a);
	for (i = 0; i < NMBR; i++) {
		a[0] ^= ki[0] ^ (rcon[i] << 16);
		a[1] ^= ki[1];
		a[2] ^= ki[2] ^ rcon[i];
		rho(a);
	}
	a[0] ^= ki[0] ^ (rcon[NMBR] << 16);
	a[1] ^= ki[1];
	a[2] ^= ki[2] ^ rcon[NMBR];
	theta(a);
	mu(a);

#ifndef WORDS_BIGENDIAN
	ta[0] = byteswap32(a[0]);
	ta[1] = byteswap32(a[1]);
	ta[2] = byteswap32(a[2]);

#else
	ta[0] = a[0];
	ta[1] = a[1];
	ta[2] = a[2];
#endif

}

WIN32DLL_DEFINE int _mcrypt_get_size()
{
	return 3 * sizeof(word32);
}
WIN32DLL_DEFINE int _mcrypt_get_block_size()
{
	return 12;
}
WIN32DLL_DEFINE int _is_block_algorithm()
{
	return 1;
}
WIN32DLL_DEFINE int _mcrypt_get_key_size()
{
	return 12;
}

static const int key_sizes[] = { 12 };
WIN32DLL_DEFINE const int *_mcrypt_get_supported_key_sizes(int *len)
{
	*len = sizeof(key_sizes)/sizeof(int);
	return key_sizes;

}
WIN32DLL_DEFINE const char *_mcrypt_get_algorithms_name()
{
return "3-WAY";
}

#define CIPHER "46823287358d68f6e034ca62"

WIN32DLL_DEFINE int _mcrypt_self_test()
{
	char *keyword;
	unsigned char plaintext[16];
	unsigned char ciphertext[16];
	int blocksize = _mcrypt_get_block_size(), j;
	void *key;
	unsigned char cipher_tmp[200];

	keyword = calloc(1, _mcrypt_get_key_size());
	if (keyword == NULL)
		return -1;

	for (j = 0; j < _mcrypt_get_key_size(); j++) {
		keyword[j] = ((j * 2 + 10) % 256);
	}

	for (j = 0; j < blocksize; j++) {
		plaintext[j] = j % 256;
	}
	key = malloc(_mcrypt_get_size());
	if (key == NULL)
		return -1;

	memcpy(ciphertext, plaintext, blocksize);

	_mcrypt_set_key(key, (void *) keyword, _mcrypt_get_key_size());
	free(keyword);

	_mcrypt_encrypt(key, (void *) ciphertext);

	for (j = 0; j < blocksize; j++) {
		sprintf(&((char *) cipher_tmp)[2 * j], "%.2x",
			ciphertext[j]);
	}

	if (strcmp((char *) cipher_tmp, CIPHER) != 0) {
		printf("failed compatibility\n");
		printf("Expected: %s\nGot: %s\n", CIPHER,
		       (char *) cipher_tmp);
		free(key);
		return -1;
	}
	_mcrypt_decrypt(key, (void *) ciphertext);
	free(key);

	if (strcmp(ciphertext, plaintext) != 0) {
		printf("failed internally\n");
		return -1;
	}

	return 0;
}

WIN32DLL_DEFINE word32 _mcrypt_algorithm_version()
{
	return 20010801;
}

#ifdef WIN32
# ifdef USE_LTDL
WIN32DLL_DEFINE int main (void)
{
       /* empty main function to avoid linker error (see cygwin FAQ) */
}
# endif
#endif