137788f25SDaniel P. Berrange /*
237788f25SDaniel P. Berrange * QEMU Crypto PBKDF support (Password-Based Key Derivation Function)
337788f25SDaniel P. Berrange *
437788f25SDaniel P. Berrange * Copyright (c) 2015-2016 Red Hat, Inc.
537788f25SDaniel P. Berrange *
637788f25SDaniel P. Berrange * This library is free software; you can redistribute it and/or
737788f25SDaniel P. Berrange * modify it under the terms of the GNU Lesser General Public
837788f25SDaniel P. Berrange * License as published by the Free Software Foundation; either
9b7cbb874SThomas Huth * version 2.1 of the License, or (at your option) any later version.
1037788f25SDaniel P. Berrange *
1137788f25SDaniel P. Berrange * This library is distributed in the hope that it will be useful,
1237788f25SDaniel P. Berrange * but WITHOUT ANY WARRANTY; without even the implied warranty of
1337788f25SDaniel P. Berrange * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1437788f25SDaniel P. Berrange * Lesser General Public License for more details.
1537788f25SDaniel P. Berrange *
1637788f25SDaniel P. Berrange * You should have received a copy of the GNU Lesser General Public
1737788f25SDaniel P. Berrange * License along with this library; if not, see <http://www.gnu.org/licenses/>.
1837788f25SDaniel P. Berrange *
1937788f25SDaniel P. Berrange */
2037788f25SDaniel P. Berrange
2137788f25SDaniel P. Berrange #include "qemu/osdep.h"
22a9c94277SMarkus Armbruster #include <gcrypt.h>
23da34e65cSMarkus Armbruster #include "qapi/error.h"
2437788f25SDaniel P. Berrange #include "crypto/pbkdf.h"
2537788f25SDaniel P. Berrange
qcrypto_pbkdf2_supports(QCryptoHashAlgo hash)26ef834aa2SMarkus Armbruster bool qcrypto_pbkdf2_supports(QCryptoHashAlgo hash)
2737788f25SDaniel P. Berrange {
2837788f25SDaniel P. Berrange switch (hash) {
29ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_MD5:
30ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_SHA1:
31ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_SHA224:
32ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_SHA256:
33ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_SHA384:
34ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_SHA512:
35ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_RIPEMD160:
36*d078da86Sliequan che #ifdef CONFIG_CRYPTO_SM3
37*d078da86Sliequan che case QCRYPTO_HASH_ALGO_SM3:
38*d078da86Sliequan che #endif
39e6c09ea4SDaniel P. Berrangé return qcrypto_hash_supports(hash);
4037788f25SDaniel P. Berrange default:
4137788f25SDaniel P. Berrange return false;
4237788f25SDaniel P. Berrange }
4337788f25SDaniel P. Berrange }
4437788f25SDaniel P. Berrange
qcrypto_pbkdf2(QCryptoHashAlgo hash,const uint8_t * key,size_t nkey,const uint8_t * salt,size_t nsalt,uint64_t iterations,uint8_t * out,size_t nout,Error ** errp)45ef834aa2SMarkus Armbruster int qcrypto_pbkdf2(QCryptoHashAlgo hash,
4637788f25SDaniel P. Berrange const uint8_t *key, size_t nkey,
4737788f25SDaniel P. Berrange const uint8_t *salt, size_t nsalt,
4859b060beSDaniel P. Berrange uint64_t iterations,
4937788f25SDaniel P. Berrange uint8_t *out, size_t nout,
5037788f25SDaniel P. Berrange Error **errp)
5137788f25SDaniel P. Berrange {
52ef834aa2SMarkus Armbruster static const int hash_map[QCRYPTO_HASH_ALGO__MAX] = {
53ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_MD5] = GCRY_MD_MD5,
54ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA1] = GCRY_MD_SHA1,
55ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA224] = GCRY_MD_SHA224,
56ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA256] = GCRY_MD_SHA256,
57ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA384] = GCRY_MD_SHA384,
58ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA512] = GCRY_MD_SHA512,
59ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_RIPEMD160] = GCRY_MD_RMD160,
60*d078da86Sliequan che #ifdef CONFIG_CRYPTO_SM3
61*d078da86Sliequan che [QCRYPTO_HASH_ALGO_SM3] = GCRY_MD_SM3,
62*d078da86Sliequan che #endif
6337788f25SDaniel P. Berrange };
6437788f25SDaniel P. Berrange int ret;
6537788f25SDaniel P. Berrange
6659b060beSDaniel P. Berrange if (iterations > ULONG_MAX) {
6759b060beSDaniel P. Berrange error_setg_errno(errp, ERANGE,
6859b060beSDaniel P. Berrange "PBKDF iterations %llu must be less than %lu",
6959b060beSDaniel P. Berrange (long long unsigned)iterations, ULONG_MAX);
7059b060beSDaniel P. Berrange return -1;
7159b060beSDaniel P. Berrange }
7259b060beSDaniel P. Berrange
7337788f25SDaniel P. Berrange if (hash >= G_N_ELEMENTS(hash_map) ||
7437788f25SDaniel P. Berrange hash_map[hash] == GCRY_MD_NONE) {
75533008f4SDaniel P. Berrange error_setg_errno(errp, ENOSYS,
76533008f4SDaniel P. Berrange "PBKDF does not support hash algorithm %s",
77ef834aa2SMarkus Armbruster QCryptoHashAlgo_str(hash));
7837788f25SDaniel P. Berrange return -1;
7937788f25SDaniel P. Berrange }
8037788f25SDaniel P. Berrange
8137788f25SDaniel P. Berrange ret = gcry_kdf_derive(key, nkey, GCRY_KDF_PBKDF2,
8237788f25SDaniel P. Berrange hash_map[hash],
8337788f25SDaniel P. Berrange salt, nsalt, iterations,
8437788f25SDaniel P. Berrange nout, out);
8537788f25SDaniel P. Berrange if (ret != 0) {
8637788f25SDaniel P. Berrange error_setg(errp, "Cannot derive password: %s",
8737788f25SDaniel P. Berrange gcry_strerror(ret));
8837788f25SDaniel P. Berrange return -1;
8937788f25SDaniel P. Berrange }
9037788f25SDaniel P. Berrange
9137788f25SDaniel P. Berrange return 0;
9237788f25SDaniel P. Berrange }
93