File: getentropy.cc

package info (click to toggle)
libnewlib-nano 2.11.2-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 114,156 kB
  • sloc: ansic: 631,393; cpp: 129,053; makefile: 62,427; asm: 49,042; sh: 15,817; xml: 15,337; perl: 4,340; lisp: 720; python: 557; exp: 470; yacc: 315; awk: 25; tcl: 4
file content (70 lines) | stat: -rw-r--r-- 1,414 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* getentropy.cc: getentropy/getrandmom functions

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include "winsup.h"
#include <ntsecapi.h>
#include <sys/param.h>
#include <sys/random.h>
#include "cygtls.h"
#include "ntdll.h"

extern "C" int
getentropy (void *ptr, size_t len)
{
  /* Per BSD man page: The maximum buffer size permitted is 256 bytes.
     If buflen exceeds this, an error of EIO will be indicated. */
  if (len > 256)
    {
      debug_printf ("len (%U) > 256", len);
      set_errno (EIO);
      return -1;
    }
  __try
    {
      if (!RtlGenRandom (ptr, len))
	{
	  debug_printf ("RtlGenRandom() = FALSE");
	  set_errno (EIO);
	  return -1;
	}
    }
  __except (EFAULT)
    {
      return -1;
    }
  __endtry
  return 0;
}

extern "C" ssize_t
getrandom (void *ptr, size_t len, unsigned int flags)
{
  if (flags & ~(GRND_NONBLOCK | GRND_RANDOM))
    {
      debug_printf ("invalid flags: %y", flags);
      set_errno (EINVAL);
      return -1;
    }
  /* Max. bytes returned by Linux call. */
  len = MIN (len, (flags & GRND_RANDOM) ? 512 : 33554431);
  __try
    {
      if (!RtlGenRandom (ptr, len))
	{
	  debug_printf ("RtlGenRandom() = FALSE");
	  set_errno (EIO);
	  return -1;
	}
    }
  __except (EFAULT)
    {
      return -1;
    }
  __endtry
  return len;
}