Skip to content

Commit

Permalink
FEAT: added new system function Random_Bytes, which fills destination…
Browse files Browse the repository at this point in the history
… buffer with given number of random bytes in OS independent way.
  • Loading branch information
Oldes committed Sep 25, 2018
1 parent 73496e9 commit 7d9a21d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 51 deletions.
31 changes: 31 additions & 0 deletions src/core/f-random.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,34 @@ static REBI64 ran_arr_cycle()
if (s < 0.0) s += 1.8446744073709552e19;
return (s * t) * r;
}

/***********************************************************************
**
*/ void Random_Bytes(REBYTE* dest, REBCNT length, REBOOL no_zeros)
/*
** Fills destination buffer with random bytes.
**
***********************************************************************/
{
REBI64 rnd;
REBCNT k = length / 8;
REBCNT r = length % 8;
REBYTE *cp = dest;

for (REBCNT i = 0; i < k; i++) {
rnd = Random_Int(TRUE);
memcpy(cp, (REBYTE*)&rnd, 8);
cp += 8;
}
if (r > 0) {
rnd = Random_Int(TRUE);
memcpy(cp, (REBYTE*)&rnd, r);
}
if(no_zeros) {
// make result without null bytes
for (REBCNT i = 0; i < length; i++) {
while (dest[i] == 0)
dest[i] = (u8)(rand());
}
}
}
3 changes: 2 additions & 1 deletion src/core/u-dh.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Simple implementation of Diffie-Hellman algorithm (c) 2013 Richard Smolak
The code uses Bigint implementation Copyright (c) 2007, Cameron Rich
*/

#include "sys-core.h"
#include "sys-dh.h"

void DH_generate_key(DH_CTX *dh_ctx)
Expand All @@ -16,7 +17,7 @@ void DH_generate_key(DH_CTX *dh_ctx)
bi_permanent(g);

//generate private key X
get_random_NZ(len, dh_ctx->x);
Random_Bytes(dh_ctx->x, len, 1);
x = bi_import(bi_ctx, dh_ctx->x, len);
bi_permanent(x);

Expand Down
45 changes: 2 additions & 43 deletions src/core/u-rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/

#include "reb-config.h"
#include "sys-core.h"

//#include <stdio.h>
#include <string.h>
Expand All @@ -47,48 +48,6 @@
#endif

#include "sys-rsa.h"
#ifdef TO_WINDOWS
#include <windows.h>
#include <wincrypt.h>
#else
#include <fcntl.h>
#endif


#ifdef TO_WINDOWS
static HCRYPTPROV gCryptProv;
#else
static int rng_fd = -1;
#endif

/**
* Set a series of bytes with a random number. Individual bytes can be 0
*/
void get_random(int num_rand_bytes, uint8_t *rand_data)
{
#ifdef TO_WINDOWS
/* use Microsoft Crypto Libraries */
CryptGenRandom(gCryptProv, num_rand_bytes, rand_data);
#else
if (rng_fd == -1) rng_fd = open("/dev/urandom", O_RDONLY);
read(rng_fd, rand_data, num_rand_bytes);
#endif
}

/**
* Set a series of bytes with a random number. Individual bytes are not zero.
*/
void get_random_NZ(int num_rand_bytes, uint8_t *rand_data)
{
int i;
get_random(num_rand_bytes, rand_data);

for (i = 0; i < num_rand_bytes; i++)
{
while (rand_data[i] == 0) /* can't be 0 */
rand_data[i] = (uint8_t)(rand());
}
}

void RSA_priv_key_new(RSA_CTX **ctx,
const uint8_t *modulus, int mod_len,
Expand Down Expand Up @@ -327,7 +286,7 @@ int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
else /* randomize the encryption padding with non-zero bytes */
{
out_data[1] = 2;
get_random_NZ(num_pads_needed, &out_data[2]);
Random_Bytes(&out_data[2], num_pads_needed, 1);
}

out_data[2+num_pads_needed] = 0;
Expand Down
7 changes: 0 additions & 7 deletions src/include/sys-rsa.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ int RSA_encrypt(const RSA_CTX *ctx, const uint8_t *in_data, uint16_t in_len,
void RSA_print(const RSA_CTX *ctx);
#endif

/**************************************************************************
* RNG declarations
**************************************************************************/
void get_random(int num_rand_bytes, uint8_t *rand_data);
void get_random_NZ(int num_rand_bytes, uint8_t *rand_data);


#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 7d9a21d

Please sign in to comment.