hmac-glib.c (d73c04e3ca3d4a1ac73da7f7952f769824417b47) | hmac-glib.c (14a5a2aef47e27b6afd1f8855eb41c75b69440dc) |
---|---|
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" | 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" |
|
18 19/* Support for HMAC Algos has been added in GLib 2.30 */ 20#if GLIB_CHECK_VERSION(2, 30, 0) 21 22static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = { 23 [QCRYPTO_HASH_ALG_MD5] = G_CHECKSUM_MD5, 24 [QCRYPTO_HASH_ALG_SHA1] = G_CHECKSUM_SHA1, 25 [QCRYPTO_HASH_ALG_SHA256] = G_CHECKSUM_SHA256, --- 18 unchanged lines hidden (view full) --- 44 if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) && 45 qcrypto_hmac_alg_map[alg] != -1) { 46 return true; 47 } 48 49 return false; 50} 51 | 19 20/* Support for HMAC Algos has been added in GLib 2.30 */ 21#if GLIB_CHECK_VERSION(2, 30, 0) 22 23static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = { 24 [QCRYPTO_HASH_ALG_MD5] = G_CHECKSUM_MD5, 25 [QCRYPTO_HASH_ALG_SHA1] = G_CHECKSUM_SHA1, 26 [QCRYPTO_HASH_ALG_SHA256] = G_CHECKSUM_SHA256, --- 18 unchanged lines hidden (view full) --- 45 if (alg < G_N_ELEMENTS(qcrypto_hmac_alg_map) && 46 qcrypto_hmac_alg_map[alg] != -1) { 47 return true; 48 } 49 50 return false; 51} 52 |
52static QCryptoHmacGlib * 53qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, 54 const uint8_t *key, size_t nkey, 55 Error **errp) | 53void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, 54 const uint8_t *key, size_t nkey, 55 Error **errp) |
56{ 57 QCryptoHmacGlib *ctx; 58 59 if (!qcrypto_hmac_supports(alg)) { 60 error_setg(errp, "Unsupported hmac algorithm %s", 61 QCryptoHashAlgorithm_lookup[alg]); 62 return NULL; 63 } --- 9 unchanged lines hidden (view full) --- 73 74 return ctx; 75 76error: 77 g_free(ctx); 78 return NULL; 79} 80 | 56{ 57 QCryptoHmacGlib *ctx; 58 59 if (!qcrypto_hmac_supports(alg)) { 60 error_setg(errp, "Unsupported hmac algorithm %s", 61 QCryptoHashAlgorithm_lookup[alg]); 62 return NULL; 63 } --- 9 unchanged lines hidden (view full) --- 73 74 return ctx; 75 76error: 77 g_free(ctx); 78 return NULL; 79} 80 |
81void qcrypto_hmac_free(QCryptoHmac *hmac) | 81static void 82qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac) |
82{ 83 QCryptoHmacGlib *ctx; 84 | 83{ 84 QCryptoHmacGlib *ctx; 85 |
85 if (!hmac) { 86 return; 87 } 88 | |
89 ctx = hmac->opaque; 90 g_hmac_unref(ctx->ghmac); 91 92 g_free(ctx); | 86 ctx = hmac->opaque; 87 g_hmac_unref(ctx->ghmac); 88 89 g_free(ctx); |
93 g_free(hmac); | |
94} 95 | 90} 91 |
96int qcrypto_hmac_bytesv(QCryptoHmac *hmac, 97 const struct iovec *iov, 98 size_t niov, 99 uint8_t **result, 100 size_t *resultlen, 101 Error **errp) | 92static int 93qcrypto_glib_hmac_bytesv(QCryptoHmac *hmac, 94 const struct iovec *iov, 95 size_t niov, 96 uint8_t **result, 97 size_t *resultlen, 98 Error **errp) |
102{ 103 QCryptoHmacGlib *ctx; 104 int i, ret; 105 106 ctx = hmac->opaque; 107 108 for (i = 0; i < niov; i++) { 109 g_hmac_update(ctx->ghmac, iov[i].iov_base, iov[i].iov_len); --- 14 unchanged lines hidden (view full) --- 124 return -1; 125 } 126 127 g_hmac_get_digest(ctx->ghmac, *result, resultlen); 128 129 return 0; 130} 131 | 99{ 100 QCryptoHmacGlib *ctx; 101 int i, ret; 102 103 ctx = hmac->opaque; 104 105 for (i = 0; i < niov; i++) { 106 g_hmac_update(ctx->ghmac, iov[i].iov_base, iov[i].iov_len); --- 14 unchanged lines hidden (view full) --- 121 return -1; 122 } 123 124 g_hmac_get_digest(ctx->ghmac, *result, resultlen); 125 126 return 0; 127} 128 |
132QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, 133 const uint8_t *key, size_t nkey, 134 Error **errp) 135{ 136 QCryptoHmac *hmac; 137 QCryptoHmacGlib *ctx; 138 139 ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp); 140 if (!ctx) { 141 return NULL; 142 } 143 144 hmac = g_new0(QCryptoHmac, 1); 145 hmac->alg = alg; 146 hmac->opaque = ctx; 147 148 return hmac; 149} 150 | |
151#else 152 153bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) 154{ 155 return false; 156} 157 | 129#else 130 131bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) 132{ 133 return false; 134} 135 |
158QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, 159 const uint8_t *key, size_t nkey, 160 Error **errp) | 136void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, 137 const uint8_t *key, size_t nkey, 138 Error **errp) |
161{ 162 return NULL; 163} 164 | 139{ 140 return NULL; 141} 142 |
165void qcrypto_hmac_free(QCryptoHmac *hmac) | 143static void 144qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac) |
166{ 167 return; 168} 169 | 145{ 146 return; 147} 148 |
170int qcrypto_hmac_bytesv(QCryptoHmac *hmac, 171 const struct iovec *iov, 172 size_t niov, 173 uint8_t **result, 174 size_t *resultlen, 175 Error **errp) | 149static int 150qcrypto_glib_hmac_bytesv(QCryptoHmac *hmac, 151 const struct iovec *iov, 152 size_t niov, 153 uint8_t **result, 154 size_t *resultlen, 155 Error **errp) |
176{ 177 return -1; 178} 179 180#endif | 156{ 157 return -1; 158} 159 160#endif |
161 162QCryptoHmacDriver qcrypto_hmac_lib_driver = { 163 .hmac_bytesv = qcrypto_glib_hmac_bytesv, 164 .hmac_free = qcrypto_glib_hmac_ctx_free, 165}; |
|