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