crypto: hmac: add hmac driver framework
1) makes the public APIs in hmac-nettle/gcrypt/glib static, and rename them with "nettle/gcrypt/glib" prefix. 2) introduces hmac framework, including QCryptoHmacDriver and new public APIs. Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
parent
d73c04e3ca
commit
14a5a2aef4
@ -15,6 +15,7 @@
|
|||||||
#include "qemu/osdep.h"
|
#include "qemu/osdep.h"
|
||||||
#include "qapi/error.h"
|
#include "qapi/error.h"
|
||||||
#include "crypto/hmac.h"
|
#include "crypto/hmac.h"
|
||||||
|
#include "hmacpriv.h"
|
||||||
#include <gcrypt.h>
|
#include <gcrypt.h>
|
||||||
|
|
||||||
static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = {
|
static int qcrypto_hmac_alg_map[QCRYPTO_HASH_ALG__MAX] = {
|
||||||
@ -42,10 +43,9 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QCryptoHmacGcrypt *
|
void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
|
||||||
qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
|
const uint8_t *key, size_t nkey,
|
||||||
const uint8_t *key, size_t nkey,
|
Error **errp)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
QCryptoHmacGcrypt *ctx;
|
QCryptoHmacGcrypt *ctx;
|
||||||
gcry_error_t err;
|
gcry_error_t err;
|
||||||
@ -81,27 +81,24 @@ error:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void qcrypto_hmac_free(QCryptoHmac *hmac)
|
static void
|
||||||
|
qcrypto_gcrypt_hmac_ctx_free(QCryptoHmac *hmac)
|
||||||
{
|
{
|
||||||
QCryptoHmacGcrypt *ctx;
|
QCryptoHmacGcrypt *ctx;
|
||||||
|
|
||||||
if (!hmac) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx = hmac->opaque;
|
ctx = hmac->opaque;
|
||||||
gcry_mac_close(ctx->handle);
|
gcry_mac_close(ctx->handle);
|
||||||
|
|
||||||
g_free(ctx);
|
g_free(ctx);
|
||||||
g_free(hmac);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
|
static int
|
||||||
const struct iovec *iov,
|
qcrypto_gcrypt_hmac_bytesv(QCryptoHmac *hmac,
|
||||||
size_t niov,
|
const struct iovec *iov,
|
||||||
uint8_t **result,
|
size_t niov,
|
||||||
size_t *resultlen,
|
uint8_t **result,
|
||||||
Error **errp)
|
size_t *resultlen,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
QCryptoHmacGcrypt *ctx;
|
QCryptoHmacGcrypt *ctx;
|
||||||
gcry_error_t err;
|
gcry_error_t err;
|
||||||
@ -147,21 +144,7 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,
|
QCryptoHmacDriver qcrypto_hmac_lib_driver = {
|
||||||
const uint8_t *key, size_t nkey,
|
.hmac_bytesv = qcrypto_gcrypt_hmac_bytesv,
|
||||||
Error **errp)
|
.hmac_free = qcrypto_gcrypt_hmac_ctx_free,
|
||||||
{
|
};
|
||||||
QCryptoHmac *hmac;
|
|
||||||
QCryptoHmacGcrypt *ctx;
|
|
||||||
|
|
||||||
ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp);
|
|
||||||
if (!ctx) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
hmac = g_new0(QCryptoHmac, 1);
|
|
||||||
hmac->alg = alg;
|
|
||||||
hmac->opaque = ctx;
|
|
||||||
|
|
||||||
return hmac;
|
|
||||||
}
|
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
#include "qemu/osdep.h"
|
#include "qemu/osdep.h"
|
||||||
#include "qapi/error.h"
|
#include "qapi/error.h"
|
||||||
#include "crypto/hmac.h"
|
#include "crypto/hmac.h"
|
||||||
|
#include "hmacpriv.h"
|
||||||
|
|
||||||
/* Support for HMAC Algos has been added in GLib 2.30 */
|
/* Support for HMAC Algos has been added in GLib 2.30 */
|
||||||
#if GLIB_CHECK_VERSION(2, 30, 0)
|
#if GLIB_CHECK_VERSION(2, 30, 0)
|
||||||
@ -49,10 +50,9 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QCryptoHmacGlib *
|
void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
|
||||||
qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
|
const uint8_t *key, size_t nkey,
|
||||||
const uint8_t *key, size_t nkey,
|
Error **errp)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
QCryptoHmacGlib *ctx;
|
QCryptoHmacGlib *ctx;
|
||||||
|
|
||||||
@ -78,27 +78,24 @@ error:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void qcrypto_hmac_free(QCryptoHmac *hmac)
|
static void
|
||||||
|
qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac)
|
||||||
{
|
{
|
||||||
QCryptoHmacGlib *ctx;
|
QCryptoHmacGlib *ctx;
|
||||||
|
|
||||||
if (!hmac) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx = hmac->opaque;
|
ctx = hmac->opaque;
|
||||||
g_hmac_unref(ctx->ghmac);
|
g_hmac_unref(ctx->ghmac);
|
||||||
|
|
||||||
g_free(ctx);
|
g_free(ctx);
|
||||||
g_free(hmac);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
|
static int
|
||||||
const struct iovec *iov,
|
qcrypto_glib_hmac_bytesv(QCryptoHmac *hmac,
|
||||||
size_t niov,
|
const struct iovec *iov,
|
||||||
uint8_t **result,
|
size_t niov,
|
||||||
size_t *resultlen,
|
uint8_t **result,
|
||||||
Error **errp)
|
size_t *resultlen,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
QCryptoHmacGlib *ctx;
|
QCryptoHmacGlib *ctx;
|
||||||
int i, ret;
|
int i, ret;
|
||||||
@ -129,25 +126,6 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,
|
|
||||||
const uint8_t *key, size_t nkey,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
QCryptoHmac *hmac;
|
|
||||||
QCryptoHmacGlib *ctx;
|
|
||||||
|
|
||||||
ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp);
|
|
||||||
if (!ctx) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
hmac = g_new0(QCryptoHmac, 1);
|
|
||||||
hmac->alg = alg;
|
|
||||||
hmac->opaque = ctx;
|
|
||||||
|
|
||||||
return hmac;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)
|
bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)
|
||||||
@ -155,26 +133,33 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,
|
void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
|
||||||
const uint8_t *key, size_t nkey,
|
const uint8_t *key, size_t nkey,
|
||||||
Error **errp)
|
Error **errp)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void qcrypto_hmac_free(QCryptoHmac *hmac)
|
static void
|
||||||
|
qcrypto_glib_hmac_ctx_free(QCryptoHmac *hmac)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
|
static int
|
||||||
const struct iovec *iov,
|
qcrypto_glib_hmac_bytesv(QCryptoHmac *hmac,
|
||||||
size_t niov,
|
const struct iovec *iov,
|
||||||
uint8_t **result,
|
size_t niov,
|
||||||
size_t *resultlen,
|
uint8_t **result,
|
||||||
Error **errp)
|
size_t *resultlen,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
QCryptoHmacDriver qcrypto_hmac_lib_driver = {
|
||||||
|
.hmac_bytesv = qcrypto_glib_hmac_bytesv,
|
||||||
|
.hmac_free = qcrypto_glib_hmac_ctx_free,
|
||||||
|
};
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
#include "qemu/osdep.h"
|
#include "qemu/osdep.h"
|
||||||
#include "qapi/error.h"
|
#include "qapi/error.h"
|
||||||
#include "crypto/hmac.h"
|
#include "crypto/hmac.h"
|
||||||
|
#include "hmacpriv.h"
|
||||||
#include <nettle/hmac.h>
|
#include <nettle/hmac.h>
|
||||||
|
|
||||||
typedef void (*qcrypto_nettle_hmac_setkey)(void *ctx,
|
typedef void (*qcrypto_nettle_hmac_setkey)(void *ctx,
|
||||||
@ -97,10 +98,9 @@ bool qcrypto_hmac_supports(QCryptoHashAlgorithm alg)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QCryptoHmacNettle *
|
void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
|
||||||
qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
|
const uint8_t *key, size_t nkey,
|
||||||
const uint8_t *key, size_t nkey,
|
Error **errp)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
QCryptoHmacNettle *ctx;
|
QCryptoHmacNettle *ctx;
|
||||||
|
|
||||||
@ -117,26 +117,22 @@ qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
|
|||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
void qcrypto_hmac_free(QCryptoHmac *hmac)
|
static void
|
||||||
|
qcrypto_nettle_hmac_ctx_free(QCryptoHmac *hmac)
|
||||||
{
|
{
|
||||||
QCryptoHmacNettle *ctx;
|
QCryptoHmacNettle *ctx;
|
||||||
|
|
||||||
if (!hmac) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx = hmac->opaque;
|
ctx = hmac->opaque;
|
||||||
|
|
||||||
g_free(ctx);
|
g_free(ctx);
|
||||||
g_free(hmac);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
|
static int
|
||||||
const struct iovec *iov,
|
qcrypto_nettle_hmac_bytesv(QCryptoHmac *hmac,
|
||||||
size_t niov,
|
const struct iovec *iov,
|
||||||
uint8_t **result,
|
size_t niov,
|
||||||
size_t *resultlen,
|
uint8_t **result,
|
||||||
Error **errp)
|
size_t *resultlen,
|
||||||
|
Error **errp)
|
||||||
{
|
{
|
||||||
QCryptoHmacNettle *ctx;
|
QCryptoHmacNettle *ctx;
|
||||||
int i;
|
int i;
|
||||||
@ -169,21 +165,7 @@ int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,
|
QCryptoHmacDriver qcrypto_hmac_lib_driver = {
|
||||||
const uint8_t *key, size_t nkey,
|
.hmac_bytesv = qcrypto_nettle_hmac_bytesv,
|
||||||
Error **errp)
|
.hmac_free = qcrypto_nettle_hmac_ctx_free,
|
||||||
{
|
};
|
||||||
QCryptoHmac *hmac;
|
|
||||||
QCryptoHmacNettle *ctx;
|
|
||||||
|
|
||||||
ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp);
|
|
||||||
if (!ctx) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
hmac = g_new0(QCryptoHmac, 1);
|
|
||||||
hmac->alg = alg;
|
|
||||||
hmac->opaque = ctx;
|
|
||||||
|
|
||||||
return hmac;
|
|
||||||
}
|
|
||||||
|
@ -12,9 +12,22 @@
|
|||||||
#include "qemu/osdep.h"
|
#include "qemu/osdep.h"
|
||||||
#include "qapi/error.h"
|
#include "qapi/error.h"
|
||||||
#include "crypto/hmac.h"
|
#include "crypto/hmac.h"
|
||||||
|
#include "hmacpriv.h"
|
||||||
|
|
||||||
static const char hex[] = "0123456789abcdef";
|
static const char hex[] = "0123456789abcdef";
|
||||||
|
|
||||||
|
int qcrypto_hmac_bytesv(QCryptoHmac *hmac,
|
||||||
|
const struct iovec *iov,
|
||||||
|
size_t niov,
|
||||||
|
uint8_t **result,
|
||||||
|
size_t *resultlen,
|
||||||
|
Error **errp)
|
||||||
|
{
|
||||||
|
QCryptoHmacDriver *drv = hmac->driver;
|
||||||
|
|
||||||
|
return drv->hmac_bytesv(hmac, iov, niov, result, resultlen, errp);
|
||||||
|
}
|
||||||
|
|
||||||
int qcrypto_hmac_bytes(QCryptoHmac *hmac,
|
int qcrypto_hmac_bytes(QCryptoHmac *hmac,
|
||||||
const char *buf,
|
const char *buf,
|
||||||
size_t len,
|
size_t len,
|
||||||
@ -70,3 +83,34 @@ int qcrypto_hmac_digest(QCryptoHmac *hmac,
|
|||||||
|
|
||||||
return qcrypto_hmac_digestv(hmac, &iov, 1, digest, errp);
|
return qcrypto_hmac_digestv(hmac, &iov, 1, digest, errp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QCryptoHmac *qcrypto_hmac_new(QCryptoHashAlgorithm alg,
|
||||||
|
const uint8_t *key, size_t nkey,
|
||||||
|
Error **errp)
|
||||||
|
{
|
||||||
|
QCryptoHmac *hmac;
|
||||||
|
void *ctx;
|
||||||
|
|
||||||
|
ctx = qcrypto_hmac_ctx_new(alg, key, nkey, errp);
|
||||||
|
if (!ctx) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
hmac = g_new0(QCryptoHmac, 1);
|
||||||
|
hmac->alg = alg;
|
||||||
|
hmac->opaque = ctx;
|
||||||
|
hmac->driver = (void *)&qcrypto_hmac_lib_driver;
|
||||||
|
|
||||||
|
return hmac;
|
||||||
|
}
|
||||||
|
|
||||||
|
void qcrypto_hmac_free(QCryptoHmac *hmac)
|
||||||
|
{
|
||||||
|
QCryptoHmacDriver *drv;
|
||||||
|
|
||||||
|
if (hmac) {
|
||||||
|
drv = hmac->driver;
|
||||||
|
drv->hmac_free(hmac);
|
||||||
|
g_free(hmac);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
36
crypto/hmacpriv.h
Normal file
36
crypto/hmacpriv.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* QEMU Crypto hmac driver supports
|
||||||
|
*
|
||||||
|
* Copyright (c) 2017 HUAWEI TECHNOLOGIES CO., LTD.
|
||||||
|
*
|
||||||
|
* Authors:
|
||||||
|
* Longpeng(Mike) <longpeng2@huawei.com>
|
||||||
|
*
|
||||||
|
* This work is licensed under the terms of the GNU GPL, version 2 or
|
||||||
|
* (at your option) any later version. See the COPYING file in the
|
||||||
|
* top-level directory.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef QCRYPTO_HMACPRIV_H
|
||||||
|
#define QCRYPTO_HMACPRIV_H
|
||||||
|
|
||||||
|
typedef struct QCryptoHmacDriver QCryptoHmacDriver;
|
||||||
|
|
||||||
|
struct QCryptoHmacDriver {
|
||||||
|
int (*hmac_bytesv)(QCryptoHmac *hmac,
|
||||||
|
const struct iovec *iov,
|
||||||
|
size_t niov,
|
||||||
|
uint8_t **result,
|
||||||
|
size_t *resultlen,
|
||||||
|
Error **errp);
|
||||||
|
|
||||||
|
void (*hmac_free)(QCryptoHmac *hmac);
|
||||||
|
};
|
||||||
|
|
||||||
|
extern void *qcrypto_hmac_ctx_new(QCryptoHashAlgorithm alg,
|
||||||
|
const uint8_t *key, size_t nkey,
|
||||||
|
Error **errp);
|
||||||
|
extern QCryptoHmacDriver qcrypto_hmac_lib_driver;
|
||||||
|
|
||||||
|
#endif
|
@ -18,6 +18,7 @@ typedef struct QCryptoHmac QCryptoHmac;
|
|||||||
struct QCryptoHmac {
|
struct QCryptoHmac {
|
||||||
QCryptoHashAlgorithm alg;
|
QCryptoHashAlgorithm alg;
|
||||||
void *opaque;
|
void *opaque;
|
||||||
|
void *driver;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user