kdf.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright 2019-2021 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_COMMON,
  19. OPT_KDFOPT, OPT_BIN, OPT_KEYLEN, OPT_OUT,
  20. OPT_CIPHER, OPT_DIGEST, OPT_MAC,
  21. OPT_PROV_ENUM
  22. } OPTION_CHOICE;
  23. const OPTIONS kdf_options[] = {
  24. {OPT_HELP_STR, 1, '-', "Usage: %s [options] kdf_name\n"},
  25. OPT_SECTION("General"),
  26. {"help", OPT_HELP, '-', "Display this summary"},
  27. {"kdfopt", OPT_KDFOPT, 's', "KDF algorithm control parameters in n:v form"},
  28. {"cipher", OPT_CIPHER, 's', "Cipher"},
  29. {"digest", OPT_DIGEST, 's', "Digest"},
  30. {"mac", OPT_MAC, 's', "MAC"},
  31. {OPT_MORE_STR, 1, '-', "See 'Supported Controls' in the EVP_KDF_ docs\n"},
  32. {"keylen", OPT_KEYLEN, 's', "The size of the output derived key"},
  33. OPT_SECTION("Output"),
  34. {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
  35. {"binary", OPT_BIN, '-',
  36. "Output in binary format (default is hexadecimal)"},
  37. OPT_PROV_OPTIONS,
  38. OPT_PARAMETERS(),
  39. {"kdf_name", 0, 0, "Name of the KDF algorithm"},
  40. {NULL}
  41. };
  42. static char *alloc_kdf_algorithm_name(STACK_OF(OPENSSL_STRING) **optp,
  43. const char *name, const char *arg)
  44. {
  45. size_t len = strlen(name) + strlen(arg) + 2;
  46. char *res;
  47. if (*optp == NULL)
  48. *optp = sk_OPENSSL_STRING_new_null();
  49. if (*optp == NULL)
  50. return NULL;
  51. res = app_malloc(len, "algorithm name");
  52. BIO_snprintf(res, len, "%s:%s", name, arg);
  53. if (sk_OPENSSL_STRING_push(*optp, res))
  54. return res;
  55. OPENSSL_free(res);
  56. return NULL;
  57. }
  58. int kdf_main(int argc, char **argv)
  59. {
  60. int ret = 1, out_bin = 0;
  61. OPTION_CHOICE o;
  62. STACK_OF(OPENSSL_STRING) *opts = NULL;
  63. char *prog, *hexout = NULL;
  64. const char *outfile = NULL;
  65. unsigned char *dkm_bytes = NULL;
  66. size_t dkm_len = 0;
  67. BIO *out = NULL;
  68. EVP_KDF *kdf = NULL;
  69. EVP_KDF_CTX *ctx = NULL;
  70. char *digest = NULL, *cipher = NULL, *mac = NULL;
  71. prog = opt_init(argc, argv, kdf_options);
  72. while ((o = opt_next()) != OPT_EOF) {
  73. switch (o) {
  74. default:
  75. opthelp:
  76. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  77. goto err;
  78. case OPT_HELP:
  79. opt_help(kdf_options);
  80. ret = 0;
  81. goto err;
  82. case OPT_BIN:
  83. out_bin = 1;
  84. break;
  85. case OPT_KEYLEN:
  86. dkm_len = (size_t)atoi(opt_arg());
  87. break;
  88. case OPT_OUT:
  89. outfile = opt_arg();
  90. break;
  91. case OPT_KDFOPT:
  92. if (opts == NULL)
  93. opts = sk_OPENSSL_STRING_new_null();
  94. if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
  95. goto opthelp;
  96. break;
  97. case OPT_CIPHER:
  98. OPENSSL_free(cipher);
  99. cipher = alloc_kdf_algorithm_name(&opts, "cipher", opt_arg());
  100. if (cipher == NULL)
  101. goto opthelp;
  102. break;
  103. case OPT_DIGEST:
  104. OPENSSL_free(digest);
  105. digest = alloc_kdf_algorithm_name(&opts, "digest", opt_arg());
  106. if (digest == NULL)
  107. goto opthelp;
  108. break;
  109. case OPT_MAC:
  110. OPENSSL_free(mac);
  111. mac = alloc_kdf_algorithm_name(&opts, "mac", opt_arg());
  112. if (mac == NULL)
  113. goto opthelp;
  114. break;
  115. case OPT_PROV_CASES:
  116. if (!opt_provider(o))
  117. goto err;
  118. break;
  119. }
  120. }
  121. /* One argument, the KDF name. */
  122. argc = opt_num_rest();
  123. argv = opt_rest();
  124. if (argc != 1)
  125. goto opthelp;
  126. if ((kdf = EVP_KDF_fetch(NULL, argv[0], NULL)) == NULL) {
  127. BIO_printf(bio_err, "Invalid KDF name %s\n", argv[0]);
  128. goto opthelp;
  129. }
  130. ctx = EVP_KDF_CTX_new(kdf);
  131. if (ctx == NULL)
  132. goto err;
  133. if (opts != NULL) {
  134. int ok = 1;
  135. OSSL_PARAM *params =
  136. app_params_new_from_opts(opts, EVP_KDF_settable_ctx_params(kdf));
  137. if (params == NULL)
  138. goto err;
  139. if (!EVP_KDF_CTX_set_params(ctx, params)) {
  140. BIO_printf(bio_err, "KDF parameter error\n");
  141. ERR_print_errors(bio_err);
  142. ok = 0;
  143. }
  144. app_params_free(params);
  145. if (!ok)
  146. goto err;
  147. }
  148. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  149. if (out == NULL)
  150. goto err;
  151. if (dkm_len <= 0) {
  152. BIO_printf(bio_err, "Invalid derived key length.\n");
  153. goto err;
  154. }
  155. dkm_bytes = app_malloc(dkm_len, "out buffer");
  156. if (dkm_bytes == NULL)
  157. goto err;
  158. if (!EVP_KDF_derive(ctx, dkm_bytes, dkm_len, NULL)) {
  159. BIO_printf(bio_err, "EVP_KDF_derive failed\n");
  160. goto err;
  161. }
  162. if (out_bin) {
  163. BIO_write(out, dkm_bytes, dkm_len);
  164. } else {
  165. hexout = OPENSSL_buf2hexstr(dkm_bytes, dkm_len);
  166. if (hexout == NULL) {
  167. BIO_printf(bio_err, "Memory allocation failure\n");
  168. goto err;
  169. }
  170. BIO_printf(out, "%s\n\n", hexout);
  171. }
  172. ret = 0;
  173. err:
  174. if (ret != 0)
  175. ERR_print_errors(bio_err);
  176. OPENSSL_clear_free(dkm_bytes, dkm_len);
  177. sk_OPENSSL_STRING_free(opts);
  178. EVP_KDF_free(kdf);
  179. EVP_KDF_CTX_free(ctx);
  180. BIO_free(out);
  181. OPENSSL_free(hexout);
  182. OPENSSL_free(cipher);
  183. OPENSSL_free(digest);
  184. OPENSSL_free(mac);
  185. return ret;
  186. }