Home | History | Annotate | Line # | Download | only in nativeclient
      1 
      2 #include <assert.h>
      3 #include <stdint.h>
      4 #include <stdlib.h>
      5 
      6 #ifdef __native_client__
      7 # include <irt.h>
      8 
      9 # include "core.h"
     10 # include "utils.h"
     11 # include "randombytes.h"
     12 # include "randombytes_nativeclient.h"
     13 
     14 static void
     15 randombytes_nativeclient_buf(void * const buf, const size_t size)
     16 {
     17     unsigned char          *buf_ = (unsigned char *) buf;
     18     struct nacl_irt_random  rand_intf;
     19     size_t                  readnb = (size_t) 0U;
     20     size_t                  toread = size;
     21 
     22     if (nacl_interface_query(NACL_IRT_RANDOM_v0_1, &rand_intf,
     23                              sizeof rand_intf) != sizeof rand_intf) {
     24         sodium_misuse();
     25     }
     26     while (toread > (size_t) 0U) {
     27         if (rand_intf.get_random_bytes(buf_, size, &readnb) != 0 ||
     28             readnb > size) {
     29             sodium_misuse();
     30         }
     31         toread -= readnb;
     32         buf_ += readnb;
     33     }
     34 }
     35 
     36 static uint32_t
     37 randombytes_nativeclient_random(void)
     38 {
     39     uint32_t r;
     40 
     41     randombytes_nativeclient_buf(&r, sizeof r);
     42 
     43     return r;
     44 }
     45 
     46 static const char *
     47 randombytes_nativeclient_implementation_name(void)
     48 {
     49     return "nativeclient";
     50 }
     51 
     52 struct randombytes_implementation randombytes_nativeclient_implementation = {
     53     SODIUM_C99(.implementation_name =) randombytes_nativeclient_implementation_name,
     54     SODIUM_C99(.random =) randombytes_nativeclient_random,
     55     SODIUM_C99(.stir =) NULL,
     56     SODIUM_C99(.uniform =) NULL,
     57     SODIUM_C99(.buf =) randombytes_nativeclient_buf,
     58     SODIUM_C99(.close =) NULL
     59 };
     60 
     61 #endif
     62