Change ossltest engine to manually allocate cipher_data
authorMatt Caswell <matt@openssl.org>
Tue, 22 Sep 2015 07:54:43 +0000 (08:54 +0100)
committerMatt Caswell <matt@openssl.org>
Fri, 25 Sep 2015 14:13:57 +0000 (15:13 +0100)
The ossltest engine wraps the built-in implementation of aes128-cbc.
Normally in an engine the cipher_data structure is automatically allocated
by the EVP layer. However this relies on the engine specifying up front
the size of that cipher_data structure. In the case of ossltest this value
isn't available at compile time. This change makes the ossltest engine
allocate its own cipher_data structure instead of leaving it to the EVP
layer.

Reviewed-by: Andy Polyakov <appro@openssl.org>
engines/e_ossltest.c
engines/e_ossltest_err.c
engines/e_ossltest_err.h

index 6e50a5fceaa57e3b2888b2e1cb3fe610f6435195..c3390268a5115b2aa8c5e781abd0f5c6501fc5e5 100644 (file)
@@ -207,23 +207,6 @@ int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                                const unsigned char *in, size_t inl);
 
 int ossltest_aes128_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
                                const unsigned char *in, size_t inl);
 
-/*
- * Copy of the definition in crypto/evp/e_aes.c. Only used for the "sizeof"
- * below
- */
-typedef struct {
-    union {
-        double align;
-        AES_KEY ks;
-    } ks;
-    block128_f block;
-    union {
-        cbc128_f cbc;
-        ctr128_f ctr;
-    } stream;
-} EVP_AES_KEY;
-
-
 static const EVP_CIPHER ossltest_aes_128_cbc = { \
     NID_aes_128_cbc,
     16, /* block size */
 static const EVP_CIPHER ossltest_aes_128_cbc = { \
     NID_aes_128_cbc,
     16, /* block size */
@@ -233,7 +216,7 @@ static const EVP_CIPHER ossltest_aes_128_cbc = { \
     ossltest_aes128_init_key,
     ossltest_aes128_cbc_cipher,
     NULL,
     ossltest_aes128_init_key,
     ossltest_aes128_cbc_cipher,
     NULL,
-    sizeof(EVP_AES_KEY),
+    0, /* We don't know the size of cipher_data at compile time */
     NULL,NULL,NULL,NULL
 };
 
     NULL,NULL,NULL,NULL
 };
 
@@ -515,6 +498,19 @@ static int digest_sha512_final(EVP_MD_CTX *ctx, unsigned char *md)
 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                              const unsigned char *iv, int enc)
 {
 int ossltest_aes128_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
                              const unsigned char *iv, int enc)
 {
+    if (ctx->cipher_data == NULL) {
+        /*
+         * Normally cipher_data is allocated automatically for an engine but
+         * we don't know the ctx_size as compile time so we have to do it at
+         * run time
+         */
+        ctx->cipher_data = OPENSSL_zalloc(EVP_aes_128_cbc()->ctx_size);
+        if (!ctx->cipher_data) {
+            OSSLTESTerr(OSSLTEST_F_OSSLTEST_AES128_INIT_KEY,
+                        ERR_R_MALLOC_FAILURE);
+            return 0;
+        }
+    }
     return EVP_aes_128_cbc()->init(ctx, key, iv, enc);
 }
 
     return EVP_aes_128_cbc()->init(ctx, key, iv, enc);
 }
 
index c1b0063b9d800b6313703001a900cc9a3257fcc7..4abb3959635d79db4665f1e550f980dfce3ee194 100644 (file)
@@ -71,6 +71,8 @@
 
 static ERR_STRING_DATA OSSLTEST_str_functs[] = {
     {ERR_FUNC(OSSLTEST_F_BIND_OSSLTEST), "BIND_OSSLTEST"},
 
 static ERR_STRING_DATA OSSLTEST_str_functs[] = {
     {ERR_FUNC(OSSLTEST_F_BIND_OSSLTEST), "BIND_OSSLTEST"},
+    {ERR_FUNC(OSSLTEST_F_OSSLTEST_AES128_INIT_KEY),
+     "OSSLTEST_AES128_INIT_KEY"},
     {0, NULL}
 };
 
     {0, NULL}
 };
 
index 8f874e00a07d7ee19b957a5680f1779b87d86cd4..b46eb05c1c5a5e38d268b6cba823ec6c3836c936 100644 (file)
@@ -73,6 +73,7 @@ static void ERR_OSSLTEST_error(int function, int reason, char *file, int line);
 
 /* Function codes. */
 # define OSSLTEST_F_BIND_OSSLTEST                         100
 
 /* Function codes. */
 # define OSSLTEST_F_BIND_OSSLTEST                         100
+# define OSSLTEST_F_OSSLTEST_AES128_INIT_KEY              101
 
 /* Reason codes. */
 # define OSSLTEST_R_INIT_FAILED                           100
 
 /* Reason codes. */
 # define OSSLTEST_R_INIT_FAILED                           100