blob: 3ef9c1b52c49de7b088358710ac8ccda735dba37 [file] [log] [blame]
Daniel P. Berrange37788f22015-10-14 13:14:04 +01001/*
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
Thomas Huthb7cbb872019-02-13 16:54:59 +01009 * version 2.1 of the License, or (at your option) any later version.
Daniel P. Berrange37788f22015-10-14 13:14:04 +010010 *
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"
Markus Armbrustera9c94272016-06-22 19:11:19 +020022#include <nettle/pbkdf2.h>
Daniel P. Berrange533008f2016-09-07 13:12:28 +010023#include <nettle/hmac.h>
Markus Armbrusterda34e652016-03-14 09:01:28 +010024#include "qapi/error.h"
Daniel P. Berrange37788f22015-10-14 13:14:04 +010025#include "crypto/pbkdf.h"
Daniel P. Berrange37788f22015-10-14 13:14:04 +010026
27
Markus Armbrusteref834aa2024-09-04 13:18:28 +020028bool qcrypto_pbkdf2_supports(QCryptoHashAlgo hash)
Daniel P. Berrange37788f22015-10-14 13:14:04 +010029{
30 switch (hash) {
Markus Armbrusteref834aa2024-09-04 13:18:28 +020031 case QCRYPTO_HASH_ALGO_SHA1:
32 case QCRYPTO_HASH_ALGO_SHA224:
33 case QCRYPTO_HASH_ALGO_SHA256:
34 case QCRYPTO_HASH_ALGO_SHA384:
35 case QCRYPTO_HASH_ALGO_SHA512:
36 case QCRYPTO_HASH_ALGO_RIPEMD160:
liequan ched078da82024-10-30 08:51:46 +000037#ifdef CONFIG_CRYPTO_SM3
38 case QCRYPTO_HASH_ALGO_SM3:
39#endif
Daniel P. Berrange37788f22015-10-14 13:14:04 +010040 return true;
41 default:
42 return false;
43 }
44}
45
Markus Armbrusteref834aa2024-09-04 13:18:28 +020046int qcrypto_pbkdf2(QCryptoHashAlgo hash,
Daniel P. Berrange37788f22015-10-14 13:14:04 +010047 const uint8_t *key, size_t nkey,
48 const uint8_t *salt, size_t nsalt,
Daniel P. Berrange59b060b2016-09-12 12:50:12 +010049 uint64_t iterations,
Daniel P. Berrange37788f22015-10-14 13:14:04 +010050 uint8_t *out, size_t nout,
51 Error **errp)
52{
Daniel P. Berrange533008f2016-09-07 13:12:28 +010053 union {
54 struct hmac_md5_ctx md5;
55 struct hmac_sha1_ctx sha1;
56 struct hmac_sha224_ctx sha224;
57 struct hmac_sha256_ctx sha256;
58 struct hmac_sha384_ctx sha384;
59 struct hmac_sha512_ctx sha512;
60 struct hmac_ripemd160_ctx ripemd160;
liequan ched078da82024-10-30 08:51:46 +000061#ifdef CONFIG_CRYPTO_SM3
62 struct hmac_sm3_ctx sm3;
63#endif
Daniel P. Berrange533008f2016-09-07 13:12:28 +010064 } ctx;
65
Daniel P. Berrange59b060b2016-09-12 12:50:12 +010066 if (iterations > UINT_MAX) {
67 error_setg_errno(errp, ERANGE,
68 "PBKDF iterations %llu must be less than %u",
69 (long long unsigned)iterations, UINT_MAX);
70 return -1;
71 }
Daniel P. Berrange533008f2016-09-07 13:12:28 +010072
Daniel P. Berrange37788f22015-10-14 13:14:04 +010073 switch (hash) {
Markus Armbrusteref834aa2024-09-04 13:18:28 +020074 case QCRYPTO_HASH_ALGO_MD5:
Daniel P. Berrange533008f2016-09-07 13:12:28 +010075 hmac_md5_set_key(&ctx.md5, nkey, key);
76 PBKDF2(&ctx.md5, hmac_md5_update, hmac_md5_digest,
77 MD5_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
78 break;
79
Markus Armbrusteref834aa2024-09-04 13:18:28 +020080 case QCRYPTO_HASH_ALGO_SHA1:
Daniel P. Berrange533008f2016-09-07 13:12:28 +010081 hmac_sha1_set_key(&ctx.sha1, nkey, key);
82 PBKDF2(&ctx.sha1, hmac_sha1_update, hmac_sha1_digest,
83 SHA1_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
84 break;
85
Markus Armbrusteref834aa2024-09-04 13:18:28 +020086 case QCRYPTO_HASH_ALGO_SHA224:
Daniel P. Berrange533008f2016-09-07 13:12:28 +010087 hmac_sha224_set_key(&ctx.sha224, nkey, key);
88 PBKDF2(&ctx.sha224, hmac_sha224_update, hmac_sha224_digest,
89 SHA224_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
Daniel P. Berrange37788f22015-10-14 13:14:04 +010090 break;
91
Markus Armbrusteref834aa2024-09-04 13:18:28 +020092 case QCRYPTO_HASH_ALGO_SHA256:
Daniel P. Berrange533008f2016-09-07 13:12:28 +010093 hmac_sha256_set_key(&ctx.sha256, nkey, key);
94 PBKDF2(&ctx.sha256, hmac_sha256_update, hmac_sha256_digest,
95 SHA256_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
96 break;
97
Markus Armbrusteref834aa2024-09-04 13:18:28 +020098 case QCRYPTO_HASH_ALGO_SHA384:
Daniel P. Berrange533008f2016-09-07 13:12:28 +010099 hmac_sha384_set_key(&ctx.sha384, nkey, key);
100 PBKDF2(&ctx.sha384, hmac_sha384_update, hmac_sha384_digest,
101 SHA384_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
102 break;
103
Markus Armbrusteref834aa2024-09-04 13:18:28 +0200104 case QCRYPTO_HASH_ALGO_SHA512:
Daniel P. Berrange533008f2016-09-07 13:12:28 +0100105 hmac_sha512_set_key(&ctx.sha512, nkey, key);
106 PBKDF2(&ctx.sha512, hmac_sha512_update, hmac_sha512_digest,
107 SHA512_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
108 break;
109
Markus Armbrusteref834aa2024-09-04 13:18:28 +0200110 case QCRYPTO_HASH_ALGO_RIPEMD160:
Daniel P. Berrange533008f2016-09-07 13:12:28 +0100111 hmac_ripemd160_set_key(&ctx.ripemd160, nkey, key);
112 PBKDF2(&ctx.ripemd160, hmac_ripemd160_update, hmac_ripemd160_digest,
113 RIPEMD160_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
Daniel P. Berrange37788f22015-10-14 13:14:04 +0100114 break;
liequan ched078da82024-10-30 08:51:46 +0000115#ifdef CONFIG_CRYPTO_SM3
116 case QCRYPTO_HASH_ALGO_SM3:
117 hmac_sm3_set_key(&ctx.sm3, nkey, key);
118 PBKDF2(&ctx.sm3, hmac_sm3_update, hmac_sm3_digest,
119 SM3_DIGEST_SIZE, iterations, nsalt, salt, nout, out);
120 break;
121#endif
Daniel P. Berrange37788f22015-10-14 13:14:04 +0100122
123 default:
124 error_setg_errno(errp, ENOSYS,
Daniel P. Berrange533008f2016-09-07 13:12:28 +0100125 "PBKDF does not support hash algorithm %s",
Markus Armbrusteref834aa2024-09-04 13:18:28 +0200126 QCryptoHashAlgo_str(hash));
Daniel P. Berrange37788f22015-10-14 13:14:04 +0100127 return -1;
128 }
129 return 0;
130}