Skip to content

Commit

Permalink
Change the default digest type to sha256, and add support for
Browse files Browse the repository at this point in the history
pbkdf2 with OpenSSL compatible flags
ok jsing@
  • Loading branch information
beck committed Jan 18, 2019
1 parent 7706ea1 commit 0bd17d3
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions src/usr.bin/openssl/enc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: enc.c,v 1.14 2018/02/07 05:47:55 jsing Exp $ */
/* $OpenBSD: enc.c,v 1.15 2019/01/18 03:45:47 beck Exp $ */
/* Copyright (C) 1995-1998 Eric Young ([email protected])
* All rights reserved.
*
Expand Down Expand Up @@ -99,6 +99,8 @@ static struct {
char *passarg;
int printkey;
int verbose;
int iter;
int pbkdf2;
} enc_config;

static int
Expand Down Expand Up @@ -273,6 +275,18 @@ static struct option enc_options[] = {
.type = OPTION_FLAG,
.opt.flag = &enc_config.verbose,
},
{
.name = "iter",
.desc = "Specify iteration count and force use of PBKDF2",
.type = OPTION_VALUE,
.opt.value = &enc_config.iter,
},
{
.name = "pbkdf2",
.desc = "Use the pbkdf2 key derivation function",
.type = OPTION_FLAG,
.opt.flag = &enc_config.pbkdf2,
},
#ifdef ZLIB
{
.name = "z",
Expand Down Expand Up @@ -416,7 +430,7 @@ enc_main(int argc, char **argv)
goto end;
}
if (dgst == NULL) {
dgst = EVP_md5(); /* XXX */
dgst = EVP_sha256();
}

if (enc_config.bufsize != NULL) {
Expand Down Expand Up @@ -604,10 +618,35 @@ enc_main(int argc, char **argv)
}
sptr = salt;
}
if (enc_config.pbkdf2 == 1 || enc_config.iter > 0) {
/*
* derive key and default iv
* concatenated into a temporary buffer
*/
unsigned char tmpkeyiv[EVP_MAX_KEY_LENGTH + EVP_MAX_IV_LENGTH];
int iklen = EVP_CIPHER_key_length(enc_config.cipher);
int ivlen = EVP_CIPHER_iv_length(enc_config.cipher);
/* not needed if HASH_UPDATE() is fixed : */
int islen = (sptr != NULL ? sizeof(salt) : 0);

if (enc_config.iter == 0)
enc_config.iter = 10000;

if (!PKCS5_PBKDF2_HMAC(enc_config.keystr,
strlen(enc_config.keystr), sptr, islen,
enc_config.iter, dgst, iklen+ivlen, tmpkeyiv)) {
BIO_printf(bio_err, "PKCS5_PBKDF2_HMAC failed\n");
goto end;
}
/* split and move data back to global buffer */
memcpy(key, tmpkeyiv, iklen);
memcpy(iv, tmpkeyiv+iklen, ivlen);
} else {
EVP_BytesToKey(enc_config.cipher, dgst, sptr,
(unsigned char *)enc_config.keystr,
strlen(enc_config.keystr), 1, key, iv);
}

EVP_BytesToKey(enc_config.cipher, dgst, sptr,
(unsigned char *)enc_config.keystr,
strlen(enc_config.keystr), 1, key, iv);
/*
* zero the complete buffer or the string passed from
* the command line bug picked up by Larry J. Hughes
Expand Down

0 comments on commit 0bd17d3

Please sign in to comment.