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 26*ef834aa2SMarkus Armbruster bool qcrypto_pbkdf2_supports(QCryptoHashAlgo hash) 2737788f25SDaniel P. Berrange { 2837788f25SDaniel P. Berrange switch (hash) { 29*ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_MD5: 30*ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_SHA1: 31*ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_SHA224: 32*ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_SHA256: 33*ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_SHA384: 34*ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_SHA512: 35*ef834aa2SMarkus Armbruster case QCRYPTO_HASH_ALGO_RIPEMD160: 36e6c09ea4SDaniel P. Berrangé return qcrypto_hash_supports(hash); 3737788f25SDaniel P. Berrange default: 3837788f25SDaniel P. Berrange return false; 3937788f25SDaniel P. Berrange } 4037788f25SDaniel P. Berrange } 4137788f25SDaniel P. Berrange 42*ef834aa2SMarkus Armbruster int qcrypto_pbkdf2(QCryptoHashAlgo hash, 4337788f25SDaniel P. Berrange const uint8_t *key, size_t nkey, 4437788f25SDaniel P. Berrange const uint8_t *salt, size_t nsalt, 4559b060beSDaniel P. Berrange uint64_t iterations, 4637788f25SDaniel P. Berrange uint8_t *out, size_t nout, 4737788f25SDaniel P. Berrange Error **errp) 4837788f25SDaniel P. Berrange { 49*ef834aa2SMarkus Armbruster static const int hash_map[QCRYPTO_HASH_ALGO__MAX] = { 50*ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_MD5] = GCRY_MD_MD5, 51*ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA1] = GCRY_MD_SHA1, 52*ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA224] = GCRY_MD_SHA224, 53*ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA256] = GCRY_MD_SHA256, 54*ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA384] = GCRY_MD_SHA384, 55*ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_SHA512] = GCRY_MD_SHA512, 56*ef834aa2SMarkus Armbruster [QCRYPTO_HASH_ALGO_RIPEMD160] = GCRY_MD_RMD160, 5737788f25SDaniel P. Berrange }; 5837788f25SDaniel P. Berrange int ret; 5937788f25SDaniel P. Berrange 6059b060beSDaniel P. Berrange if (iterations > ULONG_MAX) { 6159b060beSDaniel P. Berrange error_setg_errno(errp, ERANGE, 6259b060beSDaniel P. Berrange "PBKDF iterations %llu must be less than %lu", 6359b060beSDaniel P. Berrange (long long unsigned)iterations, ULONG_MAX); 6459b060beSDaniel P. Berrange return -1; 6559b060beSDaniel P. Berrange } 6659b060beSDaniel P. Berrange 6737788f25SDaniel P. Berrange if (hash >= G_N_ELEMENTS(hash_map) || 6837788f25SDaniel P. Berrange hash_map[hash] == GCRY_MD_NONE) { 69533008f4SDaniel P. Berrange error_setg_errno(errp, ENOSYS, 70533008f4SDaniel P. Berrange "PBKDF does not support hash algorithm %s", 71*ef834aa2SMarkus Armbruster QCryptoHashAlgo_str(hash)); 7237788f25SDaniel P. Berrange return -1; 7337788f25SDaniel P. Berrange } 7437788f25SDaniel P. Berrange 7537788f25SDaniel P. Berrange ret = gcry_kdf_derive(key, nkey, GCRY_KDF_PBKDF2, 7637788f25SDaniel P. Berrange hash_map[hash], 7737788f25SDaniel P. Berrange salt, nsalt, iterations, 7837788f25SDaniel P. Berrange nout, out); 7937788f25SDaniel P. Berrange if (ret != 0) { 8037788f25SDaniel P. Berrange error_setg(errp, "Cannot derive password: %s", 8137788f25SDaniel P. Berrange gcry_strerror(ret)); 8237788f25SDaniel P. Berrange return -1; 8337788f25SDaniel P. Berrange } 8437788f25SDaniel P. Berrange 8537788f25SDaniel P. Berrange return 0; 8637788f25SDaniel P. Berrange } 87