1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu/osdep.h"
22#include "qapi/error.h"
23#include "crypto/pbkdf.h"
24#include "nettle/pbkdf2.h"
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,
41 unsigned int iterations,
42 uint8_t *out, size_t nout,
43 Error **errp)
44{
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;
52
53 case QCRYPTO_HASH_ALG_SHA256:
54 pbkdf2_hmac_sha256(nkey, key,
55 iterations,
56 nsalt, salt,
57 nout, out);
58 break;
59
60 default:
61 error_setg_errno(errp, ENOSYS,
62 "PBKDF does not support hash algorithm %d", hash);
63 return -1;
64 }
65 return 0;
66}
67