Index: randombytes.c ================================================================== --- randombytes.c +++ randombytes.c @@ -1,16 +1,24 @@ #include #include #include "randombytes.h" -long long getrandom_impl(void *buf, unsigned int buflen); +long getrandom_impl(void *buf, unsigned int buflen); void randombytes(unsigned char *buffer, unsigned long long length) { - long long gr_ret; + long gr_ret; int errorCount = 0; - if (length > UINT_MAX || length > LONG_LONG_MAX) { + /* + * Ensure that the number of bytes requested can fit within + * the types we pass to other calls. + * + * The interface required by randombytes() used by TweetNaCl + * does not give us any way to handle errors. However, + * no buffer should exceed these amounts. + */ + if (length > UINT_MAX || length > LONG_MAX) { Tcl_Panic("Buffer length is too large"); } while (length > 0) { gr_ret = getrandom_impl(buffer, length); @@ -38,11 +46,11 @@ #if defined(HAVE_GETRANDOM) # ifdef HAVE_SYS_RANDOM_H # include # endif -long long getrandom_impl(void *buf, unsigned int buflen) { +long getrandom_impl(void *buf, unsigned int buflen) { ssize_t gr_ret; gr_ret = getrandom(buf, buflen, 0); return(gr_ret); @@ -49,11 +57,11 @@ } #elif defined(HAVE_GETENTROPY) #include -long long getrandom_impl(void *buf, unsigned int buflen) { +long getrandom_impl(void *buf, unsigned int buflen) { int ge_ret; if (buflen > 255) { buflen = 255; } @@ -65,11 +73,11 @@ return(buflen); } #elif defined(HAVE_CRYPTGENRANDOM) && 0 #include -long long getrandom_impl(void *buf, unsigned int buflen) { +long getrandom_impl(void *buf, unsigned int buflen) { Tcl_Panic("Incomplete CryptGenRandom"); } #else # ifdef HAVE_SYS_TYPES_H # include @@ -81,13 +89,13 @@ # include # endif # ifdef HAVE_UNISTD_H # include # endif -long long getrandom_impl(void *buf, unsigned int buflen) { +long getrandom_impl(void *buf, unsigned int buflen) { ssize_t read_ret; - long long retval; + long retval; int fd = -1; fd = open("/dev/urandom", O_RDONLY); if (fd < 0) { return(-1);