Skip to contentSkip to conversation

crypto: hash: add hash driver framework

PATCH Superseded Longpeng(Mike) 2 participants 2 messages Apr 10, 2017 Crypto API

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 6/19 crypto: hash: add hash driver framework

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

2) introduces hash framework, including QCryptoHashDriver and new public APIs.

---
 crypto/hash-gcrypt.c  | 17 +++++++++++------
 crypto/hash-glib.c    | 17 +++++++++++------
 crypto/hash-nettle.c  | 17 +++++++++++------
 crypto/hash.c         | 12 ++++++++++++
 include/crypto/hash.h | 12 ++++++++++++
 5 files changed, 57 insertions(+), 18 deletions(-)
crypto/hash-gcrypt.c +11 -6
index 7690690..21c990f 100644
@@ -44,12 +44,12 @@ gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
 }
 
 
-int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
-                        const struct iovec *iov,
-                        size_t niov,
-                        uint8_t **result,
-                        size_t *resultlen,
-                        Error **errp)
+static int gcrypt_hash_bytesv(QCryptoHashAlgorithm alg,
+                              const struct iovec *iov,
+                              size_t niov,
+                              uint8_t **result,
+                              size_t *resultlen,
+                              Error **errp)
 {
     int i, ret;
     gcry_md_hd_t md;
@@ -107,3 +107,8 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
     gcry_md_close(md);
     return -1;
 }
+
+
+QCryptoHashDriver qcrypto_hash_lib_driver = {
+    .hash_bytesv = gcrypt_hash_bytesv,
+};
crypto/hash-glib.c +11 -6
index ec99ac9..72be47e 100644
@@ -47,12 +47,12 @@ gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
 }
 
 
-int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
-                        const struct iovec *iov,
-                        size_t niov,
-                        uint8_t **result,
-                        size_t *resultlen,
-                        Error **errp)
+static int glib_hash_bytesv(QCryptoHashAlgorithm alg,
+                            const struct iovec *iov,
+                            size_t niov,
+                            uint8_t **result,
+                            size_t *resultlen,
+                            Error **errp)
 {
     int i, ret;
     GChecksum *cs;
@@ -95,3 +95,8 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
     g_checksum_free(cs);
     return -1;
 }
+
+
+QCryptoHashDriver qcrypto_hash_lib_driver = {
+    .hash_bytesv = glib_hash_bytesv,
+};
crypto/hash-nettle.c +11 -6
index 6a206dc..9233e10 100644
@@ -103,12 +103,12 @@ gboolean qcrypto_hash_supports(QCryptoHashAlgorithm alg)
 }
 
 
-int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
-                        const struct iovec *iov,
-                        size_t niov,
-                        uint8_t **result,
-                        size_t *resultlen,
-                        Error **errp)
+static int nettle_hash_bytesv(QCryptoHashAlgorithm alg,
+                              const struct iovec *iov,
+                              size_t niov,
+                              uint8_t **result,
+                              size_t *resultlen,
+                              Error **errp)
 {
     int i;
     union qcrypto_hash_ctx ctx;
@@ -152,3 +152,8 @@ int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
 
     return 0;
 }
+
+
+QCryptoHashDriver qcrypto_hash_lib_driver = {
+    .hash_bytesv = nettle_hash_bytesv,
+};
crypto/hash.c +12 -0
index 0f1ceac..0b0d479 100644
@@ -38,6 +38,18 @@ size_t qcrypto_hash_digest_len(QCryptoHashAlgorithm alg)
     return qcrypto_hash_alg_size[alg];
 }
 
+int qcrypto_hash_bytesv(QCryptoHashAlgorithm alg,
+                        const struct iovec *iov,
+                        size_t niov,
+                        uint8_t **result,
+                        size_t *resultlen,
+                        Error **errp)
+{
+    return qcrypto_hash_lib_driver.hash_bytesv(alg, iov, niov,
+                                               result, resultlen,
+                                               errp);
+}
+
 
 int qcrypto_hash_bytes(QCryptoHashAlgorithm alg,
                        const char *buf,
include/crypto/hash.h +12 -0
index ca3267f..00b764e 100644
@@ -25,6 +25,18 @@
 
 /* See also "QCryptoHashAlgorithm" defined in qapi/crypto.json */
 
+typedef struct QCryptoHashDriver QCryptoHashDriver;
+struct QCryptoHashDriver {
+    int (*hash_bytesv)(QCryptoHashAlgorithm alg,
+                       const struct iovec *iov,
+                       size_t niov,
+                       uint8_t **result,
+                       size_t *resultlen,
+                       Error **errp);
+};
+
+extern QCryptoHashDriver qcrypto_hash_lib_driver;
+
 /**
  * qcrypto_hash_supports:
  * @alg: the hash algorithm
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 25 lines · Longpeng(Mike)

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

2) introduces hash framework, including QCryptoHashDriver and new public APIs.

---
 crypto/hash-gcrypt.c  | 17 +++++++++++------
 crypto/hash-glib.c    | 17 +++++++++++------
 crypto/hash-nettle.c  | 17 +++++++++++------
 crypto/hash.c         | 12 ++++++++++++
 include/crypto/hash.h | 12 ++++++++++++
 5 files changed, 57 insertions(+), 18 deletions(-)
include/crypto/hash.h +1 -0
index ca3267f..00b764e 100644
@@ -25,6 +25,18 @@
 
 /* See also "QCryptoHashAlgorithm" defined in qapi/crypto.json */
 
+typedef struct QCryptoHashDriver QCryptoHashDriver;

include/crypto/hash.h line 38

Quoted 10 lines
include/crypto/hash.h +10 -0
+struct QCryptoHashDriver {
+    int (*hash_bytesv)(QCryptoHashAlgorithm alg,
+                       const struct iovec *iov,
+                       size_t niov,
+                       uint8_t **result,
+                       size_t *resultlen,
+                       Error **errp);
+};
+
+extern QCryptoHashDriver qcrypto_hash_lib_driver;

This should be in a crypto/hashpriv.h header file, since again it is not something we want exposed.

Regards,
Daniel

Signature · 3 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|