From fe7c4e39933443b423d22085d2dc23ec4d93c7ce Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Tue, 8 Sep 2015 21:13:17 +0200 Subject: [PATCH 1/5] add ltc_rng function pointer the idea is to be able to easily provide a plug-in rng for a specific platform without the need to touch the library. --- src/headers/tomcrypt_prng.h | 3 +++ src/misc/crypt/crypt_prng_rng_descriptor.c | 13 +++++++++++++ src/prngs/rng_get_bytes.c | 7 +++++++ 3 files changed, 23 insertions(+) create mode 100644 src/misc/crypt/crypt_prng_rng_descriptor.c diff --git a/src/headers/tomcrypt_prng.h b/src/headers/tomcrypt_prng.h index 4880b05..2bfe820 100644 --- a/src/headers/tomcrypt_prng.h +++ b/src/headers/tomcrypt_prng.h @@ -193,6 +193,9 @@ unsigned long rng_get_bytes(unsigned char *out, int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void)); +extern unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen, + void (*callback)(void)); + /* $Source$ */ /* $Revision$ */ diff --git a/src/misc/crypt/crypt_prng_rng_descriptor.c b/src/misc/crypt/crypt_prng_rng_descriptor.c new file mode 100644 index 0000000..14f36ff --- /dev/null +++ b/src/misc/crypt/crypt_prng_rng_descriptor.c @@ -0,0 +1,13 @@ +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ +#include "tomcrypt.h" + +unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void)); diff --git a/src/prngs/rng_get_bytes.c b/src/prngs/rng_get_bytes.c index f0536f6..7430feb 100644 --- a/src/prngs/rng_get_bytes.c +++ b/src/prngs/rng_get_bytes.c @@ -135,6 +135,13 @@ unsigned long rng_get_bytes(unsigned char *out, unsigned long outlen, LTC_ARGCHK(out != NULL); + if (ltc_rng) { + x = ltc_rng(out, outlen, callback); + if (x != 0) { + return x; + } + } + #if defined(_WIN32) || defined(_WIN32_WCE) x = rng_win32(out, outlen, callback); if (x != 0) { return x; } #elif defined(LTC_DEVRANDOM) From fcae7e2c49274ad2327f39f493720394685a15eb Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Fri, 11 Sep 2015 00:30:02 +0200 Subject: [PATCH 2/5] test the ltc_rng --- testprof/x86_prof.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/testprof/x86_prof.c b/testprof/x86_prof.c index 46ae43a..4aee85c 100644 --- a/testprof/x86_prof.c +++ b/testprof/x86_prof.c @@ -302,8 +302,23 @@ static void _unregister_all(void) #endif } /* _cleanup() */ +static unsigned long my_test_rng_read; + +static unsigned long my_test_rng(unsigned char *buf, unsigned long len, + void (*callback)(void)) +{ + unsigned long n; + LTC_UNUSED_PARAM(callback); + for (n = 0; n < len; ++n) { + buf[n] = 4; + } + my_test_rng_read += n; + return n; +} + void reg_algs(void) { + unsigned long before; int err; atexit(_unregister_all); @@ -441,6 +456,21 @@ register_prng(&rc4_desc); register_prng(&sober128_desc); #endif + ltc_rng = my_test_rng; + + before = my_test_rng_read; + if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) { + fprintf(stderr, "rng_make_prng with 'my_test_rng' failed: %s\n", error_to_string(err)); + exit(EXIT_FAILURE); + } + + if (before == my_test_rng_read) { + fprintf(stderr, "somehow there was no read from the ltc_rng! %lu == %lu\n", before, my_test_rng_read); + exit(EXIT_FAILURE); + } + + ltc_rng = NULL; + if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) { fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err)); exit(EXIT_FAILURE); From cd08a8cec3854670082e3715be32c442b0fa8650 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Wed, 15 Mar 2017 22:12:49 +0100 Subject: [PATCH 3/5] disable ltc_rng by default --- src/headers/tomcrypt_custom.h | 3 +++ src/headers/tomcrypt_prng.h | 2 ++ src/misc/crypt/crypt.c | 3 +++ src/misc/crypt/crypt_prng_rng_descriptor.c | 2 ++ src/prngs/rng_get_bytes.c | 2 ++ testprof/x86_prof.c | 6 ++++++ 6 files changed, 18 insertions(+) diff --git a/src/headers/tomcrypt_custom.h b/src/headers/tomcrypt_custom.h index 33e4bc0..6d6f4f3 100644 --- a/src/headers/tomcrypt_custom.h +++ b/src/headers/tomcrypt_custom.h @@ -304,6 +304,9 @@ /* rng_make_prng() */ #define LTC_RNG_MAKE_PRNG +/* enable the ltc_rng hook to integrate e.g. embedded hardware RNG's easily */ +/* #define LTC_PRNG_ENABLE_LTC_RNG */ + #endif /* LTC_NO_PRNGS */ #ifdef LTC_YARROW diff --git a/src/headers/tomcrypt_prng.h b/src/headers/tomcrypt_prng.h index 2bfe820..dc2cc7e 100644 --- a/src/headers/tomcrypt_prng.h +++ b/src/headers/tomcrypt_prng.h @@ -193,8 +193,10 @@ unsigned long rng_get_bytes(unsigned char *out, int rng_make_prng(int bits, int wprng, prng_state *prng, void (*callback)(void)); +#ifdef LTC_PRNG_ENABLE_LTC_RNG extern unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void)); +#endif /* $Source$ */ diff --git a/src/misc/crypt/crypt.c b/src/misc/crypt/crypt.c index 7d03cfa..aef292f 100644 --- a/src/misc/crypt/crypt.c +++ b/src/misc/crypt/crypt.c @@ -371,6 +371,9 @@ const char *crypt_build_settings = #if defined(LTC_RNG_MAKE_PRNG) " LTC_RNG_MAKE_PRNG " #endif +#if defined(LTC_PRNG_ENABLE_LTC_RNG) + " LTC_PRNG_ENABLE_LTC_RNG " +#endif #if defined(LTC_HASH_HELPERS) " LTC_HASH_HELPERS " #endif diff --git a/src/misc/crypt/crypt_prng_rng_descriptor.c b/src/misc/crypt/crypt_prng_rng_descriptor.c index 14f36ff..bf31781 100644 --- a/src/misc/crypt/crypt_prng_rng_descriptor.c +++ b/src/misc/crypt/crypt_prng_rng_descriptor.c @@ -10,4 +10,6 @@ */ #include "tomcrypt.h" +#ifdef LTC_PRNG_ENABLE_LTC_RNG unsigned long (*ltc_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void)); +#endif diff --git a/src/prngs/rng_get_bytes.c b/src/prngs/rng_get_bytes.c index 7430feb..28e8585 100644 --- a/src/prngs/rng_get_bytes.c +++ b/src/prngs/rng_get_bytes.c @@ -135,12 +135,14 @@ unsigned long rng_get_bytes(unsigned char *out, unsigned long outlen, LTC_ARGCHK(out != NULL); +#ifdef LTC_PRNG_ENABLE_LTC_RNG if (ltc_rng) { x = ltc_rng(out, outlen, callback); if (x != 0) { return x; } } +#endif #if defined(_WIN32) || defined(_WIN32_WCE) x = rng_win32(out, outlen, callback); if (x != 0) { return x; } diff --git a/testprof/x86_prof.c b/testprof/x86_prof.c index 4aee85c..2d4700f 100644 --- a/testprof/x86_prof.c +++ b/testprof/x86_prof.c @@ -302,6 +302,8 @@ static void _unregister_all(void) #endif } /* _cleanup() */ +#ifdef LTC_PRNG_ENABLE_LTC_RNG + static unsigned long my_test_rng_read; static unsigned long my_test_rng(unsigned char *buf, unsigned long len, @@ -316,6 +318,8 @@ static unsigned long my_test_rng(unsigned char *buf, unsigned long len, return n; } +#endif + void reg_algs(void) { unsigned long before; @@ -456,6 +460,7 @@ register_prng(&rc4_desc); register_prng(&sober128_desc); #endif +#ifdef LTC_PRNG_ENABLE_LTC_RNG ltc_rng = my_test_rng; before = my_test_rng_read; @@ -470,6 +475,7 @@ register_prng(&sober128_desc); } ltc_rng = NULL; +#endif if ((err = rng_make_prng(128, find_prng("yarrow"), &yarrow_prng, NULL)) != CRYPT_OK) { fprintf(stderr, "rng_make_prng failed: %s\n", error_to_string(err)); From 27f8e8bf75980d4ab329cde3d6aa7c00ffcb0448 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Wed, 15 Mar 2017 22:19:52 +0100 Subject: [PATCH 4/5] Update makefiles --- libtomcrypt_VS2005.vcproj | 4 ++++ libtomcrypt_VS2008.vcproj | 4 ++++ makefile | 4 ++-- makefile.icc | 4 ++-- makefile.mingw | 4 ++-- makefile.msvc | 4 ++-- makefile.shared | 4 ++-- makefile.unix | 4 ++-- 8 files changed, 20 insertions(+), 12 deletions(-) diff --git a/libtomcrypt_VS2005.vcproj b/libtomcrypt_VS2005.vcproj index 05649eb..fb3a12b 100644 --- a/libtomcrypt_VS2005.vcproj +++ b/libtomcrypt_VS2005.vcproj @@ -1097,6 +1097,10 @@ RelativePath="src\misc\crypt\crypt_prng_is_valid.c" > + + diff --git a/libtomcrypt_VS2008.vcproj b/libtomcrypt_VS2008.vcproj index cf631df..2d9f84f 100644 --- a/libtomcrypt_VS2008.vcproj +++ b/libtomcrypt_VS2008.vcproj @@ -1099,6 +1099,10 @@ RelativePath="src\misc\crypt\crypt_prng_is_valid.c" > + + diff --git a/makefile b/makefile index df60f89..b10e2cb 100644 --- a/makefile +++ b/makefile @@ -95,8 +95,8 @@ src/misc/crypt/crypt_find_hash_oid.o src/misc/crypt/crypt_find_prng.o src/misc/c src/misc/crypt/crypt_hash_descriptor.o src/misc/crypt/crypt_hash_is_valid.o \ src/misc/crypt/crypt_inits.o src/misc/crypt/crypt_ltc_mp_descriptor.o \ src/misc/crypt/crypt_prng_descriptor.o src/misc/crypt/crypt_prng_is_valid.o \ -src/misc/crypt/crypt_register_cipher.o src/misc/crypt/crypt_register_hash.o \ -src/misc/crypt/crypt_register_prng.o src/misc/crypt/crypt_sizes.o \ +src/misc/crypt/crypt_prng_rng_descriptor.o src/misc/crypt/crypt_register_cipher.o \ +src/misc/crypt/crypt_register_hash.o src/misc/crypt/crypt_register_prng.o src/misc/crypt/crypt_sizes.o \ src/misc/crypt/crypt_unregister_cipher.o src/misc/crypt/crypt_unregister_hash.o \ src/misc/crypt/crypt_unregister_prng.o src/misc/error_to_string.o src/misc/hkdf/hkdf.o \ src/misc/hkdf/hkdf_test.o src/misc/mem_neq.o src/misc/pk_get_oid.o src/misc/pkcs5/pkcs_5_1.o \ diff --git a/makefile.icc b/makefile.icc index 97f192d..1456dcc 100644 --- a/makefile.icc +++ b/makefile.icc @@ -152,8 +152,8 @@ src/misc/crypt/crypt_find_hash_oid.o src/misc/crypt/crypt_find_prng.o src/misc/c src/misc/crypt/crypt_hash_descriptor.o src/misc/crypt/crypt_hash_is_valid.o \ src/misc/crypt/crypt_inits.o src/misc/crypt/crypt_ltc_mp_descriptor.o \ src/misc/crypt/crypt_prng_descriptor.o src/misc/crypt/crypt_prng_is_valid.o \ -src/misc/crypt/crypt_register_cipher.o src/misc/crypt/crypt_register_hash.o \ -src/misc/crypt/crypt_register_prng.o src/misc/crypt/crypt_sizes.o \ +src/misc/crypt/crypt_prng_rng_descriptor.o src/misc/crypt/crypt_register_cipher.o \ +src/misc/crypt/crypt_register_hash.o src/misc/crypt/crypt_register_prng.o src/misc/crypt/crypt_sizes.o \ src/misc/crypt/crypt_unregister_cipher.o src/misc/crypt/crypt_unregister_hash.o \ src/misc/crypt/crypt_unregister_prng.o src/misc/error_to_string.o src/misc/hkdf/hkdf.o \ src/misc/hkdf/hkdf_test.o src/misc/mem_neq.o src/misc/pk_get_oid.o src/misc/pkcs5/pkcs_5_1.o \ diff --git a/makefile.mingw b/makefile.mingw index 2aa4735..3af944e 100644 --- a/makefile.mingw +++ b/makefile.mingw @@ -99,8 +99,8 @@ src/misc/crypt/crypt_find_hash_oid.o src/misc/crypt/crypt_find_prng.o src/misc/c src/misc/crypt/crypt_hash_descriptor.o src/misc/crypt/crypt_hash_is_valid.o \ src/misc/crypt/crypt_inits.o src/misc/crypt/crypt_ltc_mp_descriptor.o \ src/misc/crypt/crypt_prng_descriptor.o src/misc/crypt/crypt_prng_is_valid.o \ -src/misc/crypt/crypt_register_cipher.o src/misc/crypt/crypt_register_hash.o \ -src/misc/crypt/crypt_register_prng.o src/misc/crypt/crypt_sizes.o \ +src/misc/crypt/crypt_prng_rng_descriptor.o src/misc/crypt/crypt_register_cipher.o \ +src/misc/crypt/crypt_register_hash.o src/misc/crypt/crypt_register_prng.o src/misc/crypt/crypt_sizes.o \ src/misc/crypt/crypt_unregister_cipher.o src/misc/crypt/crypt_unregister_hash.o \ src/misc/crypt/crypt_unregister_prng.o src/misc/error_to_string.o src/misc/hkdf/hkdf.o \ src/misc/hkdf/hkdf_test.o src/misc/mem_neq.o src/misc/pk_get_oid.o src/misc/pkcs5/pkcs_5_1.o \ diff --git a/makefile.msvc b/makefile.msvc index 915d804..5277a67 100644 --- a/makefile.msvc +++ b/makefile.msvc @@ -57,8 +57,8 @@ src/misc/crypt/crypt_find_hash_oid.obj src/misc/crypt/crypt_find_prng.obj src/mi src/misc/crypt/crypt_hash_descriptor.obj src/misc/crypt/crypt_hash_is_valid.obj \ src/misc/crypt/crypt_inits.obj src/misc/crypt/crypt_ltc_mp_descriptor.obj \ src/misc/crypt/crypt_prng_descriptor.obj src/misc/crypt/crypt_prng_is_valid.obj \ -src/misc/crypt/crypt_register_cipher.obj src/misc/crypt/crypt_register_hash.obj \ -src/misc/crypt/crypt_register_prng.obj src/misc/crypt/crypt_sizes.obj \ +src/misc/crypt/crypt_prng_rng_descriptor.obj src/misc/crypt/crypt_register_cipher.obj \ +src/misc/crypt/crypt_register_hash.obj src/misc/crypt/crypt_register_prng.obj src/misc/crypt/crypt_sizes.obj \ src/misc/crypt/crypt_unregister_cipher.obj src/misc/crypt/crypt_unregister_hash.obj \ src/misc/crypt/crypt_unregister_prng.obj src/misc/error_to_string.obj src/misc/hkdf/hkdf.obj \ src/misc/hkdf/hkdf_test.obj src/misc/mem_neq.obj src/misc/pk_get_oid.obj src/misc/pkcs5/pkcs_5_1.obj \ diff --git a/makefile.shared b/makefile.shared index 8a2fdf0..b7e1e19 100644 --- a/makefile.shared +++ b/makefile.shared @@ -85,8 +85,8 @@ src/misc/crypt/crypt_find_hash_oid.o src/misc/crypt/crypt_find_prng.o src/misc/c src/misc/crypt/crypt_hash_descriptor.o src/misc/crypt/crypt_hash_is_valid.o \ src/misc/crypt/crypt_inits.o src/misc/crypt/crypt_ltc_mp_descriptor.o \ src/misc/crypt/crypt_prng_descriptor.o src/misc/crypt/crypt_prng_is_valid.o \ -src/misc/crypt/crypt_register_cipher.o src/misc/crypt/crypt_register_hash.o \ -src/misc/crypt/crypt_register_prng.o src/misc/crypt/crypt_sizes.o \ +src/misc/crypt/crypt_prng_rng_descriptor.o src/misc/crypt/crypt_register_cipher.o \ +src/misc/crypt/crypt_register_hash.o src/misc/crypt/crypt_register_prng.o src/misc/crypt/crypt_sizes.o \ src/misc/crypt/crypt_unregister_cipher.o src/misc/crypt/crypt_unregister_hash.o \ src/misc/crypt/crypt_unregister_prng.o src/misc/error_to_string.o src/misc/hkdf/hkdf.o \ src/misc/hkdf/hkdf_test.o src/misc/mem_neq.o src/misc/pk_get_oid.o src/misc/pkcs5/pkcs_5_1.o \ diff --git a/makefile.unix b/makefile.unix index ff58801..5b58c44 100644 --- a/makefile.unix +++ b/makefile.unix @@ -93,8 +93,8 @@ src/misc/crypt/crypt_find_hash_oid.o src/misc/crypt/crypt_find_prng.o src/misc/c src/misc/crypt/crypt_hash_descriptor.o src/misc/crypt/crypt_hash_is_valid.o \ src/misc/crypt/crypt_inits.o src/misc/crypt/crypt_ltc_mp_descriptor.o \ src/misc/crypt/crypt_prng_descriptor.o src/misc/crypt/crypt_prng_is_valid.o \ -src/misc/crypt/crypt_register_cipher.o src/misc/crypt/crypt_register_hash.o \ -src/misc/crypt/crypt_register_prng.o src/misc/crypt/crypt_sizes.o \ +src/misc/crypt/crypt_prng_rng_descriptor.o src/misc/crypt/crypt_register_cipher.o \ +src/misc/crypt/crypt_register_hash.o src/misc/crypt/crypt_register_prng.o src/misc/crypt/crypt_sizes.o \ src/misc/crypt/crypt_unregister_cipher.o src/misc/crypt/crypt_unregister_hash.o \ src/misc/crypt/crypt_unregister_prng.o src/misc/error_to_string.o src/misc/hkdf/hkdf.o \ src/misc/hkdf/hkdf_test.o src/misc/mem_neq.o src/misc/pk_get_oid.o src/misc/pkcs5/pkcs_5_1.o \ From 15db3eab5933e4aebf13cb4dc3c5e8dda0c968d7 Mon Sep 17 00:00:00 2001 From: Karel Miko Date: Tue, 21 Mar 2017 20:04:02 +0100 Subject: [PATCH 5/5] fix travis failure --- testprof/x86_prof.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testprof/x86_prof.c b/testprof/x86_prof.c index 2d4700f..fe203a0 100644 --- a/testprof/x86_prof.c +++ b/testprof/x86_prof.c @@ -322,7 +322,9 @@ static unsigned long my_test_rng(unsigned char *buf, unsigned long len, void reg_algs(void) { +#ifdef LTC_PRNG_ENABLE_LTC_RNG unsigned long before; +#endif int err; atexit(_unregister_all);