|
@@ -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
|
+
|