]> Git Repo - qemu.git/blame - crypto/pbkdf-nettle.c
crypto: increase default pbkdf2 time for luks to 2 seconds
[qemu.git] / crypto / pbkdf-nettle.c
CommitLineData
37788f25
DB
1/*
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
9 * version 2 of the License, or (at your option) any later version.
10 *
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"
a9c94277 22#include <nettle/pbkdf2.h>
da34e65c 23#include "qapi/error.h"
37788f25 24#include "crypto/pbkdf.h"
37788f25
DB
25
26
27bool qcrypto_pbkdf2_supports(QCryptoHashAlgorithm hash)
28{
29 switch (hash) {
30 case QCRYPTO_HASH_ALG_SHA1:
31 case QCRYPTO_HASH_ALG_SHA256:
32 return true;
33 default:
34 return false;
35 }
36}
37
38int qcrypto_pbkdf2(QCryptoHashAlgorithm hash,
39 const uint8_t *key, size_t nkey,
40 const uint8_t *salt, size_t nsalt,
59b060be 41 uint64_t iterations,
37788f25
DB
42 uint8_t *out, size_t nout,
43 Error **errp)
44{
59b060be
DB
45 if (iterations > UINT_MAX) {
46 error_setg_errno(errp, ERANGE,
47 "PBKDF iterations %llu must be less than %u",
48 (long long unsigned)iterations, UINT_MAX);
49 return -1;
50 }
37788f25
DB
51 switch (hash) {
52 case QCRYPTO_HASH_ALG_SHA1:
53 pbkdf2_hmac_sha1(nkey, key,
54 iterations,
55 nsalt, salt,
56 nout, out);
57 break;
58
59 case QCRYPTO_HASH_ALG_SHA256:
60 pbkdf2_hmac_sha256(nkey, key,
61 iterations,
62 nsalt, salt,
63 nout, out);
64 break;
65
66 default:
67 error_setg_errno(errp, ENOSYS,
68 "PBKDF does not support hash algorithm %d", hash);
69 return -1;
70 }
71 return 0;
72}
This page took 0.067519 seconds and 4 git commands to generate.