Skip to content

Commit

Permalink
Added app for EVP_KDF
Browse files Browse the repository at this point in the history
Reviewed-by: Tim Hudson <[email protected]>
Reviewed-by: Richard Levitte <[email protected]>
(Merged from openssl#8762)
  • Loading branch information
slontis authored and levitte committed Apr 24, 2019
1 parent bacc308 commit c54492e
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/build.info
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
qw(openssl.c
asn1pars.c ca.c ciphers.c cms.c crl.c crl2p7.c dgst.c dhparam.c
dsa.c dsaparam.c ec.c ecparam.c enc.c engine.c errstr.c gendsa.c
genpkey.c genrsa.c mac.c nseq.c ocsp.c passwd.c pkcs12.c pkcs7.c
genpkey.c genrsa.c kdf.c mac.c nseq.c ocsp.c passwd.c pkcs12.c pkcs7.c
pkcs8.c pkey.c pkeyparam.c pkeyutl.c prime.c rand.c req.c rsa.c
rsautl.c s_client.c s_server.c s_time.c sess_id.c smime.c speed.c
spkac.c srp.c ts.c verify.c version.c x509.c rehash.c storeutl.c
Expand Down
158 changes: 158 additions & 0 deletions apps/kdf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

#include <string.h>

#include "apps.h"
#include "progs.h"
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/kdf.h>

typedef enum OPTION_choice {
OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
OPT_KDFOPT, OPT_BIN, OPT_KEYLEN, OPT_OUT
} OPTION_CHOICE;

const OPTIONS kdf_options[] = {
{OPT_HELP_STR, 1, '-', "Usage: %s [options] kdf_name\n"},
{OPT_HELP_STR, 1, '-', "kdf_name\t KDF algorithm.\n"},
{"help", OPT_HELP, '-', "Display this summary"},
{"kdfopt", OPT_KDFOPT, 's', "KDF algorithm control parameters in n:v form. "
"See 'Supported Controls' in the EVP_KDF_ docs"},
{"keylen", OPT_KEYLEN, 's', "The size of the output derived key"},
{"out", OPT_OUT, '>', "Output to filename rather than stdout"},
{"binary", OPT_BIN, '-', "Output in binary format (Default is hexadecimal "
"output)"},
{NULL}
};

static int kdf_ctrl_string(EVP_KDF_CTX *ctx, const char *value)
{
int rv;
char *stmp, *vtmp = NULL;

stmp = OPENSSL_strdup(value);
if (stmp == NULL)
return -1;
vtmp = strchr(stmp, ':');
if (vtmp != NULL) {
*vtmp = 0;
vtmp++;
}
rv = EVP_KDF_ctrl_str(ctx, stmp, vtmp);
OPENSSL_free(stmp);
return rv;
}

int kdf_main(int argc, char **argv)
{
int ret = 1, i, id, out_bin = 0;
OPTION_CHOICE o;
STACK_OF(OPENSSL_STRING) *opts = NULL;
char *prog, *hexout = NULL;
const char *outfile = NULL;
unsigned char *dkm_bytes = NULL;
size_t dkm_len = 0;
BIO *out = NULL;
EVP_KDF_CTX *ctx = NULL;

prog = opt_init(argc, argv, kdf_options);
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
default:
opthelp:
BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
goto err;
case OPT_HELP:
opt_help(kdf_options);
ret = 0;
goto err;
case OPT_BIN:
out_bin = 1;
break;
case OPT_KEYLEN:
dkm_len = (size_t)atoi(opt_arg());
break;
case OPT_OUT:
outfile = opt_arg();
break;
case OPT_KDFOPT:
if (opts == NULL)
opts = sk_OPENSSL_STRING_new_null();
if (opts == NULL || !sk_OPENSSL_STRING_push(opts, opt_arg()))
goto opthelp;
break;
}
}
argc = opt_num_rest();
argv = opt_rest();

if (argc != 1) {
BIO_printf(bio_err, "Invalid number of extra arguments\n");
goto opthelp;
}

id = OBJ_sn2nid(argv[0]);
if (id == NID_undef) {
BIO_printf(bio_err, "Invalid KDF name %s\n", argv[0]);
goto opthelp;
}

ctx = EVP_KDF_CTX_new_id(id);
if (ctx == NULL)
goto err;

if (opts != NULL) {
for (i = 0; i < sk_OPENSSL_STRING_num(opts); i++) {
char *opt = sk_OPENSSL_STRING_value(opts, i);
if (kdf_ctrl_string(ctx, opt) <= 0) {
BIO_printf(bio_err, "KDF parameter error '%s'\n", opt);
ERR_print_errors(bio_err);
goto err;
}
}
}

out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
if (out == NULL)
goto err;

if (dkm_len <= 0) {
BIO_printf(bio_err, "Invalid derived key length.\n");
goto err;
}
dkm_bytes = app_malloc(dkm_len, "out buffer");
if (dkm_bytes == NULL)
goto err;

if (!EVP_KDF_derive(ctx, dkm_bytes, dkm_len)) {
BIO_printf(bio_err, "EVP_KDF_derive failed\n");
goto err;
}

if (out_bin) {
BIO_write(out, dkm_bytes, dkm_len);
} else {
hexout = OPENSSL_buf2hexstr(dkm_bytes, dkm_len);
BIO_printf(out, "%s\n\n", hexout);
}

ret = 0;
err:
if (ret != 0)
ERR_print_errors(bio_err);
OPENSSL_clear_free(dkm_bytes, dkm_len);
sk_OPENSSL_STRING_free(opts);
EVP_KDF_CTX_free(ctx);
BIO_free(out);
OPENSSL_free(hexout);
return ret;
}
167 changes: 167 additions & 0 deletions doc/man1/kdf.pod
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
=pod

=head1 NAME

openssl-kdf,
kdf - perform Key Derivation Function operations

=head1 SYNOPSIS

B<openssl kdf>
[B<-help>]
[B<-kdfopt> I<nm:v>]
[B<-keylen> I<num>]
[B<-out> I<filename>]
[B<-binary>]
I<kdf_name>

=head1 DESCRIPTION

The key derivation functions generate a derived key from either a secret or
password.

=head1 OPTIONS

=over 4

=item B<-help>

Print a usage message.

=item B<-keylen> I<num>

The output size of the derived key. This field is required.

=item B<-out> I<filename>

Filename to output to, or standard output by default.

=item B<-binary>

Output the derived key in binary form. Uses hexadecimal text format if not specified.

=item B<-kdfopt> I<nm:v>

Passes options to the KDF algorithm.
A comprehensive list of controls can be found in the EVP_KDF_CTX implementation
documentation.
Common control strings used by EVP_KDF_ctrl_str() are:

=over 4

=item B<key:>I<string>

Specifies the secret key as an alphanumeric string (use if the key contains
printable characters only).
The string length must conform to any restrictions of the KDF algorithm.
A key must be specified for most KDF algorithms.

=item B<hexkey:>I<string>

Specifies the secret key in hexadecimal form (two hex digits per byte).
The key length must conform to any restrictions of the KDF algorithm.
A key must be specified for most KDF algorithms.

=item B<pass:>I<string>

Specifies the password as an alphanumeric string (use if the password contains
printable characters only).
The password must be specified for PBKDF2 and scrypt.

=item B<hexpass:>I<string>

Specifies the password in hexadecimal form (two hex digits per byte).
The password must be specified for PBKDF2 and scrypt.

=item B<digest:>I<string>

Specifies the name of a digest as an alphanumeric string.
To see the list of supported digests, use the command I<list -digest-commands>.

=back

=item I<kdf_name>

Specifies the name of a supported KDF algorithm which will be used.
The supported algorithms names are TLS1-PRF, HKDF, SSKDF, PBKDF2, SSHKDF and id-scrypt.

=back

=head1 EXAMPLES

Use TLS1-PRF to create a hex-encoded derived key from a secret key and seed:

openssl kdf -keylen 16 -kdfopt digest:SHA256 -kdfopt key:secret \
-kdfopt seed:seed TLS1-PRF

Use HKDF to create a hex-encoded derived key from a secret key, salt and info:

openssl kdf -keylen 10 -kdfopt digest:SHA256 -kdfopt key:secret \
-kdfopt salt:salt -kdfopt info:label HKDF

Use SSKDF with KMAC to create a hex-encoded derived key from a secret key, salt and info:

openssl kdf -keylen 64 -kdfopt mac:KMAC128 -kdfopt maclen:20 \
-kdfopt hexkey:b74a149a161545 -kdfopt hexinfo:348a37a2 \
-kdfopt hexsalt:3638271ccd68a2 SSKDF

Use SSKDF with HMAC to create a hex-encoded derived key from a secret key, salt and info:

openssl kdf -keylen 16 -kdfopt mac:HMAC -kdfopt digest:SHA256 \
-kdfopt hexkey:b74a149a -kdfopt hexinfo:348a37a2 \
-kdfopt hexsalt:3638271c SSKDF

Use SSKDF with Hash to create a hex-encoded derived key from a secret key, salt and info:

openssl kdf -keylen 14 -kdfopt digest:SHA256 \
-kdfopt hexkey:6dbdc23f045488 \
-kdfopt hexinfo:a1b2c3d4 SSKDF

Use SSHKDF to create a hex-encoded derived key from a secret key, hash and session_id:

openssl kdf -keylen 16 -kdfopt digest:SHA256 \
-kdfopt hexkey:0102030405 \
-kdfopt hexxcghash:06090A \
-kdfopt hexsession_id:01020304 \
-kdfopt type:A SSHKDF

Use PBKDF2 to create a hex-encoded derived key from a password and salt:

openssl kdf -keylen 32 -kdfopt digest:SHA256 -kdfopt pass:password \
-kdfopt salt:salt -kdfopt iter:2 PBKDF2

Use scrypt to create a hex-encoded derived key from a password and salt:

openssl kdf -keylen 64 -kdfopt pass:password -kdfopt salt:NaCl \
-kdfopt N:1024 -kdfopt r:8 -kdfopt p:16 \
-kdfopt maxmem_bytes:10485760 id-scrypt

=head1 NOTES

The KDF mechanisms that are available will depend on the options
used when building OpenSSL.

=head1 SEE ALSO

L<EVP_KDF_CTX(3)>,
L<EVP_KDF_SCRYPT(7)>
L<EVP_KDF_TLS1_PRF(7)>
L<EVP_KDF_PBKDF2(7)>
L<EVP_KDF_HKDF(7)>
L<EVP_KDF_SS(7)>
L<EVP_KDF_SSHKDF(7)>

=head1 HISTORY

Added in OpenSSL 3.0

=head1 COPYRIGHT

Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the OpenSSL license (the "License"). You may not use
this file except in compliance with the License. You can obtain a copy
in the file LICENSE in the source distribution or at
L<https://www.openssl.org/source/license.html>.

=cut
8 changes: 6 additions & 2 deletions doc/man1/openssl.pod
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ Generation of RSA Private Key. Superseded by L<genpkey(1)>.

Display diverse information built into the OpenSSL libraries.

=item B<kdf>

Key Derivation Functions.

=item B<mac>

Message Authentication Code Calculation.
Expand Down Expand Up @@ -616,7 +620,7 @@ L<crl(1)>, L<crl2pkcs7(1)>, L<dgst(1)>,
L<dhparam(1)>, L<dsa(1)>, L<dsaparam(1)>,
L<ec(1)>, L<ecparam(1)>,
L<enc(1)>, L<engine(1)>, L<errstr(1)>, L<gendsa(1)>, L<genpkey(1)>,
L<genrsa(1)>, L<mac(1)>, L<nseq(1)>, L<ocsp(1)>,
L<genrsa(1)>, L<kdf(1)>, L<mac(1)>, L<nseq(1)>, L<ocsp(1)>,
L<passwd(1)>,
L<pkcs12(1)>, L<pkcs7(1)>, L<pkcs8(1)>,
L<pkey(1)>, L<pkeyparam(1)>, L<pkeyutl(1)>, L<prime(1)>,
Expand All @@ -636,7 +640,7 @@ manual pages.

=head1 COPYRIGHT

Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.

Licensed under the Apache License 2.0 (the "License"). You may not use
this file except in compliance with the License. You can obtain a copy
Expand Down
5 changes: 3 additions & 2 deletions doc/man3/EVP_KDF_CTX.pod
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ The value string is expected to be the name of a MAC.
This control expects one argument: C<EVP_MD *md>

For MAC implementations that use a message digest as an underlying computation
algorithm, this control set what the digest algorithm should be.
algorithm, this control sets what the digest algorithm should be.

EVP_KDF_ctrl_str() type string: "md"
EVP_KDF_ctrl_str() type string: "digest"

The value string is expected to be the name of a digest.

Expand Down Expand Up @@ -232,6 +232,7 @@ L<EVP_KDF_TLS1_PRF(7)>
L<EVP_KDF_PBKDF2(7)>
L<EVP_KDF_HKDF(7)>
L<EVP_KDF_SS(7)>
L<EVP_KDF_SSHKDF(7)>

=head1 HISTORY

Expand Down
Loading

0 comments on commit c54492e

Please sign in to comment.