xref: /openbmc/qemu/crypto/hash-glib.c (revision 9c489ea6)
1 /*
2  * QEMU Crypto hash algorithms
3  *
4  * Copyright (c) 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"
22 #include "qapi/error.h"
23 #include "crypto/hash.h"
24 #include "hashpriv.h"
25 
26 
27 static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG__MAX] = {
28     [QCRYPTO_HASH_ALG_MD5] = G_CHECKSUM_MD5,
29     [QCRYPTO_HASH_ALG_SHA1] = G_CHECKSUM_SHA1,
30     [QCRYPTO_HASH_ALG_SHA224] = -1,
31     [QCRYPTO_HASH_ALG_SHA256] = G_CHECKSUM_SHA256,
32     [QCRYPTO_HASH_ALG_SHA384] = -1,
33 #if GLIB_CHECK_VERSION(2, 36, 0)
34     [QCRYPTO_HASH_ALG_SHA512] = G_CHECKSUM_SHA512,
35 #else
36     [QCRYPTO_HASH_ALG_SHA512] = -1,
37 #endif
38     [QCRYPTO_HASH_ALG_RIPEMD160] = -1,
39 };
40 
41 gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
42 {
43     if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map) &&
44         qcrypto_hash_alg_map[alg] != -1) {
45         return true;
46     }
47     return false;
48 }
49 
50 
51 static int
52 qcrypto_glib_hash_bytesv(QCryptoHashAlgorithm alg,
53                          const struct iovec *iov,
54                          size_t niov,
55                          uint8_t **result,
56                          size_t *resultlen,
57                          Error **errp)
58 {
59     int i, ret;
60     GChecksum *cs;
61 
62     if (!qcrypto_hash_supports(alg)) {
63         error_setg(errp,
64                    "Unknown hash algorithm %d",
65                    alg);
66         return -1;
67     }
68 
69     cs = g_checksum_new(qcrypto_hash_alg_map[alg]);
70 
71     for (i = 0; i < niov; i++) {
72         g_checksum_update(cs, iov[i].iov_base, iov[i].iov_len);
73     }
74 
75     ret = g_checksum_type_get_length(qcrypto_hash_alg_map[alg]);
76     if (ret < 0) {
77         error_setg(errp, "%s",
78                    "Unable to get hash length");
79         goto error;
80     }
81     if (*resultlen == 0) {
82         *resultlen = ret;
83         *result = g_new0(uint8_t, *resultlen);
84     } else if (*resultlen != ret) {
85         error_setg(errp,
86                    "Result buffer size %zu is smaller than hash %d",
87                    *resultlen, ret);
88         goto error;
89     }
90 
91     g_checksum_get_digest(cs, *result, resultlen);
92 
93     g_checksum_free(cs);
94     return 0;
95 
96  error:
97     g_checksum_free(cs);
98     return -1;
99 }
100 
101 
102 QCryptoHashDriver qcrypto_hash_lib_driver = {
103     .hash_bytesv = qcrypto_glib_hash_bytesv,
104 };
105