diff --git a/pkg/entropy/constants_linux.go b/pkg/entropy/constants_linux.go new file mode 100644 index 0000000..feb9539 --- /dev/null +++ b/pkg/entropy/constants_linux.go @@ -0,0 +1,14 @@ +// generated by gen_constants.c + +package entropy + +const ( + RNDGETENTCNT = 0x80045200 + RNDADDTOENTCNT = 0x40045201 + RNDGETPOOL = 0x80085202 + RNDADDENTROPY = 0x40085203 + RNDZAPENTCNT = 0x5204 + RNDCLEARPOOL = 0x5206 + RNDRESEEDCRNG = 0x5207 +) + diff --git a/pkg/entropy/entropy_linux.go b/pkg/entropy/entropy_linux.go index 045e91d..cd17911 100644 --- a/pkg/entropy/entropy_linux.go +++ b/pkg/entropy/entropy_linux.go @@ -2,6 +2,7 @@ package entropy import ( "fmt" + "syscall" "unsafe" "golang.org/x/sys/unix" @@ -9,17 +10,6 @@ import ( var entropy_device = "/dev/urandom" -/* from linux/random.h */ -const ( - RNDGETENTCNT = 0b10>>30 | 'R'>>8 | 1>>16 - RNDADDTOENTCNT = 0x01 - RNDGETPOOL = 0x02 - RNDADDENTROPY = 0x03 - RNDZAPENTCNT = 0x04 - RNDCLEARPOOL = 0x06 - RNDRESEEDCRNG = 0x07 -) - // this is honestly easier through /proc, but in the spirit of completeness... func getEntropy() (ent int, err error) { var fd int @@ -28,13 +18,11 @@ func getEntropy() (ent int, err error) { } defer unix.Close(fd) - /* - nrshift 0 - typeshift 8 - sizeshift 16 - dirshift 30 - */ - _, _, err = unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(RNDGETENTCNT), uintptr(unsafe.Pointer(&ent))) + var errno syscall.Errno + _, _, errno = unix.Syscall(unix.SYS_IOCTL, uintptr(fd), uintptr(RNDGETENTCNT), uintptr(unsafe.Pointer(&ent))) + if errno != 0 { + err = errno + } return } diff --git a/pkg/entropy/util/gen_constants.c b/pkg/entropy/util/gen_constants.c new file mode 100644 index 0000000..64a8c37 --- /dev/null +++ b/pkg/entropy/util/gen_constants.c @@ -0,0 +1,34 @@ +// This simple program writes constants.go +// It uses the Linux headers to generate these contatns. + +#include +#include +#include +#include +#include +#include + +const char * filename = "constants_linux.go"; + +int main() { + int fd; + printf("Generating constants for entropy IOCTLs.\n"); + fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0644); + if( fd == -1 ) { + printf("Failed to open output file: %s\n", filename); + return -1; + } + dprintf(fd, "// generated by gen_constants.c\n\n"); + dprintf(fd, "package entropy\n\n"); + dprintf(fd, "const (\n"); + dprintf(fd, "\tRNDGETENTCNT = %#x\n", RNDGETENTCNT); + dprintf(fd, "\tRNDADDTOENTCNT = %#x\n", RNDADDTOENTCNT); + dprintf(fd, "\tRNDGETPOOL = %#x\n", RNDGETPOOL); + dprintf(fd, "\tRNDADDENTROPY = %#x\n", RNDADDENTROPY); + dprintf(fd, "\tRNDZAPENTCNT = %#x\n", RNDZAPENTCNT); + dprintf(fd, "\tRNDCLEARPOOL = %#x\n", RNDCLEARPOOL); + dprintf(fd, "\tRNDRESEEDCRNG = %#x\n", RNDRESEEDCRNG); + dprintf(fd, ")\n\n"); + close(fd); + return 0; +} \ No newline at end of file