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 /* Support for HMAC Algos has been added in GLib 2.30 */ 21 #if GLIB_CHECK_VERSION(2, 30, 0) 22 23 static 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, 27 /* Support for HMAC SHA-512 in GLib 2.42 */ 28 #if GLIB_CHECK_VERSION(2, 42, 0) 29 [QCRYPTO_HASH_ALG_SHA512] = G_CHECKSUM_SHA512, 30 #else 31 [QCRYPTO_HASH_ALG_SHA512] = -1, 32 #endif 33 [QCRYPTO_HASH_ALG_SHA224] = -1, 34 [QCRYPTO_HASH_ALG_SHA384] = -1, 35 [QCRYPTO_HASH_ALG_RIPEMD160] = -1, 36 }; 37 38 typedef struct QCryptoHmacGlib QCryptoHmacGlib; 39 struct QCryptoHmacGlib { 40 GHmac *ghmac; 41 }; 42 43 bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) 44 { 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 53 void *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 } 64 65 ctx = g_new0(QCryptoHmacGlib, 1); 66 67 ctx->ghmac = g_hmac_new(qcrypto_hmac_alg_map[alg], 68 (const uint8_t *)key, nkey); 69 if (!ctx->ghmac) { 70 error_setg(errp, "Cannot initialize hmac and set key"); 71 goto error; 72 } 73 74 return ctx; 75 76 error: 77 g_free(ctx); 78 return NULL; 79 } 80 81 static void 82 qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac) 83 { 84 QCryptoHmacGlib *ctx; 85 86 ctx = hmac->opaque; 87 g_hmac_unref(ctx->ghmac); 88 89 g_free(ctx); 90 } 91 92 static int 93 qcrypto_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) 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); 107 } 108 109 ret = g_checksum_type_get_length(qcrypto_hmac_alg_map[hmac->alg]); 110 if (ret < 0) { 111 error_setg(errp, "Unable to get hmac length"); 112 return -1; 113 } 114 115 if (*resultlen == 0) { 116 *resultlen = ret; 117 *result = g_new0(uint8_t, *resultlen); 118 } else if (*resultlen != ret) { 119 error_setg(errp, "Result buffer size %zu is smaller than hmac %d", 120 *resultlen, ret); 121 return -1; 122 } 123 124 g_hmac_get_digest(ctx->ghmac, *result, resultlen); 125 126 return 0; 127 } 128 129 #else 130 131 bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg) 132 { 133 return false; 134 } 135 136 void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, 137 const uint8_t *key, size_t nkey, 138 Error **errp) 139 { 140 return NULL; 141 } 142 143 static void 144 qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac) 145 { 146 return; 147 } 148 149 static int 150 qcrypto_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) 156 { 157 return -1; 158 } 159 160 #endif 161 162 QCryptoHmacDriver qcrypto_hmac_lib_driver = { 163 .hmac_bytesv = qcrypto_glib_hmac_bytesv, 164 .hmac_free = qcrypto_glib_hmac_ctx_free, 165 }; 166