/src/qemu/crypto/hmac-glib.c
Line | Count | Source |
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" |
18 | | #include "hmacpriv.h" |
19 | | |
20 | | static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALGO__MAX] = { |
21 | | [QCRYPTO_HASH_ALGO_MD5] = G_CHECKSUM_MD5, |
22 | | [QCRYPTO_HASH_ALGO_SHA1] = G_CHECKSUM_SHA1, |
23 | | [QCRYPTO_HASH_ALGO_SHA256] = G_CHECKSUM_SHA256, |
24 | | [QCRYPTO_HASH_ALGO_SHA512] = G_CHECKSUM_SHA512, |
25 | | [QCRYPTO_HASH_ALGO_SHA224] = -1, |
26 | | [QCRYPTO_HASH_ALGO_SHA384] = -1, |
27 | | [QCRYPTO_HASH_ALGO_RIPEMD160] = -1, |
28 | | }; |
29 | | |
30 | | typedef struct QCryptoHmacGlib QCryptoHmacGlib; |
31 | | struct QCryptoHmacGlib { |
32 | | GHmac *ghmac; |
33 | | }; |
34 | | |
35 | | bool qcrypto_hmac_supports(QCryptoHashAlgo alg) |
36 | 63 | { |
37 | 63 | if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) && |
38 | 63 | qcrypto_hmac_alg_map[alg] != -1) { |
39 | 63 | return true; |
40 | 63 | } |
41 | | |
42 | 0 | return false; |
43 | 63 | } |
44 | | |
45 | | void *qcrypto_hmac_ctx_new(QCryptoHashAlgo alg, |
46 | | const uint8_t *key, size_t nkey, |
47 | | Error **errp) |
48 | 0 | { |
49 | 0 | g_autofree QCryptoHmacGlib *ctx = NULL; |
50 | |
|
51 | 0 | if (!qcrypto_hmac_supports(alg)) { |
52 | 0 | error_setg(errp, "Unsupported hmac algorithm %s", |
53 | 0 | QCryptoHashAlgo_str(alg)); |
54 | 0 | return NULL; |
55 | 0 | } |
56 | | |
57 | 0 | ctx = g_new0(QCryptoHmacGlib, 1); |
58 | |
|
59 | 0 | ctx->ghmac = g_hmac_new(qcrypto_hmac_alg_map[alg], |
60 | 0 | (const uint8_t *)key, nkey); |
61 | 0 | if (!ctx->ghmac) { |
62 | 0 | error_setg(errp, "Cannot initialize hmac and set key"); |
63 | 0 | return NULL; |
64 | 0 | } |
65 | | |
66 | 0 | return g_steal_pointer(&ctx); |
67 | 0 | } |
68 | | |
69 | | static void |
70 | | qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac) |
71 | 0 | { |
72 | 0 | QCryptoHmacGlib *ctx; |
73 | |
|
74 | 0 | ctx = hmac->opaque; |
75 | 0 | g_hmac_unref(ctx->ghmac); |
76 | |
|
77 | 0 | g_free(ctx); |
78 | 0 | } |
79 | | |
80 | | static int |
81 | | qcrypto_glib_hmac_bytesv(QCryptoHmac *hmac, |
82 | | const struct iovec *iov, |
83 | | size_t niov, |
84 | | uint8_t **result, |
85 | | size_t *resultlen, |
86 | | Error **errp) |
87 | 0 | { |
88 | 0 | QCryptoHmacGlib *ctx; |
89 | 0 | int i, ret; |
90 | |
|
91 | 0 | ctx = hmac->opaque; |
92 | |
|
93 | 0 | for (i = 0; i < niov; i++) { |
94 | 0 | g_hmac_update(ctx->ghmac, iov[i].iov_base, iov[i].iov_len); |
95 | 0 | } |
96 | |
|
97 | 0 | ret = g_checksum_type_get_length(qcrypto_hmac_alg_map[hmac->alg]); |
98 | 0 | if (ret < 0) { |
99 | 0 | error_setg(errp, "Unable to get hmac length"); |
100 | 0 | return -1; |
101 | 0 | } |
102 | | |
103 | 0 | if (resultlen == NULL) { |
104 | 0 | return 0; |
105 | 0 | } else if (*resultlen == 0) { |
106 | 0 | *resultlen = ret; |
107 | 0 | *result = g_new0(uint8_t, *resultlen); |
108 | 0 | } else if (*resultlen != ret) { |
109 | 0 | error_setg(errp, "Result buffer size %zu is smaller than hmac %d", |
110 | 0 | *resultlen, ret); |
111 | 0 | return -1; |
112 | 0 | } |
113 | | |
114 | 0 | g_hmac_get_digest(ctx->ghmac, *result, resultlen); |
115 | |
|
116 | 0 | return 0; |
117 | 0 | } |
118 | | |
119 | | QCryptoHmacDriver qcrypto_hmac_lib_driver = { |
120 | | .hmac_bytesv = qcrypto_glib_hmac_bytesv, |
121 | | .hmac_free = qcrypto_glib_hmac_ctx_free, |
122 | | }; |