cya_u
Fork of CyaSSL-forEncrypt by
random.c@0:5045d2638c29, 2011-02-05 (annotated)
- Committer:
- toddouska
- Date:
- Sat Feb 05 01:09:17 2011 +0000
- Revision:
- 0:5045d2638c29
Beta Version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
toddouska | 0:5045d2638c29 | 1 | /* random.c |
toddouska | 0:5045d2638c29 | 2 | * |
toddouska | 0:5045d2638c29 | 3 | * Copyright (C) 2006-2009 Sawtooth Consulting Ltd. |
toddouska | 0:5045d2638c29 | 4 | * |
toddouska | 0:5045d2638c29 | 5 | * This file is part of CyaSSL. |
toddouska | 0:5045d2638c29 | 6 | * |
toddouska | 0:5045d2638c29 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
toddouska | 0:5045d2638c29 | 8 | * it under the terms of the GNU General Public License as published by |
toddouska | 0:5045d2638c29 | 9 | * the Free Software Foundation; either version 2 of the License, or |
toddouska | 0:5045d2638c29 | 10 | * (at your option) any later version. |
toddouska | 0:5045d2638c29 | 11 | * |
toddouska | 0:5045d2638c29 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
toddouska | 0:5045d2638c29 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
toddouska | 0:5045d2638c29 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
toddouska | 0:5045d2638c29 | 15 | * GNU General Public License for more details. |
toddouska | 0:5045d2638c29 | 16 | * |
toddouska | 0:5045d2638c29 | 17 | * You should have received a copy of the GNU General Public License |
toddouska | 0:5045d2638c29 | 18 | * along with this program; if not, write to the Free Software |
toddouska | 0:5045d2638c29 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
toddouska | 0:5045d2638c29 | 20 | */ |
toddouska | 0:5045d2638c29 | 21 | |
toddouska | 0:5045d2638c29 | 22 | |
toddouska | 0:5045d2638c29 | 23 | /* on HPUX 11 you may need to install /dev/random see |
toddouska | 0:5045d2638c29 | 24 | http://h20293.www2.hp.com/portal/swdepot/displayProductInfo.do?productNumber=KRNG11I |
toddouska | 0:5045d2638c29 | 25 | |
toddouska | 0:5045d2638c29 | 26 | */ |
toddouska | 0:5045d2638c29 | 27 | |
toddouska | 0:5045d2638c29 | 28 | #include "random.h" |
toddouska | 0:5045d2638c29 | 29 | #include "error.h" |
toddouska | 0:5045d2638c29 | 30 | |
toddouska | 0:5045d2638c29 | 31 | |
toddouska | 0:5045d2638c29 | 32 | #if defined(USE_WINDOWS_API) |
toddouska | 0:5045d2638c29 | 33 | #define _WIN32_WINNT 0x0400 |
toddouska | 0:5045d2638c29 | 34 | #include <windows.h> |
toddouska | 0:5045d2638c29 | 35 | #include <wincrypt.h> |
toddouska | 0:5045d2638c29 | 36 | #else |
toddouska | 0:5045d2638c29 | 37 | #ifndef NO_DEV_RANDOM |
toddouska | 0:5045d2638c29 | 38 | #include <fcntl.h> |
toddouska | 0:5045d2638c29 | 39 | #include <unistd.h> |
toddouska | 0:5045d2638c29 | 40 | #else |
toddouska | 0:5045d2638c29 | 41 | /* include headers that may be needed to get good seed */ |
toddouska | 0:5045d2638c29 | 42 | #endif |
toddouska | 0:5045d2638c29 | 43 | #endif /* USE_WINDOWS_API */ |
toddouska | 0:5045d2638c29 | 44 | |
toddouska | 0:5045d2638c29 | 45 | |
toddouska | 0:5045d2638c29 | 46 | |
toddouska | 0:5045d2638c29 | 47 | /* Get seed and key cipher */ |
toddouska | 0:5045d2638c29 | 48 | int InitRng(RNG* rng) |
toddouska | 0:5045d2638c29 | 49 | { |
toddouska | 0:5045d2638c29 | 50 | byte key[32]; |
toddouska | 0:5045d2638c29 | 51 | byte junk[256]; |
toddouska | 0:5045d2638c29 | 52 | |
toddouska | 0:5045d2638c29 | 53 | int ret = GenerateSeed(&rng->seed, key, sizeof(key)); |
toddouska | 0:5045d2638c29 | 54 | |
toddouska | 0:5045d2638c29 | 55 | if (ret == 0) { |
toddouska | 0:5045d2638c29 | 56 | Arc4SetKey(&rng->cipher, key, sizeof(key)); |
toddouska | 0:5045d2638c29 | 57 | RNG_GenerateBlock(rng, junk, sizeof(junk)); /* rid initial state */ |
toddouska | 0:5045d2638c29 | 58 | } |
toddouska | 0:5045d2638c29 | 59 | |
toddouska | 0:5045d2638c29 | 60 | return ret; |
toddouska | 0:5045d2638c29 | 61 | } |
toddouska | 0:5045d2638c29 | 62 | |
toddouska | 0:5045d2638c29 | 63 | |
toddouska | 0:5045d2638c29 | 64 | /* place a generated block in output */ |
toddouska | 0:5045d2638c29 | 65 | void RNG_GenerateBlock(RNG* rng, byte* output, word32 sz) |
toddouska | 0:5045d2638c29 | 66 | { |
toddouska | 0:5045d2638c29 | 67 | XMEMSET(output, 0, sz); |
toddouska | 0:5045d2638c29 | 68 | Arc4Process(&rng->cipher, output, output, sz); |
toddouska | 0:5045d2638c29 | 69 | } |
toddouska | 0:5045d2638c29 | 70 | |
toddouska | 0:5045d2638c29 | 71 | |
toddouska | 0:5045d2638c29 | 72 | byte RNG_GenerateByte(RNG* rng) |
toddouska | 0:5045d2638c29 | 73 | { |
toddouska | 0:5045d2638c29 | 74 | byte b; |
toddouska | 0:5045d2638c29 | 75 | RNG_GenerateBlock(rng, &b, 1); |
toddouska | 0:5045d2638c29 | 76 | |
toddouska | 0:5045d2638c29 | 77 | return b; |
toddouska | 0:5045d2638c29 | 78 | } |
toddouska | 0:5045d2638c29 | 79 | |
toddouska | 0:5045d2638c29 | 80 | |
toddouska | 0:5045d2638c29 | 81 | #if defined(USE_WINDOWS_API) |
toddouska | 0:5045d2638c29 | 82 | |
toddouska | 0:5045d2638c29 | 83 | |
toddouska | 0:5045d2638c29 | 84 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
toddouska | 0:5045d2638c29 | 85 | { |
toddouska | 0:5045d2638c29 | 86 | if(!CryptAcquireContext(&os->handle, 0, 0, PROV_RSA_FULL, |
toddouska | 0:5045d2638c29 | 87 | CRYPT_VERIFYCONTEXT)) |
toddouska | 0:5045d2638c29 | 88 | return WINCRYPT_E; |
toddouska | 0:5045d2638c29 | 89 | |
toddouska | 0:5045d2638c29 | 90 | if (!CryptGenRandom(os->handle, sz, output)) |
toddouska | 0:5045d2638c29 | 91 | return CRYPTGEN_E; |
toddouska | 0:5045d2638c29 | 92 | |
toddouska | 0:5045d2638c29 | 93 | CryptReleaseContext(os->handle, 0); |
toddouska | 0:5045d2638c29 | 94 | |
toddouska | 0:5045d2638c29 | 95 | return 0; |
toddouska | 0:5045d2638c29 | 96 | } |
toddouska | 0:5045d2638c29 | 97 | |
toddouska | 0:5045d2638c29 | 98 | |
toddouska | 0:5045d2638c29 | 99 | #elif defined(THREADX) |
toddouska | 0:5045d2638c29 | 100 | |
toddouska | 0:5045d2638c29 | 101 | #include "rtprand.h" /* rtp_rand () */ |
toddouska | 0:5045d2638c29 | 102 | #include "rtptime.h" /* rtp_get_system_msec() */ |
toddouska | 0:5045d2638c29 | 103 | |
toddouska | 0:5045d2638c29 | 104 | |
toddouska | 0:5045d2638c29 | 105 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
toddouska | 0:5045d2638c29 | 106 | { |
toddouska | 0:5045d2638c29 | 107 | int i; |
toddouska | 0:5045d2638c29 | 108 | rtp_srand(rtp_get_system_msec()); |
toddouska | 0:5045d2638c29 | 109 | |
toddouska | 0:5045d2638c29 | 110 | for (i = 0; i < sz; i++ ) { |
toddouska | 0:5045d2638c29 | 111 | output[i] = rtp_rand() % 256; |
toddouska | 0:5045d2638c29 | 112 | if ( (i % 8) == 7) |
toddouska | 0:5045d2638c29 | 113 | rtp_srand(rtp_get_system_msec()); |
toddouska | 0:5045d2638c29 | 114 | } |
toddouska | 0:5045d2638c29 | 115 | |
toddouska | 0:5045d2638c29 | 116 | return 0; |
toddouska | 0:5045d2638c29 | 117 | } |
toddouska | 0:5045d2638c29 | 118 | |
toddouska | 0:5045d2638c29 | 119 | |
toddouska | 0:5045d2638c29 | 120 | #elif defined(MICRIUM) |
toddouska | 0:5045d2638c29 | 121 | |
toddouska | 0:5045d2638c29 | 122 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
toddouska | 0:5045d2638c29 | 123 | { |
toddouska | 0:5045d2638c29 | 124 | #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) |
toddouska | 0:5045d2638c29 | 125 | NetSecure_InitSeed(output, sz); |
toddouska | 0:5045d2638c29 | 126 | #endif |
toddouska | 0:5045d2638c29 | 127 | return 0; |
toddouska | 0:5045d2638c29 | 128 | } |
toddouska | 0:5045d2638c29 | 129 | |
toddouska | 0:5045d2638c29 | 130 | |
toddouska | 0:5045d2638c29 | 131 | #elif defined(MBED) |
toddouska | 0:5045d2638c29 | 132 | |
toddouska | 0:5045d2638c29 | 133 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
toddouska | 0:5045d2638c29 | 134 | { |
toddouska | 0:5045d2638c29 | 135 | int i; |
toddouska | 0:5045d2638c29 | 136 | |
toddouska | 0:5045d2638c29 | 137 | for (i = 0; i < sz; i++) |
toddouska | 0:5045d2638c29 | 138 | output[i] = i; |
toddouska | 0:5045d2638c29 | 139 | |
toddouska | 0:5045d2638c29 | 140 | return 0; |
toddouska | 0:5045d2638c29 | 141 | } |
toddouska | 0:5045d2638c29 | 142 | |
toddouska | 0:5045d2638c29 | 143 | |
toddouska | 0:5045d2638c29 | 144 | #elif defined(NO_DEV_RANDOM) |
toddouska | 0:5045d2638c29 | 145 | |
toddouska | 0:5045d2638c29 | 146 | #error "you need to write an os specific GenerateSeed() here" |
toddouska | 0:5045d2638c29 | 147 | |
toddouska | 0:5045d2638c29 | 148 | |
toddouska | 0:5045d2638c29 | 149 | #else /* !USE_WINDOWS_API && !THREADX && !MICRIUM && !NO_DEV_RANDOM */ |
toddouska | 0:5045d2638c29 | 150 | |
toddouska | 0:5045d2638c29 | 151 | |
toddouska | 0:5045d2638c29 | 152 | /* may block */ |
toddouska | 0:5045d2638c29 | 153 | int GenerateSeed(OS_Seed* os, byte* output, word32 sz) |
toddouska | 0:5045d2638c29 | 154 | { |
toddouska | 0:5045d2638c29 | 155 | os->fd = open("/dev/urandom",O_RDONLY); |
toddouska | 0:5045d2638c29 | 156 | if (os->fd == -1) { |
toddouska | 0:5045d2638c29 | 157 | /* may still have /dev/random */ |
toddouska | 0:5045d2638c29 | 158 | os->fd = open("/dev/random",O_RDONLY); |
toddouska | 0:5045d2638c29 | 159 | if (os->fd == -1) |
toddouska | 0:5045d2638c29 | 160 | return OPEN_RAN_E; |
toddouska | 0:5045d2638c29 | 161 | } |
toddouska | 0:5045d2638c29 | 162 | |
toddouska | 0:5045d2638c29 | 163 | while (sz) { |
toddouska | 0:5045d2638c29 | 164 | int len = read(os->fd, output, sz); |
toddouska | 0:5045d2638c29 | 165 | if (len == -1) |
toddouska | 0:5045d2638c29 | 166 | return READ_RAN_E; |
toddouska | 0:5045d2638c29 | 167 | |
toddouska | 0:5045d2638c29 | 168 | sz -= len; |
toddouska | 0:5045d2638c29 | 169 | output += len; |
toddouska | 0:5045d2638c29 | 170 | |
toddouska | 0:5045d2638c29 | 171 | if (sz) |
toddouska | 0:5045d2638c29 | 172 | #ifdef BLOCKING |
toddouska | 0:5045d2638c29 | 173 | sleep(0); /* context switch */ |
toddouska | 0:5045d2638c29 | 174 | #else |
toddouska | 0:5045d2638c29 | 175 | return RAN_BLOCK_E; |
toddouska | 0:5045d2638c29 | 176 | #endif |
toddouska | 0:5045d2638c29 | 177 | } |
toddouska | 0:5045d2638c29 | 178 | close(os->fd); |
toddouska | 0:5045d2638c29 | 179 | |
toddouska | 0:5045d2638c29 | 180 | return 0; |
toddouska | 0:5045d2638c29 | 181 | } |
toddouska | 0:5045d2638c29 | 182 | |
toddouska | 0:5045d2638c29 | 183 | #endif /* USE_WINDOWS_API */ |
toddouska | 0:5045d2638c29 | 184 |