2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
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
14 #include <openssl/bio.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/kdf.h>
18 #include <openssl/params.h>
20 typedef enum OPTION_choice {
22 OPT_KDFOPT, OPT_BIN, OPT_KEYLEN, OPT_OUT,
23 OPT_CIPHER, OPT_DIGEST, OPT_MAC,
27 const OPTIONS kdf_options[] = {
28 {OPT_HELP_STR, 1, '-', "Usage: %s [options] kdf_name\n"},
30 OPT_SECTION("General"),
31 {"help", OPT_HELP, '-', "Display this summary"},
32 {"kdfopt", OPT_KDFOPT, 's', "KDF algorithm control parameters in n:v form"},
33 {"cipher", OPT_CIPHER, 's', "Cipher"},
34 {"digest", OPT_DIGEST, 's', "Digest"},
35 {"mac", OPT_MAC, 's', "MAC"},
36 {OPT_MORE_STR, 1, '-', "See 'Supported Controls' in the EVP_KDF_ docs\n"},
37 {"keylen", OPT_KEYLEN, 's', "The size of the output derived key"},
39 OPT_SECTION("Output"),
40 {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
41 {"binary", OPT_BIN, '-',
42 "Output in binary format (default is hexadecimal)"},
47 {"kdf_name", 0, 0, "Name of the KDF algorithm"},
51 static char *alloc_kdf_algorithm_name(STACK_OF(OPENSSL_STRING) **optp,
52 const char *name, const char *arg)
54 size_t len = strlen(name) + strlen(arg) + 2;
58 *optp = sk_OPENSSL_STRING_new_null();
62 res = app_malloc(len, "algorithm name");
63 BIO_snprintf(res, len, "%s:%s", name, arg);
64 if (sk_OPENSSL_STRING_push(*optp, res))
70 int kdf_main(int argc, char **argv)
72 int ret = 1, out_bin = 0;
74 STACK_OF(OPENSSL_STRING) *opts = NULL;
75 char *prog, *hexout = NULL;
76 const char *outfile = NULL;
77 unsigned char *dkm_bytes = NULL;
81 EVP_KDF_CTX *ctx = NULL;
82 char *digest = NULL, *cipher = NULL, *mac = NULL;
84 prog = opt_init(argc, argv, kdf_options);
85 while ((o = opt_next()) != OPT_EOF) {
89 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
92 opt_help(kdf_options);
99 dkm_len = (size_t)atoi(opt_arg());
106 opts = sk_OPENSSL_STRING_new_null();
107 if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
111 OPENSSL_free(cipher);
112 cipher = alloc_kdf_algorithm_name(&opts, "cipher", opt_arg());
117 OPENSSL_free(digest);
118 digest = alloc_kdf_algorithm_name(&opts, "digest", opt_arg());
124 mac = alloc_kdf_algorithm_name(&opts, "mac", opt_arg());
129 if (!opt_provider(o))
135 /* One argument, the KDF name. */
136 argc = opt_num_rest();
141 if ((kdf = EVP_KDF_fetch(app_get0_libctx(), argv[0],
142 app_get0_propq())) == NULL) {
143 BIO_printf(bio_err, "Invalid KDF name %s\n", argv[0]);
147 ctx = EVP_KDF_CTX_new(kdf);
154 app_params_new_from_opts(opts, EVP_KDF_settable_ctx_params(kdf));
159 if (!EVP_KDF_CTX_set_params(ctx, params)) {
160 BIO_printf(bio_err, "KDF parameter error\n");
161 ERR_print_errors(bio_err);
164 app_params_free(params);
169 out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
174 BIO_printf(bio_err, "Invalid derived key length.\n");
177 dkm_bytes = app_malloc(dkm_len, "out buffer");
178 if (dkm_bytes == NULL)
181 if (!EVP_KDF_derive(ctx, dkm_bytes, dkm_len, NULL)) {
182 BIO_printf(bio_err, "EVP_KDF_derive failed\n");
187 BIO_write(out, dkm_bytes, dkm_len);
189 hexout = OPENSSL_buf2hexstr(dkm_bytes, dkm_len);
190 if (hexout == NULL) {
191 BIO_printf(bio_err, "Memory allocation failure\n");
194 BIO_printf(out, "%s\n\n", hexout);
200 ERR_print_errors(bio_err);
201 OPENSSL_clear_free(dkm_bytes, dkm_len);
202 sk_OPENSSL_STRING_free(opts);
204 EVP_KDF_CTX_free(ctx);
206 OPENSSL_free(hexout);
207 OPENSSL_free(cipher);
208 OPENSSL_free(digest);