Explorar el Código

Allow MatrixSSL code to compile inside gPXE

tags/v0.9.3
Michael Brown hace 18 años
padre
commit
3bb7c19779
Se han modificado 3 ficheros con 1266 adiciones y 0 borrados
  1. 118
    0
      src/crypto/cryptoLayer.h
  2. 487
    0
      src/crypto/matrixssl/mpi.h
  3. 661
    0
      src/crypto/matrixssl/pscrypto.h

+ 118
- 0
src/crypto/cryptoLayer.h Ver fichero

@@ -0,0 +1,118 @@
1
+#ifndef _MATRIXSSL_CRYPTOLAYER_H
2
+#define _MATRIXSSL_CRYPTOLAYER_H
3
+
4
+/** @file
5
+ *
6
+ * Compatibility layer for MatrixSSL
7
+ *
8
+ */
9
+
10
+#include <stdint.h>
11
+#include <stddef.h>
12
+#include <stdlib.h>
13
+#include <string.h>
14
+#include <ctype.h>
15
+#include <assert.h>
16
+#include <byteswap.h>
17
+#include <gpxe/bitops.h>
18
+#include <gpxe/crypto.h>
19
+
20
+/* Drag in pscrypto.h */
21
+typedef uint64_t ulong64;
22
+typedef void psPool_t;
23
+#define SMALL_CODE
24
+#define USE_INT64
25
+#define USE_RSA
26
+#define USE_RSA_PUBLIC_ENCRYPT
27
+#define CRYPT
28
+#include "matrixssl/pscrypto.h"
29
+#define SMALL_CODE
30
+#undef CLEAN_STACK
31
+
32
+#define sslAssert( ... ) assert ( __VA_ARGS__ )
33
+
34
+static inline void * psMalloc ( psPool_t *pool __unused, size_t len ) {
35
+	return malloc ( len );
36
+}
37
+
38
+static inline void * psRealloc ( void *ptr, size_t len ) {
39
+	return realloc ( ptr, len );
40
+}
41
+
42
+static inline void psFree ( void *ptr ) {
43
+	free ( ptr );
44
+}
45
+
46
+#define matrixStrDebugMsg( ... ) DBG ( __VA_ARGS__ )
47
+#define matrixIntDebugMsg( ... ) DBG ( __VA_ARGS__ )
48
+
49
+/* Use our standard cpu_to_leXX etc. macros */
50
+
51
+#undef LOAD32L
52
+#define LOAD32L( cpu32, ptr ) do {				\
53
+		uint32_t *le32 = ( ( uint32_t * ) ptr );	\
54
+		cpu32 = le32_to_cpu ( *le32 );			\
55
+	} while ( 0 )
56
+
57
+#undef LOAD32H
58
+#define LOAD32H( cpu32, ptr ) do {				\
59
+		uint32_t *be32 = ( ( uint32_t * ) ptr );	\
60
+		cpu32 = be32_to_cpu ( *be32 );			\
61
+	} while ( 0 )
62
+
63
+#undef LOAD64L
64
+#define LOAD64L( cpu64, ptr ) do {				\
65
+		uint64_t *le64 = ( ( uint64_t * ) ptr );	\
66
+		cpu64 = le64_to_cpu ( *le64 );			\
67
+	} while ( 0 )
68
+
69
+#undef LOAD64H
70
+#define LOAD64H( cpu64, ptr ) do {				\
71
+		uint64_t *be64 = ( ( uint64_t * ) ptr );	\
72
+		cpu64 = be64_to_cpu ( *be64 );			\
73
+	} while ( 0 )
74
+
75
+#undef STORE32L
76
+#define STORE32L( cpu32, ptr ) do {				\
77
+		uint32_t *le32 = ( ( uint32_t * ) ptr );	\
78
+		*le32 = cpu_to_le32 ( cpu32 );			\
79
+	} while ( 0 )
80
+
81
+#undef STORE32H
82
+#define STORE32H( cpu32, ptr ) do {				\
83
+		uint32_t *be32 = ( ( uint32_t * ) ptr );	\
84
+		*be32 = cpu_to_be32 ( cpu32 );			\
85
+	} while ( 0 )
86
+
87
+#undef STORE64L
88
+#define STORE64L( cpu64, ptr ) do {				\
89
+		uint64_t *le64 = ( ( uint64_t * ) ptr );	\
90
+		*le64 = cpu_to_le64 ( cpu64 );			\
91
+	} while ( 0 )
92
+
93
+#undef STORE64H
94
+#define STORE64H( cpu64, ptr ) do {				\
95
+		uint64_t *be64 = ( ( uint64_t * ) ptr );	\
96
+		*be64 = cpu_to_be64 ( cpu64 );			\
97
+	} while ( 0 )
98
+
99
+/* Use rolXX etc. from bitops.h */
100
+
101
+#undef ROL
102
+#define ROL( data, rotation )	 rol32 ( (data), (rotation) )
103
+#undef ROLc
104
+#define ROLc( data, rotation )	 rol32 ( (data), (rotation) )
105
+#undef ROR
106
+#define ROR( data, rotation )	 ror32 ( (data), (rotation) )
107
+#undef RORc
108
+#define RORc( data, rotation )	 ror32 ( (data), (rotation) )
109
+#undef ROL64
110
+#define ROL64( data, rotation )	 rol64 ( (data), (rotation) )
111
+#undef ROL64c
112
+#define ROL64c( data, rotation ) rol64 ( (data), (rotation) )
113
+#undef ROR64
114
+#define ROR64( data, rotation )	 ror64 ( (data), (rotation) )
115
+#undef ROR64c
116
+#define ROR64c( data, rotation ) ror64 ( (data), (rotation) )
117
+
118
+#endif /* _MATRIXSSL_CRYPTOLAYER_H */

+ 487
- 0
src/crypto/matrixssl/mpi.h Ver fichero

@@ -0,0 +1,487 @@
1
+/*	
2
+ *	mpi.h
3
+ *	Release $Name$
4
+ *
5
+ *	multiple-precision integer library
6
+ */
7
+/*
8
+ *	Copyright (c) PeerSec Networks, 2002-2006. All Rights Reserved.
9
+ *	The latest version of this code is available at http://www.matrixssl.org
10
+ *
11
+ *	This software is open source; you can redistribute it and/or modify
12
+ *	it under the terms of the GNU General Public License as published by
13
+ *	the Free Software Foundation; either version 2 of the License, or
14
+ *	(at your option) any later version.
15
+ *
16
+ *	This General Public License does NOT permit incorporating this software 
17
+ *	into proprietary programs.  If you are unable to comply with the GPL, a 
18
+ *	commercial license for this software may be purchased from PeerSec Networks
19
+ *	at http://www.peersec.com
20
+ *	
21
+ *	This program is distributed in WITHOUT ANY WARRANTY; without even the 
22
+ *	implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
23
+ *	See the 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  USA
28
+ *	http://www.gnu.org/copyleft/gpl.html
29
+ */
30
+/******************************************************************************/
31
+
32
+#ifndef _h_MPI
33
+#define _h_MPI
34
+
35
+#include <stdio.h>
36
+#include <string.h>
37
+#include <stdlib.h>
38
+#include <ctype.h>
39
+#include <limits.h>
40
+
41
+#undef MIN
42
+#define MIN(x,y) ((x)<(y)?(x):(y))
43
+#undef MAX
44
+#define MAX(x,y) ((x)>(y)?(x):(y))
45
+
46
+#ifdef __cplusplus
47
+extern "C" {
48
+
49
+
50
+/*
51
+	C++ compilers don't like assigning void * to mp_digit *
52
+ */
53
+#define  OPT_CAST(x)  (x *)
54
+
55
+#else
56
+
57
+/*
58
+	C on the other hand doesn't care
59
+ */
60
+#define  OPT_CAST(x)
61
+
62
+#endif /* __cplusplus */
63
+
64
+/******************************************************************************/
65
+/*
66
+	some default configurations.
67
+
68
+	A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
69
+	A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
70
+
71
+	At the very least a mp_digit must be able to hold 7 bits
72
+	[any size beyond that is ok provided it doesn't overflow the data type]
73
+ */
74
+#ifdef MP_8BIT
75
+	typedef unsigned char		mp_digit;
76
+	typedef unsigned short		mp_word;
77
+#elif defined(MP_16BIT)
78
+	typedef unsigned short		mp_digit;
79
+	typedef unsigned long		mp_word;
80
+#elif defined(MP_64BIT)
81
+/*
82
+	for GCC only on supported platforms
83
+ */
84
+	#ifndef CRYPT
85
+		typedef unsigned long long	ulong64;
86
+		typedef signed long long	long64;
87
+	#endif /* CRYPT */
88
+
89
+	typedef ulong64				mp_digit;
90
+	typedef unsigned long		mp_word __attribute__ ((mode(TI)));
91
+
92
+	#define DIGIT_BIT			60
93
+#else  /* MP_8BIT */
94
+/*
95
+	this is the default case, 28-bit digits
96
+ */
97
+	#ifndef CRYPT
98
+		#if defined(_MSC_VER) || defined(__BORLANDC__) 
99
+			typedef unsigned __int64	ulong64;
100
+			typedef signed __int64		long64;
101
+		#else
102
+			typedef unsigned long long	ulong64;
103
+			typedef signed long long	long64;
104
+		#endif
105
+	#endif /* CRYPT */
106
+
107
+	typedef unsigned long		mp_digit;
108
+	typedef ulong64				mp_word;
109
+
110
+	#ifdef MP_31BIT
111
+/*
112
+		this is an extension that uses 31-bit digits
113
+ */
114
+		#define DIGIT_BIT		31
115
+	#else /* MP_31BIT */
116
+/*
117
+		default case is 28-bit digits, defines MP_28BIT as a handy macro to test
118
+ */
119
+		#define DIGIT_BIT		28
120
+		#define MP_28BIT
121
+	#endif /* MP_31BIT */
122
+#endif /* MP_8BIT */
123
+
124
+/*
125
+	otherwise the bits per digit is calculated automatically from the size of
126
+	a mp_digit
127
+ */
128
+#ifndef DIGIT_BIT
129
+	#define DIGIT_BIT	((int32)((CHAR_BIT * sizeof(mp_digit) - 1)))	/* bits per digit */
130
+#endif /* DIGIT_BIT */
131
+
132
+#define MP_DIGIT_BIT		DIGIT_BIT
133
+#define MP_MASK				((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
134
+#define MP_DIGIT_MAX		MP_MASK
135
+
136
+/******************************************************************************/
137
+/*
138
+	equalities
139
+ */
140
+#define MP_LT			-1		/* less than */
141
+#define MP_EQ			0		/* equal to */
142
+#define MP_GT			1		/* greater than */
143
+
144
+#define MP_ZPOS			0		/* positive integer */
145
+#define MP_NEG			1		/* negative */
146
+
147
+#define MP_OKAY			0		/* ok result */
148
+#define MP_MEM			-2		/* out of mem */
149
+#define MP_VAL			-3		/* invalid input */
150
+#define MP_RANGE		MP_VAL
151
+
152
+#define MP_YES			1		/* yes response */
153
+#define MP_NO			0		/* no response */
154
+
155
+typedef int32				mp_err;
156
+
157
+/******************************************************************************/
158
+/*
159
+	various build options
160
+ */
161
+#define MP_PREC			64		/* default digits of precision */
162
+
163
+/*
164
+	define this to use lower memory usage routines (exptmods mostly)
165
+ */
166
+#define MP_LOW_MEM
167
+
168
+/*
169
+	size of comba arrays, should be at least 
170
+	2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2)
171
+ */
172
+#define MP_WARRAY		(1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
173
+
174
+typedef struct  {
175
+	int32 used, alloc, sign;
176
+	mp_digit *dp;
177
+} mp_int;
178
+
179
+#define USED(m)		((m)->used)
180
+#define DIGIT(m,k)	((m)->dp[(k)])
181
+#define SIGN(m)		((m)->sign)
182
+
183
+/******************************************************************************/
184
+/*
185
+	init and deinit bignum functions
186
+ */
187
+
188
+/*
189
+	init a bignum
190
+ */
191
+extern int32 mp_init(psPool_t *pool, mp_int *a);
192
+
193
+/*
194
+	free a bignum
195
+ */
196
+extern void mp_clear(mp_int *a);
197
+
198
+/*
199
+	init a series of arguments
200
+ */
201
+extern int32 _mp_init_multi(psPool_t *pool, mp_int *mp0, mp_int *mp1, mp_int *mp2, 
202
+							mp_int *mp3, mp_int *mp4, mp_int *mp5, mp_int *mp6, 
203
+							mp_int *mp7);
204
+
205
+/*
206
+	clear a  series of arguments
207
+ */
208
+extern void _mp_clear_multi(mp_int *mp0, mp_int *mp1, mp_int *mp2, mp_int *mp3,
209
+					mp_int *mp4, mp_int *mp5, mp_int *mp6, mp_int *mp7);
210
+
211
+/*
212
+	exchange two ints
213
+ */
214
+extern void mp_exch(mp_int *a, mp_int *b);
215
+
216
+/*
217
+	shrink ram required for a bignum
218
+ */
219
+extern int32 mp_shrink(mp_int *a);
220
+
221
+/*
222
+	grow an int32 to a given size
223
+ */
224
+extern int32 mp_grow(mp_int *a, int32 size);
225
+
226
+/*
227
+	init to a given number of digits
228
+ */
229
+extern int32 mp_init_size(psPool_t *pool, mp_int *a, int32 size);
230
+
231
+/******************************************************************************/
232
+/*
233
+	Basic Manipulations
234
+ */
235
+#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
236
+#define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
237
+#define mp_isodd(a)  (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
238
+
239
+extern int32 mp_add_d (mp_int * a, mp_digit b, mp_int * c);
240
+extern int32 mp_sub_d (mp_int * a, mp_digit b, mp_int * c);
241
+/*
242
+	set to zero
243
+ */
244
+extern void mp_zero(mp_int *a);
245
+
246
+/*
247
+	set to a digit
248
+ */
249
+extern void mp_set(mp_int *a, mp_digit b);
250
+
251
+/*
252
+	copy, b = a
253
+ */
254
+extern int32 mp_copy(mp_int *a, mp_int *b);
255
+
256
+/*
257
+	inits and copies, a = b
258
+ */
259
+extern int32 mp_init_copy(psPool_t *pool, mp_int *a, mp_int *b);
260
+
261
+/*
262
+	trim unused digits
263
+ */
264
+extern void mp_clamp(mp_int *a);
265
+
266
+/******************************************************************************/
267
+/*
268
+	digit manipulation
269
+*/
270
+
271
+/*
272
+	right shift by "b" digits
273
+ */
274
+extern void mp_rshd(mp_int *a, int32 b);
275
+
276
+/*
277
+	left shift by "b" digits
278
+ */
279
+extern int32 mp_lshd(mp_int *a, int32 b);
280
+
281
+/*
282
+	c = a / 2**b
283
+ */
284
+extern int32 mp_div_2d(psPool_t *pool, mp_int *a, int32 b, mp_int *c, mp_int *d);
285
+
286
+/*
287
+	b = a/2
288
+ */
289
+extern int32 mp_div_2(mp_int *a, mp_int *b);
290
+
291
+/*
292
+	c = a * 2**b
293
+ */
294
+extern int32 mp_mul_2d(mp_int *a, int32 b, mp_int *c);
295
+
296
+/*
297
+	c = a mod 2**d
298
+ */
299
+extern int32 mp_mod_2d(mp_int *a, int32 b, mp_int *c);
300
+
301
+/*
302
+	computes a = 2**b
303
+ */
304
+extern int32 mp_2expt(mp_int *a, int32 b);
305
+
306
+/******************************************************************************/
307
+/*
308
+	Basic arithmetic
309
+ */
310
+
311
+/*
312
+	b = |a|
313
+ */
314
+extern int32 mp_abs(mp_int *a, mp_int *b);
315
+
316
+/*
317
+	compare a to b
318
+ */
319
+extern int32 mp_cmp(mp_int *a, mp_int *b);
320
+
321
+/*
322
+	compare |a| to |b|
323
+ */
324
+extern int32 mp_cmp_mag(mp_int *a, mp_int *b);
325
+
326
+/*
327
+	c = a + b
328
+ */
329
+extern int32 mp_add(mp_int *a, mp_int *b, mp_int *c);
330
+
331
+/*
332
+	c = a - b
333
+ */
334
+extern int32 mp_sub(mp_int *a, mp_int *b, mp_int *c);
335
+
336
+/*
337
+	c = a * b
338
+	b = a*a
339
+ */
340
+/* STEVE - moved mp_mul out of SLOW case */
341
+extern int32 mp_mul(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c);
342
+#ifdef USE_SMALL_WORD
343
+extern int32 mp_sqr(psPool_t *pool, mp_int *a, mp_int *b);
344
+#endif
345
+
346
+/*
347
+	a/b => cb + d == a
348
+ */
349
+extern int32 mp_div(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c, mp_int *d);
350
+
351
+/*
352
+	c = a mod b, 0 <= c < b
353
+ */
354
+extern int32 mp_mod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c);
355
+
356
+/******************************************************************************/
357
+/*
358
+	single digit functions
359
+ */
360
+
361
+/*
362
+	compare against a single digit
363
+ */
364
+extern int32 mp_cmp_d(mp_int *a, mp_digit b);
365
+
366
+/*
367
+	c = a * b
368
+ */
369
+extern int32 mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
370
+
371
+/******************************************************************************/
372
+/*
373
+	number theory
374
+ */
375
+
376
+/*
377
+	d = a + b (mod c)
378
+ */
379
+extern int32 mp_addmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c, mp_int *d);
380
+
381
+/*
382
+	d = a * b (mod c)
383
+ */
384
+extern int32 mp_mulmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c, mp_int *d);
385
+
386
+/*
387
+	c = 1/a (mod b)
388
+ */
389
+#ifdef USE_SMALL_WORD
390
+extern int32 mp_invmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c);
391
+#endif
392
+
393
+/*
394
+	setups the montgomery reduction
395
+ */
396
+extern int32 mp_montgomery_setup(mp_int *a, mp_digit *mp);
397
+
398
+/*
399
+	computes a = B**n mod b without division or multiplication useful for
400
+	normalizing numbers in a Montgomery system.
401
+ */
402
+extern int32 mp_montgomery_calc_normalization(mp_int *a, mp_int *b);
403
+
404
+/*
405
+	computes x/R == x (mod N) via Montgomery Reduction
406
+ */
407
+#ifdef USE_SMALL_WORD
408
+extern int32 mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
409
+#endif
410
+
411
+/*
412
+	d = a**b (mod c)
413
+ */
414
+/* TODO - we never define this */
415
+extern int32 mp_exptmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c, mp_int *d);
416
+
417
+/******************************************************************************/
418
+/*
419
+	If we're using 1024 or 2048 bit keys and 28 bit digits, we only need the
420
+	fast_ versions of these functions, removing the others to save space.
421
+	Otherwise, we include the slow versions as well and which version to use
422
+	is done at runtime.
423
+*/
424
+#ifdef USE_SMALL_WORD
425
+extern int32 s_mp_mul_digs(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c,
426
+						   int32 digs);
427
+extern int32 s_mp_sqr(psPool_t *pool, mp_int *a, mp_int *b);
428
+#else
429
+#define mp_montgomery_reduce fast_mp_montgomery_reduce
430
+#define mp_sqr	fast_s_mp_sqr
431
+#if STEVE
432
+#define mp_mul(P, A, B, C) fast_s_mp_mul_digs(P, A, B, C, (A)->used + (B)->used + 1)
433
+#endif
434
+#define s_mp_mul_digs	fast_s_mp_mul_digs
435
+#define mp_invmod	fast_mp_invmod
436
+#endif
437
+
438
+/******************************************************************************/
439
+/*
440
+	radix conversion
441
+ */
442
+extern int32 mp_count_bits(mp_int *a);
443
+
444
+extern int32 mp_unsigned_bin_size(mp_int *a);
445
+extern int32 mp_read_unsigned_bin(mp_int *a, unsigned char *b, int32 c);
446
+extern int32 mp_to_unsigned_bin(psPool_t *pool, mp_int *a, unsigned char *b);
447
+
448
+extern int32 mp_signed_bin_size(mp_int *a);
449
+
450
+/*
451
+	lowlevel functions, do not call!
452
+ */
453
+#if STEVE
454
+#ifdef USE_SMALL_WORD
455
+#define s_mp_mul(P, A, B, C) s_mp_mul_digs(P, A, B, C, (A)->used + (B)->used + 1)
456
+#else
457
+#define s_mp_mul(P, A, B, C) sslAssert();
458
+#endif
459
+#endif /* STEVE */
460
+/* define this in all cases for now STEVE */
461
+#define s_mp_mul(P, A, B, C) s_mp_mul_digs(P, A, B, C, (A)->used + (B)->used + 1)
462
+
463
+
464
+/*
465
+	b = a*2
466
+ */
467
+extern int32 mp_mul_2(mp_int *a, mp_int *b);
468
+
469
+extern int32 s_mp_add(mp_int *a, mp_int *b, mp_int *c);
470
+extern int32 s_mp_sub(mp_int *a, mp_int *b, mp_int *c);
471
+
472
+extern int32 fast_s_mp_mul_digs(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c,
473
+								int32 digs);
474
+extern int32 fast_s_mp_sqr(psPool_t *pool, mp_int *a, mp_int *b);
475
+
476
+extern int32 fast_mp_invmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c);
477
+extern int32 fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);
478
+
479
+extern void bn_reverse(unsigned char *s, int32 len);
480
+
481
+
482
+#ifdef __cplusplus
483
+   }
484
+#endif /* __cplusplus */
485
+
486
+#endif /* _h_MPI */
487
+

+ 661
- 0
src/crypto/matrixssl/pscrypto.h Ver fichero

@@ -0,0 +1,661 @@
1
+/*
2
+ *	pscrypto.h
3
+ *	Release $Name$
4
+ *
5
+ *	Internal definitions for PeerSec Networks MatrixSSL cryptography provider
6
+ */
7
+/*
8
+ *	Copyright (c) PeerSec Networks, 2002-2006. All Rights Reserved.
9
+ *	The latest version of this code is available at http://www.matrixssl.org
10
+ *
11
+ *	This software is open source; you can redistribute it and/or modify
12
+ *	it under the terms of the GNU General Public License as published by
13
+ *	the Free Software Foundation; either version 2 of the License, or
14
+ *	(at your option) any later version.
15
+ *
16
+ *	This General Public License does NOT permit incorporating this software 
17
+ *	into proprietary programs.  If you are unable to comply with the GPL, a 
18
+ *	commercial license for this software may be purchased from PeerSec Networks
19
+ *	at http://www.peersec.com
20
+ *	
21
+ *	This program is distributed in WITHOUT ANY WARRANTY; without even the 
22
+ *	implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
23
+ *	See the 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  USA
28
+ *	http://www.gnu.org/copyleft/gpl.html
29
+ */
30
+/******************************************************************************/
31
+
32
+#ifndef _h_PSCRYPTO
33
+#define _h_PSCRYPTO
34
+
35
+#ifdef __cplusplus
36
+extern "C" {
37
+#endif
38
+
39
+/*
40
+	PeerSec crypto-specific defines.
41
+ */
42
+#define SMALL_CODE
43
+#define CLEAN_STACK
44
+/*
45
+	If Native 64 bit integers are not supported, we must set the 16 bit flag
46
+	to produce 32 bit mp_words in mpi.h
47
+	We must also include the slow MPI functions because the fast ones only
48
+	work with larger (28 bit) digit sizes.
49
+*/
50
+#ifndef USE_INT64
51
+#define MP_16BIT
52
+#define USE_SMALL_WORD
53
+#endif /* USE_INT64 */
54
+
55
+/******************************************************************************/
56
+
57
+#ifdef USE_RSA
58
+
59
+#include "mpi.h"
60
+
61
+#if LINUX
62
+	#define _stat stat
63
+#endif
64
+
65
+/* this is the "32-bit at least" data type 
66
+ * Re-define it to suit your platform but it must be at least 32-bits 
67
+ */
68
+typedef unsigned long ulong32;
69
+
70
+/*
71
+	Primary RSA Key struct.  Define here for crypto
72
+*/
73
+typedef struct {
74
+	mp_int		e, d, N, qP, dP, dQ, p, q;
75
+	int32			size;	/* Size of the key in bytes */
76
+	int32			optimized; /* 1 for optimized */
77
+} sslRsaKey_t;
78
+
79
+#endif /* USE_RSA */
80
+
81
+
82
+/*
83
+ *	Private
84
+ */
85
+extern int32 ps_base64_decode(const unsigned char *in, uint32 len, 
86
+							unsigned char *out, uint32 *outlen);
87
+
88
+/*
89
+ *	Memory routines
90
+ */
91
+extern void psZeromem(void *dst, size_t len);
92
+extern void psBurnStack(unsigned long len);
93
+
94
+
95
+/* max size of either a cipher/hash block or symmetric key [largest of the two] */
96
+#define MAXBLOCKSIZE			24
97
+
98
+/* ch1-01-1 */
99
+/* error codes [will be expanded in future releases] */
100
+enum {
101
+	CRYPT_OK=0,					/* Result OK */
102
+	CRYPT_ERROR,				/* Generic Error */
103
+	CRYPT_NOP,					/* Not a failure but no operation was performed */
104
+
105
+	CRYPT_INVALID_KEYSIZE,		/* Invalid key size given */
106
+	CRYPT_INVALID_ROUNDS,		/* Invalid number of rounds */
107
+	CRYPT_FAIL_TESTVECTOR,		/* Algorithm failed test vectors */
108
+
109
+	CRYPT_BUFFER_OVERFLOW,		/* Not enough space for output */
110
+	CRYPT_INVALID_PACKET,		/* Invalid input packet given */
111
+
112
+	CRYPT_INVALID_PRNGSIZE,		/* Invalid number of bits for a PRNG */
113
+	CRYPT_ERROR_READPRNG,		/* Could not read enough from PRNG */
114
+
115
+	CRYPT_INVALID_CIPHER,		/* Invalid cipher specified */
116
+	CRYPT_INVALID_HASH,			/* Invalid hash specified */
117
+	CRYPT_INVALID_PRNG,			/* Invalid PRNG specified */
118
+
119
+	CRYPT_MEM,					/* Out of memory */
120
+
121
+	CRYPT_PK_TYPE_MISMATCH,		/* Not equivalent types of PK keys */
122
+	CRYPT_PK_NOT_PRIVATE,		/* Requires a private PK key */
123
+
124
+	CRYPT_INVALID_ARG,			/* Generic invalid argument */
125
+	CRYPT_FILE_NOTFOUND,		/* File Not Found */
126
+
127
+	CRYPT_PK_INVALID_TYPE,		/* Invalid type of PK key */
128
+	CRYPT_PK_INVALID_SYSTEM,	/* Invalid PK system specified */
129
+	CRYPT_PK_DUP,				/* Duplicate key already in key ring */
130
+	CRYPT_PK_NOT_FOUND,			/* Key not found in keyring */
131
+	CRYPT_PK_INVALID_SIZE,		/* Invalid size input for PK parameters */
132
+
133
+	CRYPT_INVALID_PRIME_SIZE	/* Invalid size of prime requested */
134
+};
135
+
136
+/******************************************************************************/
137
+/*
138
+	hash defines
139
+ */
140
+struct sha1_state {
141
+#ifdef USE_INT64
142
+	ulong64		length;
143
+#else
144
+	ulong32		lengthHi;
145
+	ulong32		lengthLo;
146
+#endif /* USE_INT64 */
147
+	ulong32 state[5], curlen;
148
+	unsigned char	buf[64];
149
+};
150
+
151
+struct md5_state {
152
+#ifdef USE_INT64
153
+	ulong64 length;
154
+#else
155
+	ulong32	lengthHi;
156
+	ulong32	lengthLo;
157
+#endif /* USE_INT64 */
158
+	ulong32 state[4], curlen;
159
+	unsigned char buf[64];
160
+};
161
+
162
+#ifdef USE_MD2
163
+struct md2_state {
164
+	unsigned char chksum[16], X[48], buf[16];
165
+	unsigned long curlen;
166
+};
167
+#endif /* USE_MD2 */
168
+
169
+#ifdef USE_SHA256
170
+struct sha256_state {
171
+	ulong64 length;
172
+	ulong32 state[8], curlen;
173
+	unsigned char buf[64];
174
+};
175
+#endif /* USE_SHA256 */
176
+
177
+typedef union {
178
+	struct sha1_state	sha1;
179
+	struct md5_state	md5;
180
+#ifdef USE_MD2
181
+	struct md2_state	md2;
182
+#endif /* USE_MD2 */
183
+#ifdef USE_SHA256
184
+	struct sha256_state sha256;
185
+#endif
186
+} hash_state;
187
+
188
+typedef hash_state sslSha1Context_t;
189
+typedef hash_state sslMd5Context_t;
190
+#ifdef USE_MD2
191
+typedef hash_state sslMd2Context_t;
192
+#endif /* USE_MD2 */
193
+#ifdef USE_SHA256
194
+typedef hash_state sslSha256Context_t;
195
+#endif /* USE_SHA256 */
196
+
197
+typedef struct {
198
+	unsigned char	pad[64];
199
+	union {
200
+		sslMd5Context_t		md5;
201
+		sslSha1Context_t	sha1;
202
+	} u;
203
+} sslHmacContext_t;
204
+
205
+/******************************************************************************/
206
+/*
207
+	RC4
208
+ */
209
+#ifdef USE_ARC4
210
+typedef struct {
211
+	unsigned char	state[256];
212
+	uint32	byteCount;
213
+	unsigned char	x;
214
+	unsigned char	y;
215
+} rc4_key;
216
+#endif /* USE_ARC4 */
217
+
218
+#define SSL_DES3_KEY_LEN	24
219
+#define SSL_DES3_IV_LEN		8
220
+#ifdef USE_3DES
221
+
222
+typedef struct {
223
+	ulong32 ek[3][32], dk[3][32];
224
+} des3_key;
225
+
226
+/*
227
+	A block cipher CBC structure
228
+ */
229
+typedef struct {
230
+	int32					blocklen;
231
+	unsigned char		IV[8];
232
+	des3_key			key;
233
+	int32					explicitIV; /* 1 if yes */
234
+} des3_CBC;
235
+
236
+extern int32 des3_setup(const unsigned char *key, int32 keylen, int32 num_rounds,
237
+		 des3_CBC *skey);
238
+extern void des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct,
239
+		 des3_CBC *key);
240
+extern void des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt,
241
+		 des3_CBC *key);
242
+extern int32 des3_keysize(int32 *desired_keysize);
243
+
244
+extern int32 des_setup(const unsigned char *key, int32 keylen, int32 num_rounds,
245
+		 des3_CBC *skey);
246
+extern void des_ecb_encrypt(const unsigned char *pt, unsigned char *ct,
247
+		 des3_CBC *key);
248
+extern void des_ecb_decrypt(const unsigned char *ct, unsigned char *pt,
249
+		 des3_CBC *key);
250
+
251
+#endif /* USE_3DES */
252
+
253
+
254
+typedef union {
255
+#ifdef USE_ARC4
256
+	rc4_key		arc4;
257
+#endif
258
+#ifdef USE_3DES
259
+	des3_CBC	des3;
260
+#endif
261
+} sslCipherContext_t;
262
+
263
+
264
+/*
265
+	Controls endianess and size of registers.  Leave uncommented to get
266
+	platform neutral [slower] code detect x86-32 machines somewhat
267
+ */
268
+#if (defined(_MSC_VER) && defined(WIN32)) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__)))
269
+	#define ENDIAN_LITTLE
270
+	#define ENDIAN_32BITWORD
271
+#endif
272
+
273
+
274
+/* #define ENDIAN_LITTLE */
275
+/* #define ENDIAN_BIG */
276
+
277
+/* #define ENDIAN_32BITWORD */
278
+/* #define ENDIAN_64BITWORD */
279
+
280
+#if (defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) && !(defined(ENDIAN_32BITWORD) || defined(ENDIAN_64BITWORD))
281
+		#error You must specify a word size as well as endianess
282
+#endif
283
+
284
+#if !(defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE))
285
+	#define ENDIAN_NEUTRAL
286
+#endif
287
+
288
+/*
289
+	helper macros
290
+ */
291
+#if defined (ENDIAN_NEUTRAL)
292
+
293
+#define STORE32L(x, y)                                                                     \
294
+     { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255);   \
295
+       (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
296
+
297
+#define LOAD32L(x, y)                            \
298
+     { x = ((unsigned long)((y)[3] & 255)<<24) | \
299
+           ((unsigned long)((y)[2] & 255)<<16) | \
300
+           ((unsigned long)((y)[1] & 255)<<8)  | \
301
+           ((unsigned long)((y)[0] & 255)); }
302
+
303
+#define STORE64L(x, y)                                                                     \
304
+     { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255);   \
305
+       (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255);   \
306
+       (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255);   \
307
+       (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
308
+
309
+#define LOAD64L(x, y)                                                       \
310
+     { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
311
+           (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
312
+           (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
313
+           (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
314
+
315
+#define STORE32H(x, y)                                                                     \
316
+     { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255);   \
317
+       (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
318
+
319
+#define LOAD32H(x, y)                            \
320
+     { x = ((unsigned long)((y)[0] & 255)<<24) | \
321
+           ((unsigned long)((y)[1] & 255)<<16) | \
322
+           ((unsigned long)((y)[2] & 255)<<8)  | \
323
+           ((unsigned long)((y)[3] & 255)); }
324
+
325
+#define STORE64H(x, y)                                                                     \
326
+   { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255);     \
327
+     (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255);     \
328
+     (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255);     \
329
+     (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
330
+
331
+#define LOAD64H(x, y)                                                      \
332
+   { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
333
+         (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
334
+         (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
335
+         (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
336
+
337
+#endif /* ENDIAN_NEUTRAL */
338
+
339
+#ifdef ENDIAN_LITTLE
340
+
341
+#define STORE32H(x, y)                                                                     \
342
+     { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255);   \
343
+       (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
344
+
345
+#define LOAD32H(x, y)                            \
346
+     { x = ((unsigned long)((y)[0] & 255)<<24) | \
347
+           ((unsigned long)((y)[1] & 255)<<16) | \
348
+           ((unsigned long)((y)[2] & 255)<<8)  | \
349
+           ((unsigned long)((y)[3] & 255)); }
350
+
351
+#define STORE64H(x, y)                                                                     \
352
+   { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255);     \
353
+     (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255);     \
354
+     (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255);     \
355
+     (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
356
+
357
+#define LOAD64H(x, y)                                                      \
358
+   { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
359
+         (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
360
+         (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
361
+         (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
362
+
363
+#ifdef ENDIAN_32BITWORD 
364
+
365
+#define STORE32L(x, y)        \
366
+     { unsigned long __t = (x); memcpy(y, &__t, 4); }
367
+
368
+#define LOAD32L(x, y)         \
369
+     memcpy(&(x), y, 4);
370
+
371
+#define STORE64L(x, y)                                                                     \
372
+     { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255);   \
373
+       (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255);   \
374
+       (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255);   \
375
+       (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
376
+
377
+#define LOAD64L(x, y)                                                       \
378
+     { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
379
+           (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
380
+           (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
381
+           (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
382
+
383
+#else /* 64-bit words then  */
384
+
385
+#define STORE32L(x, y)        \
386
+     { unsigned long __t = (x); memcpy(y, &__t, 4); }
387
+
388
+#define LOAD32L(x, y)         \
389
+     { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
390
+
391
+#define STORE64L(x, y)        \
392
+     { ulong64 __t = (x); memcpy(y, &__t, 8); }
393
+
394
+#define LOAD64L(x, y)         \
395
+    { memcpy(&(x), y, 8); }
396
+
397
+#endif /* ENDIAN_64BITWORD */
398
+#endif /* ENDIAN_LITTLE */
399
+
400
+#ifdef ENDIAN_BIG
401
+#define STORE32L(x, y)                                                                     \
402
+     { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255);   \
403
+       (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
404
+
405
+#define LOAD32L(x, y)                            \
406
+     { x = ((unsigned long)((y)[3] & 255)<<24) | \
407
+           ((unsigned long)((y)[2] & 255)<<16) | \
408
+           ((unsigned long)((y)[1] & 255)<<8)  | \
409
+           ((unsigned long)((y)[0] & 255)); }
410
+
411
+#define STORE64L(x, y)                                                                     \
412
+   { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255);     \
413
+     (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255);     \
414
+     (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255);     \
415
+     (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
416
+
417
+#define LOAD64L(x, y)                                                      \
418
+   { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
419
+         (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \
420
+         (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \
421
+         (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
422
+
423
+#ifdef ENDIAN_32BITWORD 
424
+
425
+#define STORE32H(x, y)        \
426
+     { unsigned long __t = (x); memcpy(y, &__t, 4); }
427
+
428
+#define LOAD32H(x, y)         \
429
+     memcpy(&(x), y, 4);
430
+
431
+#define STORE64H(x, y)                                                                     \
432
+     { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255);   \
433
+       (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255);   \
434
+       (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255);   \
435
+       (y)[6] = (unsigned char)(((x)>>8)&255);  (y)[7] = (unsigned char)((x)&255); }
436
+
437
+#define LOAD64H(x, y)                                                       \
438
+     { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \
439
+           (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \
440
+           (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \
441
+           (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); }
442
+
443
+#else /* 64-bit words then  */
444
+
445
+#define STORE32H(x, y)        \
446
+     { unsigned long __t = (x); memcpy(y, &__t, 4); }
447
+
448
+#define LOAD32H(x, y)         \
449
+     { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
450
+
451
+#define STORE64H(x, y)        \
452
+     { ulong64 __t = (x); memcpy(y, &__t, 8); }
453
+
454
+#define LOAD64H(x, y)         \
455
+    { memcpy(&(x), y, 8); }
456
+
457
+#endif /* ENDIAN_64BITWORD */
458
+#endif /* ENDIAN_BIG */
459
+
460
+/*
461
+	packet code */
462
+#if defined(USE_RSA) || defined(MDH) || defined(MECC)
463
+	#define PACKET
464
+
465
+/*
466
+	size of a packet header in bytes */
467
+	#define PACKET_SIZE				4
468
+
469
+/*
470
+	Section tags
471
+ */
472
+	#define PACKET_SECT_RSA			0
473
+	#define PACKET_SECT_DH			1
474
+	#define PACKET_SECT_ECC			2
475
+	#define PACKET_SECT_DSA			3
476
+
477
+/*
478
+	Subsection Tags for the first three sections
479
+ */
480
+	#define PACKET_SUB_KEY			0
481
+	#define PACKET_SUB_ENCRYPTED	1
482
+	#define PACKET_SUB_SIGNED		2
483
+	#define PACKET_SUB_ENC_KEY		3
484
+#endif
485
+
486
+/*
487
+	fix for MSVC ...evil!
488
+ */
489
+#ifdef WIN32
490
+#ifdef _MSC_VER
491
+	#define CONST64(n) n ## ui64
492
+	typedef unsigned __int64 ulong64;
493
+#else
494
+	#define CONST64(n) n ## ULL
495
+	typedef unsigned long long ulong64;
496
+#endif
497
+#endif /* WIN32 */
498
+
499
+
500
+#define BSWAP(x)  ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL)  | \
501
+                    ((x>>8)&0x0000FF00UL)  | ((x<<8)&0x00FF0000UL) )
502
+
503
+#ifdef _MSC_VER
504
+
505
+/*
506
+	instrinsic rotate
507
+ */
508
+#include <stdlib.h>
509
+#pragma intrinsic(_lrotr,_lrotl)
510
+#define ROR(x,n) _lrotr(x,n)
511
+#define ROL(x,n) _lrotl(x,n)
512
+#define RORc(x,n) _lrotr(x,n)
513
+#define ROLc(x,n) _lrotl(x,n)
514
+
515
+/*
516
+#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) && !defined(PS_NO_ASM)
517
+
518
+static inline unsigned ROL(unsigned word, int32 i)
519
+{
520
+	asm ("roll %%cl,%0"
521
+		:"0" (word),"c" (i));
522
+	return word;
523
+}
524
+
525
+static inline unsigned ROR(unsigned word, int32 i)
526
+{
527
+	asm ("rorl %%cl,%0"
528
+		:"=r" (word)
529
+		:"0" (word),"c" (i));
530
+	return word;
531
+}
532
+*/
533
+/*
534
+#ifndef PS_NO_ROLC
535
+
536
+static inline unsigned ROLc(unsigned word, const int32 i)
537
+{
538
+   asm ("roll %2,%0"
539
+      :"=r" (word)
540
+      :"0" (word),"I" (i));
541
+   return word;
542
+}
543
+
544
+static inline unsigned RORc(unsigned word, const int32 i)
545
+{
546
+   asm ("rorl %2,%0"
547
+      :"=r" (word)
548
+      :"0" (word),"I" (i));
549
+   return word;
550
+}
551
+
552
+#else
553
+
554
+#define ROLc ROL
555
+#define RORc ROR
556
+
557
+#endif
558
+*/
559
+
560
+#else /* _MSC_VER */
561
+
562
+/*
563
+	rotates the hard way
564
+ */
565
+#define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
566
+#define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
567
+#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
568
+#define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
569
+
570
+#endif /* _MSC_VER */
571
+
572
+/* 64-bit Rotates */
573
+#if 0
574
+
575
+#if defined(__GNUC__) && defined(__x86_64__) && !defined(PS_NO_ASM)
576
+
577
+static inline unsigned long ROL64(unsigned long word, int32 i)
578
+{
579
+   asm("rolq %%cl,%0"
580
+      :"=r" (word)
581
+      :"0" (word),"c" (i));
582
+   return word;
583
+}
584
+
585
+static inline unsigned long ROR64(unsigned long word, int32 i)
586
+{
587
+   asm("rorq %%cl,%0"
588
+      :"=r" (word)
589
+      :"0" (word),"c" (i));
590
+   return word;
591
+}
592
+
593
+#ifndef PS_NO_ROLC
594
+
595
+static inline unsigned long ROL64c(unsigned long word, const int32 i)
596
+{
597
+   asm("rolq %2,%0"
598
+      :"=r" (word)
599
+      :"0" (word),"J" (i));
600
+   return word;
601
+}
602
+
603
+static inline unsigned long ROR64c(unsigned long word, const int32 i)
604
+{
605
+   asm("rorq %2,%0"
606
+      :"=r" (word)
607
+      :"0" (word),"J" (i));
608
+   return word;
609
+}
610
+
611
+#else /* PS_NO_ROLC */
612
+
613
+#define ROL64c ROL
614
+#define ROR64c ROR
615
+
616
+#endif /* PS_NO_ROLC */
617
+#endif
618
+#endif /* commented out */
619
+
620
+#define ROL64(x, y) \
621
+    ( (((x)<<((ulong64)(y)&63)) | \
622
+      (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
623
+
624
+#define ROR64(x, y) \
625
+    ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
626
+      ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
627
+
628
+#define ROL64c(x, y) \
629
+    ( (((x)<<((ulong64)(y)&63)) | \
630
+      (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
631
+
632
+#define ROR64c(x, y) \
633
+    ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
634
+      ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
635
+
636
+#undef MAX
637
+#undef MIN
638
+#define MAX(x, y) ( ((x)>(y))?(x):(y) )
639
+#define MIN(x, y) ( ((x)<(y))?(x):(y) )
640
+
641
+/*
642
+	extract a byte portably This MSC code causes runtime errors in VS.NET,
643
+	always use the other
644
+ */
645
+/*
646
+#ifdef _MSC_VER
647
+   #define byte(x, n) ((unsigned char)((x) >> (8 * (n))))
648
+#else
649
+*/
650
+   #define byte(x, n) (((x) >> (8 * (n))) & 255)
651
+/*
652
+#endif
653
+*/
654
+#ifdef __cplusplus
655
+   }
656
+#endif /* __cplusplus */
657
+
658
+#endif /* _h_PSCRYPTO */
659
+
660
+/******************************************************************************/
661
+

Loading…
Cancelar
Guardar