3 * Release $Name: MATRIXSSL_1_8_8_OPEN $
5 * Internal definitions for PeerSec Networks MatrixSSL cryptography provider
8 * Copyright (c) PeerSec Networks, 2002-2009. All Rights Reserved.
9 * The latest version of this code is available at http://www.matrixssl.org
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.
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
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.
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
30 /******************************************************************************/
40 PeerSec crypto-specific defines.
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.
52 #define USE_SMALL_WORD
53 #endif /* USE_INT64 */
55 /******************************************************************************/
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
68 typedef unsigned long ulong32
;
71 Primary RSA Key struct. Define here for crypto
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 */
86 extern int32
ps_base64_decode(const unsigned char *in
, uint32 len
,
87 unsigned char *out
, uint32
*outlen
);
92 extern void psZeromem(void *dst
, size_t len
);
93 extern void psBurnStack(unsigned long len
);
96 /* max size of either a cipher/hash block or symmetric key [largest of the two] */
97 #define MAXBLOCKSIZE 24
100 /* error codes [will be expanded in future releases] */
102 CRYPT_OK
=0, /* Result OK */
103 CRYPT_ERROR
, /* Generic Error */
104 CRYPT_NOP
, /* Not a failure but no operation was performed */
106 CRYPT_INVALID_KEYSIZE
, /* Invalid key size given */
107 CRYPT_INVALID_ROUNDS
, /* Invalid number of rounds */
108 CRYPT_FAIL_TESTVECTOR
, /* Algorithm failed test vectors */
110 CRYPT_BUFFER_OVERFLOW
, /* Not enough space for output */
111 CRYPT_INVALID_PACKET
, /* Invalid input packet given */
113 CRYPT_INVALID_PRNGSIZE
, /* Invalid number of bits for a PRNG */
114 CRYPT_ERROR_READPRNG
, /* Could not read enough from PRNG */
116 CRYPT_INVALID_CIPHER
, /* Invalid cipher specified */
117 CRYPT_INVALID_HASH
, /* Invalid hash specified */
118 CRYPT_INVALID_PRNG
, /* Invalid PRNG specified */
120 CRYPT_MEM
, /* Out of memory */
122 CRYPT_PK_TYPE_MISMATCH
, /* Not equivalent types of PK keys */
123 CRYPT_PK_NOT_PRIVATE
, /* Requires a private PK key */
125 CRYPT_INVALID_ARG
, /* Generic invalid argument */
126 CRYPT_FILE_NOTFOUND
, /* File Not Found */
128 CRYPT_PK_INVALID_TYPE
, /* Invalid type of PK key */
129 CRYPT_PK_INVALID_SYSTEM
, /* Invalid PK system specified */
130 CRYPT_PK_DUP
, /* Duplicate key already in key ring */
131 CRYPT_PK_NOT_FOUND
, /* Key not found in keyring */
132 CRYPT_PK_INVALID_SIZE
, /* Invalid size input for PK parameters */
134 CRYPT_INVALID_PRIME_SIZE
/* Invalid size of prime requested */
137 /******************************************************************************/
147 #endif /* USE_INT64 */
148 ulong32 state
[5], curlen
;
149 unsigned char buf
[64];
158 #endif /* USE_INT64 */
159 ulong32 state
[4], curlen
;
160 unsigned char buf
[64];
165 unsigned char chksum
[16], X
[48], buf
[16];
166 unsigned long curlen
;
173 struct sha1_state sha1
;
174 struct md5_state md5
;
176 struct md2_state md2
;
180 typedef hash_state sslSha1Context_t
;
181 typedef hash_state sslMd5Context_t
;
183 typedef hash_state sslMd2Context_t
;
187 unsigned char pad
[64];
190 sslSha1Context_t sha1
;
194 /******************************************************************************/
200 unsigned char state
[256];
205 #endif /* USE_ARC4 */
208 #define SSL_DES3_KEY_LEN 24
209 #define SSL_DES3_IV_LEN 8
210 #define SSL_DES_KEY_LEN 8
215 ulong32 ek
[3][32], dk
[3][32];
219 A block cipher CBC structure
225 int32 explicitIV
; /* 1 if yes */
228 extern int32
des3_setup(const unsigned char *key
, int32 keylen
, int32 num_rounds
,
230 extern void des3_ecb_encrypt(const unsigned char *pt
, unsigned char *ct
,
232 extern void des3_ecb_decrypt(const unsigned char *ct
, unsigned char *pt
,
234 extern int32
des3_keysize(int32
*desired_keysize
);
236 extern int32
des_setup(const unsigned char *key
, int32 keylen
, int32 num_rounds
,
238 extern void des_ecb_encrypt(const unsigned char *pt
, unsigned char *ct
,
240 extern void des_ecb_decrypt(const unsigned char *ct
, unsigned char *pt
,
243 #endif /* USE_3DES */
254 } sslCipherContext_t
;
258 Controls endianess and size of registers. Leave uncommented to get
259 platform neutral [slower] code detect x86-32 machines somewhat
261 #if (defined(_MSC_VER) && defined(WIN32)) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__)))
262 #define ENDIAN_LITTLE
263 #define ENDIAN_32BITWORD
267 /* #define ENDIAN_LITTLE */
268 /* #define ENDIAN_BIG */
270 /* #define ENDIAN_32BITWORD */
271 /* #define ENDIAN_64BITWORD */
273 #if (defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) && !(defined(ENDIAN_32BITWORD) || defined(ENDIAN_64BITWORD))
274 #error You must specify a word size as well as endianess
277 #if !(defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE))
278 #define ENDIAN_NEUTRAL
284 #if defined (ENDIAN_NEUTRAL)
286 #define STORE32L(x, y) \
287 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
288 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
290 #define LOAD32L(x, y) \
291 { x = ((unsigned long)((y)[3] & 255)<<24) | \
292 ((unsigned long)((y)[2] & 255)<<16) | \
293 ((unsigned long)((y)[1] & 255)<<8) | \
294 ((unsigned long)((y)[0] & 255)); }
296 #define STORE64L(x, y) \
297 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
298 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
299 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
300 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
302 #define LOAD64L(x, y) \
303 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
304 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
305 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
306 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
308 #define STORE32H(x, y) \
309 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
310 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
312 #define LOAD32H(x, y) \
313 { x = ((unsigned long)((y)[0] & 255)<<24) | \
314 ((unsigned long)((y)[1] & 255)<<16) | \
315 ((unsigned long)((y)[2] & 255)<<8) | \
316 ((unsigned long)((y)[3] & 255)); }
318 #define STORE64H(x, y) \
319 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
320 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
321 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
322 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
324 #define LOAD64H(x, y) \
325 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
326 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
327 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
328 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
330 #endif /* ENDIAN_NEUTRAL */
334 #define STORE32H(x, y) \
335 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
336 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
338 #define LOAD32H(x, y) \
339 { x = ((unsigned long)((y)[0] & 255)<<24) | \
340 ((unsigned long)((y)[1] & 255)<<16) | \
341 ((unsigned long)((y)[2] & 255)<<8) | \
342 ((unsigned long)((y)[3] & 255)); }
344 #define STORE64H(x, y) \
345 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
346 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
347 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
348 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
350 #define LOAD64H(x, y) \
351 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
352 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
353 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
354 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
356 #ifdef ENDIAN_32BITWORD
358 #define STORE32L(x, y) \
359 { unsigned long __t = (x); memcpy(y, &__t, 4); }
361 #define LOAD32L(x, y) \
364 #define STORE64L(x, y) \
365 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
366 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
367 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
368 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
370 #define LOAD64L(x, y) \
371 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
372 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
373 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
374 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
376 #else /* 64-bit words then */
378 #define STORE32L(x, y) \
379 { unsigned long __t = (x); memcpy(y, &__t, 4); }
381 #define LOAD32L(x, y) \
382 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
384 #define STORE64L(x, y) \
385 { ulong64 __t = (x); memcpy(y, &__t, 8); }
387 #define LOAD64L(x, y) \
388 { memcpy(&(x), y, 8); }
390 #endif /* ENDIAN_64BITWORD */
391 #endif /* ENDIAN_LITTLE */
394 #define STORE32L(x, y) \
395 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
396 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
398 #define LOAD32L(x, y) \
399 { x = ((unsigned long)((y)[3] & 255)<<24) | \
400 ((unsigned long)((y)[2] & 255)<<16) | \
401 ((unsigned long)((y)[1] & 255)<<8) | \
402 ((unsigned long)((y)[0] & 255)); }
404 #define STORE64L(x, y) \
405 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
406 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
407 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
408 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
410 #define LOAD64L(x, y) \
411 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
412 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \
413 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \
414 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
416 #ifdef ENDIAN_32BITWORD
418 #define STORE32H(x, y) \
419 { unsigned long __t = (x); memcpy(y, &__t, 4); }
421 #define LOAD32H(x, y) \
424 #define STORE64H(x, y) \
425 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
426 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
427 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
428 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
430 #define LOAD64H(x, y) \
431 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \
432 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \
433 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \
434 (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); }
436 #else /* 64-bit words then */
438 #define STORE32H(x, y) \
439 { unsigned long __t = (x); memcpy(y, &__t, 4); }
441 #define LOAD32H(x, y) \
442 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }
444 #define STORE64H(x, y) \
445 { ulong64 __t = (x); memcpy(y, &__t, 8); }
447 #define LOAD64H(x, y) \
448 { memcpy(&(x), y, 8); }
450 #endif /* ENDIAN_64BITWORD */
451 #endif /* ENDIAN_BIG */
455 #if defined(USE_RSA) || defined(MDH) || defined(MECC)
459 size of a packet header in bytes */
460 #define PACKET_SIZE 4
465 #define PACKET_SECT_RSA 0
466 #define PACKET_SECT_DH 1
467 #define PACKET_SECT_ECC 2
468 #define PACKET_SECT_DSA 3
471 Subsection Tags for the first three sections
473 #define PACKET_SUB_KEY 0
474 #define PACKET_SUB_ENCRYPTED 1
475 #define PACKET_SUB_SIGNED 2
476 #define PACKET_SUB_ENC_KEY 3
480 fix for MSVC ...evil!
484 #define CONST64(n) n ## ui64
485 typedef unsigned __int64 ulong64
;
487 #define CONST64(n) n ## ULL
488 typedef unsigned long long ulong64
;
493 #define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \
494 ((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) )
502 #pragma intrinsic(_lrotr,_lrotl)
503 #define ROR(x,n) _lrotr(x,n)
504 #define ROL(x,n) _lrotl(x,n)
505 #define RORc(x,n) _lrotr(x,n)
506 #define ROLc(x,n) _lrotl(x,n)
509 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) && !defined(PS_NO_ASM)
511 static inline unsigned ROL(unsigned word, int32 i)
514 :"0" (word),"c" (i));
518 static inline unsigned ROR(unsigned word, int32 i)
522 :"0" (word),"c" (i));
529 static inline unsigned ROLc(unsigned word, const int32 i)
533 :"0" (word),"I" (i));
537 static inline unsigned RORc(unsigned word, const int32 i)
541 :"0" (word),"I" (i));
558 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
559 #define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
560 #define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
561 #define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
563 #endif /* _MSC_VER */
568 #if defined(__GNUC__) && defined(__x86_64__) && !defined(PS_NO_ASM)
570 static inline unsigned long ROL64(unsigned long word
, int32 i
)
574 :"0" (word
),"c" (i
));
578 static inline unsigned long ROR64(unsigned long word
, int32 i
)
582 :"0" (word
),"c" (i
));
588 static inline unsigned long ROL64c(unsigned long word
, const int32 i
)
592 :"0" (word
),"J" (i
));
596 static inline unsigned long ROR64c(unsigned long word
, const int32 i
)
600 :"0" (word
),"J" (i
));
604 #else /* PS_NO_ROLC */
609 #endif /* PS_NO_ROLC */
611 #endif /* commented out */
613 #define ROL64(x, y) \
614 ( (((x)<<((ulong64)(y)&63)) | \
615 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
617 #define ROR64(x, y) \
618 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
619 ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
621 #define ROL64c(x, y) \
622 ( (((x)<<((ulong64)(y)&63)) | \
623 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
625 #define ROR64c(x, y) \
626 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
627 ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
631 #define MAX(x, y) ( ((x)>(y))?(x):(y) )
632 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
635 extract a byte portably This MSC code causes runtime errors in VS.NET,
640 #define byte(x, n) ((unsigned char)((x) >> (8 * (n))))
643 #define byte(x, n) (((x) >> (8 * (n))) & 255)
649 #endif /* __cplusplus */
651 #endif /* _h_PSCRYPTO */
653 /******************************************************************************/