gameux/tests: Add a trailing '\n' to an ok() call.
[wine.git] / dlls / bcrypt / bcrypt_main.c
blobc064b637528e634fada89d4a40224dfe4bbaa44b
1 /*
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
20 #include "config.h"
21 #include "wine/port.h"
23 #include <stdarg.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>
30 #endif
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "ntsecapi.h"
37 #include "bcrypt.h"
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);
63 #undef MAKE_FUNCPTR
65 static void gnutls_log( int level, const char *msg )
67 TRACE( "<%d> %s", level, msg );
70 static BOOL gnutls_initialize(void)
72 int ret;
74 if (!(libgnutls_handle = wine_dlopen( SONAME_LIBGNUTLS, RTLD_NOW, NULL, 0 )))
76 ERR_(winediag)( "failed to load libgnutls, no support for encryption\n" );
77 return FALSE;
80 #define LOAD_FUNCPTR(f) \
81 if (!(p##f = wine_dlsym( libgnutls_handle, #f, NULL, 0 ))) \
82 { \
83 ERR( "failed to load %s\n", #f ); \
84 goto fail; \
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)
96 #undef LOAD_FUNCPTR
98 if ((ret = pgnutls_global_init()) != GNUTLS_E_SUCCESS)
100 pgnutls_perror( ret );
101 goto fail;
104 if (TRACE_ON( bcrypt ))
106 pgnutls_global_set_log_level( 4 );
107 pgnutls_global_set_log_function( gnutls_log );
110 return TRUE;
112 fail:
113 wine_dlclose( libgnutls_handle, NULL, 0 );
114 libgnutls_handle = NULL;
115 return FALSE;
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);
167 *ppAlgList=NULL;
168 *pAlgCount=0;
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')
176 struct object
178 ULONG magic;
181 enum alg_id
183 ALG_ID_AES,
184 ALG_ID_MD2,
185 ALG_ID_MD4,
186 ALG_ID_MD5,
187 ALG_ID_RNG,
188 ALG_ID_SHA1,
189 ALG_ID_SHA256,
190 ALG_ID_SHA384,
191 ALG_ID_SHA512
194 #define MAX_HASH_OUTPUT_BYTES 64
195 #define MAX_HASH_BLOCK_BITS 1024
197 static const struct {
198 ULONG object_length;
199 ULONG hash_length;
200 ULONG block_bits;
201 const WCHAR *alg_name;
202 } alg_props[] = {
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 }
214 struct algorithm
216 struct object hdr;
217 enum alg_id id;
218 BOOL hmac;
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);
228 if (!algorithm)
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;
239 if (!buffer)
240 return STATUS_INVALID_PARAMETER;
242 if (flags & ~supported_flags)
243 FIXME("unsupported flags %08x\n", flags & ~supported_flags);
245 if (algorithm)
246 FIXME("ignoring selected algorithm\n");
248 /* When zero bytes are requested the function returns success too. */
249 if (!count)
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;
266 enum alg_id alg_id;
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;
286 else
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;
299 alg->id = alg_id;
300 alg->hmac = flags & BCRYPT_ALG_HANDLE_HMAC_FLAG;
302 *handle = alg;
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);
321 if (!enabled)
322 return STATUS_INVALID_PARAMETER;
324 *enabled = FALSE;
325 return STATUS_SUCCESS;
328 struct hash_impl
330 union
332 MD2_CTX md2;
333 MD4_CTX md4;
334 MD5_CTX md5;
335 SHA_CTX sha1;
336 SHA256_CTX sha256;
337 SHA512_CTX sha512;
338 } u;
341 static NTSTATUS hash_init( struct hash_impl *hash, enum alg_id alg_id )
343 switch (alg_id)
345 case ALG_ID_MD2:
346 md2_init( &hash->u.md2 );
347 break;
349 case ALG_ID_MD4:
350 MD4Init( &hash->u.md4 );
351 break;
353 case ALG_ID_MD5:
354 MD5Init( &hash->u.md5 );
355 break;
357 case ALG_ID_SHA1:
358 A_SHAInit( &hash->u.sha1 );
359 break;
361 case ALG_ID_SHA256:
362 sha256_init( &hash->u.sha256 );
363 break;
365 case ALG_ID_SHA384:
366 sha384_init( &hash->u.sha512 );
367 break;
369 case ALG_ID_SHA512:
370 sha512_init( &hash->u.sha512 );
371 break;
373 default:
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 )
383 switch (alg_id)
385 case ALG_ID_MD2:
386 md2_update( &hash->u.md2, input, size );
387 break;
389 case ALG_ID_MD4:
390 MD4Update( &hash->u.md4, input, size );
391 break;
393 case ALG_ID_MD5:
394 MD5Update( &hash->u.md5, input, size );
395 break;
397 case ALG_ID_SHA1:
398 A_SHAUpdate( &hash->u.sha1, input, size );
399 break;
401 case ALG_ID_SHA256:
402 sha256_update( &hash->u.sha256, input, size );
403 break;
405 case ALG_ID_SHA384:
406 sha384_update( &hash->u.sha512, input, size );
407 break;
409 case ALG_ID_SHA512:
410 sha512_update( &hash->u.sha512, input, size );
411 break;
413 default:
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 )
423 switch (alg_id)
425 case ALG_ID_MD2:
426 md2_finalize( &hash->u.md2, output );
427 break;
429 case ALG_ID_MD4:
430 MD4Final( &hash->u.md4 );
431 memcpy( output, hash->u.md4.digest, 16 );
432 break;
434 case ALG_ID_MD5:
435 MD5Final( &hash->u.md5 );
436 memcpy( output, hash->u.md5.digest, 16 );
437 break;
439 case ALG_ID_SHA1:
440 A_SHAFinal( &hash->u.sha1, (ULONG *)output );
441 break;
443 case ALG_ID_SHA256:
444 sha256_finalize( &hash->u.sha256, output );
445 break;
447 case ALG_ID_SHA384:
448 sha384_finalize( &hash->u.sha512, output );
449 break;
451 case ALG_ID_SHA512:
452 sha512_finalize( &hash->u.sha512, output );
453 break;
455 default:
456 ERR( "unhandled id %u\n", alg_id );
457 return STATUS_NOT_IMPLEMENTED;
459 return STATUS_SUCCESS;
462 struct hash
464 struct object hdr;
465 enum alg_id alg_id;
466 BOOL hmac;
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;
482 if (buf)
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;
494 if(buf)
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;
504 if(buf)
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 )
514 NTSTATUS status;
516 status = generic_alg_property( id, prop, buf, size, ret_size );
517 if (status != STATUS_NOT_IMPLEMENTED)
518 return status;
520 switch (id)
522 case ALG_ID_AES:
523 if (!strcmpW( prop, BCRYPT_BLOCK_LENGTH ))
525 *ret_size = sizeof(ULONG);
526 if (size < sizeof(ULONG))
527 return STATUS_BUFFER_TOO_SMALL;
528 if (buf)
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;
540 else
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;
551 if (key_lengths)
553 key_lengths->dwMinLength = 128;
554 key_lengths->dwMaxLength = 256;
555 key_lengths->dwIncrement = 64;
557 return STATUS_SUCCESS;
559 break;
561 default:
562 break;
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 )
571 NTSTATUS status;
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) );
576 return status;
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)
590 case MAGIC_ALG:
592 const struct algorithm *alg = (const struct algorithm *)object;
593 return get_alg_property( alg->id, prop, buffer, count, res );
595 case MAGIC_HASH:
597 const struct hash *hash = (const struct hash *)object;
598 return get_hash_property( hash->alg_id, prop, buffer, count, res );
600 default:
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};
617 struct hash *hash;
618 int block_bytes;
619 NTSTATUS status;
620 int i;
622 TRACE( "%p, %p, %p, %u, %p, %u, %08x - stub\n", algorithm, handle, object, objectlen,
623 secret, secretlen, flags );
624 if (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;
653 else
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 );
662 end:
663 if (status != STATUS_SUCCESS)
665 HeapFree( GetProcessHeap(), 0, hash );
666 return status;
669 *handle = 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;
721 NTSTATUS status;
722 int hash_length;
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;
729 if (!hash->hmac)
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 )
741 NTSTATUS status;
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)
750 return status;
753 status = BCryptHashData( handle, input, inputlen, 0 );
754 if (status != STATUS_SUCCESS)
756 BCryptDestroyHash( handle );
757 return status;
760 status = BCryptFinishHash( handle, output, outputlen, 0 );
761 if (status != STATUS_SUCCESS)
763 BCryptDestroyHash( handle );
764 return status;
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 );
775 return ret;
777 #endif
779 #if defined(HAVE_GNUTLS_CIPHER_INIT) && !defined(HAVE_COMMONCRYPTO_COMMONCRYPTOR_H)
780 struct key
782 struct object hdr;
783 enum alg_id alg_id;
784 ULONG block_size;
785 gnutls_cipher_hd_t handle;
786 UCHAR *secret;
787 ULONG secret_len;
790 static NTSTATUS key_init( struct key *key, enum alg_id id, const UCHAR *secret, ULONG secret_len )
792 UCHAR *buffer;
794 if (!libgnutls_handle) return STATUS_INTERNAL_ERROR;
796 switch (id)
798 case ALG_ID_AES:
799 break;
801 default:
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 );
810 key->alg_id = id;
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 )
820 switch (key->alg_id)
822 case ALG_ID_AES:
823 FIXME( "handle block size and chaining mode\n" );
824 return GNUTLS_CIPHER_AES_128_CBC;
826 default:
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;
836 int ret;
838 if (key->handle)
840 pgnutls_cipher_deinit( key->handle );
841 key->handle = NULL;
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;
849 if (iv)
851 vector.data = iv;
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,
865 ULONG output_len )
867 int ret;
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,
879 ULONG output_len )
881 int ret;
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
900 struct key
902 struct object hdr;
903 enum alg_id alg_id;
904 ULONG block_size;
905 CCCryptorRef ref_encrypt;
906 CCCryptorRef ref_decrypt;
907 UCHAR *secret;
908 ULONG secret_len;
911 static NTSTATUS key_init( struct key *key, enum alg_id id, const UCHAR *secret, ULONG secret_len )
913 UCHAR *buffer;
915 switch (id)
917 case ALG_ID_AES:
918 break;
920 default:
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 );
929 key->alg_id = id;
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,
972 ULONG output_len )
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,
986 ULONG output_len )
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;
1007 #else
1008 struct key
1010 struct object hdr;
1011 ULONG block_size;
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,
1027 ULONG output_len )
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,
1034 ULONG output_len )
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;
1045 #endif
1047 NTSTATUS WINAPI BCryptGenerateSymmetricKey( BCRYPT_ALG_HANDLE algorithm, BCRYPT_KEY_HANDLE *handle,
1048 UCHAR *object, ULONG object_len, UCHAR *secret, ULONG secret_len,
1049 ULONG flags )
1051 struct algorithm *alg = algorithm;
1052 struct key *key;
1053 NTSTATUS status;
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 );
1066 return status;
1069 *handle = 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;
1090 NTSTATUS status;
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;
1096 if (padding)
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;
1119 src = input;
1120 dst = output;
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 );
1138 return status;
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;
1148 NTSTATUS status;
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;
1154 if (padding)
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;
1180 src = input;
1181 dst = output;
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 ] );
1200 else
1201 status = STATUS_UNSUCCESSFUL; /* FIXME: invalid padding */
1202 HeapFree( GetProcessHeap(), 0, buf );
1205 return status;
1208 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
1210 switch (reason)
1212 case DLL_PROCESS_ATTACH:
1213 instance = hinst;
1214 DisableThreadLibraryCalls( hinst );
1215 #if defined(HAVE_GNUTLS_CIPHER_INIT) && !defined(HAVE_COMMONCRYPTO_COMMONCRYPTOR_H)
1216 gnutls_initialize();
1217 #endif
1218 break;
1220 case DLL_PROCESS_DETACH:
1221 if (reserved) break;
1222 #if defined(HAVE_GNUTLS_CIPHER_INIT) && !defined(HAVE_COMMONCRYPTO_COMMONCRYPTOR_H)
1223 gnutls_uninitialize();
1224 #endif
1225 break;
1227 return TRUE;