blob: 76bbb55f7a08d1cbec0e9575732cb97c7af1247d [file] [log] [blame]
Daniel P. Berrange37788f22015-10-14 13:14:04 +01001/*
2 * QEMU Crypto PBKDF support (Password-Based Key Derivation Function)
3 *
4 * Copyright (c) 2015-2016 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
Thomas Huthb7cbb872019-02-13 16:54:59 +01009 * version 2.1 of the License, or (at your option) any later version.
Daniel P. Berrange37788f22015-10-14 13:14:04 +010010 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include "qemu/osdep.h"
Markus Armbrustera9c94272016-06-22 19:11:19 +020022#include <gcrypt.h>
Markus Armbrusterda34e652016-03-14 09:01:28 +010023#include "qapi/error.h"
Daniel P. Berrange37788f22015-10-14 13:14:04 +010024#include "crypto/pbkdf.h"
Daniel P. Berrange37788f22015-10-14 13:14:04 +010025
Markus Armbrusteref834aa2024-09-04 13:18:28 +020026bool qcrypto_pbkdf2_supports(QCryptoHashAlgo hash)
Daniel P. Berrange37788f22015-10-14 13:14:04 +010027{
28 switch (hash) {
Markus Armbrusteref834aa2024-09-04 13:18:28 +020029 case QCRYPTO_HASH_ALGO_MD5:
30 case QCRYPTO_HASH_ALGO_SHA1:
31 case QCRYPTO_HASH_ALGO_SHA224:
32 case QCRYPTO_HASH_ALGO_SHA256:
33 case QCRYPTO_HASH_ALGO_SHA384:
34 case QCRYPTO_HASH_ALGO_SHA512:
35 case QCRYPTO_HASH_ALGO_RIPEMD160:
Daniel P. Berrangée6c09ea2024-08-29 14:47:42 +010036 return qcrypto_hash_supports(hash);
Daniel P. Berrange37788f22015-10-14 13:14:04 +010037 default:
38 return false;
39 }
40}
41
Markus Armbrusteref834aa2024-09-04 13:18:28 +020042int qcrypto_pbkdf2(QCryptoHashAlgo hash,
Daniel P. Berrange37788f22015-10-14 13:14:04 +010043 const uint8_t *key, size_t nkey,
44 const uint8_t *salt, size_t nsalt,
Daniel P. Berrange59b060b2016-09-12 12:50:12 +010045 uint64_t iterations,
Daniel P. Berrange37788f22015-10-14 13:14:04 +010046 uint8_t *out, size_t nout,
47 Error **errp)
48{
Markus Armbrusteref834aa2024-09-04 13:18:28 +020049 static const int hash_map[QCRYPTO_HASH_ALGO__MAX] = {
50 [QCRYPTO_HASH_ALGO_MD5] = GCRY_MD_MD5,
51 [QCRYPTO_HASH_ALGO_SHA1] = GCRY_MD_SHA1,
52 [QCRYPTO_HASH_ALGO_SHA224] = GCRY_MD_SHA224,
53 [QCRYPTO_HASH_ALGO_SHA256] = GCRY_MD_SHA256,
54 [QCRYPTO_HASH_ALGO_SHA384] = GCRY_MD_SHA384,
55 [QCRYPTO_HASH_ALGO_SHA512] = GCRY_MD_SHA512,
56 [QCRYPTO_HASH_ALGO_RIPEMD160] = GCRY_MD_RMD160,
Daniel P. Berrange37788f22015-10-14 13:14:04 +010057 };
58 int ret;
59
Daniel P. Berrange59b060b2016-09-12 12:50:12 +010060 if (iterations > ULONG_MAX) {
61 error_setg_errno(errp, ERANGE,
62 "PBKDF iterations %llu must be less than %lu",
63 (long long unsigned)iterations, ULONG_MAX);
64 return -1;
65 }
66
Daniel P. Berrange37788f22015-10-14 13:14:04 +010067 if (hash >= G_N_ELEMENTS(hash_map) ||
68 hash_map[hash] == GCRY_MD_NONE) {
Daniel P. Berrange533008f2016-09-07 13:12:28 +010069 error_setg_errno(errp, ENOSYS,
70 "PBKDF does not support hash algorithm %s",
Markus Armbrusteref834aa2024-09-04 13:18:28 +020071 QCryptoHashAlgo_str(hash));
Daniel P. Berrange37788f22015-10-14 13:14:04 +010072 return -1;
73 }
74
75 ret = gcry_kdf_derive(key, nkey, GCRY_KDF_PBKDF2,
76 hash_map[hash],
77 salt, nsalt, iterations,
78 nout, out);
79 if (ret != 0) {
80 error_setg(errp, "Cannot derive password: %s",
81 gcry_strerror(ret));
82 return -1;
83 }
84
85 return 0;
86}