xref: /freebsd/crypto/openssl/test/rdcpu_sanitytest.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1*e7be843bSPierre Pronchery /*
2*e7be843bSPierre Pronchery  * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
3*e7be843bSPierre Pronchery  *
4*e7be843bSPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e7be843bSPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*e7be843bSPierre Pronchery  * in the file LICENSE in the source distribution or at
7*e7be843bSPierre Pronchery  * https://www.openssl.org/source/license.html
8*e7be843bSPierre Pronchery  */
9*e7be843bSPierre Pronchery 
10*e7be843bSPierre Pronchery #include <stdio.h>
11*e7be843bSPierre Pronchery #include <stdlib.h>
12*e7be843bSPierre Pronchery #include <string.h>
13*e7be843bSPierre Pronchery #include "testutil.h"
14*e7be843bSPierre Pronchery #include "internal/cryptlib.h"
15*e7be843bSPierre Pronchery 
16*e7be843bSPierre Pronchery #if (defined(__i386)   || defined(__i386__)   || defined(_M_IX86) || \
17*e7be843bSPierre Pronchery      defined(__x86_64) || defined(__x86_64__) || \
18*e7be843bSPierre Pronchery      defined(_M_AMD64) || defined (_M_X64)) && defined(OPENSSL_CPUID_OBJ)
19*e7be843bSPierre Pronchery # define IS_X_86 1
20*e7be843bSPierre Pronchery size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);
21*e7be843bSPierre Pronchery size_t OPENSSL_ia32_rdseed_bytes(unsigned char *buf, size_t len);
22*e7be843bSPierre Pronchery #else
23*e7be843bSPierre Pronchery # define IS_X_86 0
24*e7be843bSPierre Pronchery #endif
25*e7be843bSPierre Pronchery 
26*e7be843bSPierre Pronchery #if defined(__aarch64__) && defined(OPENSSL_CPUID_OBJ)
27*e7be843bSPierre Pronchery # define IS_AARCH_64 1
28*e7be843bSPierre Pronchery # include "arm_arch.h"
29*e7be843bSPierre Pronchery 
30*e7be843bSPierre Pronchery size_t OPENSSL_rndr_bytes(unsigned char *buf, size_t len);
31*e7be843bSPierre Pronchery size_t OPENSSL_rndrrs_bytes(unsigned char *buf, size_t len);
32*e7be843bSPierre Pronchery #else
33*e7be843bSPierre Pronchery # define IS_AARCH_64 0
34*e7be843bSPierre Pronchery #endif
35*e7be843bSPierre Pronchery 
36*e7be843bSPierre Pronchery #if (IS_X_86 || IS_AARCH_64)
sanity_check_bytes(size_t (* rng)(unsigned char *,size_t),int rounds,int min_failures,int max_retries,int max_zero_words)37*e7be843bSPierre Pronchery static int sanity_check_bytes(size_t (*rng)(unsigned char *, size_t),
38*e7be843bSPierre Pronchery     int rounds, int min_failures, int max_retries, int max_zero_words)
39*e7be843bSPierre Pronchery {
40*e7be843bSPierre Pronchery     int testresult = 0;
41*e7be843bSPierre Pronchery     unsigned char prior[31] = {0}, buf[31] = {0}, check[7];
42*e7be843bSPierre Pronchery     int failures = 0, zero_words = 0;
43*e7be843bSPierre Pronchery 
44*e7be843bSPierre Pronchery     int i;
45*e7be843bSPierre Pronchery     for (i = 0; i < rounds; i++) {
46*e7be843bSPierre Pronchery         size_t generated = 0;
47*e7be843bSPierre Pronchery 
48*e7be843bSPierre Pronchery         int retry;
49*e7be843bSPierre Pronchery         for (retry = 0; retry < max_retries; retry++) {
50*e7be843bSPierre Pronchery             generated = rng(buf, sizeof(buf));
51*e7be843bSPierre Pronchery             if (generated == sizeof(buf))
52*e7be843bSPierre Pronchery                 break;
53*e7be843bSPierre Pronchery             failures++;
54*e7be843bSPierre Pronchery         }
55*e7be843bSPierre Pronchery 
56*e7be843bSPierre Pronchery         /*-
57*e7be843bSPierre Pronchery          * Verify that we don't have too many unexpected runs of zeroes,
58*e7be843bSPierre Pronchery          * implying that we might be accidentally using the 32-bit RDRAND
59*e7be843bSPierre Pronchery          * instead of the 64-bit one on 64-bit systems.
60*e7be843bSPierre Pronchery          */
61*e7be843bSPierre Pronchery         size_t j;
62*e7be843bSPierre Pronchery         for (j = 0; j < sizeof(buf) - 1; j++) {
63*e7be843bSPierre Pronchery             if (buf[j] == 0 && buf[j+1] == 0) {
64*e7be843bSPierre Pronchery                 zero_words++;
65*e7be843bSPierre Pronchery             }
66*e7be843bSPierre Pronchery         }
67*e7be843bSPierre Pronchery 
68*e7be843bSPierre Pronchery         if (!TEST_int_eq(generated, sizeof(buf)))
69*e7be843bSPierre Pronchery             goto end;
70*e7be843bSPierre Pronchery         if (!TEST_false(!memcmp(prior, buf, sizeof(buf))))
71*e7be843bSPierre Pronchery             goto end;
72*e7be843bSPierre Pronchery 
73*e7be843bSPierre Pronchery         /* Verify that the last 7 bytes of buf aren't all the same value */
74*e7be843bSPierre Pronchery         unsigned char *tail = &buf[sizeof(buf) - sizeof(check)];
75*e7be843bSPierre Pronchery         memset(check, tail[0], 7);
76*e7be843bSPierre Pronchery         if (!TEST_false(!memcmp(check, tail, sizeof(check))))
77*e7be843bSPierre Pronchery             goto end;
78*e7be843bSPierre Pronchery 
79*e7be843bSPierre Pronchery         /* Save the result and make sure it's different next time */
80*e7be843bSPierre Pronchery         memcpy(prior, buf, sizeof(buf));
81*e7be843bSPierre Pronchery     }
82*e7be843bSPierre Pronchery 
83*e7be843bSPierre Pronchery     if (!TEST_int_le(zero_words, max_zero_words))
84*e7be843bSPierre Pronchery         goto end;
85*e7be843bSPierre Pronchery 
86*e7be843bSPierre Pronchery     if (!TEST_int_ge(failures, min_failures))
87*e7be843bSPierre Pronchery         goto end;
88*e7be843bSPierre Pronchery 
89*e7be843bSPierre Pronchery     testresult = 1;
90*e7be843bSPierre Pronchery end:
91*e7be843bSPierre Pronchery     return testresult;
92*e7be843bSPierre Pronchery }
93*e7be843bSPierre Pronchery #endif
94*e7be843bSPierre Pronchery 
95*e7be843bSPierre Pronchery #if IS_X_86
sanity_check_rdrand_bytes(void)96*e7be843bSPierre Pronchery static int sanity_check_rdrand_bytes(void)
97*e7be843bSPierre Pronchery {
98*e7be843bSPierre Pronchery     return sanity_check_bytes(OPENSSL_ia32_rdrand_bytes, 1000, 0, 10, 10);
99*e7be843bSPierre Pronchery }
100*e7be843bSPierre Pronchery 
sanity_check_rdseed_bytes(void)101*e7be843bSPierre Pronchery static int sanity_check_rdseed_bytes(void)
102*e7be843bSPierre Pronchery {
103*e7be843bSPierre Pronchery     /*-
104*e7be843bSPierre Pronchery      * RDSEED may take many retries to succeed; note that this is effectively
105*e7be843bSPierre Pronchery      * multiplied by the 8x retry loop in asm, and failure probabilities are
106*e7be843bSPierre Pronchery      * increased by the fact that we need either 4 or 8 samples depending on
107*e7be843bSPierre Pronchery      * the platform.
108*e7be843bSPierre Pronchery      */
109*e7be843bSPierre Pronchery     return sanity_check_bytes(OPENSSL_ia32_rdseed_bytes, 1000, 1, 10000, 10);
110*e7be843bSPierre Pronchery }
111*e7be843bSPierre Pronchery #elif IS_AARCH_64
sanity_check_rndr_bytes(void)112*e7be843bSPierre Pronchery static int sanity_check_rndr_bytes(void)
113*e7be843bSPierre Pronchery {
114*e7be843bSPierre Pronchery     return sanity_check_bytes(OPENSSL_rndr_bytes, 1000, 0, 10, 10);
115*e7be843bSPierre Pronchery }
116*e7be843bSPierre Pronchery 
sanity_check_rndrrs_bytes(void)117*e7be843bSPierre Pronchery static int sanity_check_rndrrs_bytes(void)
118*e7be843bSPierre Pronchery {
119*e7be843bSPierre Pronchery     return sanity_check_bytes(OPENSSL_rndrrs_bytes, 1000, 0, 10000, 10);
120*e7be843bSPierre Pronchery }
121*e7be843bSPierre Pronchery #endif
122*e7be843bSPierre Pronchery 
setup_tests(void)123*e7be843bSPierre Pronchery int setup_tests(void)
124*e7be843bSPierre Pronchery {
125*e7be843bSPierre Pronchery #if (IS_X_86 || IS_AARCH_64)
126*e7be843bSPierre Pronchery     OPENSSL_cpuid_setup();
127*e7be843bSPierre Pronchery 
128*e7be843bSPierre Pronchery # if IS_X_86
129*e7be843bSPierre Pronchery     int have_rdseed = (OPENSSL_ia32cap_P[2] & (1 << 18)) != 0;
130*e7be843bSPierre Pronchery     int have_rdrand = (OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0;
131*e7be843bSPierre Pronchery 
132*e7be843bSPierre Pronchery     if (have_rdrand) {
133*e7be843bSPierre Pronchery         ADD_TEST(sanity_check_rdrand_bytes);
134*e7be843bSPierre Pronchery     }
135*e7be843bSPierre Pronchery 
136*e7be843bSPierre Pronchery     if (have_rdseed) {
137*e7be843bSPierre Pronchery         ADD_TEST(sanity_check_rdseed_bytes);
138*e7be843bSPierre Pronchery     }
139*e7be843bSPierre Pronchery # elif IS_AARCH_64
140*e7be843bSPierre Pronchery     int have_rndr_rndrrs = (OPENSSL_armcap_P & (1 << 8)) != 0;
141*e7be843bSPierre Pronchery 
142*e7be843bSPierre Pronchery     if (have_rndr_rndrrs) {
143*e7be843bSPierre Pronchery         ADD_TEST(sanity_check_rndr_bytes);
144*e7be843bSPierre Pronchery         ADD_TEST(sanity_check_rndrrs_bytes);
145*e7be843bSPierre Pronchery     }
146*e7be843bSPierre Pronchery # endif
147*e7be843bSPierre Pronchery #endif
148*e7be843bSPierre Pronchery 
149*e7be843bSPierre Pronchery     return 1;
150*e7be843bSPierre Pronchery }
151