Alexandre Julliard : bcrypt: Move the symmetric key initialization to the generic code.

Alexandre Julliard julliard at winehq.org
Tue Sep 29 15:20:38 CDT 2020


Module: wine
Branch: master
Commit: 459046561328735dfe75181ecd825410747c6451
URL:    https://source.winehq.org/git/wine.git/?a=commit;h=459046561328735dfe75181ecd825410747c6451

Author: Alexandre Julliard <julliard at winehq.org>
Date:   Tue Sep 29 14:08:14 2020 +0200

bcrypt: Move the symmetric key initialization to the generic code.

Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 dlls/bcrypt/bcrypt_internal.h |  9 ++++++---
 dlls/bcrypt/bcrypt_main.c     | 33 ++++++++++++++++++++++++++++-----
 dlls/bcrypt/gnutls.c          | 28 ++++------------------------
 dlls/bcrypt/macos.c           | 33 ++++++---------------------------
 4 files changed, 44 insertions(+), 59 deletions(-)

diff --git a/dlls/bcrypt/bcrypt_internal.h b/dlls/bcrypt/bcrypt_internal.h
index 079c743809..2b26fbbc9c 100644
--- a/dlls/bcrypt/bcrypt_internal.h
+++ b/dlls/bcrypt/bcrypt_internal.h
@@ -227,6 +227,11 @@ struct key
 struct key_symmetric
 {
     enum mode_id mode;
+    ULONG        block_size;
+    UCHAR       *vector;
+    ULONG        vector_len;
+    UCHAR       *secret;
+    ULONG        secret_len;
 };
 
 struct key_asymmetric
@@ -253,10 +258,8 @@ struct secret
     struct object hdr;
 };
 
-NTSTATUS get_alg_property( const struct algorithm *, const WCHAR *, UCHAR *, ULONG, ULONG * ) DECLSPEC_HIDDEN;
-
 NTSTATUS key_set_property( struct key *, const WCHAR *, UCHAR *, ULONG, ULONG ) DECLSPEC_HIDDEN;
-NTSTATUS key_symmetric_init( struct key *, struct algorithm *, const UCHAR *, ULONG ) DECLSPEC_HIDDEN;
+NTSTATUS key_symmetric_init( struct key * ) DECLSPEC_HIDDEN;
 void     key_symmetric_vector_reset( struct key * ) DECLSPEC_HIDDEN;
 NTSTATUS key_symmetric_set_auth_data( struct key *, UCHAR *, ULONG ) DECLSPEC_HIDDEN;
 NTSTATUS key_symmetric_encrypt( struct key *, const UCHAR *, ULONG, UCHAR *, ULONG ) DECLSPEC_HIDDEN;
diff --git a/dlls/bcrypt/bcrypt_main.c b/dlls/bcrypt/bcrypt_main.c
index b77cf67c46..d0fc02b776 100644
--- a/dlls/bcrypt/bcrypt_main.c
+++ b/dlls/bcrypt/bcrypt_main.c
@@ -550,7 +550,8 @@ static NTSTATUS get_dsa_property( enum mode_id mode, const WCHAR *prop, UCHAR *b
     return STATUS_NOT_IMPLEMENTED;
 }
 
-NTSTATUS get_alg_property( const struct algorithm *alg, const WCHAR *prop, UCHAR *buf, ULONG size, ULONG *ret_size )
+static NTSTATUS get_alg_property( const struct algorithm *alg, const WCHAR *prop,
+                                  UCHAR *buf, ULONG size, ULONG *ret_size )
 {
     NTSTATUS status;
 
@@ -1328,7 +1329,7 @@ static NTSTATUS key_import_pair( struct algorithm *alg, const WCHAR *type, BCRYP
     return STATUS_NOT_SUPPORTED;
 }
 #else
-NTSTATUS key_symmetric_init( struct key *key, struct algorithm *alg, const UCHAR *secret, ULONG secret_len )
+NTSTATUS key_symmetric_init( struct key *key )
 {
     ERR( "support for keys not available at build time\n" );
     return STATUS_NOT_IMPLEMENTED;
@@ -1431,12 +1432,20 @@ NTSTATUS key_import_ecc( struct key *key, UCHAR *input, ULONG len )
 }
 #endif
 
+static ULONG get_block_size( struct algorithm *alg )
+{
+    ULONG ret = 0, size = sizeof(ret);
+    get_alg_property( alg, BCRYPT_BLOCK_LENGTH, (UCHAR *)&ret, sizeof(ret), &size );
+    return ret;
+}
+
 NTSTATUS WINAPI BCryptGenerateSymmetricKey( BCRYPT_ALG_HANDLE algorithm, BCRYPT_KEY_HANDLE *handle,
                                             UCHAR *object, ULONG object_len, UCHAR *secret, ULONG secret_len,
                                             ULONG flags )
 {
     struct algorithm *alg = algorithm;
     struct key *key;
+    ULONG block_size;
     NTSTATUS status;
 
     TRACE( "%p, %p, %p, %u, %p, %u, %08x\n", algorithm, handle, object, object_len, secret, secret_len, flags );
@@ -1444,11 +1453,25 @@ NTSTATUS WINAPI BCryptGenerateSymmetricKey( BCRYPT_ALG_HANDLE algorithm, BCRYPT_
     if (!alg || alg->hdr.magic != MAGIC_ALG) return STATUS_INVALID_HANDLE;
     if (object) FIXME( "ignoring object buffer\n" );
 
-    if (!(key = heap_alloc( sizeof(*key) ))) return STATUS_NO_MEMORY;
-    key->hdr.magic = MAGIC_KEY;
+    if (!(block_size = get_block_size( alg ))) return STATUS_INVALID_PARAMETER;
+
+    if (!(key = heap_alloc_zero( sizeof(*key) ))) return STATUS_NO_MEMORY;
+    key->hdr.magic      = MAGIC_KEY;
+    key->alg_id         = alg->id;
+    key->u.s.mode       = alg->mode;
+    key->u.s.block_size = block_size;
+
+    if (!(key->u.s.secret = heap_alloc( secret_len )))
+    {
+        heap_free( key );
+        return STATUS_NO_MEMORY;
+    }
+    memcpy( key->u.s.secret, secret, secret_len );
+    key->u.s.secret_len = secret_len;
 
-    if ((status = key_symmetric_init( key, alg, secret, secret_len )))
+    if ((status = key_symmetric_init( key )))
     {
+        heap_free( key->u.s.secret );
         heap_free( key );
         return status;
     }
diff --git a/dlls/bcrypt/gnutls.c b/dlls/bcrypt/gnutls.c
index 8b4d08f528..222861c18b 100644
--- a/dlls/bcrypt/gnutls.c
+++ b/dlls/bcrypt/gnutls.c
@@ -455,39 +455,19 @@ NTSTATUS key_set_property( struct key *key, const WCHAR *prop, UCHAR *value, ULO
     return STATUS_NOT_IMPLEMENTED;
 }
 
-static ULONG get_block_size( struct algorithm *alg )
-{
-    ULONG ret = 0, size = sizeof(ret);
-    get_alg_property( alg, BCRYPT_BLOCK_LENGTH, (UCHAR *)&ret, sizeof(ret), &size );
-    return ret;
-}
-
-NTSTATUS key_symmetric_init( struct key *key, struct algorithm *alg, const UCHAR *secret, ULONG secret_len )
+NTSTATUS key_symmetric_init( struct key *key )
 {
     if (!libgnutls_handle) return STATUS_INTERNAL_ERROR;
 
-    switch (alg->id)
+    switch (key->alg_id)
     {
     case ALG_ID_AES:
-        break;
+        return STATUS_SUCCESS;
 
     default:
-        FIXME( "algorithm %u not supported\n", alg->id );
+        FIXME( "algorithm %u not supported\n", key->alg_id );
         return STATUS_NOT_SUPPORTED;
     }
-
-    if (!(key->u.s.block_size = get_block_size( alg ))) return STATUS_INVALID_PARAMETER;
-    if (!(key->u.s.secret = heap_alloc( secret_len ))) return STATUS_NO_MEMORY;
-    memcpy( key->u.s.secret, secret, secret_len );
-    key->u.s.secret_len = secret_len;
-
-    key->alg_id         = alg->id;
-    key->u.s.mode       = alg->mode;
-    key->u.s.handle     = 0;        /* initialized on first use */
-    key->u.s.vector     = NULL;
-    key->u.s.vector_len = 0;
-
-    return STATUS_SUCCESS;
 }
 
 static gnutls_cipher_algorithm_t get_gnutls_cipher( const struct key *key )
diff --git a/dlls/bcrypt/macos.c b/dlls/bcrypt/macos.c
index 0c5f968bc6..26af5faa0c 100644
--- a/dlls/bcrypt/macos.c
+++ b/dlls/bcrypt/macos.c
@@ -68,47 +68,26 @@ NTSTATUS key_set_property( struct key *key, const WCHAR *prop, UCHAR *value, ULO
     return STATUS_NOT_IMPLEMENTED;
 }
 
-static ULONG get_block_size( struct algorithm *alg )
+NTSTATUS key_symmetric_init( struct key *key )
 {
-    ULONG ret = 0, size = sizeof(ret);
-    get_alg_property( alg, BCRYPT_BLOCK_LENGTH, (UCHAR *)&ret, sizeof(ret), &size );
-    return ret;
-}
-
-NTSTATUS key_symmetric_init( struct key *key, struct algorithm *alg, const UCHAR *secret, ULONG secret_len )
-{
-    switch (alg->id)
+    switch (key->alg_id)
     {
     case ALG_ID_AES:
-        switch (alg->mode)
+        switch (key->u.s.mode)
         {
         case MODE_ID_ECB:
         case MODE_ID_CBC:
             break;
         default:
-            FIXME( "mode %u not supported\n", alg->mode );
+            FIXME( "mode %u not supported\n", key->u.s.mode );
             return STATUS_NOT_SUPPORTED;
         }
-        break;
+        return STATUS_SUCCESS;
 
     default:
-        FIXME( "algorithm %u not supported\n", alg->id );
+        FIXME( "algorithm %u not supported\n", key->alg_id );
         return STATUS_NOT_SUPPORTED;
     }
-
-    if (!(key->u.s.block_size = get_block_size( alg ))) return STATUS_INVALID_PARAMETER;
-    if (!(key->u.s.secret = heap_alloc( secret_len ))) return STATUS_NO_MEMORY;
-    memcpy( key->u.s.secret, secret, secret_len );
-    key->u.s.secret_len = secret_len;
-
-    key->alg_id          = alg->id;
-    key->u.s.mode        = alg->mode;
-    key->u.s.ref_encrypt = NULL;        /* initialized on first use */
-    key->u.s.ref_decrypt = NULL;
-    key->u.s.vector      = NULL;
-    key->u.s.vector_len  = 0;
-
-    return STATUS_SUCCESS;
 }
 
 static CCMode get_cryptor_mode( struct key *key )




More information about the wine-cvs mailing list