Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/engine/eng_rdrand.c
Line
Count
Source
1
/*
2
 * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/* We need to use some engine deprecated APIs */
11
#define OPENSSL_SUPPRESS_DEPRECATED
12
13
#include <openssl/opensslconf.h>
14
15
#include <stdio.h>
16
#include <string.h>
17
#include "crypto/engine.h"
18
#include "internal/cryptlib.h"
19
#include <openssl/rand.h>
20
#include <openssl/err.h>
21
#include <openssl/crypto.h>
22
23
#if (defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(__x86_64) || defined(__x86_64__) || defined(_M_AMD64) || defined(_M_X64)) && defined(OPENSSL_CPUID_OBJ)
24
25
size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);
26
27
static int get_random_bytes(unsigned char *buf, int num)
28
0
{
29
0
    if (num < 0) {
30
0
        return 0;
31
0
    }
32
33
0
    return (size_t)num == OPENSSL_ia32_rdrand_bytes(buf, (size_t)num);
34
0
}
35
36
static int random_status(void)
37
0
{
38
0
    return 1;
39
0
}
40
41
static RAND_METHOD rdrand_meth = {
42
    NULL, /* seed */
43
    get_random_bytes,
44
    NULL, /* cleanup */
45
    NULL, /* add */
46
    get_random_bytes,
47
    random_status,
48
};
49
50
static int rdrand_init(ENGINE *e)
51
0
{
52
0
    return 1;
53
0
}
54
55
static const char *engine_e_rdrand_id = "rdrand";
56
static const char *engine_e_rdrand_name = "Intel RDRAND engine";
57
58
static int bind_helper(ENGINE *e)
59
0
{
60
0
    if (!ENGINE_set_id(e, engine_e_rdrand_id) || !ENGINE_set_name(e, engine_e_rdrand_name) || !ENGINE_set_flags(e, ENGINE_FLAGS_NO_REGISTER_ALL) || !ENGINE_set_init_function(e, rdrand_init) || !ENGINE_set_RAND(e, &rdrand_meth))
61
0
        return 0;
62
63
0
    return 1;
64
0
}
65
66
static ENGINE *ENGINE_rdrand(void)
67
0
{
68
0
    ENGINE *ret = ENGINE_new();
69
0
    if (ret == NULL)
70
0
        return NULL;
71
0
    if (!bind_helper(ret)) {
72
0
        ENGINE_free(ret);
73
0
        return NULL;
74
0
    }
75
0
    return ret;
76
0
}
77
78
void engine_load_rdrand_int(void)
79
0
{
80
0
    if (OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) {
81
0
        ENGINE *toadd = ENGINE_rdrand();
82
0
        if (!toadd)
83
0
            return;
84
0
        ERR_set_mark();
85
0
        ENGINE_add(toadd);
86
        /*
87
         * If the "add" worked, it gets a structural reference. So either way, we
88
         * release our just-created reference.
89
         */
90
0
        ENGINE_free(toadd);
91
        /*
92
         * If the "add" didn't work, it was probably a conflict because it was
93
         * already added (eg. someone calling ENGINE_load_blah then calling
94
         * ENGINE_load_builtin_engines() perhaps).
95
         */
96
0
        ERR_pop_to_mark();
97
0
    }
98
0
}
99
#else
100
void engine_load_rdrand_int(void)
101
{
102
}
103
#endif