xref: /openbmc/qemu/crypto/hash.c (revision 7b36064c)
1 /*
2  * QEMU Crypto hash algorithms
3  *
4  * Copyright (c) 2015 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 "crypto/hash.h"
22 
23 #ifdef CONFIG_GNUTLS_HASH
24 #include <gnutls/gnutls.h>
25 #include <gnutls/crypto.h>
26 
27 static int qcrypto_hash_alg_map[QCRYPTO_HASH_ALG_LAST] = {
28     [QCRYPTO_HASH_ALG_MD5] = GNUTLS_DIG_MD5,
29     [QCRYPTO_HASH_ALG_SHA1] = GNUTLS_DIG_SHA1,
30     [QCRYPTO_HASH_ALG_SHA256] = GNUTLS_DIG_SHA256,
31 };
32 
33 static size_t qcrypto_hash_alg_size[QCRYPTO_HASH_ALG_LAST] = {
34     [QCRYPTO_HASH_ALG_MD5] = 16,
35     [QCRYPTO_HASH_ALG_SHA1] = 20,
36     [QCRYPTO_HASH_ALG_SHA256] = 32,
37 };
38 
39 gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
40 {
41     if (alg < G_N_ELEMENTS(qcrypto_hash_alg_map)) {
42         return true;
43     }
44     return false;
45 }
46 
47 size_t qcrypto_hash_digest_len(QCryptoHashAlgorithm alg)
48 {
49     if (alg >= G_N_ELEMENTS(qcrypto_hash_alg_size)) {
50         return 0;
51     }
52     return qcrypto_hash_alg_size[alg];
53 }
54 
55 
56 int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
57                         const struct iovec *iov,
58                         size_t niov,
59                         uint8_t **result,
60                         size_t *resultlen,
61                         Error **errp)
62 {
63     int i, ret;
64     gnutls_hash_hd_t dig;
65 
66     if (alg >= G_N_ELEMENTS(qcrypto_hash_alg_map)) {
67         error_setg(errp,
68                    "Unknown hash algorithm %d",
69                    alg);
70         return -1;
71     }
72 
73     ret = gnutls_hash_init(&dig, qcrypto_hash_alg_map[alg]);
74 
75     if (ret < 0) {
76         error_setg(errp,
77                    "Unable to initialize hash algorithm: %s",
78                    gnutls_strerror(ret));
79         return -1;
80     }
81 
82     for (i = 0; i < niov; i++) {
83         ret = gnutls_hash(dig, iov[i].iov_base, iov[i].iov_len);
84         if (ret < 0) {
85             error_setg(errp,
86                        "Unable process hash data: %s",
87                        gnutls_strerror(ret));
88             goto error;
89         }
90     }
91 
92     ret = gnutls_hash_get_len(qcrypto_hash_alg_map[alg]);
93     if (ret <= 0) {
94         error_setg(errp,
95                    "Unable to get hash length: %s",
96                    gnutls_strerror(ret));
97         goto error;
98     }
99     if (*resultlen == 0) {
100         *resultlen = ret;
101         *result = g_new0(uint8_t, *resultlen);
102     } else if (*resultlen != ret) {
103         error_setg(errp,
104                    "Result buffer size %zu is smaller than hash %d",
105                    *resultlen, ret);
106         goto error;
107     }
108 
109     gnutls_hash_deinit(dig, *result);
110     return 0;
111 
112  error:
113     gnutls_hash_deinit(dig, NULL);
114     return -1;
115 }
116 
117 #else /* ! CONFIG_GNUTLS_HASH */
118 
119 gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg G_GNUC_UNUSED)
120 {
121     return false;
122 }
123 
124 int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
125                         const struct iovec *iov G_GNUC_UNUSED,
126                         size_t niov G_GNUC_UNUSED,
127                         uint8_t **result G_GNUC_UNUSED,
128                         size_t *resultlen G_GNUC_UNUSED,
129                         Error **errp)
130 {
131     error_setg(errp,
132                "Hash algorithm %d not supported without GNUTLS",
133                alg);
134     return -1;
135 }
136 
137 #endif /* ! CONFIG_GNUTLS_HASH */
138 
139 int qcrypto_hash_bytes(QCryptoHashAlgorithm alg,
140                        const char *buf,
141                        size_t len,
142                        uint8_t **result,
143                        size_t *resultlen,
144                        Error **errp)
145 {
146     struct iovec iov = { .iov_base = (char *)buf,
147                          .iov_len = len };
148     return qcrypto_hash_bytesv(alg, &iov, 1, result, resultlen, errp);
149 }
150 
151 static const char hex[] = "0123456789abcdef";
152 
153 int qcrypto_hash_digestv(QCryptoHashAlgorithm alg,
154                          const struct iovec *iov,
155                          size_t niov,
156                          char **digest,
157                          Error **errp)
158 {
159     uint8_t *result = NULL;
160     size_t resultlen = 0;
161     size_t i;
162 
163     if (qcrypto_hash_bytesv(alg, iov, niov, &result, &resultlen, errp) < 0) {
164         return -1;
165     }
166 
167     *digest = g_new0(char, (resultlen * 2) + 1);
168     for (i = 0 ; i < resultlen ; i++) {
169         (*digest)[(i * 2)] = hex[(result[i] >> 4) & 0xf];
170         (*digest)[(i * 2) + 1] = hex[result[i] & 0xf];
171     }
172     (*digest)[resultlen * 2] = '\0';
173     g_free(result);
174     return 0;
175 }
176 
177 int qcrypto_hash_digest(QCryptoHashAlgorithm alg,
178                         const char *buf,
179                         size_t len,
180                         char **digest,
181                         Error **errp)
182 {
183     struct iovec iov = { .iov_base = (char *)buf, .iov_len = len };
184 
185     return qcrypto_hash_digestv(alg, &iov, 1, digest, errp);
186 }
187 
188 int qcrypto_hash_base64v(QCryptoHashAlgorithm alg,
189                          const struct iovec *iov,
190                          size_t niov,
191                          char **base64,
192                          Error **errp)
193 {
194     uint8_t *result = NULL;
195     size_t resultlen = 0;
196 
197     if (qcrypto_hash_bytesv(alg, iov, niov, &result, &resultlen, errp) < 0) {
198         return -1;
199     }
200 
201     *base64 = g_base64_encode(result, resultlen);
202     g_free(result);
203     return 0;
204 }
205 
206 int qcrypto_hash_base64(QCryptoHashAlgorithm alg,
207                         const char *buf,
208                         size_t len,
209                         char **base64,
210                         Error **errp)
211 {
212     struct iovec iov = { .iov_base = (char *)buf, .iov_len = len };
213 
214     return qcrypto_hash_base64v(alg, &iov, 1, base64, errp);
215 }
216