2 * Copyright 2009 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/port.h"
24 #ifdef HAVE_COMMONCRYPTO_COMMONCRYPTOR_H
25 #include <AvailabilityMacros.h>
26 #include <CommonCrypto/CommonCryptor.h>
27 #elif defined(HAVE_GNUTLS_CIPHER_INIT)
28 #include <gnutls/gnutls.h>
29 #include <gnutls/crypto.h>
33 #define WIN32_NO_STATUS
39 #include "bcrypt_internal.h"
41 #include "wine/debug.h"
42 #include "wine/library.h"
43 #include "wine/unicode.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(bcrypt
);
47 static HINSTANCE instance
;
49 #if defined(HAVE_GNUTLS_CIPHER_INIT) && !defined(HAVE_COMMONCRYPTO_COMMONCRYPTOR_H)
50 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
52 static void *libgnutls_handle
;
53 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
54 MAKE_FUNCPTR(gnutls_cipher_decrypt2
);
55 MAKE_FUNCPTR(gnutls_cipher_deinit
);
56 MAKE_FUNCPTR(gnutls_cipher_encrypt2
);
57 MAKE_FUNCPTR(gnutls_cipher_init
);
58 MAKE_FUNCPTR(gnutls_global_deinit
);
59 MAKE_FUNCPTR(gnutls_global_init
);
60 MAKE_FUNCPTR(gnutls_global_set_log_function
);
61 MAKE_FUNCPTR(gnutls_global_set_log_level
);
62 MAKE_FUNCPTR(gnutls_perror
);
65 static void gnutls_log( int level
, const char *msg
)
67 TRACE( "<%d> %s", level
, msg
);
70 static BOOL
gnutls_initialize(void)
74 if (!(libgnutls_handle
= wine_dlopen( SONAME_LIBGNUTLS
, RTLD_NOW
, NULL
, 0 )))
76 ERR_(winediag
)( "failed to load libgnutls, no support for encryption\n" );
80 #define LOAD_FUNCPTR(f) \
81 if (!(p##f = wine_dlsym( libgnutls_handle, #f, NULL, 0 ))) \
83 ERR( "failed to load %s\n", #f ); \
87 LOAD_FUNCPTR(gnutls_cipher_decrypt2
)
88 LOAD_FUNCPTR(gnutls_cipher_deinit
)
89 LOAD_FUNCPTR(gnutls_cipher_encrypt2
)
90 LOAD_FUNCPTR(gnutls_cipher_init
)
91 LOAD_FUNCPTR(gnutls_global_deinit
)
92 LOAD_FUNCPTR(gnutls_global_init
)
93 LOAD_FUNCPTR(gnutls_global_set_log_function
)
94 LOAD_FUNCPTR(gnutls_global_set_log_level
)
95 LOAD_FUNCPTR(gnutls_perror
)
98 if ((ret
= pgnutls_global_init()) != GNUTLS_E_SUCCESS
)
100 pgnutls_perror( ret
);
104 if (TRACE_ON( bcrypt
))
106 pgnutls_global_set_log_level( 4 );
107 pgnutls_global_set_log_function( gnutls_log
);
113 wine_dlclose( libgnutls_handle
, NULL
, 0 );
114 libgnutls_handle
= NULL
;
118 static void gnutls_uninitialize(void)
120 pgnutls_global_deinit();
121 wine_dlclose( libgnutls_handle
, NULL
, 0 );
122 libgnutls_handle
= NULL
;
124 #endif /* HAVE_GNUTLS_CIPHER_INIT && !HAVE_COMMONCRYPTO_COMMONCRYPTOR_H */
126 NTSTATUS WINAPI
BCryptAddContextFunction(ULONG table
, LPCWSTR context
, ULONG iface
, LPCWSTR function
, ULONG pos
)
128 FIXME("%08x, %s, %08x, %s, %u: stub\n", table
, debugstr_w(context
), iface
, debugstr_w(function
), pos
);
129 return STATUS_SUCCESS
;
132 NTSTATUS WINAPI
BCryptAddContextFunctionProvider(ULONG table
, LPCWSTR context
, ULONG iface
, LPCWSTR function
, LPCWSTR provider
, ULONG pos
)
134 FIXME("%08x, %s, %08x, %s, %s, %u: stub\n", table
, debugstr_w(context
), iface
, debugstr_w(function
), debugstr_w(provider
), pos
);
135 return STATUS_SUCCESS
;
138 NTSTATUS WINAPI
BCryptRemoveContextFunction(ULONG table
, LPCWSTR context
, ULONG iface
, LPCWSTR function
)
140 FIXME("%08x, %s, %08x, %s: stub\n", table
, debugstr_w(context
), iface
, debugstr_w(function
));
141 return STATUS_NOT_IMPLEMENTED
;
144 NTSTATUS WINAPI
BCryptRemoveContextFunctionProvider(ULONG table
, LPCWSTR context
, ULONG iface
, LPCWSTR function
, LPCWSTR provider
)
146 FIXME("%08x, %s, %08x, %s, %s: stub\n", table
, debugstr_w(context
), iface
, debugstr_w(function
), debugstr_w(provider
));
147 return STATUS_NOT_IMPLEMENTED
;
150 NTSTATUS WINAPI
BCryptRegisterProvider(LPCWSTR provider
, ULONG flags
, PCRYPT_PROVIDER_REG reg
)
152 FIXME("%s, %08x, %p: stub\n", debugstr_w(provider
), flags
, reg
);
153 return STATUS_SUCCESS
;
156 NTSTATUS WINAPI
BCryptUnregisterProvider(LPCWSTR provider
)
158 FIXME("%s: stub\n", debugstr_w(provider
));
159 return STATUS_NOT_IMPLEMENTED
;
162 NTSTATUS WINAPI
BCryptEnumAlgorithms(ULONG dwAlgOperations
, ULONG
*pAlgCount
,
163 BCRYPT_ALGORITHM_IDENTIFIER
**ppAlgList
, ULONG dwFlags
)
165 FIXME("%08x, %p, %p, %08x - stub\n", dwAlgOperations
, pAlgCount
, ppAlgList
, dwFlags
);
170 return STATUS_NOT_IMPLEMENTED
;
173 #define MAGIC_ALG (('A' << 24) | ('L' << 16) | ('G' << 8) | '0')
174 #define MAGIC_HASH (('H' << 24) | ('A' << 16) | ('S' << 8) | 'H')
175 #define MAGIC_KEY (('K' << 24) | ('E' << 16) | ('Y' << 8) | '0')
194 #define MAX_HASH_OUTPUT_BYTES 64
195 #define MAX_HASH_BLOCK_BITS 1024
197 static const struct {
201 const WCHAR
*alg_name
;
203 /* ALG_ID_AES */ { 654, 0, 0, BCRYPT_AES_ALGORITHM
},
204 /* ALG_ID_MD2 */ { 270, 16, 128, BCRYPT_MD2_ALGORITHM
},
205 /* ALG_ID_MD4 */ { 270, 16, 512, BCRYPT_MD4_ALGORITHM
},
206 /* ALG_ID_MD5 */ { 274, 16, 512, BCRYPT_MD5_ALGORITHM
},
207 /* ALG_ID_RNG */ { 0, 0, 0, BCRYPT_RNG_ALGORITHM
},
208 /* ALG_ID_SHA1 */ { 278, 20, 512, BCRYPT_SHA1_ALGORITHM
},
209 /* ALG_ID_SHA256 */ { 286, 32, 512, BCRYPT_SHA256_ALGORITHM
},
210 /* ALG_ID_SHA384 */ { 382, 48, 1024, BCRYPT_SHA384_ALGORITHM
},
211 /* ALG_ID_SHA512 */ { 382, 64, 1024, BCRYPT_SHA512_ALGORITHM
}
221 NTSTATUS WINAPI
BCryptGenRandom(BCRYPT_ALG_HANDLE handle
, UCHAR
*buffer
, ULONG count
, ULONG flags
)
223 const DWORD supported_flags
= BCRYPT_USE_SYSTEM_PREFERRED_RNG
;
224 struct algorithm
*algorithm
= handle
;
226 TRACE("%p, %p, %u, %08x - semi-stub\n", handle
, buffer
, count
, flags
);
230 /* It's valid to call without an algorithm if BCRYPT_USE_SYSTEM_PREFERRED_RNG
231 * is set. In this case the preferred system RNG is used.
233 if (!(flags
& BCRYPT_USE_SYSTEM_PREFERRED_RNG
))
234 return STATUS_INVALID_HANDLE
;
236 else if (algorithm
->hdr
.magic
!= MAGIC_ALG
|| algorithm
->id
!= ALG_ID_RNG
)
237 return STATUS_INVALID_HANDLE
;
240 return STATUS_INVALID_PARAMETER
;
242 if (flags
& ~supported_flags
)
243 FIXME("unsupported flags %08x\n", flags
& ~supported_flags
);
246 FIXME("ignoring selected algorithm\n");
248 /* When zero bytes are requested the function returns success too. */
250 return STATUS_SUCCESS
;
252 if (algorithm
|| (flags
& BCRYPT_USE_SYSTEM_PREFERRED_RNG
))
254 if (RtlGenRandom(buffer
, count
))
255 return STATUS_SUCCESS
;
258 FIXME("called with unsupported parameters, returning error\n");
259 return STATUS_NOT_IMPLEMENTED
;
262 NTSTATUS WINAPI
BCryptOpenAlgorithmProvider( BCRYPT_ALG_HANDLE
*handle
, LPCWSTR id
, LPCWSTR implementation
, DWORD flags
)
264 const DWORD supported_flags
= BCRYPT_ALG_HANDLE_HMAC_FLAG
;
265 struct algorithm
*alg
;
268 TRACE( "%p, %s, %s, %08x\n", handle
, wine_dbgstr_w(id
), wine_dbgstr_w(implementation
), flags
);
270 if (!handle
|| !id
) return STATUS_INVALID_PARAMETER
;
271 if (flags
& ~supported_flags
)
273 FIXME( "unsupported flags %08x\n", flags
& ~supported_flags
);
274 return STATUS_NOT_IMPLEMENTED
;
277 if (!strcmpW( id
, BCRYPT_AES_ALGORITHM
)) alg_id
= ALG_ID_AES
;
278 else if (!strcmpW( id
, BCRYPT_MD2_ALGORITHM
)) alg_id
= ALG_ID_MD2
;
279 else if (!strcmpW( id
, BCRYPT_MD4_ALGORITHM
)) alg_id
= ALG_ID_MD4
;
280 else if (!strcmpW( id
, BCRYPT_MD5_ALGORITHM
)) alg_id
= ALG_ID_MD5
;
281 else if (!strcmpW( id
, BCRYPT_RNG_ALGORITHM
)) alg_id
= ALG_ID_RNG
;
282 else if (!strcmpW( id
, BCRYPT_SHA1_ALGORITHM
)) alg_id
= ALG_ID_SHA1
;
283 else if (!strcmpW( id
, BCRYPT_SHA256_ALGORITHM
)) alg_id
= ALG_ID_SHA256
;
284 else if (!strcmpW( id
, BCRYPT_SHA384_ALGORITHM
)) alg_id
= ALG_ID_SHA384
;
285 else if (!strcmpW( id
, BCRYPT_SHA512_ALGORITHM
)) alg_id
= ALG_ID_SHA512
;
288 FIXME( "algorithm %s not supported\n", debugstr_w(id
) );
289 return STATUS_NOT_IMPLEMENTED
;
291 if (implementation
&& strcmpW( implementation
, MS_PRIMITIVE_PROVIDER
))
293 FIXME( "implementation %s not supported\n", debugstr_w(implementation
) );
294 return STATUS_NOT_IMPLEMENTED
;
297 if (!(alg
= HeapAlloc( GetProcessHeap(), 0, sizeof(*alg
) ))) return STATUS_NO_MEMORY
;
298 alg
->hdr
.magic
= MAGIC_ALG
;
300 alg
->hmac
= flags
& BCRYPT_ALG_HANDLE_HMAC_FLAG
;
303 return STATUS_SUCCESS
;
306 NTSTATUS WINAPI
BCryptCloseAlgorithmProvider( BCRYPT_ALG_HANDLE handle
, DWORD flags
)
308 struct algorithm
*alg
= handle
;
310 TRACE( "%p, %08x\n", handle
, flags
);
312 if (!alg
|| alg
->hdr
.magic
!= MAGIC_ALG
) return STATUS_INVALID_HANDLE
;
313 HeapFree( GetProcessHeap(), 0, alg
);
314 return STATUS_SUCCESS
;
317 NTSTATUS WINAPI
BCryptGetFipsAlgorithmMode(BOOLEAN
*enabled
)
319 FIXME("%p - semi-stub\n", enabled
);
322 return STATUS_INVALID_PARAMETER
;
325 return STATUS_SUCCESS
;
341 static NTSTATUS
hash_init( struct hash_impl
*hash
, enum alg_id alg_id
)
346 md2_init( &hash
->u
.md2
);
350 MD4Init( &hash
->u
.md4
);
354 MD5Init( &hash
->u
.md5
);
358 A_SHAInit( &hash
->u
.sha1
);
362 sha256_init( &hash
->u
.sha256
);
366 sha384_init( &hash
->u
.sha512
);
370 sha512_init( &hash
->u
.sha512
);
374 ERR( "unhandled id %u\n", alg_id
);
375 return STATUS_NOT_IMPLEMENTED
;
377 return STATUS_SUCCESS
;
380 static NTSTATUS
hash_update( struct hash_impl
*hash
, enum alg_id alg_id
,
381 UCHAR
*input
, ULONG size
)
386 md2_update( &hash
->u
.md2
, input
, size
);
390 MD4Update( &hash
->u
.md4
, input
, size
);
394 MD5Update( &hash
->u
.md5
, input
, size
);
398 A_SHAUpdate( &hash
->u
.sha1
, input
, size
);
402 sha256_update( &hash
->u
.sha256
, input
, size
);
406 sha384_update( &hash
->u
.sha512
, input
, size
);
410 sha512_update( &hash
->u
.sha512
, input
, size
);
414 ERR( "unhandled id %u\n", alg_id
);
415 return STATUS_NOT_IMPLEMENTED
;
417 return STATUS_SUCCESS
;
420 static NTSTATUS
hash_finish( struct hash_impl
*hash
, enum alg_id alg_id
,
421 UCHAR
*output
, ULONG size
)
426 md2_finalize( &hash
->u
.md2
, output
);
430 MD4Final( &hash
->u
.md4
);
431 memcpy( output
, hash
->u
.md4
.digest
, 16 );
435 MD5Final( &hash
->u
.md5
);
436 memcpy( output
, hash
->u
.md5
.digest
, 16 );
440 A_SHAFinal( &hash
->u
.sha1
, (ULONG
*)output
);
444 sha256_finalize( &hash
->u
.sha256
, output
);
448 sha384_finalize( &hash
->u
.sha512
, output
);
452 sha512_finalize( &hash
->u
.sha512
, output
);
456 ERR( "unhandled id %u\n", alg_id
);
457 return STATUS_NOT_IMPLEMENTED
;
459 return STATUS_SUCCESS
;
467 struct hash_impl outer
;
468 struct hash_impl inner
;
471 #define BLOCK_LENGTH_AES 16
473 static NTSTATUS
generic_alg_property( enum alg_id id
, const WCHAR
*prop
, UCHAR
*buf
, ULONG size
, ULONG
*ret_size
)
475 if (!strcmpW( prop
, BCRYPT_OBJECT_LENGTH
))
477 if (!alg_props
[id
].object_length
)
478 return STATUS_NOT_SUPPORTED
;
479 *ret_size
= sizeof(ULONG
);
480 if (size
< sizeof(ULONG
))
481 return STATUS_BUFFER_TOO_SMALL
;
483 *(ULONG
*)buf
= alg_props
[id
].object_length
;
484 return STATUS_SUCCESS
;
487 if (!strcmpW( prop
, BCRYPT_HASH_LENGTH
))
489 if (!alg_props
[id
].hash_length
)
490 return STATUS_NOT_SUPPORTED
;
491 *ret_size
= sizeof(ULONG
);
492 if (size
< sizeof(ULONG
))
493 return STATUS_BUFFER_TOO_SMALL
;
495 *(ULONG
*)buf
= alg_props
[id
].hash_length
;
496 return STATUS_SUCCESS
;
499 if (!strcmpW( prop
, BCRYPT_ALGORITHM_NAME
))
501 *ret_size
= (strlenW(alg_props
[id
].alg_name
)+1)*sizeof(WCHAR
);
502 if (size
< *ret_size
)
503 return STATUS_BUFFER_TOO_SMALL
;
505 memcpy(buf
, alg_props
[id
].alg_name
, *ret_size
);
506 return STATUS_SUCCESS
;
509 return STATUS_NOT_IMPLEMENTED
;
512 static NTSTATUS
get_alg_property( enum alg_id id
, const WCHAR
*prop
, UCHAR
*buf
, ULONG size
, ULONG
*ret_size
)
516 status
= generic_alg_property( id
, prop
, buf
, size
, ret_size
);
517 if (status
!= STATUS_NOT_IMPLEMENTED
)
523 if (!strcmpW( prop
, BCRYPT_BLOCK_LENGTH
))
525 *ret_size
= sizeof(ULONG
);
526 if (size
< sizeof(ULONG
))
527 return STATUS_BUFFER_TOO_SMALL
;
529 *(ULONG
*)buf
= BLOCK_LENGTH_AES
;
530 return STATUS_SUCCESS
;
532 if (!strcmpW( prop
, BCRYPT_CHAINING_MODE
))
534 if (size
>= sizeof(BCRYPT_CHAIN_MODE_CBC
))
536 memcpy(buf
, BCRYPT_CHAIN_MODE_CBC
, sizeof(BCRYPT_CHAIN_MODE_CBC
));
537 *ret_size
= sizeof(BCRYPT_CHAIN_MODE_CBC
) * sizeof(WCHAR
);
538 return STATUS_SUCCESS
;
542 *ret_size
= sizeof(BCRYPT_CHAIN_MODE_CBC
) * sizeof(WCHAR
);
543 return STATUS_BUFFER_TOO_SMALL
;
546 if (!strcmpW( prop
, BCRYPT_KEY_LENGTHS
))
548 BCRYPT_KEY_LENGTHS_STRUCT
*key_lengths
= (void *)buf
;
549 *ret_size
= sizeof(*key_lengths
);
550 if (key_lengths
&& size
< *ret_size
) return STATUS_BUFFER_TOO_SMALL
;
553 key_lengths
->dwMinLength
= 128;
554 key_lengths
->dwMaxLength
= 256;
555 key_lengths
->dwIncrement
= 64;
557 return STATUS_SUCCESS
;
565 FIXME( "unsupported property %s\n", debugstr_w(prop
) );
566 return STATUS_NOT_IMPLEMENTED
;
569 static NTSTATUS
get_hash_property( enum alg_id id
, const WCHAR
*prop
, UCHAR
*buf
, ULONG size
, ULONG
*ret_size
)
573 status
= generic_alg_property( id
, prop
, buf
, size
, ret_size
);
574 if (status
== STATUS_NOT_IMPLEMENTED
)
575 FIXME( "unsupported property %s\n", debugstr_w(prop
) );
579 NTSTATUS WINAPI
BCryptGetProperty( BCRYPT_HANDLE handle
, LPCWSTR prop
, UCHAR
*buffer
, ULONG count
, ULONG
*res
, ULONG flags
)
581 struct object
*object
= handle
;
583 TRACE( "%p, %s, %p, %u, %p, %08x\n", handle
, wine_dbgstr_w(prop
), buffer
, count
, res
, flags
);
585 if (!object
) return STATUS_INVALID_HANDLE
;
586 if (!prop
|| !res
) return STATUS_INVALID_PARAMETER
;
588 switch (object
->magic
)
592 const struct algorithm
*alg
= (const struct algorithm
*)object
;
593 return get_alg_property( alg
->id
, prop
, buffer
, count
, res
);
597 const struct hash
*hash
= (const struct hash
*)object
;
598 return get_hash_property( hash
->alg_id
, prop
, buffer
, count
, res
);
601 WARN( "unknown magic %08x\n", object
->magic
);
602 return STATUS_INVALID_HANDLE
;
606 NTSTATUS WINAPI
BCryptSetProperty( BCRYPT_HANDLE handle
, const WCHAR
*prop
, UCHAR
*value
, ULONG size
, ULONG flags
)
608 FIXME( "%p, %s, %p, %u, %08x\n", handle
, debugstr_w(prop
), value
, size
, flags
);
609 return STATUS_NOT_IMPLEMENTED
;
612 NTSTATUS WINAPI
BCryptCreateHash( BCRYPT_ALG_HANDLE algorithm
, BCRYPT_HASH_HANDLE
*handle
, UCHAR
*object
, ULONG objectlen
,
613 UCHAR
*secret
, ULONG secretlen
, ULONG flags
)
615 struct algorithm
*alg
= algorithm
;
616 UCHAR buffer
[MAX_HASH_BLOCK_BITS
/ 8] = {0};
622 TRACE( "%p, %p, %p, %u, %p, %u, %08x - stub\n", algorithm
, handle
, object
, objectlen
,
623 secret
, secretlen
, flags
);
626 FIXME( "unimplemented flags %08x\n", flags
);
627 return STATUS_NOT_IMPLEMENTED
;
630 if (!alg
|| alg
->hdr
.magic
!= MAGIC_ALG
) return STATUS_INVALID_HANDLE
;
631 if (object
) FIXME( "ignoring object buffer\n" );
633 if (!(hash
= HeapAlloc( GetProcessHeap(), 0, sizeof(*hash
) ))) return STATUS_NO_MEMORY
;
634 hash
->hdr
.magic
= MAGIC_HASH
;
635 hash
->alg_id
= alg
->id
;
636 hash
->hmac
= alg
->hmac
;
638 /* initialize hash */
639 if ((status
= hash_init( &hash
->inner
, hash
->alg_id
))) goto end
;
640 if (!hash
->hmac
) goto end
;
642 /* initialize hmac */
643 if ((status
= hash_init( &hash
->outer
, hash
->alg_id
))) goto end
;
644 block_bytes
= alg_props
[hash
->alg_id
].block_bits
/ 8;
645 if (secretlen
> block_bytes
)
647 struct hash_impl temp
;
648 if ((status
= hash_init( &temp
, hash
->alg_id
))) goto end
;
649 if ((status
= hash_update( &temp
, hash
->alg_id
, secret
, secretlen
))) goto end
;
650 if ((status
= hash_finish( &temp
, hash
->alg_id
, buffer
,
651 alg_props
[hash
->alg_id
].hash_length
))) goto end
;
655 memcpy( buffer
, secret
, secretlen
);
657 for (i
= 0; i
< block_bytes
; i
++) buffer
[i
] ^= 0x5c;
658 if ((status
= hash_update( &hash
->outer
, hash
->alg_id
, buffer
, block_bytes
))) goto end
;
659 for (i
= 0; i
< block_bytes
; i
++) buffer
[i
] ^= (0x5c ^ 0x36);
660 status
= hash_update( &hash
->inner
, hash
->alg_id
, buffer
, block_bytes
);
663 if (status
!= STATUS_SUCCESS
)
665 HeapFree( GetProcessHeap(), 0, hash
);
670 return STATUS_SUCCESS
;
673 NTSTATUS WINAPI
BCryptDuplicateHash( BCRYPT_HASH_HANDLE handle
, BCRYPT_HASH_HANDLE
*handle_copy
,
674 UCHAR
*object
, ULONG objectlen
, ULONG flags
)
676 struct hash
*hash_orig
= handle
;
677 struct hash
*hash_copy
;
679 TRACE( "%p, %p, %p, %u, %u\n", handle
, handle_copy
, object
, objectlen
, flags
);
681 if (!hash_orig
|| hash_orig
->hdr
.magic
!= MAGIC_HASH
) return STATUS_INVALID_HANDLE
;
682 if (!handle_copy
) return STATUS_INVALID_PARAMETER
;
683 if (object
) FIXME( "ignoring object buffer\n" );
685 if (!(hash_copy
= HeapAlloc( GetProcessHeap(), 0, sizeof(*hash_copy
) )))
686 return STATUS_NO_MEMORY
;
688 memcpy( hash_copy
, hash_orig
, sizeof(*hash_orig
) );
690 *handle_copy
= hash_copy
;
691 return STATUS_SUCCESS
;
694 NTSTATUS WINAPI
BCryptDestroyHash( BCRYPT_HASH_HANDLE handle
)
696 struct hash
*hash
= handle
;
698 TRACE( "%p\n", handle
);
700 if (!hash
|| hash
->hdr
.magic
!= MAGIC_HASH
) return STATUS_INVALID_HANDLE
;
701 HeapFree( GetProcessHeap(), 0, hash
);
702 return STATUS_SUCCESS
;
705 NTSTATUS WINAPI
BCryptHashData( BCRYPT_HASH_HANDLE handle
, UCHAR
*input
, ULONG size
, ULONG flags
)
707 struct hash
*hash
= handle
;
709 TRACE( "%p, %p, %u, %08x\n", handle
, input
, size
, flags
);
711 if (!hash
|| hash
->hdr
.magic
!= MAGIC_HASH
) return STATUS_INVALID_HANDLE
;
712 if (!input
) return STATUS_SUCCESS
;
714 return hash_update( &hash
->inner
, hash
->alg_id
, input
, size
);
717 NTSTATUS WINAPI
BCryptFinishHash( BCRYPT_HASH_HANDLE handle
, UCHAR
*output
, ULONG size
, ULONG flags
)
719 UCHAR buffer
[MAX_HASH_OUTPUT_BYTES
];
720 struct hash
*hash
= handle
;
724 TRACE( "%p, %p, %u, %08x\n", handle
, output
, size
, flags
);
726 if (!hash
|| hash
->hdr
.magic
!= MAGIC_HASH
) return STATUS_INVALID_HANDLE
;
727 if (!output
) return STATUS_INVALID_PARAMETER
;
730 return hash_finish( &hash
->inner
, hash
->alg_id
, output
, size
);
732 hash_length
= alg_props
[hash
->alg_id
].hash_length
;
733 if ((status
= hash_finish( &hash
->inner
, hash
->alg_id
, buffer
, hash_length
))) return status
;
734 if ((status
= hash_update( &hash
->outer
, hash
->alg_id
, buffer
, hash_length
))) return status
;
735 return hash_finish( &hash
->outer
, hash
->alg_id
, output
, size
);
738 NTSTATUS WINAPI
BCryptHash( BCRYPT_ALG_HANDLE algorithm
, UCHAR
*secret
, ULONG secretlen
,
739 UCHAR
*input
, ULONG inputlen
, UCHAR
*output
, ULONG outputlen
)
742 BCRYPT_HASH_HANDLE handle
;
744 TRACE( "%p, %p, %u, %p, %u, %p, %u\n", algorithm
, secret
, secretlen
,
745 input
, inputlen
, output
, outputlen
);
747 status
= BCryptCreateHash( algorithm
, &handle
, NULL
, 0, secret
, secretlen
, 0);
748 if (status
!= STATUS_SUCCESS
)
753 status
= BCryptHashData( handle
, input
, inputlen
, 0 );
754 if (status
!= STATUS_SUCCESS
)
756 BCryptDestroyHash( handle
);
760 status
= BCryptFinishHash( handle
, output
, outputlen
, 0 );
761 if (status
!= STATUS_SUCCESS
)
763 BCryptDestroyHash( handle
);
767 return BCryptDestroyHash( handle
);
770 #if defined(HAVE_GNUTLS_CIPHER_INIT) || defined(HAVE_COMMONCRYPTO_COMMONCRYPTOR_H) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
771 static ULONG
get_block_size( enum alg_id alg
)
773 ULONG ret
= 0, size
= sizeof(ret
);
774 get_alg_property( alg
, BCRYPT_BLOCK_LENGTH
, (UCHAR
*)&ret
, sizeof(ret
), &size
);
779 #if defined(HAVE_GNUTLS_CIPHER_INIT) && !defined(HAVE_COMMONCRYPTO_COMMONCRYPTOR_H)
785 gnutls_cipher_hd_t handle
;
790 static NTSTATUS
key_init( struct key
*key
, enum alg_id id
, const UCHAR
*secret
, ULONG secret_len
)
794 if (!libgnutls_handle
) return STATUS_INTERNAL_ERROR
;
802 FIXME( "algorithm %u not supported\n", id
);
803 return STATUS_NOT_SUPPORTED
;
806 if (!(key
->block_size
= get_block_size( id
))) return STATUS_INVALID_PARAMETER
;
807 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, secret_len
))) return STATUS_NO_MEMORY
;
808 memcpy( buffer
, secret
, secret_len
);
811 key
->handle
= 0; /* initialized on first use */
812 key
->secret
= buffer
;
813 key
->secret_len
= secret_len
;
815 return STATUS_SUCCESS
;
818 static gnutls_cipher_algorithm_t
get_gnutls_cipher( const struct key
*key
)
823 FIXME( "handle block size and chaining mode\n" );
824 return GNUTLS_CIPHER_AES_128_CBC
;
827 FIXME( "algorithm %u not supported\n", key
->alg_id
);
828 return GNUTLS_CIPHER_UNKNOWN
;
832 static NTSTATUS
key_set_params( struct key
*key
, UCHAR
*iv
, ULONG iv_len
)
834 gnutls_cipher_algorithm_t cipher
;
835 gnutls_datum_t secret
, vector
;
840 pgnutls_cipher_deinit( key
->handle
);
844 if ((cipher
= get_gnutls_cipher( key
)) == GNUTLS_CIPHER_UNKNOWN
)
845 return STATUS_NOT_SUPPORTED
;
847 secret
.data
= key
->secret
;
848 secret
.size
= key
->secret_len
;
852 vector
.size
= iv_len
;
855 if ((ret
= pgnutls_cipher_init( &key
->handle
, cipher
, &secret
, iv
? &vector
: NULL
)))
857 pgnutls_perror( ret
);
858 return STATUS_INTERNAL_ERROR
;
861 return STATUS_SUCCESS
;
864 static NTSTATUS
key_encrypt( struct key
*key
, const UCHAR
*input
, ULONG input_len
, UCHAR
*output
,
869 if ((ret
= pgnutls_cipher_encrypt2( key
->handle
, input
, input_len
, output
, output_len
)))
871 pgnutls_perror( ret
);
872 return STATUS_INTERNAL_ERROR
;
875 return STATUS_SUCCESS
;
878 static NTSTATUS
key_decrypt( struct key
*key
, const UCHAR
*input
, ULONG input_len
, UCHAR
*output
,
883 if ((ret
= pgnutls_cipher_decrypt2( key
->handle
, input
, input_len
, output
, output_len
)))
885 pgnutls_perror( ret
);
886 return STATUS_INTERNAL_ERROR
;
889 return STATUS_SUCCESS
;
892 static NTSTATUS
key_destroy( struct key
*key
)
894 if (key
->handle
) pgnutls_cipher_deinit( key
->handle
);
895 HeapFree( GetProcessHeap(), 0, key
->secret
);
896 HeapFree( GetProcessHeap(), 0, key
);
897 return STATUS_SUCCESS
;
899 #elif defined(HAVE_COMMONCRYPTO_COMMONCRYPTOR_H) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
905 CCCryptorRef ref_encrypt
;
906 CCCryptorRef ref_decrypt
;
911 static NTSTATUS
key_init( struct key
*key
, enum alg_id id
, const UCHAR
*secret
, ULONG secret_len
)
921 FIXME( "algorithm %u not supported\n", id
);
922 return STATUS_NOT_SUPPORTED
;
925 if (!(key
->block_size
= get_block_size( id
))) return STATUS_INVALID_PARAMETER
;
926 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, secret_len
))) return STATUS_NO_MEMORY
;
927 memcpy( buffer
, secret
, secret_len
);
930 key
->ref_encrypt
= NULL
; /* initialized on first use */
931 key
->ref_decrypt
= NULL
;
932 key
->secret
= buffer
;
933 key
->secret_len
= secret_len
;
935 return STATUS_SUCCESS
;
938 static NTSTATUS
key_set_params( struct key
*key
, UCHAR
*iv
, ULONG iv_len
)
940 CCCryptorStatus status
;
942 if (key
->ref_encrypt
)
944 CCCryptorRelease( key
->ref_encrypt
);
945 key
->ref_encrypt
= NULL
;
947 if (key
->ref_decrypt
)
949 CCCryptorRelease( key
->ref_decrypt
);
950 key
->ref_decrypt
= NULL
;
953 if ((status
= CCCryptorCreateWithMode( kCCEncrypt
, kCCModeCBC
, kCCAlgorithmAES128
, ccNoPadding
, iv
,
954 key
->secret
, key
->secret_len
, NULL
, 0, 0, 0, &key
->ref_encrypt
)) != kCCSuccess
)
956 WARN( "CCCryptorCreateWithMode failed %d\n", status
);
957 return STATUS_INTERNAL_ERROR
;
959 if ((status
= CCCryptorCreateWithMode( kCCDecrypt
, kCCModeCBC
, kCCAlgorithmAES128
, ccNoPadding
, iv
,
960 key
->secret
, key
->secret_len
, NULL
, 0, 0, 0, &key
->ref_decrypt
)) != kCCSuccess
)
962 WARN( "CCCryptorCreateWithMode failed %d\n", status
);
963 CCCryptorRelease( key
->ref_encrypt
);
964 key
->ref_encrypt
= NULL
;
965 return STATUS_INTERNAL_ERROR
;
968 return STATUS_SUCCESS
;
971 static NTSTATUS
key_encrypt( struct key
*key
, const UCHAR
*input
, ULONG input_len
, UCHAR
*output
,
974 CCCryptorStatus status
;
976 if ((status
= CCCryptorUpdate( key
->ref_encrypt
, input
, input_len
, output
, output_len
, NULL
)) != kCCSuccess
)
978 WARN( "CCCryptorUpdate failed %d\n", status
);
979 return STATUS_INTERNAL_ERROR
;
982 return STATUS_SUCCESS
;
985 static NTSTATUS
key_decrypt( struct key
*key
, const UCHAR
*input
, ULONG input_len
, UCHAR
*output
,
988 CCCryptorStatus status
;
990 if ((status
= CCCryptorUpdate( key
->ref_decrypt
, input
, input_len
, output
, output_len
, NULL
)) != kCCSuccess
)
992 WARN( "CCCryptorUpdate failed %d\n", status
);
993 return STATUS_INTERNAL_ERROR
;
996 return STATUS_SUCCESS
;
999 static NTSTATUS
key_destroy( struct key
*key
)
1001 if (key
->ref_encrypt
) CCCryptorRelease( key
->ref_encrypt
);
1002 if (key
->ref_decrypt
) CCCryptorRelease( key
->ref_decrypt
);
1003 HeapFree( GetProcessHeap(), 0, key
->secret
);
1004 HeapFree( GetProcessHeap(), 0, key
);
1005 return STATUS_SUCCESS
;
1014 static NTSTATUS
key_init( struct key
*key
, enum alg_id id
, const UCHAR
*secret
, ULONG secret_len
)
1016 ERR( "support for keys not available at build time\n" );
1017 return STATUS_NOT_IMPLEMENTED
;
1020 static NTSTATUS
key_set_params( struct key
*key
, UCHAR
*iv
, ULONG iv_len
)
1022 ERR( "support for keys not available at build time\n" );
1023 return STATUS_NOT_IMPLEMENTED
;
1026 static NTSTATUS
key_encrypt( struct key
*key
, const UCHAR
*input
, ULONG input_len
, UCHAR
*output
,
1029 ERR( "support for keys not available at build time\n" );
1030 return STATUS_NOT_IMPLEMENTED
;
1033 static NTSTATUS
key_decrypt( struct key
*key
, const UCHAR
*input
, ULONG input_len
, UCHAR
*output
,
1036 ERR( "support for keys not available at build time\n" );
1037 return STATUS_NOT_IMPLEMENTED
;
1040 static NTSTATUS
key_destroy( struct key
*key
)
1042 ERR( "support for keys not available at build time\n" );
1043 return STATUS_NOT_IMPLEMENTED
;
1047 NTSTATUS WINAPI
BCryptGenerateSymmetricKey( BCRYPT_ALG_HANDLE algorithm
, BCRYPT_KEY_HANDLE
*handle
,
1048 UCHAR
*object
, ULONG object_len
, UCHAR
*secret
, ULONG secret_len
,
1051 struct algorithm
*alg
= algorithm
;
1055 TRACE( "%p, %p, %p, %u, %p, %u, %08x\n", algorithm
, handle
, object
, object_len
, secret
, secret_len
, flags
);
1057 if (!alg
|| alg
->hdr
.magic
!= MAGIC_ALG
) return STATUS_INVALID_HANDLE
;
1058 if (object
) FIXME( "ignoring object buffer\n" );
1060 if (!(key
= HeapAlloc( GetProcessHeap(), 0, sizeof(*key
) ))) return STATUS_NO_MEMORY
;
1061 key
->hdr
.magic
= MAGIC_KEY
;
1063 if ((status
= key_init( key
, alg
->id
, secret
, secret_len
)))
1065 HeapFree( GetProcessHeap(), 0, key
);
1070 return STATUS_SUCCESS
;
1073 NTSTATUS WINAPI
BCryptDestroyKey( BCRYPT_KEY_HANDLE handle
)
1075 struct key
*key
= handle
;
1077 TRACE( "%p\n", handle
);
1079 if (!key
|| key
->hdr
.magic
!= MAGIC_KEY
) return STATUS_INVALID_HANDLE
;
1080 return key_destroy( key
);
1083 NTSTATUS WINAPI
BCryptEncrypt( BCRYPT_KEY_HANDLE handle
, UCHAR
*input
, ULONG input_len
,
1084 void *padding
, UCHAR
*iv
, ULONG iv_len
, UCHAR
*output
,
1085 ULONG output_len
, ULONG
*ret_len
, ULONG flags
)
1087 struct key
*key
= handle
;
1088 ULONG bytes_left
= input_len
;
1089 UCHAR
*buf
, *src
, *dst
;
1092 TRACE( "%p, %p, %u, %p, %p, %u, %p, %u, %p, %08x\n", handle
, input
, input_len
,
1093 padding
, iv
, iv_len
, output
, output_len
, ret_len
, flags
);
1095 if (!key
|| key
->hdr
.magic
!= MAGIC_KEY
) return STATUS_INVALID_HANDLE
;
1098 FIXME( "padding info not implemented\n" );
1099 return STATUS_NOT_IMPLEMENTED
;
1101 if (flags
& ~BCRYPT_BLOCK_PADDING
)
1103 FIXME( "flags %08x not implemented\n", flags
);
1104 return STATUS_NOT_IMPLEMENTED
;
1107 if ((status
= key_set_params( key
, iv
, iv_len
))) return status
;
1109 *ret_len
= input_len
;
1111 if (flags
& BCRYPT_BLOCK_PADDING
)
1112 *ret_len
= (input_len
+ key
->block_size
) & ~(key
->block_size
- 1);
1113 else if (input_len
& (key
->block_size
- 1))
1114 return STATUS_INVALID_BUFFER_SIZE
;
1116 if (!output
) return STATUS_SUCCESS
;
1117 if (output_len
< *ret_len
) return STATUS_BUFFER_TOO_SMALL
;
1121 while (bytes_left
>= key
->block_size
)
1123 if ((status
= key_encrypt( key
, src
, key
->block_size
, dst
, key
->block_size
))) return status
;
1124 bytes_left
-= key
->block_size
;
1125 src
+= key
->block_size
;
1126 dst
+= key
->block_size
;
1129 if (flags
& BCRYPT_BLOCK_PADDING
)
1131 if (!(buf
= HeapAlloc( GetProcessHeap(), 0, key
->block_size
))) return STATUS_NO_MEMORY
;
1132 memcpy( buf
, src
, bytes_left
);
1133 memset( buf
+ bytes_left
, key
->block_size
- bytes_left
, key
->block_size
- bytes_left
);
1134 status
= key_encrypt( key
, buf
, key
->block_size
, dst
, key
->block_size
);
1135 HeapFree( GetProcessHeap(), 0, buf
);
1141 NTSTATUS WINAPI
BCryptDecrypt( BCRYPT_KEY_HANDLE handle
, UCHAR
*input
, ULONG input_len
,
1142 void *padding
, UCHAR
*iv
, ULONG iv_len
, UCHAR
*output
,
1143 ULONG output_len
, ULONG
*ret_len
, ULONG flags
)
1145 struct key
*key
= handle
;
1146 ULONG bytes_left
= input_len
;
1147 UCHAR
*buf
, *src
, *dst
;
1150 TRACE( "%p, %p, %u, %p, %p, %u, %p, %u, %p, %08x\n", handle
, input
, input_len
,
1151 padding
, iv
, iv_len
, output
, output_len
, ret_len
, flags
);
1153 if (!key
|| key
->hdr
.magic
!= MAGIC_KEY
) return STATUS_INVALID_HANDLE
;
1156 FIXME( "padding info not implemented\n" );
1157 return STATUS_NOT_IMPLEMENTED
;
1159 if (flags
& ~BCRYPT_BLOCK_PADDING
)
1161 FIXME( "flags %08x not supported\n", flags
);
1162 return STATUS_NOT_IMPLEMENTED
;
1165 if ((status
= key_set_params( key
, iv
, iv_len
))) return status
;
1167 *ret_len
= input_len
;
1169 if (input_len
& (key
->block_size
- 1)) return STATUS_INVALID_BUFFER_SIZE
;
1170 if (!output
) return STATUS_SUCCESS
;
1171 if (flags
& BCRYPT_BLOCK_PADDING
)
1173 if (output_len
+ key
->block_size
< *ret_len
) return STATUS_BUFFER_TOO_SMALL
;
1174 if (input_len
< key
->block_size
) return STATUS_BUFFER_TOO_SMALL
;
1175 bytes_left
-= key
->block_size
;
1177 else if (output_len
< *ret_len
)
1178 return STATUS_BUFFER_TOO_SMALL
;
1182 while (bytes_left
>= key
->block_size
)
1184 if ((status
= key_decrypt( key
, src
, key
->block_size
, dst
, key
->block_size
))) return status
;
1185 bytes_left
-= key
->block_size
;
1186 src
+= key
->block_size
;
1187 dst
+= key
->block_size
;
1190 if (flags
& BCRYPT_BLOCK_PADDING
)
1192 if (!(buf
= HeapAlloc( GetProcessHeap(), 0, key
->block_size
))) return STATUS_NO_MEMORY
;
1193 status
= key_decrypt( key
, src
, key
->block_size
, buf
, key
->block_size
);
1194 if (!status
&& buf
[ key
->block_size
- 1 ] <= key
->block_size
)
1196 *ret_len
-= buf
[ key
->block_size
- 1 ];
1197 if (output_len
< *ret_len
) status
= STATUS_BUFFER_TOO_SMALL
;
1198 else memcpy( dst
, buf
, key
->block_size
- buf
[ key
->block_size
- 1 ] );
1201 status
= STATUS_UNSUCCESSFUL
; /* FIXME: invalid padding */
1202 HeapFree( GetProcessHeap(), 0, buf
);
1208 BOOL WINAPI
DllMain( HINSTANCE hinst
, DWORD reason
, LPVOID reserved
)
1212 case DLL_PROCESS_ATTACH
:
1214 DisableThreadLibraryCalls( hinst
);
1215 #if defined(HAVE_GNUTLS_CIPHER_INIT) && !defined(HAVE_COMMONCRYPTO_COMMONCRYPTOR_H)
1216 gnutls_initialize();
1220 case DLL_PROCESS_DETACH
:
1221 if (reserved
) break;
1222 #if defined(HAVE_GNUTLS_CIPHER_INIT) && !defined(HAVE_COMMONCRYPTO_COMMONCRYPTOR_H)
1223 gnutls_uninitialize();