3 * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
5 * This file is part of CyaSSL.
7 * CyaSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CyaSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
26 /* on HPUX 11 you may need to install /dev/random see
27 http://h20293.www2.hp.com/portal/swdepot/displayProductInfo.do?productNumber=KRNG11I
31 #include <cyassl/ctaocrypt/random.h>
32 #include <cyassl/ctaocrypt/error.h>
35 #if defined(USE_WINDOWS_API)
37 #define _WIN32_WINNT 0x0400
46 /* include headers that may be needed to get good seed */
48 #endif /* USE_WINDOWS_API */
52 /* Get seed and key cipher */
58 int ret = GenerateSeed(&rng->seed, key, sizeof(key));
61 Arc4SetKey(&rng->cipher, key, sizeof(key));
62 RNG_GenerateBlock(rng, junk, sizeof(junk)); /* rid initial state */
69 /* place a generated block in output */
70 void RNG_GenerateBlock(RNG* rng, byte* output, word32 sz)
72 XMEMSET(output, 0, sz);
73 Arc4Process(&rng->cipher, output, output, sz);
77 byte RNG_GenerateByte(RNG* rng)
80 RNG_GenerateBlock(rng, &b, 1);
86 #if defined(USE_WINDOWS_API)
89 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
91 if(!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL,
95 if (!CryptGenRandom(os->handle, sz, output))
98 CryptReleaseContext(os->handle, 0);
104 #elif defined(THREADX)
106 #include "rtprand.h" /* rtp_rand () */
107 #include "rtptime.h" /* rtp_get_system_msec() */
110 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
113 rtp_srand(rtp_get_system_msec());
115 for (i = 0; i < sz; i++ ) {
116 output[i] = rtp_rand() % 256;
118 rtp_srand(rtp_get_system_msec());
125 #elif defined(MICRIUM)
127 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
129 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
130 NetSecure_InitSeed(output, sz);
137 /* write a real one !!!, just for testing board */
138 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
141 for (i = 0; i < sz; i++ )
147 #elif defined(NO_DEV_RANDOM)
149 #error "you need to write an os specific GenerateSeed() here"
152 #else /* !USE_WINDOWS_API && !THREADX && !MICRIUM && !NO_DEV_RANDOM */
156 int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
160 os->fd = open("/dev/urandom",O_RDONLY);
162 /* may still have /dev/random */
163 os->fd = open("/dev/random",O_RDONLY);
169 int len = read(os->fd, output, sz);
180 sleep(0); /* context switch */
192 #endif /* USE_WINDOWS_API */