crypto: cipher: add cipher driver framework

1) makes the public APIs in cipher-nettle/gcrypt/builtin static,
   and rename them with "nettle/gcrypt/builtin" prefix.

2) introduces cipher framework, including QCryptoCipherDriver
   and new public APIs.

Reviewed-by: Gonglei <arei.gonglei@huawei.com>
Signed-off-by: Longpeng(Mike) <longpeng2@huawei.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Longpeng(Mike) 2017-07-14 14:03:58 -04:00 committed by Daniel P. Berrange
parent d962c6266c
commit 75c8007809
6 changed files with 190 additions and 123 deletions

View File

@ -22,6 +22,7 @@
#include "crypto/aes.h" #include "crypto/aes.h"
#include "crypto/desrfb.h" #include "crypto/desrfb.h"
#include "crypto/xts.h" #include "crypto/xts.h"
#include "cipherpriv.h"
typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext; typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext;
struct QCryptoCipherBuiltinAESContext { struct QCryptoCipherBuiltinAESContext {
@ -466,21 +467,18 @@ static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
return ctxt; return ctxt;
} }
void qcrypto_cipher_free(QCryptoCipher *cipher) static void
qcrypto_builtin_cipher_ctx_free(QCryptoCipher *cipher)
{ {
QCryptoCipherBuiltin *ctxt; QCryptoCipherBuiltin *ctxt;
if (!cipher) {
return;
}
ctxt = cipher->opaque; ctxt = cipher->opaque;
ctxt->free(cipher); ctxt->free(cipher);
g_free(cipher);
} }
int qcrypto_cipher_encrypt(QCryptoCipher *cipher, static int
qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher,
const void *in, const void *in,
void *out, void *out,
size_t len, size_t len,
@ -498,7 +496,8 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
} }
int qcrypto_cipher_decrypt(QCryptoCipher *cipher, static int
qcrypto_builtin_cipher_decrypt(QCryptoCipher *cipher,
const void *in, const void *in,
void *out, void *out,
size_t len, size_t len,
@ -516,7 +515,8 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
} }
int qcrypto_cipher_setiv(QCryptoCipher *cipher, static int
qcrypto_builtin_cipher_setiv(QCryptoCipher *cipher,
const uint8_t *iv, size_t niv, const uint8_t *iv, size_t niv,
Error **errp) Error **errp)
{ {
@ -526,23 +526,9 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher,
} }
QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
QCryptoCipherMode mode, .cipher_encrypt = qcrypto_builtin_cipher_encrypt,
const uint8_t *key, size_t nkey, .cipher_decrypt = qcrypto_builtin_cipher_decrypt,
Error **errp) .cipher_setiv = qcrypto_builtin_cipher_setiv,
{ .cipher_free = qcrypto_builtin_cipher_ctx_free,
QCryptoCipher *cipher; };
QCryptoCipherBuiltin *ctxt;
ctxt = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp);
if (!ctxt) {
return NULL;
}
cipher = g_new0(QCryptoCipher, 1);
cipher->alg = alg;
cipher->mode = mode;
cipher->opaque = ctxt;
return cipher;
}

View File

@ -20,6 +20,7 @@
#include "qemu/osdep.h" #include "qemu/osdep.h"
#include "crypto/xts.h" #include "crypto/xts.h"
#include "cipherpriv.h"
#include <gcrypt.h> #include <gcrypt.h>
@ -64,7 +65,8 @@ struct QCryptoCipherGcrypt {
uint8_t *iv; uint8_t *iv;
}; };
static void gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx, static void
qcrypto_gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx,
QCryptoCipherMode mode) QCryptoCipherMode mode)
{ {
if (!ctx) { if (!ctx) {
@ -239,18 +241,15 @@ static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
return ctx; return ctx;
error: error:
gcrypt_cipher_free_ctx(ctx, mode); qcrypto_gcrypt_cipher_free_ctx(ctx, mode);
return NULL; return NULL;
} }
void qcrypto_cipher_free(QCryptoCipher *cipher) static void
qcrypto_gcrypt_cipher_ctx_free(QCryptoCipher *cipher)
{ {
if (!cipher) { qcrypto_gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode);
return;
}
gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode);
g_free(cipher);
} }
@ -274,7 +273,8 @@ static void qcrypto_gcrypt_xts_decrypt(const void *ctx,
g_assert(err == 0); g_assert(err == 0);
} }
int qcrypto_cipher_encrypt(QCryptoCipher *cipher, static int
qcrypto_gcrypt_cipher_encrypt(QCryptoCipher *cipher,
const void *in, const void *in,
void *out, void *out,
size_t len, size_t len,
@ -309,7 +309,8 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
} }
int qcrypto_cipher_decrypt(QCryptoCipher *cipher, static int
qcrypto_gcrypt_cipher_decrypt(QCryptoCipher *cipher,
const void *in, const void *in,
void *out, void *out,
size_t len, size_t len,
@ -343,7 +344,8 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
return 0; return 0;
} }
int qcrypto_cipher_setiv(QCryptoCipher *cipher, static int
qcrypto_gcrypt_cipher_setiv(QCryptoCipher *cipher,
const uint8_t *iv, size_t niv, const uint8_t *iv, size_t niv,
Error **errp) Error **errp)
{ {
@ -381,23 +383,9 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher,
} }
QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
QCryptoCipherMode mode, .cipher_encrypt = qcrypto_gcrypt_cipher_encrypt,
const uint8_t *key, size_t nkey, .cipher_decrypt = qcrypto_gcrypt_cipher_decrypt,
Error **errp) .cipher_setiv = qcrypto_gcrypt_cipher_setiv,
{ .cipher_free = qcrypto_gcrypt_cipher_ctx_free,
QCryptoCipher *cipher; };
QCryptoCipherGcrypt *ctx;
ctx = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp);
if (!ctx) {
return NULL;
}
cipher = g_new0(QCryptoCipher, 1);
cipher->alg = alg;
cipher->mode = mode;
cipher->opaque = ctx;
return cipher;
}

View File

@ -20,6 +20,7 @@
#include "qemu/osdep.h" #include "qemu/osdep.h"
#include "crypto/xts.h" #include "crypto/xts.h"
#include "cipherpriv.h"
#include <nettle/nettle-types.h> #include <nettle/nettle-types.h>
#include <nettle/aes.h> #include <nettle/aes.h>
@ -249,7 +250,8 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
} }
static void nettle_cipher_free_ctx(QCryptoCipherNettle *ctx) static void
qcrypto_nettle_cipher_free_ctx(QCryptoCipherNettle *ctx)
{ {
if (!ctx) { if (!ctx) {
return; return;
@ -434,26 +436,23 @@ static QCryptoCipherNettle *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
return ctx; return ctx;
error: error:
nettle_cipher_free_ctx(ctx); qcrypto_nettle_cipher_free_ctx(ctx);
return NULL; return NULL;
} }
void qcrypto_cipher_free(QCryptoCipher *cipher) static void
qcrypto_nettle_cipher_ctx_free(QCryptoCipher *cipher)
{ {
QCryptoCipherNettle *ctx; QCryptoCipherNettle *ctx;
if (!cipher) {
return;
}
ctx = cipher->opaque; ctx = cipher->opaque;
nettle_cipher_free_ctx(ctx); qcrypto_nettle_cipher_free_ctx(ctx);
g_free(cipher);
} }
int qcrypto_cipher_encrypt(QCryptoCipher *cipher, static int
qcrypto_nettle_cipher_encrypt(QCryptoCipher *cipher,
const void *in, const void *in,
void *out, void *out,
size_t len, size_t len,
@ -499,7 +498,8 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
} }
int qcrypto_cipher_decrypt(QCryptoCipher *cipher, static int
qcrypto_nettle_cipher_decrypt(QCryptoCipher *cipher,
const void *in, const void *in,
void *out, void *out,
size_t len, size_t len,
@ -543,7 +543,8 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
return 0; return 0;
} }
int qcrypto_cipher_setiv(QCryptoCipher *cipher, static int
qcrypto_nettle_cipher_setiv(QCryptoCipher *cipher,
const uint8_t *iv, size_t niv, const uint8_t *iv, size_t niv,
Error **errp) Error **errp)
{ {
@ -558,23 +559,9 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher,
} }
QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
QCryptoCipherMode mode, .cipher_encrypt = qcrypto_nettle_cipher_encrypt,
const uint8_t *key, size_t nkey, .cipher_decrypt = qcrypto_nettle_cipher_decrypt,
Error **errp) .cipher_setiv = qcrypto_nettle_cipher_setiv,
{ .cipher_free = qcrypto_nettle_cipher_ctx_free,
QCryptoCipher *cipher; };
QCryptoCipherNettle *ctx;
ctx = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp);
if (!ctx) {
return NULL;
}
cipher = g_new0(QCryptoCipher, 1);
cipher->alg = alg;
cipher->mode = mode;
cipher->opaque = ctx;
return cipher;
}

View File

@ -21,6 +21,7 @@
#include "qemu/osdep.h" #include "qemu/osdep.h"
#include "qapi/error.h" #include "qapi/error.h"
#include "crypto/cipher.h" #include "crypto/cipher.h"
#include "cipherpriv.h"
static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = { static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
@ -155,3 +156,67 @@ qcrypto_cipher_munge_des_rfb_key(const uint8_t *key,
#else #else
#include "crypto/cipher-builtin.c" #include "crypto/cipher-builtin.c"
#endif #endif
QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
QCryptoCipherMode mode,
const uint8_t *key, size_t nkey,
Error **errp)
{
QCryptoCipher *cipher;
void *ctx;
ctx = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp);
if (!ctx) {
return NULL;
}
cipher = g_new0(QCryptoCipher, 1);
cipher->alg = alg;
cipher->mode = mode;
cipher->opaque = ctx;
cipher->driver = (void *)&qcrypto_cipher_lib_driver;
return cipher;
}
int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
const void *in,
void *out,
size_t len,
Error **errp)
{
QCryptoCipherDriver *drv = cipher->driver;
return drv->cipher_encrypt(cipher, in, out, len, errp);
}
int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
const void *in,
void *out,
size_t len,
Error **errp)
{
QCryptoCipherDriver *drv = cipher->driver;
return drv->cipher_decrypt(cipher, in, out, len, errp);
}
int qcrypto_cipher_setiv(QCryptoCipher *cipher,
const uint8_t *iv, size_t niv,
Error **errp)
{
QCryptoCipherDriver *drv = cipher->driver;
return drv->cipher_setiv(cipher, iv, niv, errp);
}
void qcrypto_cipher_free(QCryptoCipher *cipher)
{
QCryptoCipherDriver *drv;
if (cipher) {
drv = cipher->driver;
drv->cipher_free(cipher);
g_free(cipher);
}
}

40
crypto/cipherpriv.h Normal file
View File

@ -0,0 +1,40 @@
/*
* QEMU Crypto cipher 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_CIPHERPRIV_H
#define QCRYPTO_CIPHERPRIV_H
typedef struct QCryptoCipherDriver QCryptoCipherDriver;
struct QCryptoCipherDriver {
int (*cipher_encrypt)(QCryptoCipher *cipher,
const void *in,
void *out,
size_t len,
Error **errp);
int (*cipher_decrypt)(QCryptoCipher *cipher,
const void *in,
void *out,
size_t len,
Error **errp);
int (*cipher_setiv)(QCryptoCipher *cipher,
const uint8_t *iv, size_t niv,
Error **errp);
void (*cipher_free)(QCryptoCipher *cipher);
};
#endif

View File

@ -80,6 +80,7 @@ struct QCryptoCipher {
QCryptoCipherAlgorithm alg; QCryptoCipherAlgorithm alg;
QCryptoCipherMode mode; QCryptoCipherMode mode;
void *opaque; void *opaque;
void *driver;
}; };
/** /**