Size of random output is now a long, also added option to select chunk size

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/17949)
diff --git a/apps/rand.c b/apps/rand.c
index f99c91d..e9aedb4 100644
--- a/apps/rand.c
+++ b/apps/rand.c
@@ -52,7 +52,9 @@
     BIO *out = NULL;
     char *outfile = NULL, *prog;
     OPTION_CHOICE o;
-    int format = FORMAT_BINARY, i, num = -1, r, ret = 1;
+    int format = FORMAT_BINARY, r, i, ret = 1, buflen = 131072;
+    long num = -1;
+    uint8_t *buf = NULL;
 
     prog = opt_init(argc, argv, rand_options);
     while ((o = opt_next()) != OPT_EOF) {
@@ -93,7 +95,7 @@
     argc = opt_num_rest();
     argv = opt_rest();
     if (argc == 1) {
-        if (!opt_int(argv[0], &num) || num <= 0)
+        if (!opt_long(argv[0], &num) || num <= 0)
             goto opthelp;
     } else if (!opt_check_rest_arg(NULL)) {
         goto opthelp;
@@ -113,13 +115,11 @@
         out = BIO_push(b64, out);
     }
 
+    buf = app_malloc(buflen, "buffer for output file");
     while (num > 0) {
-        unsigned char buf[4096];
-        int chunk;
+        long chunk;
 
-        chunk = num;
-        if (chunk > (int)sizeof(buf))
-            chunk = sizeof(buf);
+        chunk = (num > buflen) ? buflen : num;
         r = RAND_bytes(buf, chunk);
         if (r <= 0)
             goto end;
@@ -143,6 +143,7 @@
  end:
     if (ret != 0)
         ERR_print_errors(bio_err);
+    OPENSSL_free(buf);
     release_engine(e);
     BIO_free_all(out);
     return ret;