slirp: fix segv when init failed
[qemu.git] / crypto / pbkdf-nettle.c
blobd681a606f944616fb7595958d7243d3409984ae8
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 <nettle/pbkdf2.h>
23 #include "qapi/error.h"
24 #include "crypto/pbkdf.h"
27 bool qcrypto_pbkdf2_supports(QCryptoHashAlgorithm hash)
29 switch (hash) {
30 case QCRYPTO_HASH_ALG_SHA1:
31 case QCRYPTO_HASH_ALG_SHA256:
32 return true;
33 default:
34 return false;
38 int qcrypto_pbkdf2(QCryptoHashAlgorithm hash,
39 const uint8_t *key, size_t nkey,
40 const uint8_t *salt, size_t nsalt,
41 unsigned int iterations,
42 uint8_t *out, size_t nout,
43 Error **errp)
45 switch (hash) {
46 case QCRYPTO_HASH_ALG_SHA1:
47 pbkdf2_hmac_sha1(nkey, key,
48 iterations,
49 nsalt, salt,
50 nout, out);
51 break;
53 case QCRYPTO_HASH_ALG_SHA256:
54 pbkdf2_hmac_sha256(nkey, key,
55 iterations,
56 nsalt, salt,
57 nout, out);
58 break;
60 default:
61 error_setg_errno(errp, ENOSYS,
62 "PBKDF does not support hash algorithm %d", hash);
63 return -1;
65 return 0;