2
0

rand.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* apps/rand.c */
  2. /* ====================================================================
  3. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. #include "apps.h"
  56. #include <ctype.h>
  57. #include <stdio.h>
  58. #include <string.h>
  59. #include <openssl/bio.h>
  60. #include <openssl/err.h>
  61. #include <openssl/rand.h>
  62. #undef PROG
  63. #define PROG rand_main
  64. /* -out file - write to file
  65. * -rand file:file - PRNG seed files
  66. * -base64 - base64 encode output
  67. * -hex - hex encode output
  68. * num - write 'num' bytes
  69. */
  70. int MAIN(int, char **);
  71. int MAIN(int argc, char **argv)
  72. {
  73. #ifndef OPENSSL_NO_ENGINE
  74. ENGINE *e = NULL;
  75. #endif
  76. int i, r, ret = 1;
  77. int badopt;
  78. char *outfile = NULL;
  79. char *inrand = NULL;
  80. int base64 = 0;
  81. int hex = 0;
  82. BIO *out = NULL;
  83. int num = -1;
  84. #ifndef OPENSSL_NO_ENGINE
  85. char *engine=NULL;
  86. #endif
  87. apps_startup();
  88. if (bio_err == NULL)
  89. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  90. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
  91. if (!load_config(bio_err, NULL))
  92. goto err;
  93. badopt = 0;
  94. i = 0;
  95. while (!badopt && argv[++i] != NULL)
  96. {
  97. if (strcmp(argv[i], "-out") == 0)
  98. {
  99. if ((argv[i+1] != NULL) && (outfile == NULL))
  100. outfile = argv[++i];
  101. else
  102. badopt = 1;
  103. }
  104. #ifndef OPENSSL_NO_ENGINE
  105. else if (strcmp(argv[i], "-engine") == 0)
  106. {
  107. if ((argv[i+1] != NULL) && (engine == NULL))
  108. engine = argv[++i];
  109. else
  110. badopt = 1;
  111. }
  112. #endif
  113. else if (strcmp(argv[i], "-rand") == 0)
  114. {
  115. if ((argv[i+1] != NULL) && (inrand == NULL))
  116. inrand = argv[++i];
  117. else
  118. badopt = 1;
  119. }
  120. else if (strcmp(argv[i], "-base64") == 0)
  121. {
  122. if (!base64)
  123. base64 = 1;
  124. else
  125. badopt = 1;
  126. }
  127. else if (strcmp(argv[i], "-hex") == 0)
  128. {
  129. if (!hex)
  130. hex = 1;
  131. else
  132. badopt = 1;
  133. }
  134. else if (isdigit((unsigned char)argv[i][0]))
  135. {
  136. if (num < 0)
  137. {
  138. r = sscanf(argv[i], "%d", &num);
  139. if (r == 0 || num < 0)
  140. badopt = 1;
  141. }
  142. else
  143. badopt = 1;
  144. }
  145. else
  146. badopt = 1;
  147. }
  148. if (hex && base64)
  149. badopt = 1;
  150. if (num < 0)
  151. badopt = 1;
  152. if (badopt)
  153. {
  154. BIO_printf(bio_err, "Usage: rand [options] num\n");
  155. BIO_printf(bio_err, "where options are\n");
  156. BIO_printf(bio_err, "-out file - write to file\n");
  157. #ifndef OPENSSL_NO_ENGINE
  158. BIO_printf(bio_err, "-engine e - use engine e, possibly a hardware device.\n");
  159. #endif
  160. BIO_printf(bio_err, "-rand file%cfile%c... - seed PRNG from files\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
  161. BIO_printf(bio_err, "-base64 - base64 encode output\n");
  162. BIO_printf(bio_err, "-hex - hex encode output\n");
  163. goto err;
  164. }
  165. #ifndef OPENSSL_NO_ENGINE
  166. e = setup_engine(bio_err, engine, 0);
  167. #endif
  168. app_RAND_load_file(NULL, bio_err, (inrand != NULL));
  169. if (inrand != NULL)
  170. BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
  171. app_RAND_load_files(inrand));
  172. out = BIO_new(BIO_s_file());
  173. if (out == NULL)
  174. goto err;
  175. if (outfile != NULL)
  176. r = BIO_write_filename(out, outfile);
  177. else
  178. {
  179. r = BIO_set_fp(out, stdout, BIO_NOCLOSE | BIO_FP_TEXT);
  180. #ifdef OPENSSL_SYS_VMS
  181. {
  182. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  183. out = BIO_push(tmpbio, out);
  184. }
  185. #endif
  186. }
  187. if (r <= 0)
  188. goto err;
  189. if (base64)
  190. {
  191. BIO *b64 = BIO_new(BIO_f_base64());
  192. if (b64 == NULL)
  193. goto err;
  194. out = BIO_push(b64, out);
  195. }
  196. while (num > 0)
  197. {
  198. unsigned char buf[4096];
  199. int chunk;
  200. chunk = num;
  201. if (chunk > (int)sizeof(buf))
  202. chunk = sizeof buf;
  203. r = RAND_bytes(buf, chunk);
  204. if (r <= 0)
  205. goto err;
  206. if (!hex)
  207. BIO_write(out, buf, chunk);
  208. else
  209. {
  210. for (i = 0; i < chunk; i++)
  211. BIO_printf(out, "%02x", buf[i]);
  212. }
  213. num -= chunk;
  214. }
  215. if (hex)
  216. BIO_puts(out, "\n");
  217. (void)BIO_flush(out);
  218. app_RAND_write_file(NULL, bio_err);
  219. ret = 0;
  220. err:
  221. ERR_print_errors(bio_err);
  222. if (out)
  223. BIO_free_all(out);
  224. apps_shutdown();
  225. OPENSSL_EXIT(ret);
  226. }