wxx: Add support for ncurses
[qemu/ar7.git] / crypto / pbkdf-gcrypt.c
blob885614dfcca27e8c1f09d1938a72d8728587b19a
1 /*
2 * QEMU Crypto PBKDF support (Password-Based Key Derivation Function)
4 * Copyright (c) 2015-2016 Red Hat, Inc.
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
9 * version 2 of the License, or (at your option) any later version.
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.
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/>.
21 #include "qemu/osdep.h"
22 #include "crypto/pbkdf.h"
23 #include "gcrypt.h"
25 bool qcrypto_pbkdf2_supports(QCryptoHashAlgorithm hash)
27 switch (hash) {
28 case QCRYPTO_HASH_ALG_MD5:
29 case QCRYPTO_HASH_ALG_SHA1:
30 case QCRYPTO_HASH_ALG_SHA256:
31 return true;
32 default:
33 return false;
37 int qcrypto_pbkdf2(QCryptoHashAlgorithm hash,
38 const uint8_t *key, size_t nkey,
39 const uint8_t *salt, size_t nsalt,
40 unsigned int iterations,
41 uint8_t *out, size_t nout,
42 Error **errp)
44 static const int hash_map[QCRYPTO_HASH_ALG__MAX] = {
45 [QCRYPTO_HASH_ALG_MD5] = GCRY_MD_MD5,
46 [QCRYPTO_HASH_ALG_SHA1] = GCRY_MD_SHA1,
47 [QCRYPTO_HASH_ALG_SHA256] = GCRY_MD_SHA256,
49 int ret;
51 if (hash >= G_N_ELEMENTS(hash_map) ||
52 hash_map[hash] == GCRY_MD_NONE) {
53 error_setg(errp, "Unexpected hash algorithm %d", hash);
54 return -1;
57 ret = gcry_kdf_derive(key, nkey, GCRY_KDF_PBKDF2,
58 hash_map[hash],
59 salt, nsalt, iterations,
60 nout, out);
61 if (ret != 0) {
62 error_setg(errp, "Cannot derive password: %s",
63 gcry_strerror(ret));
64 return -1;
67 return 0;