rand.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 1998-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "apps.h"
  10. #include "progs.h"
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <openssl/bio.h>
  15. #include <openssl/err.h>
  16. #include <openssl/rand.h>
  17. typedef enum OPTION_choice {
  18. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  19. OPT_OUT, OPT_ENGINE, OPT_BASE64, OPT_HEX,
  20. OPT_R_ENUM, OPT_PROV_ENUM
  21. } OPTION_CHOICE;
  22. const OPTIONS rand_options[] = {
  23. {OPT_HELP_STR, 1, '-', "Usage: %s [options] num\n"},
  24. OPT_SECTION("General"),
  25. {"help", OPT_HELP, '-', "Display this summary"},
  26. #ifndef OPENSSL_NO_ENGINE
  27. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  28. #endif
  29. OPT_SECTION("Output"),
  30. {"out", OPT_OUT, '>', "Output file"},
  31. {"base64", OPT_BASE64, '-', "Base64 encode output"},
  32. {"hex", OPT_HEX, '-', "Hex encode output"},
  33. OPT_R_OPTIONS,
  34. OPT_PROV_OPTIONS,
  35. OPT_PARAMETERS(),
  36. {"num", 0, 0, "Number of bytes to generate"},
  37. {NULL}
  38. };
  39. int rand_main(int argc, char **argv)
  40. {
  41. ENGINE *e = NULL;
  42. BIO *out = NULL;
  43. char *outfile = NULL, *prog;
  44. OPTION_CHOICE o;
  45. int format = FORMAT_BINARY, i, num = -1, r, ret = 1;
  46. prog = opt_init(argc, argv, rand_options);
  47. while ((o = opt_next()) != OPT_EOF) {
  48. switch (o) {
  49. case OPT_EOF:
  50. case OPT_ERR:
  51. opthelp:
  52. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  53. goto end;
  54. case OPT_HELP:
  55. opt_help(rand_options);
  56. ret = 0;
  57. goto end;
  58. case OPT_OUT:
  59. outfile = opt_arg();
  60. break;
  61. case OPT_ENGINE:
  62. e = setup_engine(opt_arg(), 0);
  63. break;
  64. case OPT_R_CASES:
  65. if (!opt_rand(o))
  66. goto end;
  67. break;
  68. case OPT_BASE64:
  69. format = FORMAT_BASE64;
  70. break;
  71. case OPT_HEX:
  72. format = FORMAT_TEXT;
  73. break;
  74. case OPT_PROV_CASES:
  75. if (!opt_provider(o))
  76. goto end;
  77. break;
  78. }
  79. }
  80. /* Optional argument is number of bytes to generate. */
  81. argc = opt_num_rest();
  82. argv = opt_rest();
  83. if (argc == 1) {
  84. if (!opt_int(argv[0], &num) || num <= 0)
  85. goto opthelp;
  86. } else if (argc != 0) {
  87. goto opthelp;
  88. }
  89. out = bio_open_default(outfile, 'w', format);
  90. if (out == NULL)
  91. goto end;
  92. if (format == FORMAT_BASE64) {
  93. BIO *b64 = BIO_new(BIO_f_base64());
  94. if (b64 == NULL)
  95. goto end;
  96. out = BIO_push(b64, out);
  97. }
  98. while (num > 0) {
  99. unsigned char buf[4096];
  100. int chunk;
  101. chunk = num;
  102. if (chunk > (int)sizeof(buf))
  103. chunk = sizeof(buf);
  104. r = RAND_bytes(buf, chunk);
  105. if (r <= 0)
  106. goto end;
  107. if (format != FORMAT_TEXT) {
  108. if (BIO_write(out, buf, chunk) != chunk)
  109. goto end;
  110. } else {
  111. for (i = 0; i < chunk; i++)
  112. if (BIO_printf(out, "%02x", buf[i]) != 2)
  113. goto end;
  114. }
  115. num -= chunk;
  116. }
  117. if (format == FORMAT_TEXT)
  118. BIO_puts(out, "\n");
  119. if (BIO_flush(out) <= 0)
  120. goto end;
  121. ret = 0;
  122. end:
  123. if (ret != 0)
  124. ERR_print_errors(bio_err);
  125. release_engine(e);
  126. BIO_free_all(out);
  127. return ret;
  128. }