+++ /dev/null
-#ifndef _MATRIXSSL_CRYPTOLAYER_H
-#define _MATRIXSSL_CRYPTOLAYER_H
-
-/** @file
- *
- * Compatibility layer for MatrixSSL
- *
- */
-
-#include <stdint.h>
-#include <stddef.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-#include <assert.h>
-#include <byteswap.h>
-#include <gpxe/rotate.h>
-#include <gpxe/crypto.h>
-
-/* Drag in pscrypto.h */
-typedef uint64_t ulong64;
-typedef void psPool_t;
-#define SMALL_CODE
-#define USE_INT64
-#define USE_RSA
-#define USE_RSA_PUBLIC_ENCRYPT
-#define CRYPT
-#include "matrixssl/pscrypto.h"
-#define SMALL_CODE
-#undef CLEAN_STACK
-
-#define sslAssert( ... ) assert ( __VA_ARGS__ )
-
-static inline __attribute__ (( always_inline )) void * __malloc
-psMalloc ( psPool_t *pool __unused, size_t len ) {
- return malloc ( len );
-}
-
-static inline __attribute__ (( always_inline )) void *
-psRealloc ( void *ptr, size_t len ) {
- return realloc ( ptr, len );
-}
-
-static inline __attribute__ (( always_inline )) void psFree ( void *ptr ) {
- free ( ptr );
-}
-
-#define matrixStrDebugMsg( ... ) DBG ( __VA_ARGS__ )
-#define matrixIntDebugMsg( ... ) DBG ( __VA_ARGS__ )
-
-/* Use our standard cpu_to_leXX etc. macros */
-
-#undef LOAD32L
-#define LOAD32L( cpu32, ptr ) do { \
- uint32_t *le32 = ( ( uint32_t * ) ptr ); \
- cpu32 = le32_to_cpu ( *le32 ); \
- } while ( 0 )
-
-#undef LOAD32H
-#define LOAD32H( cpu32, ptr ) do { \
- uint32_t *be32 = ( ( uint32_t * ) ptr ); \
- cpu32 = be32_to_cpu ( *be32 ); \
- } while ( 0 )
-
-#undef LOAD64L
-#define LOAD64L( cpu64, ptr ) do { \
- uint64_t *le64 = ( ( uint64_t * ) ptr ); \
- cpu64 = le64_to_cpu ( *le64 ); \
- } while ( 0 )
-
-#undef LOAD64H
-#define LOAD64H( cpu64, ptr ) do { \
- uint64_t *be64 = ( ( uint64_t * ) ptr ); \
- cpu64 = be64_to_cpu ( *be64 ); \
- } while ( 0 )
-
-#undef STORE32L
-#define STORE32L( cpu32, ptr ) do { \
- uint32_t *le32 = ( ( uint32_t * ) ptr ); \
- *le32 = cpu_to_le32 ( cpu32 ); \
- } while ( 0 )
-
-#undef STORE32H
-#define STORE32H( cpu32, ptr ) do { \
- uint32_t *be32 = ( ( uint32_t * ) ptr ); \
- *be32 = cpu_to_be32 ( cpu32 ); \
- } while ( 0 )
-
-#undef STORE64L
-#define STORE64L( cpu64, ptr ) do { \
- uint64_t *le64 = ( ( uint64_t * ) ptr ); \
- *le64 = cpu_to_le64 ( cpu64 ); \
- } while ( 0 )
-
-#undef STORE64H
-#define STORE64H( cpu64, ptr ) do { \
- uint64_t *be64 = ( ( uint64_t * ) ptr ); \
- *be64 = cpu_to_be64 ( cpu64 ); \
- } while ( 0 )
-
-/* Use rolXX etc. from bitops.h */
-
-#undef ROL
-#define ROL( data, rotation ) rol32 ( (data), (rotation) )
-#undef ROLc
-#define ROLc( data, rotation ) rol32 ( (data), (rotation) )
-#undef ROR
-#define ROR( data, rotation ) ror32 ( (data), (rotation) )
-#undef RORc
-#define RORc( data, rotation ) ror32 ( (data), (rotation) )
-#undef ROL64
-#define ROL64( data, rotation ) rol64 ( (data), (rotation) )
-#undef ROL64c
-#define ROL64c( data, rotation ) rol64 ( (data), (rotation) )
-#undef ROR64
-#define ROR64( data, rotation ) ror64 ( (data), (rotation) )
-#undef ROR64c
-#define ROR64c( data, rotation ) ror64 ( (data), (rotation) )
-
-#endif /* _MATRIXSSL_CRYPTOLAYER_H */
+++ /dev/null
-/* mcb - this file breaks the build process; temporarily deactivating */
-#if 0
-
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#include "ssl.h"
-
-int main(int argc, char *argv[])
-{
- SSL_t ssl;
- int sockfd, portno, rc;
- struct sockaddr_in serv_addr;
- struct hostent *server;
-
- portno = 443;
- sockfd = socket(AF_INET,SOCK_STREAM,0);
- if(sockfd<0){
- fprintf(stderr,"Error creating socket\n");
- exit(sockfd);
- }
-
- server = gethostbyname(argv[1]);
- if(server==NULL){
- fprintf(stderr,"Error looking up host %s\n",argv[1]);
- exit(1);
- }
-
- /**
- *matrixSslOpen()
- *matrixSslReadKeys()
- **/
- printf("Calling CreateSSLHello()\n");
- rc = CreateSSLHello(&ssl);
- printf("Finished calling CreateSSLHello()\n");
-
- bzero((char *) &serv_addr, sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
- serv_addr.sin_port = htons(portno);
- if(connect(sockfd,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
- fprintf(stderr,"ERROR connecting to server\n");
- exit(1);
- }
-
- PrintSSLPacket(&ssl);
-
- printf("Write ssl.buffer\n");
- write(sockfd,ssl.buffer,ssl.length);
- printf("Finished writing\n");
- ssl.length = read(sockfd,ssl.buffer,ssl.max_size);
- ReadSSLHello(&ssl);
-
- /**
- *matrixSslNewSession()
- *matrixSslSetCetValidator()
- *encodeSslHandshake()
-
- *write handshake buffer
-
- *readSslResponse() <-+
- |
- *read return code |-- similar/same function??
- |
- *sslEncode() |
- *sslDecode() <-------+
-
- *encodeSslCloseAlert()
-
- *write close alert buffer
- **/
- close(sockfd);
-
- /**
- *sslClose()
- * -free connection
- * -free keys
- * -close pki interface
- **/
-
- return 0;
-}
-
-#endif
+++ /dev/null
-/* \r
- * mpi.h\r
- * Release $Name$\r
- *\r
- * multiple-precision integer library\r
- */\r
-/*\r
- * Copyright (c) PeerSec Networks, 2002-2006. All Rights Reserved.\r
- * The latest version of this code is available at http://www.matrixssl.org\r
- *\r
- * This software is open source; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This General Public License does NOT permit incorporating this software \r
- * into proprietary programs. If you are unable to comply with the GPL, a \r
- * commercial license for this software may be purchased from PeerSec Networks\r
- * at http://www.peersec.com\r
- * \r
- * This program is distributed in WITHOUT ANY WARRANTY; without even the \r
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. \r
- * See the GNU General Public License for more details.\r
- * \r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
- * http://www.gnu.org/copyleft/gpl.html\r
- */\r
-/******************************************************************************/\r
-\r
-#ifndef _h_MPI\r
-#define _h_MPI\r
-\r
-#include <stdio.h>\r
-#include <string.h>\r
-#include <stdlib.h>\r
-#include <ctype.h>\r
-#include <limits.h>\r
-\r
-#undef MIN\r
-#define MIN(x,y) ((x)<(y)?(x):(y))\r
-#undef MAX\r
-#define MAX(x,y) ((x)>(y)?(x):(y))\r
-\r
-#ifdef __cplusplus\r
-extern "C" {\r
-\r
-\r
-/*\r
- C++ compilers don't like assigning void * to mp_digit *\r
- */\r
-#define OPT_CAST(x) (x *)\r
-\r
-#else\r
-\r
-/*\r
- C on the other hand doesn't care\r
- */\r
-#define OPT_CAST(x)\r
-\r
-#endif /* __cplusplus */\r
-\r
-/******************************************************************************/\r
-/*\r
- some default configurations.\r
-\r
- A "mp_digit" must be able to hold DIGIT_BIT + 1 bits\r
- A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits\r
-\r
- At the very least a mp_digit must be able to hold 7 bits\r
- [any size beyond that is ok provided it doesn't overflow the data type]\r
- */\r
-#ifdef MP_8BIT\r
- typedef unsigned char mp_digit;\r
- typedef unsigned short mp_word;\r
-#elif defined(MP_16BIT)\r
- typedef unsigned short mp_digit;\r
- typedef unsigned long mp_word;\r
-#elif defined(MP_64BIT)\r
-/*\r
- for GCC only on supported platforms\r
- */\r
- #ifndef CRYPT\r
- typedef unsigned long long ulong64;\r
- typedef signed long long long64;\r
- #endif /* CRYPT */\r
-\r
- typedef ulong64 mp_digit;\r
- typedef unsigned long mp_word __attribute__ ((mode(TI)));\r
-\r
- #define DIGIT_BIT 60\r
-#else /* MP_8BIT */\r
-/*\r
- this is the default case, 28-bit digits\r
- */\r
- #ifndef CRYPT\r
- #if defined(_MSC_VER) || defined(__BORLANDC__) \r
- typedef unsigned __int64 ulong64;\r
- typedef signed __int64 long64;\r
- #else\r
- typedef unsigned long long ulong64;\r
- typedef signed long long long64;\r
- #endif\r
- #endif /* CRYPT */\r
-\r
- typedef unsigned long mp_digit;\r
- typedef ulong64 mp_word;\r
-\r
- #ifdef MP_31BIT\r
-/*\r
- this is an extension that uses 31-bit digits\r
- */\r
- #define DIGIT_BIT 31\r
- #else /* MP_31BIT */\r
-/*\r
- default case is 28-bit digits, defines MP_28BIT as a handy macro to test\r
- */\r
- #define DIGIT_BIT 28\r
- #define MP_28BIT\r
- #endif /* MP_31BIT */\r
-#endif /* MP_8BIT */\r
-\r
-/*\r
- otherwise the bits per digit is calculated automatically from the size of\r
- a mp_digit\r
- */\r
-#ifndef DIGIT_BIT\r
- #define DIGIT_BIT ((int32)((CHAR_BIT * sizeof(mp_digit) - 1))) /* bits per digit */\r
-#endif /* DIGIT_BIT */\r
-\r
-#define MP_DIGIT_BIT DIGIT_BIT\r
-#define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))\r
-#define MP_DIGIT_MAX MP_MASK\r
-\r
-/******************************************************************************/\r
-/*\r
- equalities\r
- */\r
-#define MP_LT -1 /* less than */\r
-#define MP_EQ 0 /* equal to */\r
-#define MP_GT 1 /* greater than */\r
-\r
-#define MP_ZPOS 0 /* positive integer */\r
-#define MP_NEG 1 /* negative */\r
-\r
-#define MP_OKAY 0 /* ok result */\r
-#define MP_MEM -2 /* out of mem */\r
-#define MP_VAL -3 /* invalid input */\r
-#define MP_RANGE MP_VAL\r
-\r
-#define MP_YES 1 /* yes response */\r
-#define MP_NO 0 /* no response */\r
-\r
-typedef int32 mp_err;\r
-\r
-/******************************************************************************/\r
-/*\r
- various build options\r
- */\r
-#define MP_PREC 64 /* default digits of precision */\r
-\r
-/*\r
- define this to use lower memory usage routines (exptmods mostly)\r
- */\r
-#define MP_LOW_MEM\r
-\r
-/*\r
- size of comba arrays, should be at least \r
- 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2)\r
- */\r
-#define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))\r
-\r
-typedef struct {\r
- int32 used, alloc, sign;\r
- mp_digit *dp;\r
-} mp_int;\r
-\r
-#define USED(m) ((m)->used)\r
-#define DIGIT(m,k) ((m)->dp[(k)])\r
-#define SIGN(m) ((m)->sign)\r
-\r
-/******************************************************************************/\r
-/*\r
- init and deinit bignum functions\r
- */\r
-\r
-/*\r
- init a bignum\r
- */\r
-extern int32 mp_init(psPool_t *pool, mp_int *a);\r
-\r
-/*\r
- free a bignum\r
- */\r
-extern void mp_clear(mp_int *a);\r
-\r
-/*\r
- init a series of arguments\r
- */\r
-extern int32 _mp_init_multi(psPool_t *pool, mp_int *mp0, mp_int *mp1, mp_int *mp2, \r
- mp_int *mp3, mp_int *mp4, mp_int *mp5, mp_int *mp6, \r
- mp_int *mp7);\r
-\r
-/*\r
- clear a series of arguments\r
- */\r
-extern void _mp_clear_multi(mp_int *mp0, mp_int *mp1, mp_int *mp2, mp_int *mp3,\r
- mp_int *mp4, mp_int *mp5, mp_int *mp6, mp_int *mp7);\r
-\r
-/*\r
- exchange two ints\r
- */\r
-extern void mp_exch(mp_int *a, mp_int *b);\r
-\r
-/*\r
- shrink ram required for a bignum\r
- */\r
-extern int32 mp_shrink(mp_int *a);\r
-\r
-/*\r
- grow an int32 to a given size\r
- */\r
-extern int32 mp_grow(mp_int *a, int32 size);\r
-\r
-/*\r
- init to a given number of digits\r
- */\r
-extern int32 mp_init_size(psPool_t *pool, mp_int *a, int32 size);\r
-\r
-/******************************************************************************/\r
-/*\r
- Basic Manipulations\r
- */\r
-#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)\r
-#define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)\r
-#define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)\r
-\r
-extern int32 mp_add_d (mp_int * a, mp_digit b, mp_int * c);\r
-extern int32 mp_sub_d (mp_int * a, mp_digit b, mp_int * c);\r
-/*\r
- set to zero\r
- */\r
-extern void mp_zero(mp_int *a);\r
-\r
-/*\r
- set to a digit\r
- */\r
-extern void mp_set(mp_int *a, mp_digit b);\r
-\r
-/*\r
- copy, b = a\r
- */\r
-extern int32 mp_copy(mp_int *a, mp_int *b);\r
-\r
-/*\r
- inits and copies, a = b\r
- */\r
-extern int32 mp_init_copy(psPool_t *pool, mp_int *a, mp_int *b);\r
-\r
-/*\r
- trim unused digits\r
- */\r
-extern void mp_clamp(mp_int *a);\r
-\r
-/******************************************************************************/\r
-/*\r
- digit manipulation\r
-*/\r
-\r
-/*\r
- right shift by "b" digits\r
- */\r
-extern void mp_rshd(mp_int *a, int32 b);\r
-\r
-/*\r
- left shift by "b" digits\r
- */\r
-extern int32 mp_lshd(mp_int *a, int32 b);\r
-\r
-/*\r
- c = a / 2**b\r
- */\r
-extern int32 mp_div_2d(psPool_t *pool, mp_int *a, int32 b, mp_int *c, mp_int *d);\r
-\r
-/*\r
- b = a/2\r
- */\r
-extern int32 mp_div_2(mp_int *a, mp_int *b);\r
-\r
-/*\r
- c = a * 2**b\r
- */\r
-extern int32 mp_mul_2d(mp_int *a, int32 b, mp_int *c);\r
-\r
-/*\r
- c = a mod 2**d\r
- */\r
-extern int32 mp_mod_2d(mp_int *a, int32 b, mp_int *c);\r
-\r
-/*\r
- computes a = 2**b\r
- */\r
-extern int32 mp_2expt(mp_int *a, int32 b);\r
-\r
-/******************************************************************************/\r
-/*\r
- Basic arithmetic\r
- */\r
-\r
-/*\r
- b = |a|\r
- */\r
-extern int32 mp_abs(mp_int *a, mp_int *b);\r
-\r
-/*\r
- compare a to b\r
- */\r
-extern int32 mp_cmp(mp_int *a, mp_int *b);\r
-\r
-/*\r
- compare |a| to |b|\r
- */\r
-extern int32 mp_cmp_mag(mp_int *a, mp_int *b);\r
-\r
-/*\r
- c = a + b\r
- */\r
-extern int32 mp_add(mp_int *a, mp_int *b, mp_int *c);\r
-\r
-/*\r
- c = a - b\r
- */\r
-extern int32 mp_sub(mp_int *a, mp_int *b, mp_int *c);\r
-\r
-/*\r
- c = a * b\r
- b = a*a\r
- */\r
-/* STEVE - moved mp_mul out of SLOW case */\r
-extern int32 mp_mul(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c);\r
-#ifdef USE_SMALL_WORD\r
-extern int32 mp_sqr(psPool_t *pool, mp_int *a, mp_int *b);\r
-#endif\r
-\r
-/*\r
- a/b => cb + d == a\r
- */\r
-extern int32 mp_div(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c, mp_int *d);\r
-\r
-/*\r
- c = a mod b, 0 <= c < b\r
- */\r
-extern int32 mp_mod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c);\r
-\r
-/******************************************************************************/\r
-/*\r
- single digit functions\r
- */\r
-\r
-/*\r
- compare against a single digit\r
- */\r
-extern int32 mp_cmp_d(mp_int *a, mp_digit b);\r
-\r
-/*\r
- c = a * b\r
- */\r
-extern int32 mp_mul_d(mp_int *a, mp_digit b, mp_int *c);\r
-\r
-/******************************************************************************/\r
-/*\r
- number theory\r
- */\r
-\r
-/*\r
- d = a + b (mod c)\r
- */\r
-extern int32 mp_addmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c, mp_int *d);\r
-\r
-/*\r
- d = a * b (mod c)\r
- */\r
-extern int32 mp_mulmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c, mp_int *d);\r
-\r
-/*\r
- c = 1/a (mod b)\r
- */\r
-#ifdef USE_SMALL_WORD\r
-extern int32 mp_invmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c);\r
-#endif\r
-\r
-/*\r
- setups the montgomery reduction\r
- */\r
-extern int32 mp_montgomery_setup(mp_int *a, mp_digit *mp);\r
-\r
-/*\r
- computes a = B**n mod b without division or multiplication useful for\r
- normalizing numbers in a Montgomery system.\r
- */\r
-extern int32 mp_montgomery_calc_normalization(mp_int *a, mp_int *b);\r
-\r
-/*\r
- computes x/R == x (mod N) via Montgomery Reduction\r
- */\r
-#ifdef USE_SMALL_WORD\r
-extern int32 mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);\r
-#endif\r
-\r
-/*\r
- d = a**b (mod c)\r
- */\r
-/* TODO - we never define this */\r
-extern int32 mp_exptmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c, mp_int *d);\r
-\r
-/******************************************************************************/\r
-/*\r
- If we're using 1024 or 2048 bit keys and 28 bit digits, we only need the\r
- fast_ versions of these functions, removing the others to save space.\r
- Otherwise, we include the slow versions as well and which version to use\r
- is done at runtime.\r
-*/\r
-#ifdef USE_SMALL_WORD\r
-extern int32 s_mp_mul_digs(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c,\r
- int32 digs);\r
-extern int32 s_mp_sqr(psPool_t *pool, mp_int *a, mp_int *b);\r
-#else\r
-#define mp_montgomery_reduce fast_mp_montgomery_reduce\r
-#define mp_sqr fast_s_mp_sqr\r
-#if STEVE\r
-#define mp_mul(P, A, B, C) fast_s_mp_mul_digs(P, A, B, C, (A)->used + (B)->used + 1)\r
-#endif\r
-#define s_mp_mul_digs fast_s_mp_mul_digs\r
-#define mp_invmod fast_mp_invmod\r
-#endif\r
-\r
-/******************************************************************************/\r
-/*\r
- radix conversion\r
- */\r
-extern int32 mp_count_bits(mp_int *a);\r
-\r
-extern int32 mp_unsigned_bin_size(mp_int *a);\r
-extern int32 mp_read_unsigned_bin(mp_int *a, unsigned char *b, int32 c);\r
-extern int32 mp_to_unsigned_bin(psPool_t *pool, mp_int *a, unsigned char *b);\r
-\r
-extern int32 mp_signed_bin_size(mp_int *a);\r
-\r
-/*\r
- lowlevel functions, do not call!\r
- */\r
-#if STEVE\r
-#ifdef USE_SMALL_WORD\r
-#define s_mp_mul(P, A, B, C) s_mp_mul_digs(P, A, B, C, (A)->used + (B)->used + 1)\r
-#else\r
-#define s_mp_mul(P, A, B, C) sslAssert();\r
-#endif\r
-#endif /* STEVE */\r
-/* define this in all cases for now STEVE */\r
-#define s_mp_mul(P, A, B, C) s_mp_mul_digs(P, A, B, C, (A)->used + (B)->used + 1)\r
-\r
-\r
-/*\r
- b = a*2\r
- */\r
-extern int32 mp_mul_2(mp_int *a, mp_int *b);\r
-\r
-extern int32 s_mp_add(mp_int *a, mp_int *b, mp_int *c);\r
-extern int32 s_mp_sub(mp_int *a, mp_int *b, mp_int *c);\r
-\r
-extern int32 fast_s_mp_mul_digs(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c,\r
- int32 digs);\r
-extern int32 fast_s_mp_sqr(psPool_t *pool, mp_int *a, mp_int *b);\r
-\r
-extern int32 fast_mp_invmod(psPool_t *pool, mp_int *a, mp_int *b, mp_int *c);\r
-extern int32 fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp);\r
-\r
-extern void bn_reverse(unsigned char *s, int32 len);\r
-\r
-\r
-#ifdef __cplusplus\r
- }\r
-#endif /* __cplusplus */\r
-\r
-#endif /* _h_MPI */\r
-\r
+++ /dev/null
-/*\r
- * pscrypto.h\r
- * Release $Name$\r
- *\r
- * Internal definitions for PeerSec Networks MatrixSSL cryptography provider\r
- */\r
-/*\r
- * Copyright (c) PeerSec Networks, 2002-2006. All Rights Reserved.\r
- * The latest version of this code is available at http://www.matrixssl.org\r
- *\r
- * This software is open source; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This General Public License does NOT permit incorporating this software \r
- * into proprietary programs. If you are unable to comply with the GPL, a \r
- * commercial license for this software may be purchased from PeerSec Networks\r
- * at http://www.peersec.com\r
- * \r
- * This program is distributed in WITHOUT ANY WARRANTY; without even the \r
- * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. \r
- * See the GNU General Public License for more details.\r
- * \r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
- * http://www.gnu.org/copyleft/gpl.html\r
- */\r
-/******************************************************************************/\r
-\r
-#ifndef _h_PSCRYPTO\r
-#define _h_PSCRYPTO\r
-\r
-#ifdef __cplusplus\r
-extern "C" {\r
-#endif\r
-\r
-/*\r
- PeerSec crypto-specific defines.\r
- */\r
-#define SMALL_CODE\r
-#define CLEAN_STACK\r
-/*\r
- If Native 64 bit integers are not supported, we must set the 16 bit flag\r
- to produce 32 bit mp_words in mpi.h\r
- We must also include the slow MPI functions because the fast ones only\r
- work with larger (28 bit) digit sizes.\r
-*/\r
-#ifndef USE_INT64\r
-#define MP_16BIT\r
-#define USE_SMALL_WORD\r
-#endif /* USE_INT64 */\r
-\r
-/******************************************************************************/\r
-\r
-#ifdef USE_RSA\r
-\r
-#include "mpi.h"\r
-\r
-#if LINUX\r
- #define _stat stat\r
-#endif\r
-\r
-/* this is the "32-bit at least" data type \r
- * Re-define it to suit your platform but it must be at least 32-bits \r
- */\r
-typedef unsigned long ulong32;\r
-\r
-/*\r
- Primary RSA Key struct. Define here for crypto\r
-*/\r
-typedef struct {\r
- mp_int e, d, N, qP, dP, dQ, p, q;\r
- int32 size; /* Size of the key in bytes */\r
- int32 optimized; /* 1 for optimized */\r
-} sslRsaKey_t;\r
-\r
-#endif /* USE_RSA */\r
-\r
-\r
-/*\r
- * Private\r
- */\r
-extern int32 ps_base64_decode(const unsigned char *in, uint32 len, \r
- unsigned char *out, uint32 *outlen);\r
-\r
-/*\r
- * Memory routines\r
- */\r
-extern void psZeromem(void *dst, size_t len);\r
-extern void psBurnStack(unsigned long len);\r
-\r
-\r
-/* max size of either a cipher/hash block or symmetric key [largest of the two] */\r
-#define MAXBLOCKSIZE 24\r
-\r
-/* ch1-01-1 */\r
-/* error codes [will be expanded in future releases] */\r
-enum {\r
- CRYPT_OK=0, /* Result OK */\r
- CRYPT_ERROR, /* Generic Error */\r
- CRYPT_NOP, /* Not a failure but no operation was performed */\r
-\r
- CRYPT_INVALID_KEYSIZE, /* Invalid key size given */\r
- CRYPT_INVALID_ROUNDS, /* Invalid number of rounds */\r
- CRYPT_FAIL_TESTVECTOR, /* Algorithm failed test vectors */\r
-\r
- CRYPT_BUFFER_OVERFLOW, /* Not enough space for output */\r
- CRYPT_INVALID_PACKET, /* Invalid input packet given */\r
-\r
- CRYPT_INVALID_PRNGSIZE, /* Invalid number of bits for a PRNG */\r
- CRYPT_ERROR_READPRNG, /* Could not read enough from PRNG */\r
-\r
- CRYPT_INVALID_CIPHER, /* Invalid cipher specified */\r
- CRYPT_INVALID_HASH, /* Invalid hash specified */\r
- CRYPT_INVALID_PRNG, /* Invalid PRNG specified */\r
-\r
- CRYPT_MEM, /* Out of memory */\r
-\r
- CRYPT_PK_TYPE_MISMATCH, /* Not equivalent types of PK keys */\r
- CRYPT_PK_NOT_PRIVATE, /* Requires a private PK key */\r
-\r
- CRYPT_INVALID_ARG, /* Generic invalid argument */\r
- CRYPT_FILE_NOTFOUND, /* File Not Found */\r
-\r
- CRYPT_PK_INVALID_TYPE, /* Invalid type of PK key */\r
- CRYPT_PK_INVALID_SYSTEM, /* Invalid PK system specified */\r
- CRYPT_PK_DUP, /* Duplicate key already in key ring */\r
- CRYPT_PK_NOT_FOUND, /* Key not found in keyring */\r
- CRYPT_PK_INVALID_SIZE, /* Invalid size input for PK parameters */\r
-\r
- CRYPT_INVALID_PRIME_SIZE /* Invalid size of prime requested */\r
-};\r
-\r
-/******************************************************************************/\r
-/*\r
- hash defines\r
- */\r
-struct sha1_state {\r
-#ifdef USE_INT64\r
- ulong64 length;\r
-#else\r
- ulong32 lengthHi;\r
- ulong32 lengthLo;\r
-#endif /* USE_INT64 */\r
- ulong32 state[5], curlen;\r
- unsigned char buf[64];\r
-};\r
-\r
-struct md5_state {\r
-#ifdef USE_INT64\r
- ulong64 length;\r
-#else\r
- ulong32 lengthHi;\r
- ulong32 lengthLo;\r
-#endif /* USE_INT64 */\r
- ulong32 state[4], curlen;\r
- unsigned char buf[64];\r
-};\r
-\r
-#ifdef USE_MD2\r
-struct md2_state {\r
- unsigned char chksum[16], X[48], buf[16];\r
- unsigned long curlen;\r
-};\r
-#endif /* USE_MD2 */\r
-\r
-#ifdef USE_SHA256\r
-struct sha256_state {\r
- ulong64 length;\r
- ulong32 state[8], curlen;\r
- unsigned char buf[64];\r
-};\r
-#endif /* USE_SHA256 */\r
-\r
-typedef union {\r
- struct sha1_state sha1;\r
- struct md5_state md5;\r
-#ifdef USE_MD2\r
- struct md2_state md2;\r
-#endif /* USE_MD2 */\r
-#ifdef USE_SHA256\r
- struct sha256_state sha256;\r
-#endif\r
-} hash_state;\r
-\r
-typedef hash_state sslSha1Context_t;\r
-typedef hash_state sslMd5Context_t;\r
-#ifdef USE_MD2\r
-typedef hash_state sslMd2Context_t;\r
-#endif /* USE_MD2 */\r
-#ifdef USE_SHA256\r
-typedef hash_state sslSha256Context_t;\r
-#endif /* USE_SHA256 */\r
-\r
-typedef struct {\r
- unsigned char pad[64];\r
- union {\r
- sslMd5Context_t md5;\r
- sslSha1Context_t sha1;\r
- } u;\r
-} sslHmacContext_t;\r
-\r
-/******************************************************************************/\r
-/*\r
- RC4\r
- */\r
-#ifdef USE_ARC4\r
-typedef struct {\r
- unsigned char state[256];\r
- uint32 byteCount;\r
- unsigned char x;\r
- unsigned char y;\r
-} rc4_key;\r
-#endif /* USE_ARC4 */\r
-\r
-#define SSL_DES3_KEY_LEN 24\r
-#define SSL_DES3_IV_LEN 8\r
-#ifdef USE_3DES\r
-\r
-typedef struct {\r
- ulong32 ek[3][32], dk[3][32];\r
-} des3_key;\r
-\r
-/*\r
- A block cipher CBC structure\r
- */\r
-typedef struct {\r
- int32 blocklen;\r
- unsigned char IV[8];\r
- des3_key key;\r
- int32 explicitIV; /* 1 if yes */\r
-} des3_CBC;\r
-\r
-extern int32 des3_setup(const unsigned char *key, int32 keylen, int32 num_rounds,\r
- des3_CBC *skey);\r
-extern void des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct,\r
- des3_CBC *key);\r
-extern void des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt,\r
- des3_CBC *key);\r
-extern int32 des3_keysize(int32 *desired_keysize);\r
-\r
-extern int32 des_setup(const unsigned char *key, int32 keylen, int32 num_rounds,\r
- des3_CBC *skey);\r
-extern void des_ecb_encrypt(const unsigned char *pt, unsigned char *ct,\r
- des3_CBC *key);\r
-extern void des_ecb_decrypt(const unsigned char *ct, unsigned char *pt,\r
- des3_CBC *key);\r
-\r
-#endif /* USE_3DES */\r
-\r
-\r
-typedef union {\r
-#ifdef USE_ARC4\r
- rc4_key arc4;\r
-#endif\r
-#ifdef USE_3DES\r
- des3_CBC des3;\r
-#endif\r
-} sslCipherContext_t;\r
-\r
-\r
-/*\r
- Controls endianess and size of registers. Leave uncommented to get\r
- platform neutral [slower] code detect x86-32 machines somewhat\r
- */\r
-#if (defined(_MSC_VER) && defined(WIN32)) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__)))\r
- #define ENDIAN_LITTLE\r
- #define ENDIAN_32BITWORD\r
-#endif\r
-\r
-\r
-/* #define ENDIAN_LITTLE */\r
-/* #define ENDIAN_BIG */\r
-\r
-/* #define ENDIAN_32BITWORD */\r
-/* #define ENDIAN_64BITWORD */\r
-\r
-#if (defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE)) && !(defined(ENDIAN_32BITWORD) || defined(ENDIAN_64BITWORD))\r
- #error You must specify a word size as well as endianess\r
-#endif\r
-\r
-#if !(defined(ENDIAN_BIG) || defined(ENDIAN_LITTLE))\r
- #define ENDIAN_NEUTRAL\r
-#endif\r
-\r
-/*\r
- helper macros\r
- */\r
-#if defined (ENDIAN_NEUTRAL)\r
-\r
-#define STORE32L(x, y) \\r
- { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \\r
- (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }\r
-\r
-#define LOAD32L(x, y) \\r
- { x = ((unsigned long)((y)[3] & 255)<<24) | \\r
- ((unsigned long)((y)[2] & 255)<<16) | \\r
- ((unsigned long)((y)[1] & 255)<<8) | \\r
- ((unsigned long)((y)[0] & 255)); }\r
-\r
-#define STORE64L(x, y) \\r
- { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \\r
- (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \\r
- (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \\r
- (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }\r
-\r
-#define LOAD64L(x, y) \\r
- { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \\r
- (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \\r
- (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \\r
- (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }\r
-\r
-#define STORE32H(x, y) \\r
- { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \\r
- (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }\r
-\r
-#define LOAD32H(x, y) \\r
- { x = ((unsigned long)((y)[0] & 255)<<24) | \\r
- ((unsigned long)((y)[1] & 255)<<16) | \\r
- ((unsigned long)((y)[2] & 255)<<8) | \\r
- ((unsigned long)((y)[3] & 255)); }\r
-\r
-#define STORE64H(x, y) \\r
- { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \\r
- (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \\r
- (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \\r
- (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }\r
-\r
-#define LOAD64H(x, y) \\r
- { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \\r
- (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \\r
- (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \\r
- (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }\r
-\r
-#endif /* ENDIAN_NEUTRAL */\r
-\r
-#ifdef ENDIAN_LITTLE\r
-\r
-#define STORE32H(x, y) \\r
- { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \\r
- (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }\r
-\r
-#define LOAD32H(x, y) \\r
- { x = ((unsigned long)((y)[0] & 255)<<24) | \\r
- ((unsigned long)((y)[1] & 255)<<16) | \\r
- ((unsigned long)((y)[2] & 255)<<8) | \\r
- ((unsigned long)((y)[3] & 255)); }\r
-\r
-#define STORE64H(x, y) \\r
- { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \\r
- (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \\r
- (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \\r
- (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }\r
-\r
-#define LOAD64H(x, y) \\r
- { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \\r
- (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \\r
- (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \\r
- (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }\r
-\r
-#ifdef ENDIAN_32BITWORD \r
-\r
-#define STORE32L(x, y) \\r
- { unsigned long __t = (x); memcpy(y, &__t, 4); }\r
-\r
-#define LOAD32L(x, y) \\r
- memcpy(&(x), y, 4);\r
-\r
-#define STORE64L(x, y) \\r
- { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \\r
- (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \\r
- (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \\r
- (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }\r
-\r
-#define LOAD64L(x, y) \\r
- { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \\r
- (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \\r
- (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \\r
- (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }\r
-\r
-#else /* 64-bit words then */\r
-\r
-#define STORE32L(x, y) \\r
- { unsigned long __t = (x); memcpy(y, &__t, 4); }\r
-\r
-#define LOAD32L(x, y) \\r
- { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }\r
-\r
-#define STORE64L(x, y) \\r
- { ulong64 __t = (x); memcpy(y, &__t, 8); }\r
-\r
-#define LOAD64L(x, y) \\r
- { memcpy(&(x), y, 8); }\r
-\r
-#endif /* ENDIAN_64BITWORD */\r
-#endif /* ENDIAN_LITTLE */\r
-\r
-#ifdef ENDIAN_BIG\r
-#define STORE32L(x, y) \\r
- { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \\r
- (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }\r
-\r
-#define LOAD32L(x, y) \\r
- { x = ((unsigned long)((y)[3] & 255)<<24) | \\r
- ((unsigned long)((y)[2] & 255)<<16) | \\r
- ((unsigned long)((y)[1] & 255)<<8) | \\r
- ((unsigned long)((y)[0] & 255)); }\r
-\r
-#define STORE64L(x, y) \\r
- { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \\r
- (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \\r
- (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \\r
- (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }\r
-\r
-#define LOAD64L(x, y) \\r
- { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \\r
- (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \\r
- (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \\r
- (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }\r
-\r
-#ifdef ENDIAN_32BITWORD \r
-\r
-#define STORE32H(x, y) \\r
- { unsigned long __t = (x); memcpy(y, &__t, 4); }\r
-\r
-#define LOAD32H(x, y) \\r
- memcpy(&(x), y, 4);\r
-\r
-#define STORE64H(x, y) \\r
- { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \\r
- (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \\r
- (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \\r
- (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }\r
-\r
-#define LOAD64H(x, y) \\r
- { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \\r
- (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \\r
- (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \\r
- (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); }\r
-\r
-#else /* 64-bit words then */\r
-\r
-#define STORE32H(x, y) \\r
- { unsigned long __t = (x); memcpy(y, &__t, 4); }\r
-\r
-#define LOAD32H(x, y) \\r
- { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; }\r
-\r
-#define STORE64H(x, y) \\r
- { ulong64 __t = (x); memcpy(y, &__t, 8); }\r
-\r
-#define LOAD64H(x, y) \\r
- { memcpy(&(x), y, 8); }\r
-\r
-#endif /* ENDIAN_64BITWORD */\r
-#endif /* ENDIAN_BIG */\r
-\r
-/*\r
- packet code */\r
-#if defined(USE_RSA) || defined(MDH) || defined(MECC)\r
- #define PACKET\r
-\r
-/*\r
- size of a packet header in bytes */\r
- #define PACKET_SIZE 4\r
-\r
-/*\r
- Section tags\r
- */\r
- #define PACKET_SECT_RSA 0\r
- #define PACKET_SECT_DH 1\r
- #define PACKET_SECT_ECC 2\r
- #define PACKET_SECT_DSA 3\r
-\r
-/*\r
- Subsection Tags for the first three sections\r
- */\r
- #define PACKET_SUB_KEY 0\r
- #define PACKET_SUB_ENCRYPTED 1\r
- #define PACKET_SUB_SIGNED 2\r
- #define PACKET_SUB_ENC_KEY 3\r
-#endif\r
-\r
-/*\r
- fix for MSVC ...evil!\r
- */\r
-#ifdef WIN32\r
-#ifdef _MSC_VER\r
- #define CONST64(n) n ## ui64\r
- typedef unsigned __int64 ulong64;\r
-#else\r
- #define CONST64(n) n ## ULL\r
- typedef unsigned long long ulong64;\r
-#endif\r
-#endif /* WIN32 */\r
-\r
-\r
-#define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \\r
- ((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) )\r
-\r
-#ifdef _MSC_VER\r
-\r
-/*\r
- instrinsic rotate\r
- */\r
-#include <stdlib.h>\r
-#pragma intrinsic(_lrotr,_lrotl)\r
-#define ROR(x,n) _lrotr(x,n)\r
-#define ROL(x,n) _lrotl(x,n)\r
-#define RORc(x,n) _lrotr(x,n)\r
-#define ROLc(x,n) _lrotl(x,n)\r
-\r
-/*\r
-#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) && !defined(PS_NO_ASM)\r
-\r
-static inline unsigned ROL(unsigned word, int32 i)\r
-{\r
- asm ("roll %%cl,%0"\r
- :"0" (word),"c" (i));\r
- return word;\r
-}\r
-\r
-static inline unsigned ROR(unsigned word, int32 i)\r
-{\r
- asm ("rorl %%cl,%0"\r
- :"=r" (word)\r
- :"0" (word),"c" (i));\r
- return word;\r
-}\r
-*/\r
-/*\r
-#ifndef PS_NO_ROLC\r
-\r
-static inline unsigned ROLc(unsigned word, const int32 i)\r
-{\r
- asm ("roll %2,%0"\r
- :"=r" (word)\r
- :"0" (word),"I" (i));\r
- return word;\r
-}\r
-\r
-static inline unsigned RORc(unsigned word, const int32 i)\r
-{\r
- asm ("rorl %2,%0"\r
- :"=r" (word)\r
- :"0" (word),"I" (i));\r
- return word;\r
-}\r
-\r
-#else\r
-\r
-#define ROLc ROL\r
-#define RORc ROR\r
-\r
-#endif\r
-*/\r
-\r
-#else /* _MSC_VER */\r
-\r
-/*\r
- rotates the hard way\r
- */\r
-#define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)\r
-#define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)\r
-#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)\r
-#define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)\r
-\r
-#endif /* _MSC_VER */\r
-\r
-/* 64-bit Rotates */\r
-#if 0\r
-\r
-#if defined(__GNUC__) && defined(__x86_64__) && !defined(PS_NO_ASM)\r
-\r
-static inline unsigned long ROL64(unsigned long word, int32 i)\r
-{\r
- asm("rolq %%cl,%0"\r
- :"=r" (word)\r
- :"0" (word),"c" (i));\r
- return word;\r
-}\r
-\r
-static inline unsigned long ROR64(unsigned long word, int32 i)\r
-{\r
- asm("rorq %%cl,%0"\r
- :"=r" (word)\r
- :"0" (word),"c" (i));\r
- return word;\r
-}\r
-\r
-#ifndef PS_NO_ROLC\r
-\r
-static inline unsigned long ROL64c(unsigned long word, const int32 i)\r
-{\r
- asm("rolq %2,%0"\r
- :"=r" (word)\r
- :"0" (word),"J" (i));\r
- return word;\r
-}\r
-\r
-static inline unsigned long ROR64c(unsigned long word, const int32 i)\r
-{\r
- asm("rorq %2,%0"\r
- :"=r" (word)\r
- :"0" (word),"J" (i));\r
- return word;\r
-}\r
-\r
-#else /* PS_NO_ROLC */\r
-\r
-#define ROL64c ROL\r
-#define ROR64c ROR\r
-\r
-#endif /* PS_NO_ROLC */\r
-#endif\r
-#endif /* commented out */\r
-\r
-#define ROL64(x, y) \\r
- ( (((x)<<((ulong64)(y)&63)) | \\r
- (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))\r
-\r
-#define ROR64(x, y) \\r
- ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \\r
- ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))\r
-\r
-#define ROL64c(x, y) \\r
- ( (((x)<<((ulong64)(y)&63)) | \\r
- (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))\r
-\r
-#define ROR64c(x, y) \\r
- ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \\r
- ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))\r
-\r
-#undef MAX\r
-#undef MIN\r
-#define MAX(x, y) ( ((x)>(y))?(x):(y) )\r
-#define MIN(x, y) ( ((x)<(y))?(x):(y) )\r
-\r
-/*\r
- extract a byte portably This MSC code causes runtime errors in VS.NET,\r
- always use the other\r
- */\r
-/*\r
-#ifdef _MSC_VER\r
- #define byte(x, n) ((unsigned char)((x) >> (8 * (n))))\r
-#else\r
-*/\r
- #define byte(x, n) (((x) >> (8 * (n))) & 255)\r
-/*\r
-#endif\r
-*/\r
-#ifdef __cplusplus\r
- }\r
-#endif /* __cplusplus */\r
-\r
-#endif /* _h_PSCRYPTO */\r
-\r
-/******************************************************************************/\r
-\r
+++ /dev/null
-#if 0
-
-#include "ssl.h"
-#include "ssl_constructs.h"
-#include <string.h> // for bcopy()
-#include <time.h> // for time()
-#include <stdlib.h> // for rand(), htons?, htonl?
-// note net byte order is big-endian
-// Need to set error codes
-
-int CreateSSLHello(SSL_t *ssl)
-{
- printf("In CreateSSLHello()\n",ssl);
-
- // Initalize the structure
- bzero(ssl,sizeof(SSL_t));
- //ssl->max_size = sizeof(ssl->buffer);
- ssl->max_size = 18456;
-
- // Declare variables
- int i; void *ptr;
-
- // Set pointers into buffer
- SSLPlaintext *record = (SSLPlaintext *)ssl->buffer;
- Handshake *handshake = (Handshake *)record->fragment;
- // the body starts right after the handshake
- printf("sizeof(Handshake) = %d\n",sizeof(Handshake));
- ClientHello *hello = (ClientHello *)(handshake + 1);
-
- printf("record->%#x, handshake->%#x, hello->%#x\n",record,handshake,hello);
-
- // Construct ClientHello Message
- hello->client_version = version;
- i = htonl(time(NULL));
- bcopy(&i,hello->random.gmt_unix_time,4);
- for(i=0;i<28;i++){ hello->random.random_bytes[i] = (uint8)rand(); }
- hello->session_id_length = 0;
- hello->session_id = &hello->session_id_length;
- hello->session_id_end = hello->session_id;
- hello->cipher_suites_length = (CipherSuiteLength *)(hello->session_id_end + 1);
- hello->cipher_suites = (hello->cipher_suites_length + 1);
- hello->cipher_suites_end = hello->cipher_suites;
- i = htons(2*5); // 2 bytes per Suite * 5 Suites
- bcopy(&i,hello->cipher_suites_length,2);
- bcopy(SSL_NULL_WITH_NULL_NULL,hello->cipher_suites_end,sizeof(CipherSuite));
- *hello->cipher_suites_end++;
- bcopy(SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA,hello->cipher_suites_end,sizeof(CipherSuite));
- *hello->cipher_suites_end++;
- bcopy(SSL_DH_DSS_WITH_DES_CBC_SHA,hello->cipher_suites_end,sizeof(CipherSuite));
- *hello->cipher_suites_end++;
- bcopy(SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA,hello->cipher_suites_end,sizeof(CipherSuite));
- *hello->cipher_suites_end++;
- bcopy(SSL_DH_anon_WITH_RC4_128_MD5,hello->cipher_suites_end,sizeof(CipherSuite));
- hello->compression_methods_length = (CompressionMethodLength *)(hello->cipher_suites_end + 1);
- hello->compression_methods = (hello->compression_methods_length + 1);
- hello->compression_methods_end = hello->compression_methods;
- *hello->compression_methods_length = 1;
- *hello->compression_methods_end = compression_method_null;
-
- // Construct Handshake Message
- handshake->msg_type = handshake_type_client_hello;
- i = (void *)(hello->compression_methods_end + 1) - (void *)hello;
- printf("Handshake.length = %d\n", i);
- handshake->length[0] = (char)*(&i+8);
- handshake->length[1] = (char)*(&i+8);
- handshake->length[2] = (char)i;
- //bcopy((&i+1),handshake->length,3); // +1 so we copy 3 bytes
-
- // Construct SSL Record
- printf("sizeof(ContentType)=%d\n",sizeof(ContentType));
- printf("sizeof(uint8)=%d\n",sizeof(uint8));
- record->type = content_type_handshake;
- record->version = version;
- i += sizeof(Handshake);
- printf("SSLPlaintext.length = %d\n",i);
- record->length[0] = (char)*(&i+8);
- record->length[1] = (char)i;
- //bcopy(&i,record->length,4); // length of handshake
-
- // Set total size of message
- i += sizeof(ContentType) + sizeof(ProtocolVersion) + sizeof(uint16);
- ssl->length = i;
- printf("End of CreateSSLHello\n");
- return 0;
-}
-
-void PrintSSLPacket(SSL_t *ssl)
-{
- printf("Printing packet with length:%d\n", ssl->length);
- char *ptr = ssl->buffer;
- char *begin = ptr;
- char *tmp;
- char *end = ssl->buffer + ssl->length;
- printf("Record Layer:\n");
- printf("\tContentType: %2hhX\n",(char)*ptr++);
- printf("\tVersion: %2hhX %2hhX\n", (char)*ptr++, (char)*ptr++);
- printf("\tLength: %2hhX %2hhX\n", (char)*ptr++, (char)*ptr++);
-
- printf("Handshake:\n");
- printf("\tType: %2hhX\n", (char)*ptr++);
- printf("\tLength: %2hhX %2hhX %2hhX\n", (char)*ptr++, (char)*ptr++, (char)*ptr++);
- printf("\tVersion: %2hhX %2hhX\n", (char)*ptr++, (char)*ptr++);
- printf("\tgmt_unix_time: %2hhX %2hhX %2hhX %2hhX\n", (char)*ptr++, (char)*ptr++, (char)*ptr++, (char)*ptr++);
- printf("\trandom: ");
- tmp = ptr + 28;
- for(;ptr<tmp;ptr++){printf("%2hhX ", (char)*ptr);}
-
- printf("\n\nHexDump:\n");
-
- int ctr = 0;
- for(;begin<end;begin++){printf("%2hhX ",(char)*begin);if(++ctr%10==0){printf("\n");}}
- printf("\n\n");
-}
-
-int ReadSSLHello(SSL_t *ssl)
-{
- SSLCiphertext *ct = (SSLCiphertext *)ssl->buffer;
-
- if(ct->type == content_type_alert){
- // assuming text is still plaintext
- Alert *a = (Alert *)&ct->fragment;
- if(a->level == alert_level_fatal){
- printf("Fatal Alert %d, connection terminated\n",a->description);
- return (1);
- }else if(a->level == alert_level_warning){
- printf("Warning Alert %d\n", a->description);
- }else{
- printf("Unknown alert level %d\n", a->level);
- }
- }else{
- printf("SSL type %d\n",ct->type);
- }
- return (0);
-}
-
-#endif
+++ /dev/null
-// At the moment I have hard coded one buffer. The size
-// is the max size of SSLCiphertext.length (so, actually it should
-// be increased to include the other information in the struct)
-// I might need to make a new, or split the current, buffer because
-// I have to have space to read in and write out, as well as keep
-// any data that has not been translated.
-// It works for now.
-typedef struct _ssl_t{
- char buffer[18456];
- int length;
- int max_size; // can't define const here
- // Current CipherSuite
- // Client random / Server random ???
- // pointers to different crypto functions
-} SSL_t;
-
-int CreateSSLHello(SSL_t *ssl);
-int ReadSSLHello(SSL_t *ssl);
-void PrintSSLPacket(SSL_t *ssl);
+++ /dev/null
-// Note: This file still needs some work.
-// Note: I had to redefine the enums to a set of const values,
-// so that the size of the variable would be correct.
-
-// Typedefs
-// (As defined by the SSL v3.0 RFC Draft)
-// URL: http://wp.netscape.com/eng/ssl3/draft302.txt
-typedef unsigned char uint8;
-typedef uint8 uint16[2];
-typedef uint8 uint24[3];
-typedef uint8 uint32[4];
-typedef uint8 uint64[8];
-
-// Record layers
-typedef struct _ProtocolVersion{
- uint8 major, minor;
-} ProtocolVersion;
-
-const ProtocolVersion version = { 3, 0 };
-
-typedef uint8 ContentType;
-const ContentType content_type_change_cipher_spec_type = 20;
-const ContentType content_type_alert = 21;
-const ContentType content_type_handshake = 22;
-const ContentType content_type_application_data = 23;
-
-typedef struct _SSLPlaintext{
- ContentType type;
- ProtocolVersion version;
- uint16 length; // can not exceed 2^14 bytes
- uint8 fragment[16384]; // 2^14 = 16,384 bytes
-} SSLPlaintext;
-
-typedef struct _SSLCompressed{
- ContentType type;
- ProtocolVersion version;
- uint16 length; // can not exceed 2^14 + 1024
- uint8 fragment[17408]; // SSLCompressed.length
-} SSLCompressed;
-
-typedef struct _SSLCiphertext{
- ContentType type;
- ProtocolVersion version;
- uint16 length;
- uint8 fragment; // so we have a pointer to the data, and don't have to do math
- // fragment; type GenericStreamCipher or GenericBlockCipher
-} SSLCiphertext; // recast to get fragment
-
-typedef struct _GenericStreamCipher{
- uint8 content[17408]; // SSLCompressed.length
- uint8 MAC[]; // CipherSpec.hash_size
-} GenericStreamCipher;
-
-typedef struct _SSLStreamCiphertext{
- ContentType type;
- ProtocolVersion version;
- uint16 length; // can not exceed 2^14 + 2048 = 18,456
- GenericStreamCipher fragment;
-} SSLStreamCiphertext;
-
-typedef struct _GenericBlockCipher{
- uint8 content[17408]; // SSLConpressed.length
- uint8 MAC[0]; // CipherSpec.hash_size
- // padding is used to bring the plaintext to
- // a multiple of the block cipher's block length.
- uint8 padding[0]; // GenericBlockCipher.padding_length
- uint8 padding_length;
-} GenericBlockCipher;
-
-typedef struct _SSLBlockCiphertext{
- ContentType type;
- ProtocolVersion version;
- uint16 length; // can not exceed 2^14 + 2048 = 18,456
- GenericBlockCipher fragment;
-} SSLBlockCiphertext;
-
-// Change cipher specs message
-typedef struct _ChangeCipherSpec{
- enum { type_change_cipher_spec=1, type_size=255 } type;
-} ChangeCipherSpec;
-
-// Alert messages
-typedef uint8 AlertLevel;
-const AlertLevel alert_level_warning = 1;
-const AlertLevel alert_level_fatal=2;
-
-typedef uint8 AlertDescription;
-const AlertDescription alert_description_close_notify = 0;
-const AlertDescription alert_description_unexpected_message = 10;
-const AlertDescription alert_description_bad_record_mac = 20;
-const AlertDescription alert_description_decompression_failure = 30;
-const AlertDescription alert_description_handshake_failure = 40;
-const AlertDescription alert_description_no_certificate = 41;
-const AlertDescription alert_description_bad_certificate = 42;
-const AlertDescription alert_description_unsupported_certificate = 43;
-const AlertDescription alert_description_certificate_revoked = 44;
-const AlertDescription alert_description_certificate_expired = 45;
-const AlertDescription alert_description_certificate_unknown = 46;
-const AlertDescription alert_description_illegal_parameter = 47;
-
-typedef struct _Alert{
- AlertLevel level;
- AlertDescription description;
-} Alert;
-
-// Handshake protocol
-// What is the best way to have a generic pointer to the body struct??
-typedef uint8 HandshakeType;
-const HandshakeType handshake_type_hello_request = 0;
-const HandshakeType handshake_type_client_hello = 1;
-const HandshakeType handshake_type_server_hello = 2;
-const HandshakeType handshake_type_certificate = 11;
-const HandshakeType handshake_type_server_key_exchange = 12;
-const HandshakeType handshake_type_certificate_request = 13;
-const HandshakeType handshake_type_server_done = 14;
-const HandshakeType handshake_type_certificate_verify = 15;
-const HandshakeType handshake_type_client_key_exchange = 16;
-const HandshakeType handshake_type_finished = 20;
-
-typedef struct _Handshake{
- HandshakeType msg_type;
- uint24 length;
- // body; // one of HandshakeType structs
-} Handshake; // generic Handshake, need to recast to get body
-
-// Hello messages
-typedef struct _HelloRequest{} HelloRequest;
-
-typedef struct _HelloRequestHandshake{
- HandshakeType msg_type;
- uint24 length;
- HelloRequest body;
-} HelloRequestHandshake;
-
-typedef struct _Random{
- uint32 gmt_unix_time;
- uint8 random_bytes[28];
-} Random;
-
-//typedef uint8 SessionID[32]; // <0..32>
-typedef uint8 SessionIDLength;
-typedef uint8 SessionID;
-
-typedef uint16 CipherSuiteLength;
-typedef uint8 CipherSuite[2];
-
-typedef uint8 CompressionMethodLength;
-typedef uint8 CompressionMethod;
-const CompressionMethod compression_method_null = 0;
-
-
-typedef struct _ClientHello{
- ProtocolVersion client_version;
- Random random;
- SessionIDLength session_id_length;
- SessionID *session_id;
- SessionID *session_id_end;
- CipherSuiteLength *cipher_suites_length;
- CipherSuite *cipher_suites; // min size is one entry
- CipherSuite *cipher_suites_end;
- //CipherSuite cipher_suites[32768]; // <2..2^16-1> = 65,536 bytes and CipherSuite is 2 bytes
- CompressionMethodLength *compression_methods_length;
- CompressionMethod *compression_methods;
- CompressionMethod *compression_methods_end;
- //CompressionMethod *compression_methods; // min size is zero
- //CompressionMethod compression_methods[256]; // <0..2^8-1> = 256 bytes and CompressionMethod is 1 byte
-} ClientHello;
-
-typedef struct _ClientHelloHandshake{
- //HandshakeType msg_type;
- uint8 msg_type;
- uint24 length;
- ClientHello body;
-} ClientHelloHandshake;
-
-typedef struct _ServerHello{
- ProtocolVersion server_version;
- Random random;
- SessionID session_id;
- CipherSuite cipher_suite;
- CompressionMethod compression_method;
-} ServerHello;
-
-typedef struct _ServerHelloHandshake{
- HandshakeType msg_type;
- uint24 length;
- ServerHello body;
-} ServerHelloHandshake;
-
-// Server authentication and key exchange messages
-typedef uint8 ASN1Cert[16777216]; // <1..2^24-1> = 16,777,216 bytes
-
-typedef struct _Certificate{
- ASN1Cert certificate_list[1]; // <1..2^24-1> / ANS1Cert = 1
- // for some reason the size of certificate_list and ASN1Cert is the same, so only one certificate in the list
-} Certificate;
-
-typedef uint8 KeyExchangeAlgorithm;
-const KeyExchangeAlgorithm key_exchange_algorithm_rsa = 0;
-const KeyExchangeAlgorithm key_exchange_algorithm_diffie_hellman = 1;
-const KeyExchangeAlgorithm key_exchange_algorithm_fortezza_kea = 2;
-
-typedef struct _AnonSignature{
- struct {};
-} AnonSignature;
-
-typedef struct _RSASignature{
- uint8 md5_hash[16];
- uint8 sha_hash[20];
-} RSASignature;
-
-typedef struct _DSASignature{
- uint8 sha_hash[20];
-} DSASignature;
-
-// use union??, make a mess to reference, but easy to make Signature type.
-typedef union _Signature{ AnonSignature anon; RSASignature rsa; DSASignature dsa; } Signature;
-
-typedef struct _ServerRSAParams{
- uint8 RSA_modulus[65536]; // <1..2^16-1> = 65,536
- uint8 RSA_exponent[65536]; // <1..2^16-1> = 65,536
-} ServerRSAParams;
-
-typedef struct _ServerDHParams{
- uint8 DH_p[65536]; // <1..2^16-1>
- uint8 DH_g[65536]; // <1..2^16-1>
- uint8 DH_Ys[65536]; // <1..2^16-1>
-} ServerDHParams;
-
-typedef struct _ServerDHKeyExchange{
- ServerDHParams params;
- Signature signed_params;
-} ServerDHKeyExchange;
-
-typedef struct _ServerRSAKeyExchange{
- ServerRSAParams params;
- Signature signed_params;
-} ServerRSAKeyExchange;
-
-typedef uint8 SignatureAlgorithm;
-const SignatureAlgorithm signature_algorithm_anonymous = 0;
-const SignatureAlgorithm signature_algorithm_rsa = 1;
-const SignatureAlgorithm signature_algorithm_dsa = 2;
-
-typedef uint8 CertificateType;
-const CertificateType certificate_type_RSA_sign = 1;
-const CertificateType certificate_type_DSS_sign = 2;
-const CertificateType certificate_type_RSA_fixed_DH = 3;
-const CertificateType certificate_type_DSS_fixed_DH = 4;
-const CertificateType certificate_type_RSA_ephemeral_DH = 5;
-const CertificateType certificate_type_DSS_ephemeral_DH = 6;
-const CertificateType certificate_type_FORTEZZA_MISSI = 20;
-
-typedef uint8 DistinguishedName[65536]; // <1..2^16-1> = 65,536
-
-typedef struct _CertificateRequest{
- CertificateType certificate_types[256]; // <1..2^8-1>
- DistinguishedName certificate_authorities[1]; // <3...2^16-1> / DistinguishedName
- // this is another one that is odd with a list size of 1
-} CertificateRequest;
-
-typedef struct _ServerHelloDone{} ServerHelloDone;
-
-// Client authentication and key exchange messages
-typedef struct _PreMasterSecret{
- ProtocolVersion client_version;
- uint8 random[46];
-} PreMasterSecret;
-
-typedef struct _EncryptedPreMasterSecret{
- PreMasterSecret pre_master_secret;
-} EncryptedPreMasterSecret;
-
-typedef struct _RSAClientKeyExchange{
- EncryptedPreMasterSecret exchange_keys;
-} RSAClientKeyExchange;
-
-typedef uint8 PublicValueEncoding;
-const PublicValueEncoding public_value_encoding_implicit = 0;
-const PublicValueEncoding public_value_encoding_explicit = 1;
-
-typedef struct _ClientDiffieHellmanPublic{
- // This is a select on PublicValueEncoding, and I chose the larger size
- uint8 dh_public[65536]; // DH_Yc<1..2^16-1>, the dh public value
-} ClientDiffieHellmanPublic;
-
-typedef struct _DHClientKeyExhange{
- ClientDiffieHellmanPublic exchange_keys;
-} DHClientKeyExchange;
-
-typedef struct _CertificateVerify{
- Signature signature;
-} CertificateVerify;
-
-// Handshake finalization message
-typedef struct _Finished{
- uint8 md5_hash[16];
- uint8 sha_hash[20];
-} Finished;
-
-// The CipherSuite
-CipherSuite SSL_NULL_WITH_NULL_NULL = { 0x00, 0x13 };
-CipherSuite SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA = { 0x00, 0x0B };
-CipherSuite SSL_DH_DSS_WITH_DES_CBC_SHA = { 0x00, 0x0C };
-CipherSuite SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA = { 0x00, 0x11 };
-CipherSuite SSL_DH_anon_EXPORT_WITH_RC4_40_MD5 = { 0x00, 0x17 };
-CipherSuite SSL_DH_anon_WITH_RC4_128_MD5 = { 0x00, 0x18 };
-
-// The CipherSpec
-typedef uint8 CipherType;
-const CipherType cipher_type_stream = 0;
-const CipherType cipher_type_block = 1;
-
-typedef uint8 IsExportable;
-const IsExportable is_exportable_true = 0;
-const IsExportable is_exportable_false = 1;
-
-typedef uint8 BulkCipherAlgorithm;
-const BulkCipherAlgorithm bulk_cipher_algorithm_null = 0;
-const BulkCipherAlgorithm bulk_cipher_algorithm_rc4 = 1;
-const BulkCipherAlgorithm bulk_cipher_algorithm_rc2 = 2;
-const BulkCipherAlgorithm bulk_cipher_algorithm_des = 3;
-const BulkCipherAlgorithm bulk_cipher_algorithm_3des = 4;
-const BulkCipherAlgorithm bulk_cipher_algorithm_des40 = 5;
-const BulkCipherAlgorithm bulk_cipher_algorithm_fortezza = 6;
-
-typedef uint8 MACAlgorithm;
-const MACAlgorithm mac_algorithm_null = 0;
-const MACAlgorithm mac_algorithm_md5 = 1;
-const MACAlgorithm mac_algorithm_sha = 2;
-
-typedef struct _CipherSpec{
- BulkCipherAlgorithm bulk_cipher_algorithm;
- MACAlgorithm mac_algorithm;
- CipherType cipher_type;
- IsExportable is_exportable;
- uint8 hash_size;
- uint8 key_material;
- uint8 IV_size;
-} CipherSpec;
-
-