Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1346735 - drop netstat from unix_rand; add new urandom only entro…
…py source, r=mt,ttaubert Differential Revision: https://nss-review.dev.mozaws.net/D45 --HG-- extra : rebase_source : 043c91ac1b4d2ba04dbc6cb2a955552687e6d1dd
- Loading branch information
1 parent
f9fec7e
commit e048621
Showing
5 changed files
with
59 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include "secerr.h" | ||
#include "secrng.h" | ||
#include "prprf.h" | ||
|
||
void | ||
RNG_SystemInfoForRNG(void) | ||
{ | ||
PRUint8 bytes[SYSTEM_RNG_SEED_COUNT]; | ||
size_t numBytes = RNG_SystemRNG(bytes, SYSTEM_RNG_SEED_COUNT); | ||
if (!numBytes) { | ||
/* error is set */ | ||
return; | ||
} | ||
RNG_RandomUpdate(bytes, numBytes); | ||
} | ||
|
||
size_t | ||
RNG_SystemRNG(void *dest, size_t maxLen) | ||
{ | ||
int fd; | ||
int bytes; | ||
size_t fileBytes = 0; | ||
unsigned char *buffer = dest; | ||
|
||
fd = open("/dev/urandom", O_RDONLY); | ||
if (fd < 0) { | ||
PORT_SetError(SEC_ERROR_NEED_RANDOM); | ||
return 0; | ||
} | ||
while (fileBytes < maxLen) { | ||
bytes = read(fd, buffer, maxLen - fileBytes); | ||
if (bytes <= 0) { | ||
break; | ||
} | ||
fileBytes += bytes; | ||
buffer += bytes; | ||
} | ||
(void)close(fd); | ||
if (fileBytes != maxLen) { | ||
PORT_SetError(SEC_ERROR_NEED_RANDOM); | ||
return 0; | ||
} | ||
return fileBytes; | ||
} |