1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery */
9*b077aed3SPierre Pronchery
10*b077aed3SPierre Pronchery #include <string.h>
11*b077aed3SPierre Pronchery
12*b077aed3SPierre Pronchery #include "apps.h"
13*b077aed3SPierre Pronchery #include "progs.h"
14*b077aed3SPierre Pronchery #include <openssl/bio.h>
15*b077aed3SPierre Pronchery #include <openssl/err.h>
16*b077aed3SPierre Pronchery #include <openssl/evp.h>
17*b077aed3SPierre Pronchery #include <openssl/kdf.h>
18*b077aed3SPierre Pronchery #include <openssl/params.h>
19*b077aed3SPierre Pronchery
20*b077aed3SPierre Pronchery typedef enum OPTION_choice {
21*b077aed3SPierre Pronchery OPT_COMMON,
22*b077aed3SPierre Pronchery OPT_KDFOPT, OPT_BIN, OPT_KEYLEN, OPT_OUT,
23*b077aed3SPierre Pronchery OPT_CIPHER, OPT_DIGEST, OPT_MAC,
24*b077aed3SPierre Pronchery OPT_PROV_ENUM
25*b077aed3SPierre Pronchery } OPTION_CHOICE;
26*b077aed3SPierre Pronchery
27*b077aed3SPierre Pronchery const OPTIONS kdf_options[] = {
28*b077aed3SPierre Pronchery {OPT_HELP_STR, 1, '-', "Usage: %s [options] kdf_name\n"},
29*b077aed3SPierre Pronchery
30*b077aed3SPierre Pronchery OPT_SECTION("General"),
31*b077aed3SPierre Pronchery {"help", OPT_HELP, '-', "Display this summary"},
32*b077aed3SPierre Pronchery {"kdfopt", OPT_KDFOPT, 's', "KDF algorithm control parameters in n:v form"},
33*b077aed3SPierre Pronchery {"cipher", OPT_CIPHER, 's', "Cipher"},
34*b077aed3SPierre Pronchery {"digest", OPT_DIGEST, 's', "Digest"},
35*b077aed3SPierre Pronchery {"mac", OPT_MAC, 's', "MAC"},
36*b077aed3SPierre Pronchery {OPT_MORE_STR, 1, '-', "See 'Supported Controls' in the EVP_KDF_ docs\n"},
37*b077aed3SPierre Pronchery {"keylen", OPT_KEYLEN, 's', "The size of the output derived key"},
38*b077aed3SPierre Pronchery
39*b077aed3SPierre Pronchery OPT_SECTION("Output"),
40*b077aed3SPierre Pronchery {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
41*b077aed3SPierre Pronchery {"binary", OPT_BIN, '-',
42*b077aed3SPierre Pronchery "Output in binary format (default is hexadecimal)"},
43*b077aed3SPierre Pronchery
44*b077aed3SPierre Pronchery OPT_PROV_OPTIONS,
45*b077aed3SPierre Pronchery
46*b077aed3SPierre Pronchery OPT_PARAMETERS(),
47*b077aed3SPierre Pronchery {"kdf_name", 0, 0, "Name of the KDF algorithm"},
48*b077aed3SPierre Pronchery {NULL}
49*b077aed3SPierre Pronchery };
50*b077aed3SPierre Pronchery
alloc_kdf_algorithm_name(STACK_OF (OPENSSL_STRING)** optp,const char * name,const char * arg)51*b077aed3SPierre Pronchery static char *alloc_kdf_algorithm_name(STACK_OF(OPENSSL_STRING) **optp,
52*b077aed3SPierre Pronchery const char *name, const char *arg)
53*b077aed3SPierre Pronchery {
54*b077aed3SPierre Pronchery size_t len = strlen(name) + strlen(arg) + 2;
55*b077aed3SPierre Pronchery char *res;
56*b077aed3SPierre Pronchery
57*b077aed3SPierre Pronchery if (*optp == NULL)
58*b077aed3SPierre Pronchery *optp = sk_OPENSSL_STRING_new_null();
59*b077aed3SPierre Pronchery if (*optp == NULL)
60*b077aed3SPierre Pronchery return NULL;
61*b077aed3SPierre Pronchery
62*b077aed3SPierre Pronchery res = app_malloc(len, "algorithm name");
63*b077aed3SPierre Pronchery BIO_snprintf(res, len, "%s:%s", name, arg);
64*b077aed3SPierre Pronchery if (sk_OPENSSL_STRING_push(*optp, res))
65*b077aed3SPierre Pronchery return res;
66*b077aed3SPierre Pronchery OPENSSL_free(res);
67*b077aed3SPierre Pronchery return NULL;
68*b077aed3SPierre Pronchery }
69*b077aed3SPierre Pronchery
kdf_main(int argc,char ** argv)70*b077aed3SPierre Pronchery int kdf_main(int argc, char **argv)
71*b077aed3SPierre Pronchery {
72*b077aed3SPierre Pronchery int ret = 1, out_bin = 0;
73*b077aed3SPierre Pronchery OPTION_CHOICE o;
74*b077aed3SPierre Pronchery STACK_OF(OPENSSL_STRING) *opts = NULL;
75*b077aed3SPierre Pronchery char *prog, *hexout = NULL;
76*b077aed3SPierre Pronchery const char *outfile = NULL;
77*b077aed3SPierre Pronchery unsigned char *dkm_bytes = NULL;
78*b077aed3SPierre Pronchery size_t dkm_len = 0;
79*b077aed3SPierre Pronchery BIO *out = NULL;
80*b077aed3SPierre Pronchery EVP_KDF *kdf = NULL;
81*b077aed3SPierre Pronchery EVP_KDF_CTX *ctx = NULL;
82*b077aed3SPierre Pronchery char *digest = NULL, *cipher = NULL, *mac = NULL;
83*b077aed3SPierre Pronchery
84*b077aed3SPierre Pronchery prog = opt_init(argc, argv, kdf_options);
85*b077aed3SPierre Pronchery while ((o = opt_next()) != OPT_EOF) {
86*b077aed3SPierre Pronchery switch (o) {
87*b077aed3SPierre Pronchery default:
88*b077aed3SPierre Pronchery opthelp:
89*b077aed3SPierre Pronchery BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
90*b077aed3SPierre Pronchery goto err;
91*b077aed3SPierre Pronchery case OPT_HELP:
92*b077aed3SPierre Pronchery opt_help(kdf_options);
93*b077aed3SPierre Pronchery ret = 0;
94*b077aed3SPierre Pronchery goto err;
95*b077aed3SPierre Pronchery case OPT_BIN:
96*b077aed3SPierre Pronchery out_bin = 1;
97*b077aed3SPierre Pronchery break;
98*b077aed3SPierre Pronchery case OPT_KEYLEN:
99*b077aed3SPierre Pronchery dkm_len = (size_t)atoi(opt_arg());
100*b077aed3SPierre Pronchery break;
101*b077aed3SPierre Pronchery case OPT_OUT:
102*b077aed3SPierre Pronchery outfile = opt_arg();
103*b077aed3SPierre Pronchery break;
104*b077aed3SPierre Pronchery case OPT_KDFOPT:
105*b077aed3SPierre Pronchery if (opts == NULL)
106*b077aed3SPierre Pronchery opts = sk_OPENSSL_STRING_new_null();
107*b077aed3SPierre Pronchery if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
108*b077aed3SPierre Pronchery goto opthelp;
109*b077aed3SPierre Pronchery break;
110*b077aed3SPierre Pronchery case OPT_CIPHER:
111*b077aed3SPierre Pronchery OPENSSL_free(cipher);
112*b077aed3SPierre Pronchery cipher = alloc_kdf_algorithm_name(&opts, "cipher", opt_arg());
113*b077aed3SPierre Pronchery if (cipher == NULL)
114*b077aed3SPierre Pronchery goto opthelp;
115*b077aed3SPierre Pronchery break;
116*b077aed3SPierre Pronchery case OPT_DIGEST:
117*b077aed3SPierre Pronchery OPENSSL_free(digest);
118*b077aed3SPierre Pronchery digest = alloc_kdf_algorithm_name(&opts, "digest", opt_arg());
119*b077aed3SPierre Pronchery if (digest == NULL)
120*b077aed3SPierre Pronchery goto opthelp;
121*b077aed3SPierre Pronchery break;
122*b077aed3SPierre Pronchery case OPT_MAC:
123*b077aed3SPierre Pronchery OPENSSL_free(mac);
124*b077aed3SPierre Pronchery mac = alloc_kdf_algorithm_name(&opts, "mac", opt_arg());
125*b077aed3SPierre Pronchery if (mac == NULL)
126*b077aed3SPierre Pronchery goto opthelp;
127*b077aed3SPierre Pronchery break;
128*b077aed3SPierre Pronchery case OPT_PROV_CASES:
129*b077aed3SPierre Pronchery if (!opt_provider(o))
130*b077aed3SPierre Pronchery goto err;
131*b077aed3SPierre Pronchery break;
132*b077aed3SPierre Pronchery }
133*b077aed3SPierre Pronchery }
134*b077aed3SPierre Pronchery
135*b077aed3SPierre Pronchery /* One argument, the KDF name. */
136*b077aed3SPierre Pronchery argc = opt_num_rest();
137*b077aed3SPierre Pronchery argv = opt_rest();
138*b077aed3SPierre Pronchery if (argc != 1)
139*b077aed3SPierre Pronchery goto opthelp;
140*b077aed3SPierre Pronchery
141*b077aed3SPierre Pronchery if ((kdf = EVP_KDF_fetch(app_get0_libctx(), argv[0],
142*b077aed3SPierre Pronchery app_get0_propq())) == NULL) {
143*b077aed3SPierre Pronchery BIO_printf(bio_err, "Invalid KDF name %s\n", argv[0]);
144*b077aed3SPierre Pronchery goto opthelp;
145*b077aed3SPierre Pronchery }
146*b077aed3SPierre Pronchery
147*b077aed3SPierre Pronchery ctx = EVP_KDF_CTX_new(kdf);
148*b077aed3SPierre Pronchery if (ctx == NULL)
149*b077aed3SPierre Pronchery goto err;
150*b077aed3SPierre Pronchery
151*b077aed3SPierre Pronchery if (opts != NULL) {
152*b077aed3SPierre Pronchery int ok = 1;
153*b077aed3SPierre Pronchery OSSL_PARAM *params =
154*b077aed3SPierre Pronchery app_params_new_from_opts(opts, EVP_KDF_settable_ctx_params(kdf));
155*b077aed3SPierre Pronchery
156*b077aed3SPierre Pronchery if (params == NULL)
157*b077aed3SPierre Pronchery goto err;
158*b077aed3SPierre Pronchery
159*b077aed3SPierre Pronchery if (!EVP_KDF_CTX_set_params(ctx, params)) {
160*b077aed3SPierre Pronchery BIO_printf(bio_err, "KDF parameter error\n");
161*b077aed3SPierre Pronchery ERR_print_errors(bio_err);
162*b077aed3SPierre Pronchery ok = 0;
163*b077aed3SPierre Pronchery }
164*b077aed3SPierre Pronchery app_params_free(params);
165*b077aed3SPierre Pronchery if (!ok)
166*b077aed3SPierre Pronchery goto err;
167*b077aed3SPierre Pronchery }
168*b077aed3SPierre Pronchery
169*b077aed3SPierre Pronchery out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
170*b077aed3SPierre Pronchery if (out == NULL)
171*b077aed3SPierre Pronchery goto err;
172*b077aed3SPierre Pronchery
173*b077aed3SPierre Pronchery if (dkm_len <= 0) {
174*b077aed3SPierre Pronchery BIO_printf(bio_err, "Invalid derived key length.\n");
175*b077aed3SPierre Pronchery goto err;
176*b077aed3SPierre Pronchery }
177*b077aed3SPierre Pronchery dkm_bytes = app_malloc(dkm_len, "out buffer");
178*b077aed3SPierre Pronchery if (dkm_bytes == NULL)
179*b077aed3SPierre Pronchery goto err;
180*b077aed3SPierre Pronchery
181*b077aed3SPierre Pronchery if (!EVP_KDF_derive(ctx, dkm_bytes, dkm_len, NULL)) {
182*b077aed3SPierre Pronchery BIO_printf(bio_err, "EVP_KDF_derive failed\n");
183*b077aed3SPierre Pronchery goto err;
184*b077aed3SPierre Pronchery }
185*b077aed3SPierre Pronchery
186*b077aed3SPierre Pronchery if (out_bin) {
187*b077aed3SPierre Pronchery BIO_write(out, dkm_bytes, dkm_len);
188*b077aed3SPierre Pronchery } else {
189*b077aed3SPierre Pronchery hexout = OPENSSL_buf2hexstr(dkm_bytes, dkm_len);
190*b077aed3SPierre Pronchery if (hexout == NULL) {
191*b077aed3SPierre Pronchery BIO_printf(bio_err, "Memory allocation failure\n");
192*b077aed3SPierre Pronchery goto err;
193*b077aed3SPierre Pronchery }
194*b077aed3SPierre Pronchery BIO_printf(out, "%s\n\n", hexout);
195*b077aed3SPierre Pronchery }
196*b077aed3SPierre Pronchery
197*b077aed3SPierre Pronchery ret = 0;
198*b077aed3SPierre Pronchery err:
199*b077aed3SPierre Pronchery if (ret != 0)
200*b077aed3SPierre Pronchery ERR_print_errors(bio_err);
201*b077aed3SPierre Pronchery OPENSSL_clear_free(dkm_bytes, dkm_len);
202*b077aed3SPierre Pronchery sk_OPENSSL_STRING_free(opts);
203*b077aed3SPierre Pronchery EVP_KDF_free(kdf);
204*b077aed3SPierre Pronchery EVP_KDF_CTX_free(ctx);
205*b077aed3SPierre Pronchery BIO_free(out);
206*b077aed3SPierre Pronchery OPENSSL_free(hexout);
207*b077aed3SPierre Pronchery OPENSSL_free(cipher);
208*b077aed3SPierre Pronchery OPENSSL_free(digest);
209*b077aed3SPierre Pronchery OPENSSL_free(mac);
210*b077aed3SPierre Pronchery return ret;
211*b077aed3SPierre Pronchery }
212