Coverage Report

Created: 2026-05-24 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libspdm/os_stub/cryptlib_mbedtls/rand/rand.c
Line
Count
Source
1
/**
2
 *  Copyright Notice:
3
 *  Copyright 2021-2022 DMTF. All rights reserved.
4
 *  License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
5
 **/
6
7
/** @file
8
 * Pseudorandom Number generator Wrapper Implementation.
9
 **/
10
11
#include "internal_crypt_lib.h"
12
#include "library/rnglib.h"
13
14
/**
15
 * Generates a random byte stream of the specified size.
16
 *
17
 * If output is NULL, then return false.
18
 * If this interface is not supported, then return false.
19
 *
20
 * @param[out]  output  Pointer to buffer to receive random value.
21
 * @param[in]   size    Size of random bytes to generate.
22
 *
23
 * @retval true   Random byte stream generated successfully.
24
 * @retval false  Generation of random byte stream failed.
25
 **/
26
bool libspdm_random_bytes(uint8_t *output, size_t size)
27
60.3k
{
28
60.3k
    bool ret;
29
60.3k
    uint64_t temp_rand;
30
60.3k
    size_t dst_size;
31
32
60.3k
    ret = false;
33
60.3k
    dst_size = size;
34
35
202k
    while (size > 0) {
36
        /* Use rnglib to get random number*/
37
142k
        ret = libspdm_get_random_number_64(&temp_rand);
38
39
142k
        if (!ret) {
40
0
            return ret;
41
0
        }
42
43
142k
        if (size >= sizeof(uint64_t)) {
44
93.5k
            libspdm_copy_mem(output, dst_size, &temp_rand, sizeof(uint64_t));
45
93.5k
            output += sizeof(uint64_t);
46
93.5k
            size -= sizeof(uint64_t);
47
93.5k
            dst_size -= sizeof(uint64_t);
48
93.5k
        } else {
49
49.0k
            libspdm_copy_mem(output, dst_size, &temp_rand, size);
50
49.0k
            size = 0;
51
49.0k
        }
52
142k
    }
53
54
60.3k
    return ret;
55
60.3k
}
56
57
int libspdm_myrand(void *rng_state, unsigned char *output, size_t len)
58
6.55k
{
59
6.55k
    bool result = libspdm_random_bytes(output, len);
60
61
62
    /* The MbedTLS function f_rng, which myrand implements, is not
63
     * documented well. From looking at code: zero is considered success,
64
     * while non-zero return value is considered failure.*/
65
66
6.55k
    return result ? 0 : -1;
67
6.55k
}