Add tests for RNDR and combine tests with RDRAND

Add test cases for RNDR and RNDRRS. Combine tests for RDRAND and RNDR to
share common logic.

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15361)
This commit is contained in:
Orr Toledano 2021-05-06 18:46:27 +00:00 committed by Tomas Mraz
parent eb28fda797
commit 1f8ce0c9fa
3 changed files with 46 additions and 18 deletions

View File

@ -584,7 +584,7 @@ IF[{- !$disabled{tests} -}]
IF[1] IF[1]
PROGRAMS{noinst}=asn1_internal_test modes_internal_test x509_internal_test \ PROGRAMS{noinst}=asn1_internal_test modes_internal_test x509_internal_test \
tls13encryptiontest wpackettest ctype_internal_test \ tls13encryptiontest wpackettest ctype_internal_test \
rdrand_sanitytest property_test ideatest rsa_mp_test \ rdcpu_sanitytest property_test ideatest rsa_mp_test \
rsa_sp800_56b_test bn_internal_test ecdsatest rsa_test \ rsa_sp800_56b_test bn_internal_test ecdsatest rsa_test \
rc2test rc4test rc5test hmactest ffc_internal_test \ rc2test rc4test rc5test hmactest ffc_internal_test \
asn1_dsa_internal_test dsatest dsa_no_digest_size_test \ asn1_dsa_internal_test dsatest dsa_no_digest_size_test \
@ -737,9 +737,9 @@ IF[{- !$disabled{tests} -}]
INCLUDE[rc4test]=../include ../apps/include INCLUDE[rc4test]=../include ../apps/include
DEPEND[rc4test]=../libcrypto.a libtestutil.a DEPEND[rc4test]=../libcrypto.a libtestutil.a
SOURCE[rdrand_sanitytest]=rdrand_sanitytest.c SOURCE[rdcpu_sanitytest]=rdcpu_sanitytest.c
INCLUDE[rdrand_sanitytest]=../include ../apps/include INCLUDE[rdcpu_sanitytest]=../include ../apps/include ../crypto
DEPEND[rdrand_sanitytest]=../libcrypto.a libtestutil.a DEPEND[rdcpu_sanitytest]=../libcrypto.a libtestutil.a
SOURCE[rsa_sp800_56b_test]=rsa_sp800_56b_test.c SOURCE[rsa_sp800_56b_test]=rsa_sp800_56b_test.c
INCLUDE[rsa_sp800_56b_test]=.. ../include ../crypto/rsa ../apps/include INCLUDE[rsa_sp800_56b_test]=.. ../include ../crypto/rsa ../apps/include

View File

@ -16,10 +16,24 @@
#if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \ #if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
defined(__x86_64) || defined(__x86_64__) || \ defined(__x86_64) || defined(__x86_64__) || \
defined(_M_AMD64) || defined (_M_X64)) && defined(OPENSSL_CPUID_OBJ) defined(_M_AMD64) || defined (_M_X64)) && defined(OPENSSL_CPUID_OBJ)
# define IS_X_86 1
size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len); size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);
size_t OPENSSL_ia32_rdseed_bytes(unsigned char *buf, size_t len); size_t OPENSSL_ia32_rdseed_bytes(unsigned char *buf, size_t len);
#else
# define IS_X_86 0
#endif
#if defined(__aarch64__)
# define IS_AARCH_64 1
# include "arm_arch.h"
size_t OPENSSL_rndr_bytes(unsigned char *buf, size_t len);
size_t OPENSSL_rndrrs_bytes(unsigned char *buf, size_t len);
#else
# define IS_AARCH_64 0
#endif
#if (IS_X_86 || IS_AARCH_64)
static int sanity_check_bytes(size_t (*rng)(unsigned char *, size_t), static int sanity_check_bytes(size_t (*rng)(unsigned char *, size_t),
int rounds, int min_failures, int max_retries, int max_zero_words) int rounds, int min_failures, int max_retries, int max_zero_words)
{ {
@ -76,7 +90,9 @@ static int sanity_check_bytes(size_t (*rng)(unsigned char *, size_t),
end: end:
return testresult; return testresult;
} }
#endif
#if IS_X_86
static int sanity_check_rdrand_bytes(void) static int sanity_check_rdrand_bytes(void)
{ {
return sanity_check_bytes(OPENSSL_ia32_rdrand_bytes, 1000, 0, 10, 10); return sanity_check_bytes(OPENSSL_ia32_rdrand_bytes, 1000, 0, 10, 10);
@ -92,11 +108,24 @@ static int sanity_check_rdseed_bytes(void)
*/ */
return sanity_check_bytes(OPENSSL_ia32_rdseed_bytes, 1000, 1, 10000, 10); return sanity_check_bytes(OPENSSL_ia32_rdseed_bytes, 1000, 1, 10000, 10);
} }
#elif IS_AARCH_64
static int sanity_check_rndr_bytes(void)
{
return sanity_check_bytes(OPENSSL_rndr_bytes, 1000, 0, 10, 10);
}
static int sanity_check_rndrrs_bytes(void)
{
return sanity_check_bytes(OPENSSL_rndrrs_bytes, 1000, 0, 10000, 10);
}
#endif
int setup_tests(void) int setup_tests(void)
{ {
#if (IS_X_86 || IS_AARCH_64)
OPENSSL_cpuid_setup(); OPENSSL_cpuid_setup();
# if IS_X_86
int have_rdseed = (OPENSSL_ia32cap_P[2] & (1 << 18)) != 0; int have_rdseed = (OPENSSL_ia32cap_P[2] & (1 << 18)) != 0;
int have_rdrand = (OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0; int have_rdrand = (OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0;
@ -107,16 +136,15 @@ int setup_tests(void)
if (have_rdseed) { if (have_rdseed) {
ADD_TEST(sanity_check_rdseed_bytes); ADD_TEST(sanity_check_rdseed_bytes);
} }
# elif IS_AARCH_64
int have_rndr_rndrrs = (OPENSSL_armcap_P & (1 << 8)) != 0;
return 1; if (have_rndr_rndrrs) {
} ADD_TEST(sanity_check_rndr_bytes);
ADD_TEST(sanity_check_rndrrs_bytes);
}
#else # endif
int setup_tests(void)
{
return 1;
}
#endif #endif
return 1;
}

View File

@ -13,10 +13,10 @@ use OpenSSL::Test; # get 'plan'
use OpenSSL::Test::Simple; use OpenSSL::Test::Simple;
use OpenSSL::Test::Utils; use OpenSSL::Test::Utils;
setup("test_rdrand_sanity"); setup("test_rdcpu_sanity");
# We also need static builds to be enabled even on linux # We also need static builds to be enabled even on linux
plan skip_all => "This test is unsupported if static builds are not enabled" plan skip_all => "This test is unsupported if static builds are not enabled"
if disabled("static"); if disabled("static");
simple_test("test_rdrand_sanity", "rdrand_sanitytest"); simple_test("test_rdcpu_sanity", "rdcpu_sanitytest");