-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61985da
commit f72e51b
Showing
10 changed files
with
220 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,88 @@ | ||
#include "signature.h" | ||
#include "hash.h" | ||
|
||
|
||
NTSTATUS WINAPI EcdsaSignHash(_In_reads_bytes_(PrivateKeyLen) PUCHAR PrivateKey, | ||
_In_ ULONG PrivateKeyLen, | ||
_In_reads_bytes_(DataSize) PUCHAR Data, | ||
_In_ ULONG DataSize, | ||
_Out_writes_bytes_all_(*SignSize) PUCHAR * Sign, | ||
_In_ ULONG * SignSize | ||
) | ||
{ | ||
PUCHAR Hash = nullptr; | ||
ULONG HashSize = 0; | ||
BOOL ret = CngHashData(BCRYPT_SHA1_ALGORITHM, Data, DataSize, &Hash, &HashSize); | ||
ASSERT(ret); | ||
|
||
NTSTATUS status = STATUS_UNSUCCESSFUL; | ||
BCRYPT_ALG_HANDLE hSignAlg = NULL; | ||
status = BCryptOpenAlgorithmProvider(&hSignAlg, BCRYPT_ECDSA_P256_ALGORITHM, NULL, 0); | ||
ASSERT(NT_SUCCESS(status)); | ||
|
||
BCRYPT_KEY_HANDLE hPrivateKey = NULL; | ||
status = BCryptImportKeyPair(hSignAlg, | ||
NULL, | ||
BCRYPT_ECCPRIVATE_BLOB, | ||
&hPrivateKey, | ||
PrivateKey, | ||
PrivateKeyLen, | ||
BCRYPT_NO_KEY_VALIDATION); | ||
ASSERT(NT_SUCCESS(status)); | ||
|
||
status = BCryptSignHash(hPrivateKey, NULL, Hash, HashSize, NULL, 0, SignSize, 0); | ||
ASSERT(NT_SUCCESS(status)); | ||
|
||
*Sign = (PUCHAR)ExAllocatePoolWithTag(NonPagedPool, *SignSize, TAG); | ||
ASSERT(*Sign); | ||
|
||
ULONG Result = 0; | ||
status = BCryptSignHash(hPrivateKey, NULL, Hash, HashSize, *Sign, *SignSize, &Result, 0); | ||
ASSERT(NT_SUCCESS(status)); | ||
|
||
BCryptCloseAlgorithmProvider(hSignAlg, 0); | ||
BCryptDestroyKey(hPrivateKey); | ||
|
||
return status; | ||
} | ||
|
||
|
||
BOOL WINAPI EcdsaVerifySignature(_In_reads_bytes_(PublicKeyLen) PUCHAR PublicKey, | ||
_In_ ULONG PublicKeyLen, | ||
_In_reads_bytes_(DataSize) PUCHAR Data, | ||
_In_ ULONG DataSize, | ||
_Out_writes_bytes_all_(SignSize) PUCHAR Sign, | ||
_In_ ULONG SignSize | ||
) | ||
{ | ||
PUCHAR Hash = nullptr; | ||
ULONG HashSize = 0; | ||
BOOL IsVerify = FALSE; | ||
BOOL ret = CngHashData(BCRYPT_SHA1_ALGORITHM, Data, DataSize, &Hash, &HashSize); | ||
ASSERT(ret); | ||
|
||
NTSTATUS status = STATUS_UNSUCCESSFUL; | ||
BCRYPT_ALG_HANDLE hSignAlg = NULL; | ||
status = BCryptOpenAlgorithmProvider(&hSignAlg, BCRYPT_ECDSA_P256_ALGORITHM, NULL, 0); | ||
ASSERT(NT_SUCCESS(status)); | ||
|
||
BCRYPT_KEY_HANDLE hPublicKey = NULL; | ||
status = BCryptImportKeyPair(hSignAlg, | ||
NULL, | ||
BCRYPT_ECCPUBLIC_BLOB, | ||
&hPublicKey, | ||
PublicKey, | ||
PublicKeyLen, | ||
BCRYPT_NO_KEY_VALIDATION); | ||
ASSERT(NT_SUCCESS(status)); | ||
|
||
status = BCryptVerifySignature(hPublicKey, NULL, Hash, HashSize, Sign, SignSize, 0); | ||
if (NT_SUCCESS(status)) { | ||
IsVerify = TRUE; | ||
} | ||
|
||
BCryptCloseAlgorithmProvider(hSignAlg, 0); | ||
BCryptDestroyKey(hPublicKey); | ||
|
||
return IsVerify; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "pch.h" | ||
#include "signature.h" | ||
|
||
|
||
void TestEcdsaSignature() | ||
/* | ||
功能:签名和验签的测试。 | ||
注意: | ||
1.驱动竟然没有BCRYPT_DSA_ALGORITHM的算法,返回STATUS_NOT_FOUND。 | ||
2.BCRYPT_ECDSA_P256_ALGORITHM倒是在驱动下支持。 | ||
3. | ||
心得: | ||
1.签名的哈希不能是BCRYPT_SHA256_ALGORITHM,只能是BCRYPT_SHA1_ALGORITHM。 | ||
2.签名的算法不能是BCRYPT_RSA_ALGORITHM和BCRYPT_RSA_SIGN_ALGORITHM。 | ||
3.签名算法测试成功的有BCRYPT_DSA_ALGORITHM(3072和2048失败)和BCRYPT_ECDSA_P256_ALGORITHM。 | ||
参考: | ||
1.https://docs.microsoft.com/zh-cn/windows/win32/seccng/signing-data-with-cng | ||
2.Windows-classic-samples\Samples\Security\SignHashAndVerifySignature | ||
3.ProcessHacker | ||
*/ | ||
{ | ||
BCRYPT_ALG_HANDLE hAlgorithm = nullptr; | ||
LPCWSTR AlgId = BCRYPT_ECDSA_P256_ALGORITHM; | ||
LPCWSTR Implementation = nullptr; | ||
ULONG Flags = 0; | ||
NTSTATUS NtStatus = BCryptOpenAlgorithmProvider(&hAlgorithm, AlgId, Implementation, Flags); | ||
if (STATUS_SUCCESS != NtStatus) { | ||
PrintEx(DPFLTR_DEFAULT_ID, DPFLTR_ERROR_LEVEL, "Status:%#x", NtStatus); | ||
return; | ||
} | ||
|
||
BCRYPT_KEY_HANDLE hKey = nullptr; | ||
ULONG Length = 256; | ||
NtStatus = BCryptGenerateKeyPair(hAlgorithm, &hKey, Length, 0); | ||
if (STATUS_SUCCESS != NtStatus) { | ||
BCryptCloseAlgorithmProvider(hAlgorithm, 0); | ||
return; | ||
} | ||
|
||
//NtStatus = BCryptSetProperty | ||
|
||
NtStatus = BCryptFinalizeKeyPair(hKey, 0);//这个还是很费时的。 | ||
ASSERT(STATUS_SUCCESS == NtStatus); | ||
|
||
////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
ULONG PrivateKeyLen = 0; | ||
NtStatus = BCryptExportKey(hKey, NULL, BCRYPT_ECCPRIVATE_BLOB, NULL, 0, &PrivateKeyLen, 0); | ||
ASSERT(STATUS_SUCCESS == NtStatus); | ||
|
||
PBCRYPT_DSA_KEY_BLOB PrivateKey = (PBCRYPT_DSA_KEY_BLOB)ExAllocatePoolWithTag(NonPagedPool, PrivateKeyLen, TAG); | ||
ASSERT(PrivateKey); | ||
|
||
NtStatus = BCryptExportKey(hKey, NULL, BCRYPT_ECCPRIVATE_BLOB, (PUCHAR)PrivateKey, PrivateKeyLen, &PrivateKeyLen, 0); | ||
ASSERT(STATUS_SUCCESS == NtStatus); | ||
|
||
////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
ULONG PublicKeyLen = 0; | ||
NtStatus = BCryptExportKey(hKey, NULL, BCRYPT_ECCPUBLIC_BLOB, NULL, 0, &PublicKeyLen, 0); | ||
ASSERT(STATUS_SUCCESS == NtStatus); | ||
|
||
PBCRYPT_DSA_KEY_BLOB PublicKey = (PBCRYPT_DSA_KEY_BLOB)ExAllocatePoolWithTag(NonPagedPool, PublicKeyLen, TAG); | ||
ASSERT(PublicKey); | ||
|
||
NtStatus = BCryptExportKey(hKey, NULL, BCRYPT_ECCPUBLIC_BLOB, (PUCHAR)PublicKey, PublicKeyLen, &PublicKeyLen, 0); | ||
ASSERT(STATUS_SUCCESS == NtStatus); | ||
|
||
////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
const char * Data = "test"; | ||
ULONG DataSize = (ULONG)strlen(Data); | ||
|
||
PUCHAR Sign = nullptr; | ||
ULONG SignSize = 0; | ||
|
||
EcdsaSignHash((PUCHAR)PrivateKey, PrivateKeyLen, (PUCHAR)Data, DataSize, &Sign, &SignSize); | ||
|
||
EcdsaVerifySignature((PUCHAR)PublicKey, PublicKeyLen, (PUCHAR)Data, DataSize, Sign, SignSize); | ||
|
||
ExFreePoolWithTag(Sign, TAG); | ||
|
||
////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
ExFreePoolWithTag(PublicKey, TAG); | ||
ExFreePoolWithTag(PrivateKey, TAG); | ||
|
||
NtStatus = BCryptDestroyKey(hKey); | ||
NtStatus = BCryptCloseAlgorithmProvider(hAlgorithm, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
class signature | ||
{ | ||
|
||
}; | ||
|
||
void TestEcdsaSignature(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters