kdf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright 2019-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 <string.h>
  10. #include "apps.h"
  11. #include "progs.h"
  12. #include <openssl/bio.h>
  13. #include <openssl/err.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/kdf.h>
  16. #include <openssl/params.h>
  17. typedef enum OPTION_choice {
  18. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  19. OPT_KDFOPT, OPT_BIN, OPT_KEYLEN, OPT_OUT,
  20. OPT_PROV_ENUM
  21. } OPTION_CHOICE;
  22. const OPTIONS kdf_options[] = {
  23. {OPT_HELP_STR, 1, '-', "Usage: %s [options] kdf_name\n"},
  24. OPT_SECTION("General"),
  25. {"help", OPT_HELP, '-', "Display this summary"},
  26. {"kdfopt", OPT_KDFOPT, 's', "KDF algorithm control parameters in n:v form"},
  27. {OPT_MORE_STR, 1, '-', "See 'Supported Controls' in the EVP_KDF_ docs\n"},
  28. {"keylen", OPT_KEYLEN, 's', "The size of the output derived key"},
  29. OPT_SECTION("Output"),
  30. {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
  31. {"binary", OPT_BIN, '-',
  32. "Output in binary format (default is hexadecimal)"},
  33. OPT_PROV_OPTIONS,
  34. OPT_PARAMETERS(),
  35. {"kdf_name", 0, 0, "Name of the KDF algorithm"},
  36. {NULL}
  37. };
  38. int kdf_main(int argc, char **argv)
  39. {
  40. int ret = 1, out_bin = 0;
  41. OPTION_CHOICE o;
  42. STACK_OF(OPENSSL_STRING) *opts = NULL;
  43. char *prog, *hexout = NULL;
  44. const char *outfile = NULL;
  45. unsigned char *dkm_bytes = NULL;
  46. size_t dkm_len = 0;
  47. BIO *out = NULL;
  48. EVP_KDF *kdf = NULL;
  49. EVP_KDF_CTX *ctx = NULL;
  50. prog = opt_init(argc, argv, kdf_options);
  51. while ((o = opt_next()) != OPT_EOF) {
  52. switch (o) {
  53. default:
  54. opthelp:
  55. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  56. goto err;
  57. case OPT_HELP:
  58. opt_help(kdf_options);
  59. ret = 0;
  60. goto err;
  61. case OPT_BIN:
  62. out_bin = 1;
  63. break;
  64. case OPT_KEYLEN:
  65. dkm_len = (size_t)atoi(opt_arg());
  66. break;
  67. case OPT_OUT:
  68. outfile = opt_arg();
  69. break;
  70. case OPT_KDFOPT:
  71. if (opts == NULL)
  72. opts = sk_OPENSSL_STRING_new_null();
  73. if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
  74. goto opthelp;
  75. break;
  76. case OPT_PROV_CASES:
  77. if (!opt_provider(o))
  78. goto err;
  79. break;
  80. }
  81. }
  82. argc = opt_num_rest();
  83. argv = opt_rest();
  84. if (argc != 1) {
  85. BIO_printf(bio_err, "Invalid number of extra arguments\n");
  86. goto opthelp;
  87. }
  88. if ((kdf = EVP_KDF_fetch(NULL, argv[0], NULL)) == NULL) {
  89. BIO_printf(bio_err, "Invalid KDF name %s\n", argv[0]);
  90. goto opthelp;
  91. }
  92. ctx = EVP_KDF_CTX_new(kdf);
  93. if (ctx == NULL)
  94. goto err;
  95. if (opts != NULL) {
  96. int ok = 1;
  97. OSSL_PARAM *params =
  98. app_params_new_from_opts(opts, EVP_KDF_settable_ctx_params(kdf));
  99. if (params == NULL)
  100. goto err;
  101. if (!EVP_KDF_CTX_set_params(ctx, params)) {
  102. BIO_printf(bio_err, "KDF parameter error\n");
  103. ERR_print_errors(bio_err);
  104. ok = 0;
  105. }
  106. app_params_free(params);
  107. if (!ok)
  108. goto err;
  109. }
  110. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  111. if (out == NULL)
  112. goto err;
  113. if (dkm_len <= 0) {
  114. BIO_printf(bio_err, "Invalid derived key length.\n");
  115. goto err;
  116. }
  117. dkm_bytes = app_malloc(dkm_len, "out buffer");
  118. if (dkm_bytes == NULL)
  119. goto err;
  120. if (!EVP_KDF_derive(ctx, dkm_bytes, dkm_len)) {
  121. BIO_printf(bio_err, "EVP_KDF_derive failed\n");
  122. goto err;
  123. }
  124. if (out_bin) {
  125. BIO_write(out, dkm_bytes, dkm_len);
  126. } else {
  127. hexout = OPENSSL_buf2hexstr(dkm_bytes, dkm_len);
  128. if (hexout == NULL) {
  129. BIO_printf(bio_err, "Memory allocation failure\n");
  130. goto err;
  131. }
  132. BIO_printf(out, "%s\n\n", hexout);
  133. }
  134. ret = 0;
  135. err:
  136. if (ret != 0)
  137. ERR_print_errors(bio_err);
  138. OPENSSL_clear_free(dkm_bytes, dkm_len);
  139. sk_OPENSSL_STRING_free(opts);
  140. EVP_KDF_free(kdf);
  141. EVP_KDF_CTX_free(ctx);
  142. BIO_free(out);
  143. OPENSSL_free(hexout);
  144. return ret;
  145. }