1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5e71b7053SJung-uk KimEVP_BytesToKey - password based encryption routine 6e71b7053SJung-uk Kim 7e71b7053SJung-uk Kim=head1 SYNOPSIS 8e71b7053SJung-uk Kim 9e71b7053SJung-uk Kim #include <openssl/evp.h> 10e71b7053SJung-uk Kim 11e71b7053SJung-uk Kim int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, 12e71b7053SJung-uk Kim const unsigned char *salt, 13e71b7053SJung-uk Kim const unsigned char *data, int datal, int count, 14e71b7053SJung-uk Kim unsigned char *key, unsigned char *iv); 15e71b7053SJung-uk Kim 16e71b7053SJung-uk Kim=head1 DESCRIPTION 17e71b7053SJung-uk Kim 18e71b7053SJung-uk KimEVP_BytesToKey() derives a key and IV from various parameters. B<type> is 19e71b7053SJung-uk Kimthe cipher to derive the key and IV for. B<md> is the message digest to use. 20e71b7053SJung-uk KimThe B<salt> parameter is used as a salt in the derivation: it should point to 21e71b7053SJung-uk Kiman 8 byte buffer or NULL if no salt is used. B<data> is a buffer containing 22e71b7053SJung-uk KimB<datal> bytes which is used to derive the keying data. B<count> is the 23e71b7053SJung-uk Kimiteration count to use. The derived key and IV will be written to B<key> 24e71b7053SJung-uk Kimand B<iv> respectively. 25e71b7053SJung-uk Kim 26e71b7053SJung-uk Kim=head1 NOTES 27e71b7053SJung-uk Kim 28e71b7053SJung-uk KimA typical application of this function is to derive keying material for an 29e71b7053SJung-uk Kimencryption algorithm from a password in the B<data> parameter. 30e71b7053SJung-uk Kim 31e71b7053SJung-uk KimIncreasing the B<count> parameter slows down the algorithm which makes it 32e71b7053SJung-uk Kimharder for an attacker to perform a brute force attack using a large number 33e71b7053SJung-uk Kimof candidate passwords. 34e71b7053SJung-uk Kim 35e71b7053SJung-uk KimIf the total key and IV length is less than the digest length and 36e71b7053SJung-uk KimB<MD5> is used then the derivation algorithm is compatible with PKCS#5 v1.5 37e71b7053SJung-uk Kimotherwise a non standard extension is used to derive the extra data. 38e71b7053SJung-uk Kim 39e71b7053SJung-uk KimNewer applications should use a more modern algorithm such as PBKDF2 as 40e71b7053SJung-uk Kimdefined in PKCS#5v2.1 and provided by PKCS5_PBKDF2_HMAC. 41e71b7053SJung-uk Kim 42e71b7053SJung-uk Kim=head1 KEY DERIVATION ALGORITHM 43e71b7053SJung-uk Kim 44e71b7053SJung-uk KimThe key and IV is derived by concatenating D_1, D_2, etc until 45e71b7053SJung-uk Kimenough data is available for the key and IV. D_i is defined as: 46e71b7053SJung-uk Kim 47e71b7053SJung-uk Kim D_i = HASH^count(D_(i-1) || data || salt) 48e71b7053SJung-uk Kim 49e71b7053SJung-uk Kimwhere || denotes concatenation, D_0 is empty, HASH is the digest 50e71b7053SJung-uk Kimalgorithm in use, HASH^1(data) is simply HASH(data), HASH^2(data) 51e71b7053SJung-uk Kimis HASH(HASH(data)) and so on. 52e71b7053SJung-uk Kim 53e71b7053SJung-uk KimThe initial bytes are used for the key and the subsequent bytes for 54e71b7053SJung-uk Kimthe IV. 55e71b7053SJung-uk Kim 56e71b7053SJung-uk Kim=head1 RETURN VALUES 57e71b7053SJung-uk Kim 58e71b7053SJung-uk KimIf B<data> is NULL, then EVP_BytesToKey() returns the number of bytes 59e71b7053SJung-uk Kimneeded to store the derived key. 60e71b7053SJung-uk KimOtherwise, EVP_BytesToKey() returns the size of the derived key in bytes, 61e71b7053SJung-uk Kimor 0 on error. 62e71b7053SJung-uk Kim 63e71b7053SJung-uk Kim=head1 SEE ALSO 64e71b7053SJung-uk Kim 65e71b7053SJung-uk KimL<evp(7)>, L<RAND_bytes(3)>, 66e71b7053SJung-uk KimL<PKCS5_PBKDF2_HMAC(3)>, 67e71b7053SJung-uk KimL<EVP_EncryptInit(3)> 68e71b7053SJung-uk Kim 69e71b7053SJung-uk Kim=head1 COPYRIGHT 70e71b7053SJung-uk Kim 71e71b7053SJung-uk KimCopyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved. 72e71b7053SJung-uk Kim 73*b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 74e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 75e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 76e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 77e71b7053SJung-uk Kim 78e71b7053SJung-uk Kim=cut 79