xref: /openbmc/qemu/crypto/pbkdf-gcrypt.c (revision 571bdc97)
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.1 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"
22 #include <gcrypt.h>
23 #include "qapi/error.h"
24 #include "crypto/pbkdf.h"
25 
26 bool qcrypto_pbkdf2_supports(QCryptoHashAlgo hash)
27 {
28     switch (hash) {
29     case QCRYPTO_HASH_ALGO_MD5:
30     case QCRYPTO_HASH_ALGO_SHA1:
31     case QCRYPTO_HASH_ALGO_SHA224:
32     case QCRYPTO_HASH_ALGO_SHA256:
33     case QCRYPTO_HASH_ALGO_SHA384:
34     case QCRYPTO_HASH_ALGO_SHA512:
35     case QCRYPTO_HASH_ALGO_RIPEMD160:
36 #ifdef CONFIG_CRYPTO_SM3
37     case QCRYPTO_HASH_ALGO_SM3:
38 #endif
39         return qcrypto_hash_supports(hash);
40     default:
41         return false;
42     }
43 }
44 
45 int qcrypto_pbkdf2(QCryptoHashAlgo hash,
46                    const uint8_t *key, size_t nkey,
47                    const uint8_t *salt, size_t nsalt,
48                    uint64_t iterations,
49                    uint8_t *out, size_t nout,
50                    Error **errp)
51 {
52     static const int hash_map[QCRYPTO_HASH_ALGO__MAX] = {
53         [QCRYPTO_HASH_ALGO_MD5] = GCRY_MD_MD5,
54         [QCRYPTO_HASH_ALGO_SHA1] = GCRY_MD_SHA1,
55         [QCRYPTO_HASH_ALGO_SHA224] = GCRY_MD_SHA224,
56         [QCRYPTO_HASH_ALGO_SHA256] = GCRY_MD_SHA256,
57         [QCRYPTO_HASH_ALGO_SHA384] = GCRY_MD_SHA384,
58         [QCRYPTO_HASH_ALGO_SHA512] = GCRY_MD_SHA512,
59         [QCRYPTO_HASH_ALGO_RIPEMD160] = GCRY_MD_RMD160,
60 #ifdef CONFIG_CRYPTO_SM3
61         [QCRYPTO_HASH_ALGO_SM3] = GCRY_MD_SM3,
62 #endif
63     };
64     int ret;
65 
66     if (iterations > ULONG_MAX) {
67         error_setg_errno(errp, ERANGE,
68                          "PBKDF iterations %llu must be less than %lu",
69                          (long long unsigned)iterations, ULONG_MAX);
70         return -1;
71     }
72 
73     if (hash >= G_N_ELEMENTS(hash_map) ||
74         hash_map[hash] == GCRY_MD_NONE) {
75         error_setg_errno(errp, ENOSYS,
76                          "PBKDF does not support hash algorithm %s",
77                          QCryptoHashAlgo_str(hash));
78         return -1;
79     }
80 
81     ret = gcry_kdf_derive(key, nkey, GCRY_KDF_PBKDF2,
82                           hash_map[hash],
83                           salt, nsalt, iterations,
84                           nout, out);
85     if (ret != 0) {
86         error_setg(errp, "Cannot derive password: %s",
87                    gcry_strerror(ret));
88         return -1;
89     }
90 
91     return 0;
92 }
93