Skip to content

Commit

Permalink
tls: add cryptorand support for TLS module, add some more comments to…
Browse files Browse the repository at this point in the history
… existing code
  • Loading branch information
henningw committed Oct 7, 2019
1 parent 4e6c8fe commit 8539b7c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/modules/tls/tls_rand.c
Expand Up @@ -16,7 +16,11 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


/*
* OpenSSL docs:
* https://www.openssl.org/docs/man1.1.1/man7/RAND.html
* https://www.openssl.org/docs/man1.1.1/man3/RAND_set_rand_method.html
*/

#include <stdlib.h>
#include <string.h>
Expand All @@ -28,7 +32,12 @@
#include "../../core/dprint.h"
#include "../../core/rand/kam_rand.h"
#include "../../core/rand/fastrand.h"
#include "../../core/rand/fortuna/random.h"

/*
* Implementation for tests with system library PNRG,
* do not use this in production.
*/
static int ksr_krand_bytes(unsigned char *outdata, int size)
{
int r;
Expand Down Expand Up @@ -76,6 +85,11 @@ const RAND_METHOD *RAND_ksr_krand_method(void)
return &_ksr_krand_method;
}

/*
* Implementation for tests with fastrand implementation,
* better as system library but still not secure enough.
* Do not use this in production.y
*/
static int ksr_fastrand_bytes(unsigned char *outdata, int size)
{
int r;
Expand Down Expand Up @@ -123,4 +137,47 @@ const RAND_METHOD *RAND_ksr_fastrand_method(void)
return &_ksr_fastrand_method;
}

/*
* Implementation with Fortuna cryptographic PRNG.
* We are not strictly implementing the OpenSSL API here - we will
* not return an error if the PRNG has not been seeded with enough
* randomness to ensure an unpredictable byte sequence.
*/
static int ksr_cryptorand_bytes(unsigned char *outdata, int size)
{
if (size < 0) {
return 0;
} else if (size == 0) {
return 1;
}

sr_get_pseudo_random_bytes(outdata, size);
return 1;
}

static int ksr_cryptorand_status(void)
{
return 1;
}

/*
* We don't have a dedicated function for pseudo-random
* bytes, just use the secure version as well for it.
*/
const RAND_METHOD _ksr_cryptorand_method = {
NULL,
ksr_cryptorand_bytes,
NULL,
NULL,
ksr_cryptorand_bytes,
ksr_cryptorand_status
};

const RAND_METHOD *RAND_ksr_cryptorand_method(void)
{
return &_ksr_cryptorand_method;
}



#endif /* OPENSSL_VERSION_NUMBER >= 0x10100000L */
1 change: 1 addition & 0 deletions src/modules/tls/tls_rand.h
Expand Up @@ -27,6 +27,7 @@

const RAND_METHOD *RAND_ksr_krand_method(void);
const RAND_METHOD *RAND_ksr_fastrand_method(void);
const RAND_METHOD *RAND_ksr_cryptorand_method(void);

#endif /* OPENSSL_VERSION_NUMBER >= 0x10100000L */
#endif

0 comments on commit 8539b7c

Please sign in to comment.