projects
/
openssl.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
PR: 2020
[openssl.git]
/
apps
/
rand.c
diff --git
a/apps/rand.c
b/apps/rand.c
index f51f5bec3560d487c38d6e6fc45b2d51b56accda..44a1d46a03cdcdb48116fcfefa2c06300a8cee05 100644
(file)
--- a/
apps/rand.c
+++ b/
apps/rand.c
@@
-68,7
+68,8
@@
/* -out file - write to file
* -rand file:file - PRNG seed files
/* -out file - write to file
* -rand file:file - PRNG seed files
- * -base64 - encode output
+ * -base64 - base64 encode output
+ * -hex - hex encode output
* num - write 'num' bytes
*/
* num - write 'num' bytes
*/
@@
-76,15
+77,20
@@
int MAIN(int, char **);
int MAIN(int argc, char **argv)
{
int MAIN(int argc, char **argv)
{
+#ifndef OPENSSL_NO_ENGINE
ENGINE *e = NULL;
ENGINE *e = NULL;
+#endif
int i, r, ret = 1;
int badopt;
char *outfile = NULL;
char *inrand = NULL;
int base64 = 0;
int i, r, ret = 1;
int badopt;
char *outfile = NULL;
char *inrand = NULL;
int base64 = 0;
+ int hex = 0;
BIO *out = NULL;
int num = -1;
BIO *out = NULL;
int num = -1;
+#ifndef OPENSSL_NO_ENGINE
char *engine=NULL;
char *engine=NULL;
+#endif
apps_startup();
apps_startup();
@@
-106,6
+112,7
@@
int MAIN(int argc, char **argv)
else
badopt = 1;
}
else
badopt = 1;
}
+#ifndef OPENSSL_NO_ENGINE
else if (strcmp(argv[i], "-engine") == 0)
{
if ((argv[i+1] != NULL) && (engine == NULL))
else if (strcmp(argv[i], "-engine") == 0)
{
if ((argv[i+1] != NULL) && (engine == NULL))
@@
-113,6
+120,7
@@
int MAIN(int argc, char **argv)
else
badopt = 1;
}
else
badopt = 1;
}
+#endif
else if (strcmp(argv[i], "-rand") == 0)
{
if ((argv[i+1] != NULL) && (inrand == NULL))
else if (strcmp(argv[i], "-rand") == 0)
{
if ((argv[i+1] != NULL) && (inrand == NULL))
@@
-127,6
+135,13
@@
int MAIN(int argc, char **argv)
else
badopt = 1;
}
else
badopt = 1;
}
+ else if (strcmp(argv[i], "-hex") == 0)
+ {
+ if (!hex)
+ hex = 1;
+ else
+ badopt = 1;
+ }
else if (isdigit((unsigned char)argv[i][0]))
{
if (num < 0)
else if (isdigit((unsigned char)argv[i][0]))
{
if (num < 0)
@@
-142,6
+157,9
@@
int MAIN(int argc, char **argv)
badopt = 1;
}
badopt = 1;
}
+ if (hex && base64)
+ badopt = 1;
+
if (num < 0)
badopt = 1;
if (num < 0)
badopt = 1;
@@
-150,13
+168,18
@@
int MAIN(int argc, char **argv)
BIO_printf(bio_err, "Usage: rand [options] num\n");
BIO_printf(bio_err, "where options are\n");
BIO_printf(bio_err, "-out file - write to file\n");
BIO_printf(bio_err, "Usage: rand [options] num\n");
BIO_printf(bio_err, "where options are\n");
BIO_printf(bio_err, "-out file - write to file\n");
+#ifndef OPENSSL_NO_ENGINE
BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n");
BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n");
+#endif
BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
- BIO_printf(bio_err, "-base64 - encode output\n");
+ BIO_printf(bio_err, "-base64 - base64 encode output\n");
+ BIO_printf(bio_err, "-hex - hex encode output\n");
goto err;
}
goto err;
}
+#ifndef OPENSSL_NO_ENGINE
e = setup_engine(bio_err, engine, 0);
e = setup_engine(bio_err, engine, 0);
+#endif
app_RAND_load_file(NULL, bio_err, (inrand != NULL));
if (inrand != NULL)
app_RAND_load_file(NULL, bio_err, (inrand != NULL));
if (inrand != NULL)
@@
-195,15
+218,23
@@
int MAIN(int argc, char **argv)
int chunk;
chunk = num;
int chunk;
chunk = num;
- if (chunk >
sizeof buf
)
+ if (chunk >
(int)sizeof(buf)
)
chunk = sizeof buf;
r = RAND_bytes(buf, chunk);
if (r <= 0)
goto err;
chunk = sizeof buf;
r = RAND_bytes(buf, chunk);
if (r <= 0)
goto err;
- BIO_write(out, buf, chunk);
+ if (!hex)
+ BIO_write(out, buf, chunk);
+ else
+ {
+ for (i = 0; i < chunk; i++)
+ BIO_printf(out, "%02x", buf[i]);
+ }
num -= chunk;
}
num -= chunk;
}
- BIO_flush(out);
+ if (hex)
+ BIO_puts(out, "\n");
+ (void)BIO_flush(out);
app_RAND_write_file(NULL, bio_err);
ret = 0;
app_RAND_write_file(NULL, bio_err);
ret = 0;
@@
-213,5
+244,5
@@
err:
if (out)
BIO_free_all(out);
apps_shutdown();
if (out)
BIO_free_all(out);
apps_shutdown();
- EXIT(ret);
+
OPENSSL_
EXIT(ret);
}
}