--- gwenhywfar-2.6.1/src/crypt/crypt.c.bak 2006-04-08 11:05:39.000000000 +0300 +++ gwenhywfar-2.6.1/src/crypt/crypt.c 2007-10-31 14:39:30.510318445 +0200 @@ -44,19 +44,18 @@ #include #include #include - +#include +#include +#include +#include +#include +#include static int gwen_crypt_is_initialized=0; static GWEN_ERRORTYPEREGISTRATIONFORM *gwen_crypt_errorform=0; static GWEN_CRYPTKEY_PROVIDER *gwen_crypt_providers=0; -#define GWEN_RANDSTATE_BUFSIZE 256 -#ifdef HAVE_RANDOM -static char gwen_random_state[GWEN_RANDSTATE_BUFSIZE]; -#endif - - GWEN_INHERIT_FUNCTIONS(GWEN_CRYPTKEY) GWEN_LIST_FUNCTIONS(GWEN_CRYPTKEY, GWEN_CryptKey) GWEN_LIST2_FUNCTIONS(GWEN_CRYPTKEY, GWEN_CryptKey) @@ -123,18 +122,7 @@ GWEN_ERRORCODE GWEN_Crypt_ModuleInit(){ return err; } - /* seed random number generator*/ -#ifdef HAVE_SRANDOM - { - char* prev_randstate = initstate((unsigned int)time(0), - gwen_random_state, - GWEN_RANDSTATE_BUFSIZE); - setstate(prev_randstate); - } -#else - srand((unsigned int)time(0)); -#endif - + (void)GWEN_Random(); gwen_crypt_is_initialized=1; } return 0; @@ -848,25 +836,82 @@ void GWEN_CryptKey_SubFlags(GWEN_CRYPTKE key->flags&=~fl; } +#define R(a,b) (((a) << (b)) | ((a) >> (32 - (b)))) +static void salsa20(void * __out, void *__in) +{ + int i; + uint32_t x[16]; + uint32_t *in = __in; + uint32_t *out = __out; + + for (i = 0; i < 16; ++i) x[i] = in[i]; + + for (i = 20;i > 0;i -= 2) { + x[ 4] ^= R(x[ 0]+x[12], 7); + x[ 8] ^= R(x[ 4]+x[ 0], 9); + x[12] ^= R(x[ 8]+x[ 4],13); + x[ 0] ^= R(x[12]+x[ 8],18); + x[ 9] ^= R(x[ 5]+x[ 1], 7); + x[13] ^= R(x[ 9]+x[ 5], 9); + x[ 1] ^= R(x[13]+x[ 9],13); + x[ 5] ^= R(x[ 1]+x[13],18); + x[14] ^= R(x[10]+x[ 6], 7); + x[ 2] ^= R(x[14]+x[10], 9); + x[ 6] ^= R(x[ 2]+x[14],13); + x[10] ^= R(x[ 6]+x[ 2],18); + x[ 3] ^= R(x[15]+x[11], 7); + x[ 7] ^= R(x[ 3]+x[15], 9); + x[11] ^= R(x[ 7]+x[ 3],13); + x[15] ^= R(x[11]+x[ 7],18); + x[ 1] ^= R(x[ 0]+x[ 3], 7); + x[ 2] ^= R(x[ 1]+x[ 0], 9); + x[ 3] ^= R(x[ 2]+x[ 1],13); + x[ 0] ^= R(x[ 3]+x[ 2],18); + x[ 6] ^= R(x[ 5]+x[ 4], 7); + x[ 7] ^= R(x[ 6]+x[ 5], 9); + x[ 4] ^= R(x[ 7]+x[ 6],13); + x[ 5] ^= R(x[ 4]+x[ 7],18); + x[11] ^= R(x[10]+x[ 9], 7); + x[ 8] ^= R(x[11]+x[10], 9); + x[ 9] ^= R(x[ 8]+x[11],13); + x[10] ^= R(x[ 9]+x[ 8],18); + x[12] ^= R(x[15]+x[14], 7); + x[13] ^= R(x[12]+x[15], 9); + x[14] ^= R(x[13]+x[12],13); + x[15] ^= R(x[14]+x[13],18); + } + for (i = 0;i < 16;++i) out[i] = x[i] + in[i]; +} + +int64_t GWEN_Random(void) +{ + static uint64_t salsa20_out[8]; + static uint64_t salsa20_in[8]; + static int nrints; + static int rndinit; + + if (!rndinit) { + int fd; + + rndinit = 1; + salsa20_in[0] = time(NULL); + salsa20_in[1] = getpid(); + fd = open("/dev/urandom", O_RDONLY); + if (fd != -1) { + read(fd, salsa20_in, sizeof(salsa20_in)); + close(fd); + } + } -long int GWEN_Random(){ - long int result; -#ifdef HAVE_RANDOM - char* prev_randstate = setstate(gwen_random_state); - result = random(); - setstate(prev_randstate); -#else - result = rand(); -#endif - return result; -} - - - - - - + if (nrints == 0) { + if (!++salsa20_in[0]) if (!++salsa20_in[1]) if (!++salsa20_in[2]) if (!++salsa20_in[3]) + if (!++salsa20_in[4]) if (!++salsa20_in[5]) if (!++salsa20_in[6]) ++salsa20_in[7]; + salsa20(salsa20_out, salsa20_in); + nrints = 8; + } + return salsa20_out[--nrints]; +} GWEN_CRYPTKEY_PROVIDER *GWEN_CryptProvider_new(){ --- gwenhywfar-2.6.1/src/crypt/padd.c.bak 2006-04-08 11:05:39.000000000 +0300 +++ gwenhywfar-2.6.1/src/crypt/padd.c 2007-10-31 14:34:02.654318282 +0200 @@ -246,10 +246,10 @@ int GWEN_Padd_PaddWithPkcs1Bt2(GWEN_BUFF *(p++)=0x00; *(p++)=0x02; /* block type 02 */ for (i=0; i