getentropy(2) 맨 페이지 - 윈디하나의 솔라나라

개요

섹션
맨 페이지 이름
검색(S)

getentropy(2)

getrandom(2)                     System Calls                     getrandom(2)

NAME
       getrandom, getentropy - retrieve data from the kernel random pool

SYNOPSIS
       #include <sys/random.h>

       ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);


       #include <unistd.h>

       int getentropy(void *buf, size_t buflen);

DESCRIPTION
       The  getrandom()  function  can be used to request buflen bytes of data
       from the kernel random pool, which is to  be  placed  into  the  buffer
       pointed  to  by  buf. It is recommended to use the getrandom() function
       instead  of  open(2)  and  read(2)  functions  on  the  /dev/random  or
       /dev/urandom device.


       The  random data returned by the getrandom() function is processed by a
       FIPS 140-2 approved deterministic random bit generator (DRBG).


       If the GRND_RANDOM flag is set, then the implementation uses  the  same
       pool as /dev/random, otherwise the /dev/urandom pool is used.


       If  no  entropy is available in the pool, the getrandom() function will
       block unless the GRND_NONBLOCK flag is set. In this case, the  function
       returns  -1  and  errno is set to EAGAIN. Note that the number of bytes
       returned can be less than requested, including 0. Callers need to check
       the return value to determine if random bytes were returned. This means
       this is not an acceptable calling sequence:

         (void) getrandom(&buf, sizeof (buf), 0);



       The getentropy() function is always a blocking call, it is expected  to
       be used only to seed a userspace implementation of a random bit genera‐
       tor.

RETURN VALUES
       Upon successful completion, the getrandom() function returns the number
       of  bytes written to buf. Otherwise, it returns 0 and sets errno to in‐
       dicate the error.


       Upon successful completion, the getentropy() function returns 0. Other‐
       wise, it returns -1 and sets errno to indicate the error.

ERRORS
       The getrandom() and getentropy() functions will fail if:

       EINVAL    Invalid flags or flag combinations

                     o      bufsz is <= 0 or > 1040, when GRND_RANDOM is set


                     o      bufsz is <= 0 or > 133120, when GRND_RANDOM is not
                            set



       EFAULT    buf is an invalid address.


       EAGAIN    No entropy is available and GRND_NONBLOCK is set.



       The getentropy() call also fails if:

       EIO    More than 256 bytes are requested, or the returned amount of en‐
              tropy does not match the request.


EXAMPLES
       Example 1 Using the getrandom() function


         #include <sys/random.h>
         #include <stdlib.h>
         .
         size_t bufsz = 1024;
         char *buf;
         int ret;
         .
         ...
         buf = malloc(bufsz);
         ...
         ret = getrandom(buf, bufsz, GRND_RANDOM);
         if (ret != bufsz) {
                 perror("getrandom failed");
                 ...
         }
         ...


       Example 2 Using the getentropy() function


         #include <sys/random.h>
         #include <stdlib.h>
         .
         size_t entsz = 128;
         char *entropy;
         int err;
         .
         ...
         entropy = malloc(entsz);
         ...
         err = getentropy(entropy, entsz);
         if (err != 0) {
                 perror("getentropy failed");
                 ...
         }
         /* Use entropy to seed our RNG */


ATTRIBUTES
       See attributes(7) for descriptions of the following attributes:

       tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) ATTRIBUTE  TYPEAT‐
       TRIBUTE VALUE _ Interface StabilityCommitted _ MT-LevelMT-Safe


SEE ALSO
       random(4D)

HISTORY
       The getrandom() and getentropy() functions were added to Oracle Solaris
       in the Solaris 11.3.0 release.


       The  function  prototype  for  getentropy() was added to the <unistd.h>
       header in Oracle Solaris 11.4.16. Prior to that, applications needed to
       include <sys/random.h> as well.


       The getentropy() function first appeared in OpenBSD  5.6.  The  getran‐
       dom() function first appeared in GNU libc 2.25.

Oracle Solaris 11.4               30 Jan 2023                     getrandom(2)
맨 페이지 내용의 저작권은 맨 페이지 작성자에게 있습니다.
RSS ATOM XHTML 5 CSS3