LCOV - code coverage report
Current view: top level - util/rng - fd_rng_secure.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 10 13 76.9 %
Date: 2024-11-13 11:58:15 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include "fd_rng.h"
       2             : #include "../log/fd_log.h"
       3             : 
       4             : /* Choose best fd_rng_secure based on platform */
       5             : 
       6             : #if defined(__linux__) || defined(__FreeBSD__)
       7             : 
       8             : #include <assert.h>
       9             : #include <errno.h>
      10             : #include <sys/random.h>
      11             : 
      12             : FD_FN_SENSITIVE __attribute__((warn_unused_result))
      13             : void *
      14             : fd_rng_secure( void * d,
      15       25320 :                ulong  sz ) {
      16       25320 :   uchar * out = d;
      17       50640 :   while( sz ) {
      18       25320 :     long res = getrandom( out, sz, 0 );
      19       25320 :     if( FD_UNLIKELY( res<0 ) ) {
      20           0 :       FD_LOG_WARNING(( "getrandom(sz=%lu) failed (%d-%s)", sz, errno, fd_io_strerror( errno ) ));
      21           0 :       return NULL;
      22           0 :     }
      23       25320 :     assert( (ulong)res <= sz );
      24       25320 :     sz -= (ulong)res;
      25       25320 :   }
      26       25320 :   return d;
      27       25320 : } 
      28             : 
      29             : #elif defined(__APPLE__)
      30             : 
      31             : #include <CommonCrypto/CommonRandom.h>
      32             : 
      33             : FD_FN_SENSITIVE __attribute__((warn_unused_result))
      34             : void *
      35             : fd_rng_secure( void * d,
      36             :                ulong  sz ) {
      37             : 
      38             :   int status = CCRandomGenerateBytes( d, sz );
      39             :   if( FD_UNLIKELY( status!=kCCSuccess ) ) {
      40             :     FD_LOG_WARNING(( "CCRandomGenerateBytes(sz=%lu) failed (%d)", sz, status ));
      41             :     return NULL;
      42             :   }
      43             : 
      44             :   return d;
      45             : }
      46             : 
      47             : #else
      48             : 
      49             : FD_FN_SENSITIVE __attribute__((warn_unused_result))
      50             : void *
      51             : fd_rng_secure( void * d,
      52             :                ulong  sz ) {
      53             :   FD_LOG_WARNING(( "fd_rng_secure failed (not supported by this build)" ));
      54             :   return NULL;
      55             : } 
      56             : 
      57             : #endif

Generated by: LCOV version 1.14