Extract entropy from /dev/urandom */
| 1077 | |
| 1078 | /* Extract entropy from /dev/urandom */ |
| 1079 | static int |
| 1080 | writeRandomBytes_dev_urandom(void *target, size_t count) { |
| 1081 | int success = 0; /* full count bytes written? */ |
| 1082 | size_t bytesWrittenTotal = 0; |
| 1083 | |
| 1084 | const int fd = open("/dev/urandom", O_RDONLY); |
| 1085 | if (fd < 0) { |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | do { |
| 1090 | void *const currentTarget = (void *)((char *)target + bytesWrittenTotal); |
| 1091 | const size_t bytesToWrite = count - bytesWrittenTotal; |
| 1092 | |
| 1093 | const ssize_t bytesWrittenMore = read(fd, currentTarget, bytesToWrite); |
| 1094 | |
| 1095 | if (bytesWrittenMore > 0) { |
| 1096 | bytesWrittenTotal += bytesWrittenMore; |
| 1097 | if (bytesWrittenTotal >= count) |
| 1098 | success = 1; |
| 1099 | } |
| 1100 | } while (! success && (errno == EINTR)); |
| 1101 | |
| 1102 | close(fd); |
| 1103 | return success; |
| 1104 | } |
| 1105 | |
| 1106 | # endif /* ! defined(_WIN32) && defined(XML_DEV_URANDOM) */ |
| 1107 |
no test coverage detected
searching dependent graphs…