Dr. Stephen Henson | 55e42c9 | 2001-12-03 03:07:37 +0000 | [diff] [blame] | 1 | =pod |
| 2 | |
| 3 | =head1 NAME |
| 4 | |
Richard Levitte | d90e74c | 2003-09-30 17:22:19 +0000 | [diff] [blame] | 5 | EVP_BytesToKey - password based encryption routine |
Dr. Stephen Henson | 55e42c9 | 2001-12-03 03:07:37 +0000 | [diff] [blame] | 6 | |
| 7 | =head1 SYNOPSIS |
| 8 | |
| 9 | #include <openssl/evp.h> |
| 10 | |
Rich Salz | aebb9aa | 2016-07-19 09:27:53 -0400 | [diff] [blame] | 11 | int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md, |
| 12 | const unsigned char *salt, |
| 13 | const unsigned char *data, int datal, int count, |
| 14 | unsigned char *key, unsigned char *iv); |
Dr. Stephen Henson | 55e42c9 | 2001-12-03 03:07:37 +0000 | [diff] [blame] | 15 | |
| 16 | =head1 DESCRIPTION |
| 17 | |
| 18 | EVP_BytesToKey() derives a key and IV from various parameters. B<type> is |
| 19 | the cipher to derive the key and IV for. B<md> is the message digest to use. |
Scott Schaefer | 2b4ffc6 | 2013-12-23 19:28:30 +0100 | [diff] [blame] | 20 | The B<salt> parameter is used as a salt in the derivation: it should point to |
Dr. Stephen Henson | 55e42c9 | 2001-12-03 03:07:37 +0000 | [diff] [blame] | 21 | an 8 byte buffer or NULL if no salt is used. B<data> is a buffer containing |
| 22 | B<datal> bytes which is used to derive the keying data. B<count> is the |
| 23 | iteration count to use. The derived key and IV will be written to B<key> |
| 24 | and B<iv> respectively. |
| 25 | |
| 26 | =head1 NOTES |
| 27 | |
| 28 | A typical application of this function is to derive keying material for an |
| 29 | encryption algorithm from a password in the B<data> parameter. |
| 30 | |
| 31 | Increasing the B<count> parameter slows down the algorithm which makes it |
Alok Menghrajani | 186bb90 | 2015-04-13 11:05:13 -0700 | [diff] [blame] | 32 | harder for an attacker to perform a brute force attack using a large number |
Dr. Stephen Henson | 55e42c9 | 2001-12-03 03:07:37 +0000 | [diff] [blame] | 33 | of candidate passwords. |
| 34 | |
| 35 | If the total key and IV length is less than the digest length and |
| 36 | B<MD5> is used then the derivation algorithm is compatible with PKCS#5 v1.5 |
| 37 | otherwise a non standard extension is used to derive the extra data. |
| 38 | |
Jeffrey Walton | 82c4d79 | 2015-06-06 21:52:56 -0400 | [diff] [blame] | 39 | Newer applications should use a more modern algorithm such as PBKDF2 as |
| 40 | defined in PKCS#5v2.1 and provided by PKCS5_PBKDF2_HMAC. |
Dr. Stephen Henson | 55e42c9 | 2001-12-03 03:07:37 +0000 | [diff] [blame] | 41 | |
| 42 | =head1 KEY DERIVATION ALGORITHM |
| 43 | |
| 44 | The key and IV is derived by concatenating D_1, D_2, etc until |
| 45 | enough data is available for the key and IV. D_i is defined as: |
| 46 | |
Rich Salz | 1bc7451 | 2016-05-20 08:11:46 -0400 | [diff] [blame] | 47 | D_i = HASH^count(D_(i-1) || data || salt) |
Dr. Stephen Henson | 55e42c9 | 2001-12-03 03:07:37 +0000 | [diff] [blame] | 48 | |
Alok Menghrajani | 186bb90 | 2015-04-13 11:05:13 -0700 | [diff] [blame] | 49 | where || denotes concatenation, D_0 is empty, HASH is the digest |
Dr. Stephen Henson | 55e42c9 | 2001-12-03 03:07:37 +0000 | [diff] [blame] | 50 | algorithm in use, HASH^1(data) is simply HASH(data), HASH^2(data) |
| 51 | is HASH(HASH(data)) and so on. |
| 52 | |
| 53 | The initial bytes are used for the key and the subsequent bytes for |
| 54 | the IV. |
| 55 | |
| 56 | =head1 RETURN VALUES |
| 57 | |
Rich Salz | 5aed169 | 2014-09-08 11:48:34 -0400 | [diff] [blame] | 58 | If B<data> is NULL, then EVP_BytesToKey() returns the number of bytes |
| 59 | needed to store the derived key. |
| 60 | Otherwise, EVP_BytesToKey() returns the size of the derived key in bytes, |
| 61 | or 0 on error. |
Dr. Stephen Henson | 55e42c9 | 2001-12-03 03:07:37 +0000 | [diff] [blame] | 62 | |
| 63 | =head1 SEE ALSO |
| 64 | |
Richard Levitte | b97fdb5 | 2016-11-11 09:33:09 +0100 | [diff] [blame] | 65 | L<evp(7)>, L<RAND_bytes(3)>, |
Rich Salz | 9b86974 | 2015-08-17 15:21:33 -0400 | [diff] [blame] | 66 | L<PKCS5_PBKDF2_HMAC(3)>, |
| 67 | L<EVP_EncryptInit(3)> |
Dr. Stephen Henson | 55e42c9 | 2001-12-03 03:07:37 +0000 | [diff] [blame] | 68 | |
Rich Salz | e2f9261 | 2016-05-18 11:44:05 -0400 | [diff] [blame] | 69 | =head1 COPYRIGHT |
| 70 | |
| 71 | Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved. |
| 72 | |
| 73 | Licensed under the OpenSSL license (the "License"). You may not use |
| 74 | this file except in compliance with the License. You can obtain a copy |
| 75 | in the file LICENSE in the source distribution or at |
| 76 | L<https://www.openssl.org/source/license.html>. |
| 77 | |
| 78 | =cut |