Skip to contentSkip to conversation

crypto: cipher: add cipher 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 5/19 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.

---
 crypto/cipher-builtin.c | 59 +++++++++++++++++--------------------------------
 crypto/cipher-gcrypt.c  | 58 +++++++++++++++++-------------------------------
 crypto/cipher-nettle.c  | 59 +++++++++++++++++--------------------------------
 crypto/cipher.c         | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/crypto/cipher.h | 22 ++++++++++++++++++
 5 files changed, 141 insertions(+), 116 deletions(-)
crypto/cipher-builtin.c +20 -39
index 8cf47d1..a35f461 100644
@@ -466,25 +466,20 @@ static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
     return ctxt;
 }
 
-void qcrypto_cipher_free(QCryptoCipher *cipher)
+static void builtin_cipher_ctx_free(QCryptoCipher *cipher)
 {
     QCryptoCipherBuiltin *ctxt;
 
-    if (!cipher) {
-        return;
-    }
-
     ctxt = cipher->opaque;
     ctxt->free(cipher);
-    g_free(cipher);
 }
 
 
-int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
-                           const void *in,
-                           void *out,
-                           size_t len,
-                           Error **errp)
+static int builtin_cipher_encrypt(QCryptoCipher *cipher,
+                                  const void *in,
+                                  void *out,
+                                  size_t len,
+                                  Error **errp)
 {
     QCryptoCipherBuiltin *ctxt = cipher->opaque;
 
@@ -498,11 +493,11 @@ int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
 }
 
 
-int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
-                           const void *in,
-                           void *out,
-                           size_t len,
-                           Error **errp)
+static int builtin_cipher_decrypt(QCryptoCipher *cipher,
+                                  const void *in,
+                                  void *out,
+                                  size_t len,
+                                  Error **errp)
 {
     QCryptoCipherBuiltin *ctxt = cipher->opaque;
 
@@ -516,9 +511,9 @@ int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
 }
 
 
-int qcrypto_cipher_setiv(QCryptoCipher *cipher,
-                         const uint8_t *iv, size_t niv,
-                         Error **errp)
+static int builtin_cipher_setiv(QCryptoCipher *cipher,
+                                const uint8_t *iv, size_t niv,
+                                Error **errp)
 {
     QCryptoCipherBuiltin *ctxt = cipher->opaque;
 
@@ -526,23 +521,9 @@ int qcrypto_cipher_setiv(QCryptoCipher *cipher,
 }
 
 
-QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
-                                  QCryptoCipherMode mode,
-                                  const uint8_t *key, size_t nkey,
-                                  Error **errp)
-{
-    QCryptoCipher *cipher;
-    QCryptoCipherBuiltin *ctxt;
-
-    ctxt = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp);
-    if (ctxt == NULL) {
-        return NULL;
-    }
-
-    cipher = g_new0(QCryptoCipher, 1);
-    cipher->alg = alg;
-    cipher->mode = mode;
-    cipher->opaque = ctxt;
-
-    return cipher;
-}
+static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
+    .cipher_encrypt = builtin_cipher_encrypt,
+    .cipher_decrypt = builtin_cipher_decrypt,
+    .cipher_setiv = builtin_cipher_setiv,
+    .cipher_free = builtin_cipher_ctx_free,
+};
crypto/cipher-gcrypt.c +20 -38

87 lines not on this page · raw message

crypto/cipher-nettle.c +20 -39

88 lines not on this page · raw message

crypto/cipher.c +59 -0

62 lines not on this page · raw message

include/crypto/cipher.h +22 -0
index bec9f41..32b6065 100644
@@ -23,6 +23,7 @@
 
 #include "qapi-types.h"
 
+typedef struct QCryptoCipherDriver QCryptoCipherDriver;
 typedef struct QCryptoCipher QCryptoCipher;
 
 /* See also "QCryptoCipherAlgorithm" and "QCryptoCipherMode"
@@ -76,7 +77,28 @@ typedef struct QCryptoCipher QCryptoCipher;
  *
  */
 
+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);
+};
+
 struct QCryptoCipher {
+    QCryptoCipherDriver *driver;
     QCryptoCipherAlgorithm alg;
     QCryptoCipherMode mode;
     void *opaque;
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 41 lines · Longpeng(Mike)

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.

---
 crypto/cipher-builtin.c | 59 +++++++++++++++++--------------------------------
 crypto/cipher-gcrypt.c  | 58 +++++++++++++++++-------------------------------
 crypto/cipher-nettle.c  | 59 +++++++++++++++++--------------------------------
 crypto/cipher.c         | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/crypto/cipher.h | 22 ++++++++++++++++++
 5 files changed, 141 insertions(+), 116 deletions(-)
include/crypto/cipher.h +10 -0
index bec9f41..32b6065 100644
@@ -23,6 +23,7 @@
 
 #include "qapi-types.h"
 
+typedef struct QCryptoCipherDriver QCryptoCipherDriver;
 typedef struct QCryptoCipher QCryptoCipher;
 
 /* See also "QCryptoCipherAlgorithm" and "QCryptoCipherMode"
@@ -76,7 +77,28 @@ typedef struct QCryptoCipher QCryptoCipher;
  *
  */
 
+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,

include/crypto/cipher.h typedef struct QCryptoCipher QCryptoCipher; · line 98

Quoted 10 lines
include/crypto/cipher.h +10 -0
+                          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);
+};

Please put this in a crypto/cipherpriv.h header file, since it is not something we want to expose to the rest of QEMU source code.

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/ :|