2
0

rand.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 1998-2018 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
  21. } OPTION_CHOICE;
  22. const OPTIONS rand_options[] = {
  23. {OPT_HELP_STR, 1, '-', "Usage: %s [flags] num\n"},
  24. {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
  25. {"help", OPT_HELP, '-', "Display this summary"},
  26. {"out", OPT_OUT, '>', "Output file"},
  27. OPT_R_OPTIONS,
  28. {"base64", OPT_BASE64, '-', "Base64 encode output"},
  29. {"hex", OPT_HEX, '-', "Hex encode output"},
  30. #ifndef OPENSSL_NO_ENGINE
  31. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  32. #endif
  33. {NULL}
  34. };
  35. int rand_main(int argc, char **argv)
  36. {
  37. ENGINE *e = NULL;
  38. BIO *out = NULL;
  39. char *outfile = NULL, *prog;
  40. OPTION_CHOICE o;
  41. int format = FORMAT_BINARY, i, num = -1, r, ret = 1;
  42. prog = opt_init(argc, argv, rand_options);
  43. while ((o = opt_next()) != OPT_EOF) {
  44. switch (o) {
  45. case OPT_EOF:
  46. case OPT_ERR:
  47. opthelp:
  48. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  49. goto end;
  50. case OPT_HELP:
  51. opt_help(rand_options);
  52. ret = 0;
  53. goto end;
  54. case OPT_OUT:
  55. outfile = opt_arg();
  56. break;
  57. case OPT_ENGINE:
  58. e = setup_engine(opt_arg(), 0);
  59. break;
  60. case OPT_R_CASES:
  61. if (!opt_rand(o))
  62. goto end;
  63. break;
  64. case OPT_BASE64:
  65. format = FORMAT_BASE64;
  66. break;
  67. case OPT_HEX:
  68. format = FORMAT_TEXT;
  69. break;
  70. }
  71. }
  72. argc = opt_num_rest();
  73. argv = opt_rest();
  74. if (argc == 1) {
  75. if (!opt_int(argv[0], &num) || num <= 0)
  76. goto end;
  77. } else if (argc > 0) {
  78. BIO_printf(bio_err, "Extra arguments given.\n");
  79. goto opthelp;
  80. }
  81. out = bio_open_default(outfile, 'w', format);
  82. if (out == NULL)
  83. goto end;
  84. if (format == FORMAT_BASE64) {
  85. BIO *b64 = BIO_new(BIO_f_base64());
  86. if (b64 == NULL)
  87. goto end;
  88. out = BIO_push(b64, out);
  89. }
  90. while (num > 0) {
  91. unsigned char buf[4096];
  92. int chunk;
  93. chunk = num;
  94. if (chunk > (int)sizeof(buf))
  95. chunk = sizeof(buf);
  96. r = RAND_bytes(buf, chunk);
  97. if (r <= 0)
  98. goto end;
  99. if (format != FORMAT_TEXT) {
  100. if (BIO_write(out, buf, chunk) != chunk)
  101. goto end;
  102. } else {
  103. for (i = 0; i < chunk; i++)
  104. if (BIO_printf(out, "%02x", buf[i]) != 2)
  105. goto end;
  106. }
  107. num -= chunk;
  108. }
  109. if (format == FORMAT_TEXT)
  110. BIO_puts(out, "\n");
  111. if (BIO_flush(out) <= 0)
  112. goto end;
  113. ret = 0;
  114. end:
  115. if (ret != 0)
  116. ERR_print_errors(bio_err);
  117. release_engine(e);
  118. BIO_free_all(out);
  119. return ret;
  120. }