]> git.proxmox.com Git - mirror_qemu.git/blame - crypto/hmac-glib.c
migration/postcopy: break the loop when there is no more page to discard
[mirror_qemu.git] / crypto / hmac-glib.c
CommitLineData
12a4f216
LM
1/*
2 * QEMU Crypto hmac algorithms (based on glib)
3 *
4 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
5 *
6 * Authors:
7 * Longpeng(Mike) <longpeng2@huawei.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or
10 * (at your option) any later version. See the COPYING file in the
11 * top-level directory.
12 *
13 */
14
15#include "qemu/osdep.h"
16#include "qapi/error.h"
17#include "crypto/hmac.h"
14a5a2ae 18#include "hmacpriv.h"
12a4f216 19
e1168052
LM
20static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = {
21 [QCRYPTO_HASH_ALG_MD5] = G_CHECKSUM_MD5,
22 [QCRYPTO_HASH_ALG_SHA1] = G_CHECKSUM_SHA1,
23 [QCRYPTO_HASH_ALG_SHA256] = G_CHECKSUM_SHA256,
24/* Support for HMAC SHA-512 in GLib 2.42 */
25#if GLIB_CHECK_VERSION(2, 42, 0)
26 [QCRYPTO_HASH_ALG_SHA512] = G_CHECKSUM_SHA512,
27#else
28 [QCRYPTO_HASH_ALG_SHA512] = -1,
29#endif
30 [QCRYPTO_HASH_ALG_SHA224] = -1,
31 [QCRYPTO_HASH_ALG_SHA384] = -1,
32 [QCRYPTO_HASH_ALG_RIPEMD160] = -1,
33};
34
35typedef struct QCryptoHmacGlib QCryptoHmacGlib;
36struct QCryptoHmacGlib {
37 GHmac *ghmac;
38};
39
40bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)
41{
42 if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) &&
43 qcrypto_hmac_alg_map[alg] != -1) {
44 return true;
45 }
46
47 return false;
48}
49
14a5a2ae
LM
50void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
51 const uint8_t *key, size_t nkey,
52 Error **errp)
e1168052 53{
e1168052
LM
54 QCryptoHmacGlib *ctx;
55
56 if (!qcrypto_hmac_supports(alg)) {
57 error_setg(errp, "Unsupported hmac algorithm %s",
977c736f 58 QCryptoHashAlgorithm_str(alg));
e1168052
LM
59 return NULL;
60 }
61
e1168052
LM
62 ctx = g_new0(QCryptoHmacGlib, 1);
63
64 ctx->ghmac = g_hmac_new(qcrypto_hmac_alg_map[alg],
65 (const uint8_t *)key, nkey);
66 if (!ctx->ghmac) {
67 error_setg(errp, "Cannot initialize hmac and set key");
68 goto error;
69 }
70
d73c04e3 71 return ctx;
e1168052
LM
72
73error:
74 g_free(ctx);
e1168052
LM
75 return NULL;
76}
77
14a5a2ae
LM
78static void
79qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac)
e1168052
LM
80{
81 QCryptoHmacGlib *ctx;
82
e1168052
LM
83 ctx = hmac->opaque;
84 g_hmac_unref(ctx->ghmac);
85
86 g_free(ctx);
e1168052
LM
87}
88
14a5a2ae
LM
89static int
90qcrypto_glib_hmac_bytesv(QCryptoHmac *hmac,
91 const struct iovec *iov,
92 size_t niov,
93 uint8_t **result,
94 size_t *resultlen,
95 Error **errp)
e1168052
LM
96{
97 QCryptoHmacGlib *ctx;
98 int i, ret;
99
100 ctx = hmac->opaque;
101
102 for (i = 0; i < niov; i++) {
103 g_hmac_update(ctx->ghmac, iov[i].iov_base, iov[i].iov_len);
104 }
105
106 ret = g_checksum_type_get_length(qcrypto_hmac_alg_map[hmac->alg]);
107 if (ret < 0) {
108 error_setg(errp, "Unable to get hmac length");
109 return -1;
110 }
111
112 if (*resultlen == 0) {
113 *resultlen = ret;
114 *result = g_new0(uint8_t, *resultlen);
115 } else if (*resultlen != ret) {
116 error_setg(errp, "Result buffer size %zu is smaller than hmac %d",
117 *resultlen, ret);
118 return -1;
119 }
120
121 g_hmac_get_digest(ctx->ghmac, *result, resultlen);
122
123 return 0;
124}
125
14a5a2ae
LM
126QCryptoHmacDriver qcrypto_hmac_lib_driver = {
127 .hmac_bytesv = qcrypto_glib_hmac_bytesv,
128 .hmac_free = qcrypto_glib_hmac_ctx_free,
129};