void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg, const uint8_t *key, size_t nkey, Error **errp) { QCryptoHmacGlib *ctx; if (!qcrypto_hmac_supports(alg)) { error_setg(errp, "Unsupported hmac algorithm %s", QCryptoHashAlgorithm_str(alg)); return NULL; } ctx = g_new0(QCryptoHmacGlib, 1); ctx->ghmac = g_hmac_new(qcrypto_hmac_alg_map[alg], (const uint8_t *)key, nkey); if (!ctx->ghmac) { error_setg(errp, "Cannot initialize hmac and set key"); goto error; } return ctx; error: g_free(ctx); return NULL; }
QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg, const uint8_t *key, size_t nkey, Error **errp) { QCryptoHmac *hmac; QCryptoHmacGcrypt *ctx; gcry_error_t err; if (!qcrypto_hmac_supports(alg)) { error_setg(errp, "Unsupported hmac algorithm %s", QCryptoHashAlgorithm_lookup[alg]); return NULL; } hmac = g_new0(QCryptoHmac, 1); hmac->alg = alg; ctx = g_new0(QCryptoHmacGcrypt, 1); err = gcry_mac_open(&ctx->handle, qcrypto_hmac_alg_map[alg], GCRY_MAC_FLAG_SECURE, NULL); if (err != 0) { error_setg(errp, "Cannot initialize hmac: %s", gcry_strerror(err)); goto error; } err = gcry_mac_setkey(ctx->handle, (const void *)key, nkey); if (err != 0) { error_setg(errp, "Cannot set key: %s", gcry_strerror(err)); goto error; } hmac->opaque = ctx; return hmac; error: g_free(ctx); g_free(hmac); return NULL; }