Commit 45904656 authored by Alexandre Julliard's avatar Alexandre Julliard

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

parent b8ada902
......@@ -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;
......
......@@ -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;
}
......
......@@ -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 )
......
......@@ -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 )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment