LCOV - code coverage report
Current view: top level - os_stub/cryptlib_mbedtls/rand - rand.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 94.4 % 18 17
Test Date: 2025-06-29 08:09:00 Functions: 100.0 % 2 2

            Line data    Source code
       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         3460 : bool libspdm_random_bytes(uint8_t *output, size_t size)
      27              : {
      28              :     bool ret;
      29              :     uint64_t temp_rand;
      30              :     size_t dst_size;
      31              : 
      32         3460 :     ret = false;
      33         3460 :     dst_size = size;
      34              : 
      35        17416 :     while (size > 0) {
      36              :         /* Use rnglib to get random number*/
      37        13956 :         ret = libspdm_get_random_number_64(&temp_rand);
      38              : 
      39        13956 :         if (!ret) {
      40            0 :             return ret;
      41              :         }
      42              : 
      43        13956 :         if (size >= sizeof(uint64_t)) {
      44        12809 :             libspdm_copy_mem(output, dst_size, &temp_rand, sizeof(uint64_t));
      45        12809 :             output += sizeof(uint64_t);
      46        12809 :             size -= sizeof(uint64_t);
      47        12809 :             dst_size -= sizeof(uint64_t);
      48              :         } else {
      49         1147 :             libspdm_copy_mem(output, dst_size, &temp_rand, size);
      50         1147 :             size = 0;
      51              :         }
      52              :     }
      53              : 
      54         3460 :     return ret;
      55              : }
      56              : 
      57         1388 : int libspdm_myrand(void *rng_state, unsigned char *output, size_t len)
      58              : {
      59         1388 :     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         1388 :     return result ? 0 : -1;
      67              : }
        

Generated by: LCOV version 2.0-1