NEINT32 RSAinit_random(R_RANDOM_STRUCT *rStruct) { NEINT32 ret,i, num ; NEINT8 *p_randblock, *paddr ; static NEINT8 _randbuf[] = { 0x12, 0xf3 , 0x44, 0x78, 0xad,0x9f,0xf1,0x01,0xb3,0x32,0x5a,0x67,0x8c,0x2f,0xb8,0xde} ; if(R_RandomInit(rStruct)){ return -1; } R_GetRandomBytesNeeded(&ret, rStruct) ; p_randblock = malloc(ret) ; if(!p_randblock) return 1 ; num = ret/sizeof(_randbuf) ; paddr = p_randblock ; for(i=0; i<num; i++){ memcpy(paddr,_randbuf,sizeof(_randbuf) ) ; paddr += sizeof(_randbuf) ; } R_RandomUpdate(rStruct, p_randblock, ret) ; free(p_randblock); return 0; }
void main(void) { unsigned char freshout[20], seedbyte = 1; unsigned int bytesneeded; R_RANDOM_STRUCT randomstruct; fprintf(stderr, "RANDEMO - Random Object Demo Using %s.\n", RSAEURO_IDENT); R_RandomCreate(&randomstruct); R_GenerateBytes(freshout, sizeof(freshout), &randomstruct); printf("\n Random Output 1 - "); printbuff(freshout, sizeof(freshout)); R_RandomMix(&randomstruct); R_GenerateBytes(freshout, sizeof(freshout), &randomstruct); printf(" Mixed Random Output 1 - "); printbuff(freshout, sizeof(freshout)); R_RandomFinal(&randomstruct); R_RandomInit(&randomstruct); while(1) { R_GetRandomBytesNeeded(&bytesneeded, &randomstruct); if(bytesneeded == 0) break; R_RandomUpdate(&randomstruct, &seedbyte, 1); } R_GenerateBytes(freshout, sizeof(freshout), &randomstruct); printf(" Random Output 2 - "); printbuff(freshout, sizeof(freshout)); R_RandomMix(&randomstruct); R_GenerateBytes(freshout, sizeof(freshout), &randomstruct); printf(" Mixed Random Output 2 - "); printbuff(freshout, sizeof(freshout)); R_GenerateBytes(freshout, sizeof(freshout), &randomstruct); printf(" Mixed Random Output 2 - "); printbuff(freshout, sizeof(freshout)); R_RandomFinal(&randomstruct); }
static int rsaref_public_encrypt(int len, const unsigned char *from, unsigned char *to, RSA *rsa, int padding) { int outlen= -1; int i; R_RSA_PUBLIC_KEY RSAkey; R_RANDOM_STRUCT rnd; unsigned char buf[16]; if (padding != RSA_PKCS1_PADDING && padding != RSA_SSLV23_PADDING) { RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE); goto err; } R_RandomInit(&rnd); R_GetRandomBytesNeeded((unsigned int *)&i,&rnd); while (i > 0) { if (RAND_bytes(buf,16) <= 0) goto err; R_RandomUpdate(&rnd,buf,(unsigned int)((i>16)?16:i)); i-=16; } if (!RSAref_Public_eay2ref(rsa,&RSAkey)) goto err; if ((i=RSAPublicEncrypt(to,(unsigned int *)&outlen,(unsigned char *)from,len,&RSAkey,&rnd)) != 0) { RSAREFerr(RSAREF_F_RSAREF_PUBLIC_ENCRYPT,i); outlen= -1; goto err; } err: memset(&RSAkey,0,sizeof(RSAkey)); R_RandomFinal(&rnd); memset(&rnd,0,sizeof(rnd)); return(outlen); }