Skip to content

Commit 7d9a21d

Browse files
committed
FEAT: added new system function Random_Bytes, which fills destination buffer with given number of random bytes in OS independent way.
1 parent 73496e9 commit 7d9a21d

File tree

4 files changed

+35
-51
lines changed

4 files changed

+35
-51
lines changed

src/core/f-random.c

+31
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,34 @@ static REBI64 ran_arr_cycle()
179179
if (s < 0.0) s += 1.8446744073709552e19;
180180
return (s * t) * r;
181181
}
182+
183+
/***********************************************************************
184+
**
185+
*/ void Random_Bytes(REBYTE* dest, REBCNT length, REBOOL no_zeros)
186+
/*
187+
** Fills destination buffer with random bytes.
188+
**
189+
***********************************************************************/
190+
{
191+
REBI64 rnd;
192+
REBCNT k = length / 8;
193+
REBCNT r = length % 8;
194+
REBYTE *cp = dest;
195+
196+
for (REBCNT i = 0; i < k; i++) {
197+
rnd = Random_Int(TRUE);
198+
memcpy(cp, (REBYTE*)&rnd, 8);
199+
cp += 8;
200+
}
201+
if (r > 0) {
202+
rnd = Random_Int(TRUE);
203+
memcpy(cp, (REBYTE*)&rnd, r);
204+
}
205+
if(no_zeros) {
206+
// make result without null bytes
207+
for (REBCNT i = 0; i < length; i++) {
208+
while (dest[i] == 0)
209+
dest[i] = (u8)(rand());
210+
}
211+
}
212+
}

src/core/u-dh.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Simple implementation of Diffie-Hellman algorithm (c) 2013 Richard Smolak
33
The code uses Bigint implementation Copyright (c) 2007, Cameron Rich
44
*/
55

6+
#include "sys-core.h"
67
#include "sys-dh.h"
78

89
void DH_generate_key(DH_CTX *dh_ctx)
@@ -16,7 +17,7 @@ void DH_generate_key(DH_CTX *dh_ctx)
1617
bi_permanent(g);
1718

1819
//generate private key X
19-
get_random_NZ(len, dh_ctx->x);
20+
Random_Bytes(dh_ctx->x, len, 1);
2021
x = bi_import(bi_ctx, dh_ctx->x, len);
2122
bi_permanent(x);
2223

src/core/u-rsa.c

+2-43
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
*/
3535

3636
#include "reb-config.h"
37+
#include "sys-core.h"
3738

3839
//#include <stdio.h>
3940
#include <string.h>
@@ -47,48 +48,6 @@
4748
#endif
4849

4950
#include "sys-rsa.h"
50-
#ifdef TO_WINDOWS
51-
#include <windows.h>
52-
#include <wincrypt.h>
53-
#else
54-
#include <fcntl.h>
55-
#endif
56-
57-
58-
#ifdef TO_WINDOWS
59-
static HCRYPTPROV gCryptProv;
60-
#else
61-
static int rng_fd = -1;
62-
#endif
63-
64-
/**
65-
* Set a series of bytes with a random number. Individual bytes can be 0
66-
*/
67-
void get_random(int num_rand_bytes, uint8_t *rand_data)
68-
{
69-
#ifdef TO_WINDOWS
70-
/* use Microsoft Crypto Libraries */
71-
CryptGenRandom(gCryptProv, num_rand_bytes, rand_data);
72-
#else
73-
if (rng_fd == -1) rng_fd = open("/dev/urandom", O_RDONLY);
74-
read(rng_fd, rand_data, num_rand_bytes);
75-
#endif
76-
}
77-
78-
/**
79-
* Set a series of bytes with a random number. Individual bytes are not zero.
80-
*/
81-
void get_random_NZ(int num_rand_bytes, uint8_t *rand_data)
82-
{
83-
int i;
84-
get_random(num_rand_bytes, rand_data);
85-
86-
for (i = 0; i < num_rand_bytes; i++)
87-
{
88-
while (rand_data[i] == 0) /* can't be 0 */
89-
rand_data[i] = (uint8_t)(rand());
90-
}
91-
}
9251

9352
void RSA_priv_key_new(RSA_CTX **ctx,
9453
const uint8_t *modulus, int mod_len,
@@ -327,7 +286,7 @@ int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
327286
else /* randomize the encryption padding with non-zero bytes */
328287
{
329288
out_data[1] = 2;
330-
get_random_NZ(num_pads_needed, &out_data[2]);
289+
Random_Bytes(&out_data[2], num_pads_needed, 1);
331290
}
332291

333292
out_data[2+num_pads_needed] = 0;

src/include/sys-rsa.h

-7
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,6 @@ int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
104104
void RSA_print(const RSA_CTX *ctx);
105105
#endif
106106

107-
/**************************************************************************
108-
* RNG declarations
109-
**************************************************************************/
110-
void get_random(int num_rand_bytes, uint8_t *rand_data);
111-
void get_random_NZ(int num_rand_bytes, uint8_t *rand_data);
112-
113-
114107
#ifdef __cplusplus
115108
}
116109
#endif

0 commit comments

Comments
 (0)