kdf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /* One argument, the KDF name. */
  83. argc = opt_num_rest();
  84. argv = opt_rest();
  85. if (argc != 1)
  86. goto opthelp;
  87. if ((kdf = EVP_KDF_fetch(NULL, argv[0], NULL)) == NULL) {
  88. BIO_printf(bio_err, "Invalid KDF name %s\n", argv[0]);
  89. goto opthelp;
  90. }
  91. ctx = EVP_KDF_CTX_new(kdf);
  92. if (ctx == NULL)
  93. goto err;
  94. if (opts != NULL) {
  95. int ok = 1;
  96. OSSL_PARAM *params =
  97. app_params_new_from_opts(opts, EVP_KDF_settable_ctx_params(kdf));
  98. if (params == NULL)
  99. goto err;
  100. if (!EVP_KDF_CTX_set_params(ctx, params)) {
  101. BIO_printf(bio_err, "KDF parameter error\n");
  102. ERR_print_errors(bio_err);
  103. ok = 0;
  104. }
  105. app_params_free(params);
  106. if (!ok)
  107. goto err;
  108. }
  109. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  110. if (out == NULL)
  111. goto err;
  112. if (dkm_len <= 0) {
  113. BIO_printf(bio_err, "Invalid derived key length.\n");
  114. goto err;
  115. }
  116. dkm_bytes = app_malloc(dkm_len, "out buffer");
  117. if (dkm_bytes == NULL)
  118. goto err;
  119. if (!EVP_KDF_derive(ctx, dkm_bytes, dkm_len)) {
  120. BIO_printf(bio_err, "EVP_KDF_derive failed\n");
  121. goto err;
  122. }
  123. if (out_bin) {
  124. BIO_write(out, dkm_bytes, dkm_len);
  125. } else {
  126. hexout = OPENSSL_buf2hexstr(dkm_bytes, dkm_len);
  127. if (hexout == NULL) {
  128. BIO_printf(bio_err, "Memory allocation failure\n");
  129. goto err;
  130. }
  131. BIO_printf(out, "%s\n\n", hexout);
  132. }
  133. ret = 0;
  134. err:
  135. if (ret != 0)
  136. ERR_print_errors(bio_err);
  137. OPENSSL_clear_free(dkm_bytes, dkm_len);
  138. sk_OPENSSL_STRING_free(opts);
  139. EVP_KDF_free(kdf);
  140. EVP_KDF_CTX_free(ctx);
  141. BIO_free(out);
  142. OPENSSL_free(hexout);
  143. return ret;
  144. }