rand.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <errno.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <limits.h>
  14. #include <gmssl/mem.h>
  15. #include <gmssl/rand.h>
  16. static const char *options = "[-hex] [-rdrand] -outlen num [-out file]";
  17. int rand_main(int argc, char **argv)
  18. {
  19. int ret = 1;
  20. char *prog = argv[0];
  21. int hex = 0;
  22. int rdrand = 0;
  23. int outlen = 0;
  24. char *outfile = NULL;
  25. FILE *outfp = stdout;
  26. uint8_t buf[2048];
  27. argc--;
  28. argv++;
  29. if (argc < 1) {
  30. fprintf(stderr, "usage: %s %s\n", prog, options);
  31. return 1;
  32. }
  33. while (argc > 0) {
  34. if (!strcmp(*argv, "-help")) {
  35. printf("usage: %s %s\n", prog, options);
  36. ret = 0;
  37. goto end;
  38. } else if (!strcmp(*argv, "-hex")) {
  39. hex = 1;
  40. } else if (!strcmp(*argv, "-rdrand")) {
  41. rdrand = 1;
  42. } else if (!strcmp(*argv, "-outlen")) {
  43. if (--argc < 1) goto bad;
  44. outlen = atoi(*(++argv));
  45. if (outlen < 1 || outlen > INT_MAX) {
  46. fprintf(stderr, "%s: invalid outlen\n", prog);
  47. goto end;
  48. }
  49. } else if (!strcmp(*argv, "-out")) {
  50. if (--argc < 1) goto bad;
  51. outfile = *(++argv);
  52. if (!(outfp = fopen(outfile, "wb"))) {
  53. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  54. goto end;
  55. }
  56. } else {
  57. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  58. goto end;
  59. bad:
  60. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  61. goto end;
  62. }
  63. argc--;
  64. argv++;
  65. }
  66. if (!outlen) {
  67. fprintf(stderr, "%s: option -outlen missing\n", prog);
  68. goto end;
  69. }
  70. while (outlen > 0) {
  71. size_t len = outlen < sizeof(buf) ? outlen : sizeof(buf);
  72. if (rdrand) {
  73. /*
  74. if (rdrand_bytes(buf, len) != 1) {
  75. fprintf(stderr, "%s: inner error\n", prog);
  76. goto end;
  77. }
  78. */
  79. } else {
  80. if (rand_bytes(buf, len) != 1) {
  81. fprintf(stderr, "%s: inner error\n", prog);
  82. goto end;
  83. }
  84. }
  85. if (hex) {
  86. int i;
  87. for (i = 0; i < len; i++) {
  88. fprintf(outfp, "%02X", buf[i]);
  89. }
  90. } else {
  91. if (fwrite(buf, 1, len, outfp) != len) {
  92. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  93. goto end;
  94. }
  95. }
  96. outlen -= (int)len;
  97. }
  98. if (hex) {
  99. fprintf(outfp, "\n");
  100. }
  101. ret = 0;
  102. end:
  103. gmssl_secure_clear(buf, sizeof(buf));
  104. if (outfile && outfp) fclose(outfp);
  105. return ret;
  106. }