-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdllmain.cpp
1886 lines (1582 loc) · 49.8 KB
/
dllmain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// dllmain.cpp : Определяет точку входа для приложения DLL.
#include "pch.h"
/*
* Copyright 2009 Henri Verbeet for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
#include <malloc.h>
#include <ntstatus.h>
#define WIN32_NO_STATUS
#include <windef.h>
#include <winbase.h>
#include <ntsecapi.h>
#include "bcrypt.h"
#include <stdio.h>
#define SONAME_LIBMBEDTLS
#ifdef SONAME_LIBMBEDTLS
#include <mbedtls/md.h>
#include <mbedtls/md5.h>
#include <mbedtls/sha1.h>
#include <mbedtls/sha256.h>
#include <mbedtls/sha512.h>
#endif
void WARN(const char* format, ...);
void TRACE(const char* format, ...);
void FIXME(const char* format, ...);
void ERR(const char* format, ...);
char* wide_to_char(LPCWSTR wide_str);
static HINSTANCE instance;
//WINE_DECLARE_DEBUG_CHANNEL(winediag);
static BOOL mbedtls_initialize(void)
{
return TRUE;
}
/*
enum alg_id
{
ALG_ID_MD5,
ALG_ID_RNG,
ALG_ID_SHA1,
ALG_ID_SHA256,
ALG_ID_SHA384,
ALG_ID_SHA512,
ALG_ID_ECDSA_P256,
ALG_ID_ECDSA_P384,
};*/
enum alg_id
{
/* cipher */
ALG_ID_3DES,
ALG_ID_AES,
/* hash */
ALG_ID_SHA256,
ALG_ID_SHA384,
ALG_ID_SHA512,
ALG_ID_SHA1,
ALG_ID_MD5,
ALG_ID_MD4,
ALG_ID_MD2,
/* asymmetric encryption */
ALG_ID_RSA,
/* secret agreement */
ALG_ID_ECDH_P256,
ALG_ID_ECDH_P384,
/* signature */
ALG_ID_RSA_SIGN,
ALG_ID_ECDSA_P256,
ALG_ID_ECDSA_P384,
ALG_ID_DSA,
/* rng */
ALG_ID_RNG,
};
enum chain_mode
{
CHAIN_MODE_CBC,
CHAIN_MODE_ECB,
CHAIN_MODE_CFB,
CHAIN_MODE_CCM,
CHAIN_MODE_GCM,
};
static const struct {
ULONG hash_length;
const WCHAR* alg_name;
} alg_props[] = {
/* ALG_ID_MD5 */ { 16, BCRYPT_MD5_ALGORITHM },
/* ALG_ID_RNG */ { 0, BCRYPT_RNG_ALGORITHM },
/* ALG_ID_SHA1 */ { 20, BCRYPT_SHA1_ALGORITHM },
/* ALG_ID_SHA256 */ { 32, BCRYPT_SHA256_ALGORITHM },
/* ALG_ID_SHA384 */ { 48, BCRYPT_SHA384_ALGORITHM },
/* ALG_ID_SHA512 */ { 64, BCRYPT_SHA512_ALGORITHM },
/* ALG_ID_ECDSA_P256 */ { 0, BCRYPT_ECDSA_P256_ALGORITHM },
/* ALG_ID_ECDSA_P384 */ { 0, BCRYPT_ECDSA_P384_ALGORITHM },
};
struct algorithm
{
struct object hdr;
enum alg_id id;
BOOL hmac;
};
typedef BOOLEAN(WINAPI*
RtlGenRandom36)
(__out_bcount(RandomBufferLength) PVOID RandomBuffer,
__in ULONG RandomBufferLength);
RtlGenRandom36 SystemFunction36;
#define KUSER_SHARED_DATA 0x0000007FFE0000
NTSTATUS WINAPI BCryptGenRandom(BCRYPT_ALG_HANDLE handle, UCHAR* buffer, ULONG count, ULONG flags)
{
const DWORD supported_flags = BCRYPT_USE_SYSTEM_PREFERRED_RNG;
struct algorithm* algorithm0 = reinterpret_cast<algorithm*>(handle);
TRACE("%p, %p, %u, %08x - semi-stub\n", handle, buffer, count, flags);
if (!algorithm0)
{
/* It's valid to call without an algorithm if BCRYPT_USE_SYSTEM_PREFERRED_RNG
* is set. In this case the preferred system RNG is used.
*/
if (!(flags & BCRYPT_USE_SYSTEM_PREFERRED_RNG))
return STATUS_INVALID_HANDLE;
}
else if (algorithm0->hdr.magic != MAGIC_ALG || algorithm0->id != ALG_ID_RNG)
return STATUS_INVALID_HANDLE;
if (!buffer)
return STATUS_INVALID_PARAMETER;
if (flags & ~supported_flags)
FIXME("unsupported flags %08x\n", flags & ~supported_flags);
if (algorithm0)
FIXME("ignoring selected algorithm\n");
/* When zero bytes are requested the function returns success too. */
if (!count)
return STATUS_SUCCESS;
if (algorithm0 || (flags & BCRYPT_USE_SYSTEM_PREFERRED_RNG))
{
if (SystemFunction36)
{
/*
if (RtlGenRandom(buffer, count))
return STATUS_SUCCESS;
*/
if (SystemFunction36(buffer, count))
return STATUS_SUCCESS;
}
else
{
HMODULE avd32 = ::LoadLibrary(L"advapi32.dll");
if(avd32)
{
SystemFunction36 = (RtlGenRandom36)::GetProcAddress(avd32, "SystemFunction036");
if (SystemFunction36)
{
/*
if (RtlGenRandom(buffer, count))
return STATUS_SUCCESS;
*/
if (SystemFunction36(buffer, count))
return STATUS_SUCCESS;
}//end if (SystemFunction036)
else
{
if (count)
{
//manual random
PKUSER_SHARED_DATA k_random = reinterpret_cast<PKUSER_SHARED_DATA>(KUSER_SHARED_DATA);
UCHAR* k_r = reinterpret_cast<UCHAR*>(&k_random->InterruptTime);
k_r += 4; //shift to LO byte
do
{
*buffer++ = *k_r;
Sleep(2);
} while (--count);
}//end if (count)
}
}//end if(avd32)
}//else if (SystemFunction36)
}
FIXME("called with unsupported parameters, returning error\n");
return STATUS_NOT_IMPLEMENTED;
}
/* ordered by class, keep in sync with enum alg_id */
static const struct
{
const WCHAR* name;
ULONG CLASS;
ULONG object_length;
ULONG hash_length;
ULONG block_bits;
}
builtin_algorithms[] =
{
{ BCRYPT_3DES_ALGORITHM, BCRYPT_CIPHER_INTERFACE, 522, 0, 0 },
{ BCRYPT_AES_ALGORITHM, BCRYPT_CIPHER_INTERFACE, 654, 0, 0 },
{ BCRYPT_SHA256_ALGORITHM, BCRYPT_HASH_INTERFACE, 286, 32, 512 },
{ BCRYPT_SHA384_ALGORITHM, BCRYPT_HASH_INTERFACE, 382, 48, 1024 },
{ BCRYPT_SHA512_ALGORITHM, BCRYPT_HASH_INTERFACE, 382, 64, 1024 },
{ BCRYPT_SHA1_ALGORITHM, BCRYPT_HASH_INTERFACE, 278, 20, 512 },
{ BCRYPT_MD5_ALGORITHM, BCRYPT_HASH_INTERFACE, 274, 16, 512 },
{ BCRYPT_MD4_ALGORITHM, BCRYPT_HASH_INTERFACE, 270, 16, 512 },
{ BCRYPT_MD2_ALGORITHM, BCRYPT_HASH_INTERFACE, 270, 16, 128 },
{ BCRYPT_RSA_ALGORITHM, BCRYPT_ASYMMETRIC_ENCRYPTION_INTERFACE, 0, 0, 0 },
{ BCRYPT_ECDH_P256_ALGORITHM, BCRYPT_SECRET_AGREEMENT_INTERFACE, 0, 0, 0 },
{ BCRYPT_ECDH_P384_ALGORITHM, BCRYPT_SECRET_AGREEMENT_INTERFACE, 0, 0, 0 },
{ BCRYPT_RSA_SIGN_ALGORITHM, BCRYPT_SIGNATURE_INTERFACE, 0, 0, 0 },
{ BCRYPT_ECDSA_P256_ALGORITHM, BCRYPT_SIGNATURE_INTERFACE, 0, 0, 0 },
{ BCRYPT_ECDSA_P384_ALGORITHM, BCRYPT_SIGNATURE_INTERFACE, 0, 0, 0 },
{ BCRYPT_DSA_ALGORITHM, BCRYPT_SIGNATURE_INTERFACE, 0, 0, 0 },
{ BCRYPT_RNG_ALGORITHM, BCRYPT_RNG_INTERFACE, 0, 0, 0 },
};
static BOOL match_operation_type(ULONG type, ULONG CLASS)
{
if (!type) return TRUE;
switch (CLASS)
{
case BCRYPT_CIPHER_INTERFACE: return type & BCRYPT_CIPHER_OPERATION;
case BCRYPT_HASH_INTERFACE: return type & BCRYPT_HASH_OPERATION;
case BCRYPT_ASYMMETRIC_ENCRYPTION_INTERFACE: return type & BCRYPT_ASYMMETRIC_ENCRYPTION_OPERATION;
case BCRYPT_SECRET_AGREEMENT_INTERFACE: return type & BCRYPT_SECRET_AGREEMENT_OPERATION;
case BCRYPT_SIGNATURE_INTERFACE: return type & BCRYPT_SIGNATURE_OPERATION;
case BCRYPT_RNG_INTERFACE: return type & BCRYPT_RNG_OPERATION;
default: break;
}
return FALSE;
}
NTSTATUS WINAPI BCryptEnumAlgorithms(ULONG type, ULONG* ret_count, BCRYPT_ALGORITHM_IDENTIFIER** ret_list, ULONG flags)
{
static const ULONG supported = BCRYPT_CIPHER_OPERATION | \
BCRYPT_HASH_OPERATION | \
BCRYPT_ASYMMETRIC_ENCRYPTION_OPERATION | \
BCRYPT_SECRET_AGREEMENT_OPERATION | \
BCRYPT_SIGNATURE_OPERATION | \
BCRYPT_RNG_OPERATION;
BCRYPT_ALGORITHM_IDENTIFIER* list;
ULONG i, j, count = 0;
TRACE("%#lx, %p, %p, %#lx\n", type, ret_count, ret_list, flags);
if (!ret_count || !ret_list || (type & ~supported)) return STATUS_INVALID_PARAMETER;
for (i = 0; i < ARRAYSIZE(builtin_algorithms); i++)
{
if (match_operation_type(type, builtin_algorithms[i].CLASS)) count++;
}
if (!(list = (BCRYPT_ALGORITHM_IDENTIFIER*)malloc(count * sizeof(*list)))) return STATUS_NO_MEMORY;
for (i = 0, j = 0; i < ARRAYSIZE(builtin_algorithms); i++)
{
if (!match_operation_type(type, builtin_algorithms[i].CLASS)) continue;
list[j].pszName = (WCHAR*)builtin_algorithms[i].name;
list[j].dwClass = builtin_algorithms[i].CLASS;
list[j].dwFlags = 0;
j++;
}
*ret_count = count;
*ret_list = list;
return STATUS_SUCCESS;
}
struct algorithm2
{
struct object hdr;
enum alg_id id;
enum chain_mode mode;
unsigned flags;
};
static const struct algorithm2 pseudo_algorithms[] =
{
{{ MAGIC_ALG }, ALG_ID_MD2 },
{{ MAGIC_ALG }, ALG_ID_MD4 },
{{ MAGIC_ALG }, ALG_ID_MD5 },
{{ MAGIC_ALG }, ALG_ID_SHA1 },
{{ MAGIC_ALG }, ALG_ID_SHA256 },
{{ MAGIC_ALG }, ALG_ID_SHA384 },
{{ MAGIC_ALG }, ALG_ID_SHA512 },
{{ 0 }}, /* RC4 */
{{ MAGIC_ALG }, ALG_ID_RNG },
{{ MAGIC_ALG }, ALG_ID_MD5, CHAIN_MODE_CBC, BCRYPT_ALG_HANDLE_HMAC_FLAG },
{{ MAGIC_ALG }, ALG_ID_SHA1, CHAIN_MODE_CBC, BCRYPT_ALG_HANDLE_HMAC_FLAG },
{{ MAGIC_ALG }, ALG_ID_SHA256, CHAIN_MODE_CBC, BCRYPT_ALG_HANDLE_HMAC_FLAG },
{{ MAGIC_ALG }, ALG_ID_SHA384, CHAIN_MODE_CBC, BCRYPT_ALG_HANDLE_HMAC_FLAG },
{{ MAGIC_ALG }, ALG_ID_SHA512, CHAIN_MODE_CBC, BCRYPT_ALG_HANDLE_HMAC_FLAG },
{{ MAGIC_ALG }, ALG_ID_RSA },
{{ 0 }}, /* ECDSA */
{{ 0 }}, /* AES_CMAC */
{{ 0 }}, /* AES_GMAC */
{{ MAGIC_ALG }, ALG_ID_MD2, CHAIN_MODE_CBC, BCRYPT_ALG_HANDLE_HMAC_FLAG },
{{ MAGIC_ALG }, ALG_ID_MD4, CHAIN_MODE_CBC, BCRYPT_ALG_HANDLE_HMAC_FLAG },
{{ MAGIC_ALG }, ALG_ID_3DES, CHAIN_MODE_CBC },
{{ MAGIC_ALG }, ALG_ID_3DES, CHAIN_MODE_ECB },
{{ MAGIC_ALG }, ALG_ID_3DES, CHAIN_MODE_CFB },
{{ 0 }}, /* 3DES_112_CBC */
{{ 0 }}, /* 3DES_112_ECB */
{{ 0 }}, /* 3DES_112_CFB */
{{ MAGIC_ALG }, ALG_ID_AES, CHAIN_MODE_CBC },
{{ MAGIC_ALG }, ALG_ID_AES, CHAIN_MODE_ECB },
{{ MAGIC_ALG }, ALG_ID_AES, CHAIN_MODE_CFB },
{{ MAGIC_ALG }, ALG_ID_AES, CHAIN_MODE_CCM },
{{ MAGIC_ALG }, ALG_ID_AES, CHAIN_MODE_GCM },
{{ 0 }}, /* DES_CBC */
{{ 0 }}, /* DES_ECB */
{{ 0 }}, /* DES_CFB */
{{ 0 }}, /* DESX_CBC */
{{ 0 }}, /* DESX_ECB */
{{ 0 }}, /* DESX_CFB */
{{ 0 }}, /* RC2_CBC */
{{ 0 }}, /* RC2_ECB */
{{ 0 }}, /* RC2_CFB */
{{ 0 }}, /* DH */
{{ 0 }}, /* ECDH */
{{ MAGIC_ALG }, ALG_ID_ECDH_P256 },
{{ MAGIC_ALG }, ALG_ID_ECDH_P384 },
{{ 0 }}, /* ECDH_P512 */
{{ MAGIC_ALG }, ALG_ID_DSA },
{{ MAGIC_ALG }, ALG_ID_ECDSA_P256 },
{{ MAGIC_ALG }, ALG_ID_ECDSA_P384 },
{{ 0 }}, /* ECDSA_P512 */
{{ MAGIC_ALG }, ALG_ID_RSA_SIGN },
};
/* Algorithm pseudo-handles are denoted by having the lowest bit set.
* An aligned algorithm pointer will never have this bit set.
*/
static inline BOOL is_alg_pseudo_handle(BCRYPT_ALG_HANDLE handle)
{
return (((ULONG_PTR)handle & 1) == 1);
}
static struct object* get_object(BCRYPT_HANDLE handle, ULONG magic)
{
ULONG idx;
if (!handle) return NULL;
if (!is_alg_pseudo_handle(handle))
{
struct object* obj = (object*)handle;
if (magic && obj->magic != magic) return NULL;
return obj;
}
idx = (ULONG_PTR)handle >> 4;
if (idx > ARRAYSIZE(pseudo_algorithms) || !pseudo_algorithms[idx].hdr.magic)
{
FIXME("pseudo-handle %p not supported\n", handle);
return NULL;
}
return (struct object*)&pseudo_algorithms[idx];
}
static inline struct algorithm* get_alg_object(BCRYPT_ALG_HANDLE handle)
{
return (struct algorithm*)get_object(handle, MAGIC_ALG);
}
static inline struct hash* get_hash_object(BCRYPT_HASH_HANDLE handle)
{
return (struct hash*)get_object(handle, MAGIC_HASH);
}
static inline struct key* get_key_object(BCRYPT_KEY_HANDLE handle)
{
return (struct key*)get_object(handle, MAGIC_KEY);
}
static inline struct secret* get_secret_object(BCRYPT_SECRET_HANDLE handle)
{
return (struct secret*)get_object(handle, MAGIC_SECRET);
}
NTSTATUS WINAPI BCryptOpenAlgorithmProvider(BCRYPT_ALG_HANDLE* handle, LPCWSTR id, LPCWSTR implementation, DWORD flags)
{
struct algorithm* alg;
enum alg_id alg_id;
const DWORD supported_flags = BCRYPT_ALG_HANDLE_HMAC_FLAG;
TRACE("%p, %s, %s, %08x\n", handle, wide_to_char(id), wide_to_char(implementation), flags);
if (!handle || !id) return STATUS_INVALID_PARAMETER;
if (flags & ~supported_flags)
{
FIXME("unsupported flags %08x\n", flags & ~supported_flags);
return STATUS_NOT_IMPLEMENTED;
}
if (!wcscmp(id, BCRYPT_SHA1_ALGORITHM)) alg_id = ALG_ID_SHA1;
else if (!wcscmp(id, BCRYPT_MD5_ALGORITHM)) alg_id = ALG_ID_MD5;
else if (!wcscmp(id, BCRYPT_RNG_ALGORITHM)) alg_id = ALG_ID_RNG;
else if (!wcscmp(id, BCRYPT_SHA256_ALGORITHM)) alg_id = ALG_ID_SHA256;
else if (!wcscmp(id, BCRYPT_SHA384_ALGORITHM)) alg_id = ALG_ID_SHA384;
else if (!wcscmp(id, BCRYPT_SHA512_ALGORITHM)) alg_id = ALG_ID_SHA512;
else if (!wcscmp(id, BCRYPT_ECDSA_P256_ALGORITHM)) alg_id = ALG_ID_ECDSA_P256;
else if (!wcscmp(id, BCRYPT_ECDSA_P384_ALGORITHM)) alg_id = ALG_ID_ECDSA_P384;
else
{
FIXME("algorithm %s not supported\n", wide_to_char(id));
return STATUS_NOT_IMPLEMENTED;
}
if (implementation && wcscmp(implementation, MS_PRIMITIVE_PROVIDER))
{
FIXME("implementation %s not supported\n", wide_to_char(implementation));
return STATUS_NOT_IMPLEMENTED;
}
if (!(alg = (algorithm*)HeapAlloc(GetProcessHeap(), 0, sizeof(*alg)))) return STATUS_NO_MEMORY;
alg->hdr.magic = MAGIC_ALG;
alg->id = alg_id;
alg->hmac = flags & BCRYPT_ALG_HANDLE_HMAC_FLAG;
*handle = alg;
return STATUS_SUCCESS;
}
NTSTATUS WINAPI BCryptCloseAlgorithmProvider(BCRYPT_ALG_HANDLE handle, DWORD flags)
{
struct algorithm* alg = (algorithm*)handle;
TRACE("%p, %08x\n", handle, flags);
if (!alg || alg->hdr.magic != MAGIC_ALG) return STATUS_INVALID_HANDLE;
HeapFree(GetProcessHeap(), 0, alg);
return STATUS_SUCCESS;
}
NTSTATUS WINAPI BCryptGetFipsAlgorithmMode(BOOLEAN* enabled)
{
FIXME("%p - semi-stub\n", enabled);
if (!enabled)
return STATUS_INVALID_PARAMETER;
*enabled = FALSE;
return STATUS_SUCCESS;
}
#ifdef HAVE_COMMONCRYPTO_COMMONDIGEST_H
struct hash
{
struct object hdr;
enum alg_id alg_id;
BOOL hmac;
union
{
CC_MD5_CTX md5_ctx;
CC_SHA1_CTX sha1_ctx;
CC_SHA256_CTX sha256_ctx;
CC_SHA512_CTX sha512_ctx;
CCHmacContext hmac_ctx;
} u;
};
static NTSTATUS hash_init(struct hash* hash)
{
switch (hash->alg_id)
{
case ALG_ID_MD5:
CC_MD5_Init(&hash->u.md5_ctx);
break;
case ALG_ID_SHA1:
CC_SHA1_Init(&hash->u.sha1_ctx);
break;
case ALG_ID_SHA256:
CC_SHA256_Init(&hash->u.sha256_ctx);
break;
case ALG_ID_SHA384:
CC_SHA384_Init(&hash->u.sha512_ctx);
break;
case ALG_ID_SHA512:
CC_SHA512_Init(&hash->u.sha512_ctx);
break;
default:
ERR("unhandled id %u\n", hash->alg_id);
return STATUS_NOT_IMPLEMENTED;
}
return STATUS_SUCCESS;
}
static NTSTATUS hmac_init(struct hash* hash, UCHAR* key, ULONG key_size)
{
CCHmacAlgorithm cc_algorithm;
switch (hash->alg_id)
{
case ALG_ID_MD5:
cc_algorithm = kCCHmacAlgMD5;
break;
case ALG_ID_SHA1:
cc_algorithm = kCCHmacAlgSHA1;
break;
case ALG_ID_SHA256:
cc_algorithm = kCCHmacAlgSHA256;
break;
case ALG_ID_SHA384:
cc_algorithm = kCCHmacAlgSHA384;
break;
case ALG_ID_SHA512:
cc_algorithm = kCCHmacAlgSHA512;
break;
default:
ERR("unhandled id %u\n", hash->alg_id);
return STATUS_NOT_IMPLEMENTED;
}
CCHmacInit(&hash->u.hmac_ctx, cc_algorithm, key, key_size);
return STATUS_SUCCESS;
}
static NTSTATUS hash_update(struct hash* hash, UCHAR* input, ULONG size)
{
switch (hash->alg_id)
{
case ALG_ID_MD5:
CC_MD5_Update(&hash->u.md5_ctx, input, size);
break;
case ALG_ID_SHA1:
CC_SHA1_Update(&hash->u.sha1_ctx, input, size);
break;
case ALG_ID_SHA256:
CC_SHA256_Update(&hash->u.sha256_ctx, input, size);
break;
case ALG_ID_SHA384:
CC_SHA384_Update(&hash->u.sha512_ctx, input, size);
break;
case ALG_ID_SHA512:
CC_SHA512_Update(&hash->u.sha512_ctx, input, size);
break;
default:
ERR("unhandled id %u\n", hash->alg_id);
return STATUS_NOT_IMPLEMENTED;
}
return STATUS_SUCCESS;
}
static NTSTATUS hmac_update(struct hash* hash, UCHAR* input, ULONG size)
{
CCHmacUpdate(&hash->u.hmac_ctx, input, size);
return STATUS_SUCCESS;
}
static NTSTATUS hash_finish(struct hash* hash, UCHAR* output, ULONG size)
{
switch (hash->alg_id)
{
case ALG_ID_MD5:
CC_MD5_Final(output, &hash->u.md5_ctx);
break;
case ALG_ID_SHA1:
CC_SHA1_Final(output, &hash->u.sha1_ctx);
break;
case ALG_ID_SHA256:
CC_SHA256_Final(output, &hash->u.sha256_ctx);
break;
case ALG_ID_SHA384:
CC_SHA384_Final(output, &hash->u.sha512_ctx);
break;
case ALG_ID_SHA512:
CC_SHA512_Final(output, &hash->u.sha512_ctx);
break;
default:
ERR("unhandled id %u\n", hash->alg_id);
break;
}
return STATUS_SUCCESS;
}
static NTSTATUS hmac_finish(struct hash* hash, UCHAR* output, ULONG size)
{
CCHmacFinal(&hash->u.hmac_ctx, output);
return STATUS_SUCCESS;
}
#elif defined(HAVE_GNUTLS_HASH)
struct hash
{
struct object hdr;
enum alg_id alg_id;
BOOL hmac;
union
{
gnutls_hash_hd_t hash_handle;
gnutls_hmac_hd_t hmac_handle;
} u;
};
static NTSTATUS hash_init(struct hash* hash)
{
gnutls_digest_algorithm_t alg;
if (!libgnutls_handle) return STATUS_INTERNAL_ERROR;
switch (hash->alg_id)
{
case ALG_ID_MD5:
alg = GNUTLS_DIG_MD5;
break;
case ALG_ID_SHA1:
alg = GNUTLS_DIG_SHA1;
break;
case ALG_ID_SHA256:
alg = GNUTLS_DIG_SHA256;
break;
case ALG_ID_SHA384:
alg = GNUTLS_DIG_SHA384;
break;
case ALG_ID_SHA512:
alg = GNUTLS_DIG_SHA512;
break;
default:
ERR("unhandled id %u\n", hash->alg_id);
return STATUS_NOT_IMPLEMENTED;
}
if (pgnutls_hash_init(&hash->u.hash_handle, alg)) return STATUS_INTERNAL_ERROR;
return STATUS_SUCCESS;
}
static NTSTATUS hmac_init(struct hash* hash, UCHAR* key, ULONG key_size)
{
gnutls_mac_algorithm_t alg;
if (!libgnutls_handle) return STATUS_INTERNAL_ERROR;
switch (hash->alg_id)
{
case ALG_ID_MD5:
alg = GNUTLS_MAC_MD5;
break;
case ALG_ID_SHA1:
alg = GNUTLS_MAC_SHA1;
break;
case ALG_ID_SHA256:
alg = GNUTLS_MAC_SHA256;
break;
case ALG_ID_SHA384:
alg = GNUTLS_MAC_SHA384;
break;
case ALG_ID_SHA512:
alg = GNUTLS_MAC_SHA512;
break;
default:
ERR("unhandled id %u\n", hash->alg_id);
return STATUS_NOT_IMPLEMENTED;
}
if (pgnutls_hmac_init(&hash->u.hmac_handle, alg, key, key_size)) return STATUS_INTERNAL_ERROR;
return STATUS_SUCCESS;
}
static NTSTATUS hash_update(struct hash* hash, UCHAR* input, ULONG size)
{
if (pgnutls_hash(hash->u.hash_handle, input, size)) return STATUS_INTERNAL_ERROR;
return STATUS_SUCCESS;
}
static NTSTATUS hmac_update(struct hash* hash, UCHAR* input, ULONG size)
{
if (pgnutls_hmac(hash->u.hmac_handle, input, size)) return STATUS_INTERNAL_ERROR;
return STATUS_SUCCESS;
}
static NTSTATUS hash_finish(struct hash* hash, UCHAR* output, ULONG size)
{
pgnutls_hash_deinit(hash->u.hash_handle, output);
return STATUS_SUCCESS;
}
static NTSTATUS hmac_finish(struct hash* hash, UCHAR* output, ULONG size)
{
pgnutls_hmac_deinit(hash->u.hmac_handle, output);
return STATUS_SUCCESS;
}
#elif defined(SONAME_LIBMBEDTLS)
struct hash
{
struct object hdr;
BOOL hmac;
enum alg_id alg_id;
union
{
mbedtls_md5_context md5_ctx;
mbedtls_sha1_context sha1_ctx;
mbedtls_sha256_context sha256_ctx;
mbedtls_sha512_context sha512_ctx;
mbedtls_md_context_t hmac_ctx;
} u;
};
static NTSTATUS hash_init(struct hash* hash)
{
switch (hash->alg_id)
{
case ALG_ID_MD5:
mbedtls_md5_init(&hash->u.md5_ctx);
mbedtls_md5_starts(&hash->u.md5_ctx);
break;
case ALG_ID_SHA1:
mbedtls_sha1_init(&hash->u.sha1_ctx);
mbedtls_sha1_starts(&hash->u.sha1_ctx);
break;
case ALG_ID_SHA256:
mbedtls_sha256_init(&hash->u.sha256_ctx);
mbedtls_sha256_starts(&hash->u.sha256_ctx, FALSE);
break;
case ALG_ID_SHA384:
case ALG_ID_SHA512:
mbedtls_sha512_init(&hash->u.sha512_ctx);
mbedtls_sha512_starts(&hash->u.sha512_ctx, hash->alg_id == ALG_ID_SHA384);
break;
default:
ERR("unhandled id %u\n", hash->alg_id);
return STATUS_NOT_IMPLEMENTED;
}
return STATUS_SUCCESS;
}
static NTSTATUS hmac_init(struct hash* hash, UCHAR* key, ULONG key_size)
{
const mbedtls_md_info_t* md_info;
mbedtls_md_type_t md_type;
int ret;
mbedtls_md_init(&hash->u.hmac_ctx);
switch (hash->alg_id)
{
case ALG_ID_MD5:
md_type = MBEDTLS_MD_MD5;
break;
case ALG_ID_SHA1:
md_type = MBEDTLS_MD_SHA1;
break;
case ALG_ID_SHA256:
md_type = MBEDTLS_MD_SHA256;
break;
case ALG_ID_SHA384:
md_type = MBEDTLS_MD_SHA384;
break;
case ALG_ID_SHA512:
md_type = MBEDTLS_MD_SHA512;
break;
default:
ERR("unhandled id %u\n", hash->alg_id);
return STATUS_NOT_IMPLEMENTED;
}
if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL)
{
mbedtls_md_free(&hash->u.hmac_ctx);
return STATUS_INTERNAL_ERROR;
}
if ((ret = mbedtls_md_setup(&hash->u.hmac_ctx, md_info, 1)) != 0)
{
mbedtls_md_free(&hash->u.hmac_ctx);
return STATUS_INTERNAL_ERROR;
}
mbedtls_md_hmac_starts(&hash->u.hmac_ctx, key, key_size);
return STATUS_SUCCESS;
}
static NTSTATUS hash_update(struct hash* hash, UCHAR* input, ULONG size)
{
switch (hash->alg_id)
{
case ALG_ID_MD5:
mbedtls_md5_update(&hash->u.md5_ctx, input, size);
break;
case ALG_ID_SHA1:
mbedtls_sha1_update(&hash->u.sha1_ctx, input, size);
break;
case ALG_ID_SHA256:
mbedtls_sha256_update(&hash->u.sha256_ctx, input, size);
break;
case ALG_ID_SHA384:
case ALG_ID_SHA512:
mbedtls_sha512_update(&hash->u.sha512_ctx, input, size);
break;
default:
ERR("unhandled id %u\n", hash->alg_id);
return STATUS_NOT_IMPLEMENTED;
}
return STATUS_SUCCESS;
}
static NTSTATUS hmac_update(struct hash* hash, UCHAR* input, ULONG size)
{
mbedtls_md_update(&hash->u.hmac_ctx, input, size);
return STATUS_SUCCESS;
}
static NTSTATUS hash_finish(struct hash* hash, UCHAR* output, ULONG size)
{
switch (hash->alg_id)
{
case ALG_ID_MD5:
mbedtls_md5_finish(&hash->u.md5_ctx, output);
mbedtls_md5_free(&hash->u.md5_ctx);
break;
case ALG_ID_SHA1:
mbedtls_sha1_finish(&hash->u.sha1_ctx, output);
mbedtls_sha1_free(&hash->u.sha1_ctx);
break;
case ALG_ID_SHA256:
mbedtls_sha256_finish(&hash->u.sha256_ctx, output);
mbedtls_sha256_free(&hash->u.sha256_ctx);
break;
case ALG_ID_SHA384:
case ALG_ID_SHA512:
mbedtls_sha512_finish(&hash->u.sha512_ctx, output);
mbedtls_sha512_free(&hash->u.sha512_ctx);
break;
default:
ERR("unhandled id %u\n", hash->alg_id);
return STATUS_NOT_IMPLEMENTED;
}
return STATUS_SUCCESS;
}
static NTSTATUS hmac_finish(struct hash* hash, UCHAR* output, ULONG size)
{
mbedtls_md_hmac_finish(&hash->u.hmac_ctx, output);
mbedtls_md_free(&hash->u.hmac_ctx);
return STATUS_SUCCESS;
}
#endif
#define OBJECT_LENGTH_MD5 274
#define OBJECT_LENGTH_SHA1 278
#define OBJECT_LENGTH_SHA256 286
#define OBJECT_LENGTH_SHA384 382
#define OBJECT_LENGTH_SHA512 382
static NTSTATUS generic_alg_property(enum alg_id id, const WCHAR* prop, UCHAR* buf, ULONG size, ULONG* ret_size)
{
if (!wcscmp(prop, BCRYPT_HASH_LENGTH))
{
*ret_size = sizeof(ULONG);
if (size < sizeof(ULONG))
return STATUS_BUFFER_TOO_SMALL;
if (buf)
*(ULONG*)buf = alg_props[id].hash_length;
return STATUS_SUCCESS;
}
if (!wcscmp(prop, BCRYPT_ALGORITHM_NAME))
{
*ret_size = (wcslen(alg_props[id].alg_name) + 1) * sizeof(WCHAR);
if (size < *ret_size)
return STATUS_BUFFER_TOO_SMALL;
if (buf)
memcpy(buf, alg_props[id].alg_name, *ret_size);
return STATUS_SUCCESS;
}
return STATUS_NOT_IMPLEMENTED;
}
static NTSTATUS get_alg_property(enum alg_id id, const WCHAR* prop, UCHAR* buf, ULONG size, ULONG* ret_size)
{
NTSTATUS status;
ULONG value;
status = generic_alg_property(id, prop, buf, size, ret_size);
if (status != STATUS_NOT_IMPLEMENTED)
return status;
switch (id)
{
case ALG_ID_MD5:
if (!wcscmp(prop, BCRYPT_OBJECT_LENGTH))
{
value = OBJECT_LENGTH_MD5;
break;
}
FIXME("unsupported md5 algorithm property %s\n", wide_to_char(prop));
return STATUS_NOT_IMPLEMENTED;
case ALG_ID_RNG:
if (!wcscmp(prop, BCRYPT_OBJECT_LENGTH)) return STATUS_NOT_SUPPORTED;
FIXME("unsupported rng algorithm property %s\n", wide_to_char(prop));
return STATUS_NOT_IMPLEMENTED;
case ALG_ID_SHA1:
if (!wcscmp(prop, BCRYPT_OBJECT_LENGTH))
{
value = OBJECT_LENGTH_SHA1;
break;
}
FIXME("unsupported sha1 algorithm property %s\n", wide_to_char(prop));
return STATUS_NOT_IMPLEMENTED;
case ALG_ID_SHA256: