1
0
mirror of https://github.com/veracrypt/VeraCrypt.git synced 2025-11-12 11:28:26 -06:00

Initial support of SM4 cipher for normal volumes

This commit is contained in:
Mounir IDRASSI
2025-05-04 02:27:05 +09:00
parent 798985bf25
commit 7924f06e39
97 changed files with 2596 additions and 368 deletions

View File

@@ -69,6 +69,7 @@ static Cipher Ciphers[] =
{ TWOFISH, L"Twofish", 16, 32, TWOFISH_KS },
{ CAMELLIA, L"Camellia", 16, 32, CAMELLIA_KS },
{ KUZNYECHIK, L"Kuznyechik",16, 32, KUZNYECHIK_KS },
{ SM4, L"SM4", 16, 16, SM4_KS },
#endif
#endif
{ 0, 0, 0, 0, 0 }
@@ -89,6 +90,7 @@ static EncryptionAlgorithm EncryptionAlgorithms[] =
{ { TWOFISH, 0 }, { XTS, 0 }, 1, 1 },
{ { CAMELLIA, 0 }, { XTS, 0 }, 1, 1 },
{ { KUZNYECHIK, 0 }, { XTS, 0 }, 0, 1 },
{ { SM4, 0 }, { XTS, 0 }, 0, 1 },
{ { TWOFISH, AES, 0 }, { XTS, 0 }, 1, 1 },
{ { SERPENT, TWOFISH, AES, 0 }, { XTS, 0 }, 1, 1 },
{ { AES, SERPENT, 0 }, { XTS, 0 }, 1, 1 },
@@ -96,9 +98,13 @@ static EncryptionAlgorithm EncryptionAlgorithms[] =
{ { SERPENT, TWOFISH, 0 }, { XTS, 0 }, 1, 1 },
{ { KUZNYECHIK, CAMELLIA, 0 }, { XTS, 0 }, 0, 1 },
{ { TWOFISH, KUZNYECHIK, 0 }, { XTS, 0 }, 0, 1 },
{ { SM4, KUZNYECHIK, 0 }, { XTS, 0 }, 0, 1 },
{ { SM4, SERPENT, 0 }, { XTS, 0 }, 0, 1 },
{ { TWOFISH, SM4, 0 }, { XTS, 0 }, 0, 1 },
{ { SERPENT, CAMELLIA, 0 }, { XTS, 0 }, 0, 1 },
{ { AES, KUZNYECHIK, 0 }, { XTS, 0 }, 0, 1 },
{ { CAMELLIA, SERPENT, KUZNYECHIK, 0 }, { XTS, 0 }, 0, 1 },
{ { SM4, SERPENT, TWOFISH, 0 }, { XTS, 0 }, 0, 1 },
#endif
{ { 0, 0 }, { 0, 0}, 0, 0 } // Must be all-zero
@@ -177,6 +183,9 @@ int CipherInit (int cipher, unsigned char *key, unsigned __int8 *ks)
case KUZNYECHIK:
kuznyechik_set_key(key, (kuznyechik_kds*)ks);
break;
case SM4:
sm4_set_key(key, (sm4_kds*)ks);
break;
#endif // !defined(TC_WINDOWS_BOOT)
#endif
@@ -209,6 +218,7 @@ void EncipherBlock(int cipher, void *data, void *ks)
#endif
#if !defined(TC_WINDOWS_BOOT)
case KUZNYECHIK: kuznyechik_encrypt_block(data, data, ks); break;
case SM4: sm4_encrypt_block(data, data, ks); break;
#endif // !defined(TC_WINDOWS_BOOT)
#endif
default: TC_THROW_FATAL_EXCEPTION; // Unknown/wrong ID
@@ -260,6 +270,12 @@ void EncipherBlocks (int cipher, void *dataPtr, void *ks, size_t blockCount)
{
kuznyechik_encrypt_blocks (data, data, blockCount, ks);
}
else if (cipher == SM4
&& HasSSE41() && HasAESNI()
)
{
sm4_encrypt_blocks(data, data, blockCount, ks);
}
#endif
#endif
else
@@ -287,6 +303,7 @@ void DecipherBlock(int cipher, void *data, void *ks)
#endif
#if !defined(TC_WINDOWS_BOOT)
case KUZNYECHIK: kuznyechik_decrypt_block(data, data, ks); break;
case SM4: sm4_decrypt_block(data, data, ks); break;
#endif // !defined(TC_WINDOWS_BOOT)
#endif
@@ -352,6 +369,12 @@ void DecipherBlocks (int cipher, void *dataPtr, void *ks, size_t blockCount)
{
kuznyechik_decrypt_blocks (data, data, blockCount, ks);
}
else if (cipher == SM4
&& HasSSE41() && HasAESNI()
)
{
sm4_decrypt_blocks(data, data, blockCount, ks);
}
#endif
#endif
else
@@ -422,6 +445,7 @@ BOOL CipherSupportsIntraDataUnitParallelization (int cipher)
#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE && !defined (_UEFI)
|| (cipher == SERPENT && HasSSE2())
|| (cipher == KUZNYECHIK && HasSSE2())
|| (cipher == SM4 && HasSSE41() && HasAESNI())
#endif
#if CRYPTOPP_BOOL_X64 && !defined(CRYPTOPP_DISABLE_ASM)
|| (cipher == TWOFISH)

View File

@@ -112,7 +112,8 @@ enum
SERPENT,
TWOFISH,
CAMELLIA,
KUZNYECHIK
KUZNYECHIK,
SM4
};
typedef struct
@@ -172,7 +173,7 @@ typedef struct
#ifdef TC_WINDOWS_BOOT
#define MAX_EXPANDED_KEY VC_MAX((AES_KS + SERPENT_KS + TWOFISH_KS), CAMELLIA_KS)
#else
#define MAX_EXPANDED_KEY VC_MAX(VC_MAX(VC_MAX((AES_KS + SERPENT_KS + TWOFISH_KS), CAMELLIA_KS + KUZNYECHIK_KS + SERPENT_KS), KUZNYECHIK_KS + TWOFISH_KS), AES_KS + KUZNYECHIK_KS)
#define MAX_EXPANDED_KEY VC_MAX(VC_MAX(VC_MAX(VC_MAX(VC_MAX((AES_KS + SERPENT_KS + TWOFISH_KS), CAMELLIA_KS + KUZNYECHIK_KS + SERPENT_KS), KUZNYECHIK_KS + TWOFISH_KS), AES_KS + KUZNYECHIK_KS), SM4_KS + SERPENT_KS + TWOFISH_KS), SM4_KS + KUZNYECHIK_KS)
#endif
#endif
@@ -206,6 +207,7 @@ typedef struct
# include "Streebog.h"
# include "kuznyechik.h"
# include "Camellia.h"
# include "sm4.h"
#if !defined (_UEFI)
# include "chachaRng.h"
# include "t1ha.h"

View File

@@ -7621,13 +7621,13 @@ CipherTestDialogProc (HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// Secondary key
if (GetWindowText(GetDlgItem(hwndDlg, IDC_SECONDARY_KEY), szTmp, ARRAYSIZE(szTmp)) != 64)
if (GetWindowText(GetDlgItem(hwndDlg, IDC_SECONDARY_KEY), szTmp, ARRAYSIZE(szTmp)) != ks * 2)
{
Warning ("TEST_INCORRECT_SECONDARY_KEY_SIZE", hwndDlg);
return 1;
}
for (n = 0; n < 64; n ++)
for (n = 0; n < ks; n ++)
{
wchar_t szTmp2[3], *ptr;
long x;
@@ -7804,18 +7804,30 @@ ResetCipherTest(HWND hwndDlg, int idTestCipher)
SendMessage(GetDlgItem(hwndDlg, IDC_TEST_BLOCK_NUMBER), CB_SETCURSEL, 0, 0);
SetWindowText(GetDlgItem(hwndDlg, IDC_SECONDARY_KEY), L"0000000000000000000000000000000000000000000000000000000000000000");
SetWindowText(GetDlgItem(hwndDlg, IDC_TEST_DATA_UNIT_NUMBER), L"0");
SetWindowText(GetDlgItem(hwndDlg, IDC_PLAINTEXT), L"0000000000000000");
SetWindowText(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), L"0000000000000000");
if (idTestCipher == AES || idTestCipher == SERPENT || idTestCipher == TWOFISH || idTestCipher == CAMELLIA
|| idTestCipher == KUZNYECHIK
|| idTestCipher == KUZNYECHIK || idTestCipher == SM4
)
{
ndx = (int) SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) L"256");
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 32);
if (idTestCipher == SM4) // SM4 key size is 128 bits
{
ndx = (int) SendMessage (GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0,(LPARAM) L"128");
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx, (LPARAM)16);
SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), L"00000000000000000000000000000000");
SetWindowText(GetDlgItem(hwndDlg, IDC_SECONDARY_KEY), L"00000000000000000000000000000000");
}
else
{
ndx = (int)SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_ADDSTRING, 0, (LPARAM)L"256");
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETITEMDATA, ndx, (LPARAM)32);
SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), L"0000000000000000000000000000000000000000000000000000000000000000");
SetWindowText(GetDlgItem(hwndDlg, IDC_SECONDARY_KEY), L"0000000000000000000000000000000000000000000000000000000000000000");
}
SendMessage(GetDlgItem(hwndDlg, IDC_KEY_SIZE), CB_SETCURSEL, ndx,0);
SendMessage (GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_RESETCONTENT, 0,0);
@@ -7823,7 +7835,6 @@ ResetCipherTest(HWND hwndDlg, int idTestCipher)
SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_SETITEMDATA, ndx,(LPARAM) 16);
SendMessage(GetDlgItem(hwndDlg, IDC_PLAINTEXT_SIZE), CB_SETCURSEL, ndx,0);
SetWindowText(GetDlgItem(hwndDlg, IDC_KEY), L"0000000000000000000000000000000000000000000000000000000000000000");
SetWindowText(GetDlgItem(hwndDlg, IDC_PLAINTEXT), L"00000000000000000000000000000000");
SetWindowText(GetDlgItem(hwndDlg, IDC_CIPHERTEXT), L"00000000000000000000000000000000");
}
@@ -11339,6 +11350,10 @@ void Applink (const char *dest)
{
StringCbCopyW (page, sizeof (page),L"Camellia.html");
}
else if (strcmp(dest, "sm4") == 0)
{
StringCbCopyW (page, sizeof (page),L"SM4.html");
}
else if (strcmp(dest, "cascades") == 0)
{
StringCbCopyW (page, sizeof (page),L"Cascades.html");

View File

@@ -1644,6 +1644,7 @@
<entry lang="en" key="MOUNTPOINT_BLOCKED">ERROR: The volume mount point is blocked because it overrides a protected system directory.\n\nPlease choose a different mount point.</entry>
<entry lang="en" key="MOUNTPOINT_NOTALLOWED">ERROR: The volume mount point is not allowed because it overrides a directory that is part of the PATH environment variable.\n\nPlease choose a different mount point.</entry>
<entry lang="en" key="INSECURE_MODE">[INSECURE MODE]</entry>
<entry lang="en" key="SM4_HELP">SM4 is a block cipher standard published by the Chinese National Cryptography Administration in 2006 as part of the GB/T 32907-2016 standard. 128-bit key, 128-bit block. Mode of operation is XTS. It is widely used in Chinese national standards and commercial applications.</entry>
</localization>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="VeraCrypt">

View File

@@ -391,7 +391,7 @@ KUZNYECHIK_TEST kuznyechik_vectors[KUZNYECHIK_TEST_COUNT] = {
{
0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11, 0x22, 0x33,
0x44, 0x55, 0x66, 0x77, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x11, 0x22, 0x33, 0x44,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x11, 0x22, 0x33, 0x44,
0x55, 0x66, 0x77, 0x00, 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88,
0x7F, 0x67, 0x9D, 0x90, 0xBE, 0xBC, 0x24, 0x30, 0x5A, 0x46, 0x8D, 0x42,
0xB9, 0xD4, 0xED, 0xCD
@@ -399,29 +399,101 @@ KUZNYECHIK_TEST kuznyechik_vectors[KUZNYECHIK_TEST_COUNT] = {
{
0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11, 0x22, 0x33,
0x44, 0x55, 0x66, 0x77, 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x00, 0x11, 0x22, 0x33,
0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xEE, 0xFF, 0x0A,
0xB4, 0x29, 0x91, 0x2C, 0x6E, 0x00, 0x32, 0xF9, 0x28, 0x54, 0x52, 0xD7,
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x00, 0x11, 0x22, 0x33,
0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA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0xAB, 0xCD, 0xEF, 0x22, 0x33, 0x44, 0x55,
0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xEE, 0xFF, 0x0A, 0x00, 0x11,
0xD0, 0xB0, 0x9C, 0xCD, 0xE8, 0x30, 0xB9, 0xEB, 0x3A, 0x02, 0xC4, 0xC5,
0xAA, 0x8A, 0xDA, 0x98
}
};
// SM4 ECB test vectors
#define SM4_TEST_COUNT 11
typedef struct {
unsigned char key[16];
unsigned char plaintext[16];
unsigned char ciphertext[16];
} SM4_TEST;
// Based on test vector fron cppcrypto (cppcrypto/testvectors/block_cipher/sm4.txt)
SM4_TEST sm4_vectors[SM4_TEST_COUNT] = {
{
// KEY 0
{ 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10 },
{ 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10 },
{ 0x68,0x1e,0xdf,0x34,0xd2,0x06,0x96,0x5e,0x86,0xb3,0xe9,0x4f,0x53,0x6e,0x42,0x46 }
},
{
// KEY 1
{ 0x68,0x1e,0xdf,0x34,0xd2,0x06,0x96,0x5e,0x86,0xb3,0xe9,0x4f,0x53,0x6e,0x42,0x46 },
{ 0xf4,0x21,0x31,0xb0,0x02,0x42,0x5b,0x6f,0x5c,0xf5,0x2a,0x81,0x06,0x82,0xa0,0x9d },
{ 0xec,0x4b,0x7b,0x17,0x57,0xfe,0xe9,0xce,0x45,0x51,0x97,0xe5,0xbf,0x9c,0x3a,0x90 }
},
{
// After KEY 1, PT/CT pairs
{ 0x68,0x1e,0xdf,0x34,0xd2,0x06,0x96,0x5e,0x86,0xb3,0xe9,0x4f,0x53,0x6e,0x42,0x46 },
{ 0x07,0xbc,0xae,0x6a,0x83,0x88,0xe1,0x46,0x51,0xfe,0xd8,0x4b,0x37,0x49,0xd3,0x86 },
{ 0x89,0xf2,0xc4,0x1e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e,0xdf,0x34,0xd2,0x06,0x96,0x5e,0x86,0xb3,0xe9,0x4f,0x53,0x6e,0x42,0x47 },
{ 0xfa,0x59,0x80,0x11,0xf7,0xc2,0x10,0x07,0x99,0x45,0x1e,0x62,0xf3,0xb5,0xcf,0x09 },
{ 0x62,0x55,0x45,0x91,0x00,0x95,0x8f,0x4d,0x95,0x3a,0x9d,0x56,0x67,0x69,0x2d,0x6d }
},
{
{ 0x78,0x1e,0xdf,0x34,0xd2,0x06,0x96,0x5e,0x86,0xb3,0xe9,0x4f,0x53,0x6e,0x42,0x47 },
{ 0xba,0x1f,0x85,0x55,0xb2,0xdd,0xab,0x0e,0x4e,0x4d,0x80,0x26,0xb0,0x5a,0xf3,0x89 },
{ 0x37,0x6f,0xeb,0x09,0x78,0xb5,0x2a,0xb9,0xc9,0x84,0xa1,0x4d,0x7e,0x66,0xf6,0x71 }
},
{
{ 0x78,0x1e,0xdf,0x34,0xd2,0x06,0x96,0x5e,0x86,0xb3,0xe9,0x4f,0x53,0x6e,0x42,0x47 },
{ 0x50,0xc6,0x3c,0xe2,0x55,0x82,0x57,0x1a,0xa5,0xd8,0xee,0x22,0x08,0x9c,0x1b,0x59 },
{ 0x31,0xff,0xaf,0x2c,0xad,0x65,0x49,0xf3,0xd9,0xfc,0xd7,0xf0,0x2d,0xf5,0x81,0x24 }
},
{
{ 0x78,0x1e,0xdf,0x34,0xd2,0x06,0x96,0x5e,0x86,0xb3,0xe9,0x4f,0x53,0x6e,0x42,0x47 },
{ 0x22,0x9a,0xd7,0xa8,0xa8,0x3c,0x5e,0x23,0x84,0xb4,0x08,0x2e,0x50,0xd0,0x6e,0xbf },
{ 0x76,0xf2,0x9e,0x93,0xdd,0xf5,0x79,0x32,0xa4,0x1e,0x83,0xbb,0x7b,0x61,0xa4,0x06 }
}
};
#endif
/* Test vectors from FIPS 198a, RFC 4231, RFC 2104, RFC 2202, and other sources. */
@@ -646,22 +718,12 @@ void CipherInit2(int cipher, void* key, void* ks)
{
case AES:
CipherInit(cipher,key,ks);
break;
case SERPENT:
CipherInit(cipher,key,ks);
break;
case TWOFISH:
CipherInit(cipher,key,ks);
break;
case CAMELLIA:
CipherInit(cipher,key,ks);
break;
case KUZNYECHIK:
CipherInit(cipher, key, ks);
case SM4:
CipherInit(cipher,key,ks);
break;
default:
/* Unknown/wrong ID */
@@ -879,7 +941,33 @@ BOOL TestSectorBufEncryption (PCRYPTO_INFO ci)
nTestsPerformed++;
break;
}
}
}
else if (wcscmp (name, L"SM4") == 0)
{
switch (testCase)
{
case 0:
if (crc != 0x561b1367)
return FALSE;
nTestsPerformed++;
break;
case 1:
if (crc != 0x8f72e14d)
return FALSE;
nTestsPerformed++;
break;
case 2:
if (crc != 0xf96df16f)
return FALSE;
nTestsPerformed++;
break;
case 3:
if (crc != 0x8997e6eb)
return FALSE;
nTestsPerformed++;
break;
}
}
else if (wcscmp (name, L"AES-Twofish") == 0)
{
switch (testCase)
@@ -1114,6 +1202,84 @@ BOOL TestSectorBufEncryption (PCRYPTO_INFO ci)
break;
}
}
else if (wcscmp (name, L"Kuznyechik-SM4") == 0)
{
switch (testCase)
{
case 0:
if (crc != 0xb126b7f8)
return FALSE;
nTestsPerformed++;
break;
case 1:
if (crc != 0xa117004a)
return FALSE;
nTestsPerformed++;
break;
case 2:
if (crc != 0xc561be46)
return FALSE;
nTestsPerformed++;
break;
case 3:
if (crc != 0x47106ce3)
return FALSE;
nTestsPerformed++;
break;
}
}
else if (wcscmp (name, L"Serpent-SM4") == 0)
{
switch (testCase)
{
case 0:
if (crc != 0x40a9eaa5)
return FALSE;
nTestsPerformed++;
break;
case 1:
if (crc != 0xce6873f1)
return FALSE;
nTestsPerformed++;
break;
case 2:
if (crc != 0x92cafcad)
return FALSE;
nTestsPerformed++;
break;
case 3:
if (crc != 0x7e1463ca)
return FALSE;
nTestsPerformed++;
break;
}
}
else if (wcscmp (name, L"SM4-Twofish") == 0)
{
switch (testCase)
{
case 0:
if (crc != 0xd9a46a64)
return FALSE;
nTestsPerformed++;
break;
case 1:
if (crc != 0x371fdc08)
return FALSE;
nTestsPerformed++;
break;
case 2:
if (crc != 0x231c5104)
return FALSE;
nTestsPerformed++;
break;
case 3:
if (crc != 0xa920424b)
return FALSE;
nTestsPerformed++;
break;
}
}
else if (wcscmp (name, L"Kuznyechik-Serpent-Camellia") == 0)
{
switch (testCase)
@@ -1140,6 +1306,32 @@ BOOL TestSectorBufEncryption (PCRYPTO_INFO ci)
break;
}
}
else if (wcscmp (name, L"Twofish-Serpent-SM4") == 0)
{
switch (testCase)
{
case 0:
if (crc != 0x881b6e3d)
return FALSE;
nTestsPerformed++;
break;
case 1:
if (crc != 0x37ed1418)
return FALSE;
nTestsPerformed++;
break;
case 2:
if (crc != 0x8e563eef)
return FALSE;
nTestsPerformed++;
break;
case 3:
if (crc != 0xdcbc41ac)
return FALSE;
nTestsPerformed++;
break;
}
}
#endif
if (crc == 0x9f5edd58)
return FALSE;
@@ -1217,6 +1409,12 @@ BOOL TestSectorBufEncryption (PCRYPTO_INFO ci)
return FALSE;
nTestsPerformed++;
}
else if (wcscmp (name, L"SM4") == 0)
{
if (crc != 0x7b600d06)
return FALSE;
nTestsPerformed++;
}
else if (wcscmp (name, L"AES-Twofish") == 0)
{
if (crc != 0x14ce7385)
@@ -1271,12 +1469,36 @@ BOOL TestSectorBufEncryption (PCRYPTO_INFO ci)
return FALSE;
nTestsPerformed++;
}
else if (wcscmp (name, L"Kuznyechik-SM4") == 0)
{
if (crc != 0x8190551b)
return FALSE;
nTestsPerformed++;
}
else if (wcscmp (name, L"Serpent-SM4") == 0)
{
if (crc != 0x31408c47)
return FALSE;
nTestsPerformed++;
}
else if (wcscmp (name, L"SM4-Twofish") == 0)
{
if (crc != 0x1eaede31)
return FALSE;
nTestsPerformed++;
}
else if (wcscmp (name, L"Kuznyechik-Serpent-Camellia") == 0)
{
if (crc != 0x755dad72)
return FALSE;
nTestsPerformed++;
}
else if (wcscmp (name, L"Twofish-Serpent-SM4") == 0)
{
if (crc != 0x033093e5)
return FALSE;
nTestsPerformed++;
}
#endif
if (crc == 0x9f5edd58)
return FALSE;
@@ -1288,7 +1510,7 @@ BOOL TestSectorBufEncryption (PCRYPTO_INFO ci)
nTestsPerformed++;
}
return (nTestsPerformed == 150);
return (nTestsPerformed == 200);
}
static BOOL DoAutoTestAlgorithms (void)
@@ -1430,6 +1652,26 @@ static BOOL DoAutoTestAlgorithms (void)
}
if (i != KUZNYECHIK_TEST_COUNT)
bFailed = TRUE;
/* SM4 */
for (i = 0; i < SM4_TEST_COUNT; i++)
{
int cipher = SM4;
memcpy(key, sm4_vectors[i].key, 16);
memcpy(tmp, sm4_vectors[i].plaintext, 16);
CipherInit(cipher, key, ks_tmp);
EncipherBlock(cipher, tmp, ks_tmp);
if (memcmp(sm4_vectors[i].ciphertext, tmp, 16) != 0)
break;
DecipherBlock(cipher, tmp, ks_tmp);
if (memcmp(sm4_vectors[i].plaintext, tmp, 16) != 0)
break;
}
if (i != SM4_TEST_COUNT)
bFailed = TRUE;
#endif
/* PKCS #5 and HMACs */