pbkdf2.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <gmssl/mem.h>
  14. #include <gmssl/hex.h>
  15. #include <gmssl/pbkdf2.h>
  16. static const char *options = "-pass str -salt hex -iter num -outlen num [-bin|-hex] [-out file]";
  17. int pbkdf2_main(int argc, char **argv)
  18. {
  19. int ret = 1;
  20. char *prog = argv[0];
  21. char *pass = NULL;
  22. char *salthex = NULL;
  23. uint8_t salt[PBKDF2_MAX_SALT_SIZE];
  24. size_t saltlen;
  25. int iter = 0;
  26. int outlen = 0;
  27. int bin = 0;
  28. char *outfile = NULL;
  29. uint8_t outbuf[64];
  30. FILE *outfp = stdout;
  31. int i;
  32. argc--;
  33. argv++;
  34. if (argc < 1) {
  35. fprintf(stderr, "usage: %s %s\n", prog, options);
  36. return 1;
  37. }
  38. while (argc > 0) {
  39. if (!strcmp(*argv, "-help")) {
  40. printf("usage: %s %s\n", prog, options);
  41. ret = 0;
  42. goto end;
  43. } else if (!strcmp(*argv, "-pass")) {
  44. if (--argc < 1) goto bad;
  45. pass = *(++argv);
  46. } else if (!strcmp(*argv, "-salt")) {
  47. if (--argc < 1) goto bad;
  48. salthex = *(++argv);
  49. if (strlen(salthex) > sizeof(salt) * 2) {
  50. fprintf(stderr, "%s: invalid salt length\n", prog);
  51. goto end;
  52. }
  53. if (hex_to_bytes(salthex, strlen(salthex), salt, &saltlen) != 1) {
  54. fprintf(stderr, "%s: invalid HEX digits\n", prog);
  55. goto end;
  56. }
  57. } else if (!strcmp(*argv, "-iter")) {
  58. if (--argc < 1) goto bad;
  59. iter = atoi(*(++argv));
  60. if (iter < PBKDF2_MIN_ITER || iter > INT_MAX) {
  61. fprintf(stderr, "%s: invalid '-iter' value\n", prog);
  62. goto end;
  63. }
  64. } else if (!strcmp(*argv, "-outlen")) {
  65. if (--argc < 1) goto bad;
  66. outlen = atoi(*(++argv));
  67. if (outlen < 1 || outlen > sizeof(outbuf)) {
  68. fprintf(stderr, "%s: invalid outlen\n", prog);
  69. goto end;
  70. }
  71. } else if (!strcmp(*argv, "-hex")) {
  72. bin = 0;
  73. } else if (!strcmp(*argv, "-bin")) {
  74. bin = 1;
  75. } else if (!strcmp(*argv, "-out")) {
  76. if (--argc < 1) goto bad;
  77. outfile = *(++argv);
  78. if (!(outfp = fopen(outfile, "wb"))) {
  79. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  80. goto end;
  81. }
  82. } else {
  83. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  84. goto end;
  85. bad:
  86. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  87. goto end;
  88. }
  89. argc--;
  90. argv++;
  91. }
  92. if (!pass) {
  93. fprintf(stderr, "%s: option '-pass' required\n", prog);
  94. goto end;
  95. }
  96. if (!salthex) {
  97. fprintf(stderr, "%s: option '-salt' required\n", prog);
  98. goto end;
  99. }
  100. if (!iter) {
  101. fprintf(stderr, "%s: option '-iter' required\n", prog);
  102. goto end;
  103. }
  104. if (!outlen) {
  105. fprintf(stderr, "%s: option '-outlen' required\n", prog);
  106. goto end;
  107. }
  108. if (pbkdf2_hmac_sm3_genkey(pass, strlen(pass), salt, saltlen, iter, outlen, outbuf) != 1) {
  109. fprintf(stderr, "%s: inner error\n", prog);
  110. goto end;
  111. }
  112. if (bin) {
  113. if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
  114. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  115. goto end;
  116. }
  117. } else {
  118. for (i = 0; i < outlen; i++) {
  119. fprintf(outfp, "%02x", outbuf[i]);
  120. }
  121. fprintf(outfp, "\n");
  122. }
  123. ret = 0;
  124. end:
  125. gmssl_secure_clear(outbuf, sizeof(outbuf));
  126. gmssl_secure_clear(salt, sizeof(salt));
  127. if (outfile && outfp) fclose(outfp);
  128. return ret;
  129. }