From b0348bed8e5e58858e8633cd16f7a359429b5373 Mon Sep 17 00:00:00 2001 From: Zhi Guan Date: Wed, 5 Jun 2024 12:30:57 +0800 Subject: [PATCH] Add speed tests to `sdftest` command --- tools/sdftest.c | 487 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 487 insertions(+) diff --git a/tools/sdftest.c b/tools/sdftest.c index f59297d9..c0163568 100644 --- a/tools/sdftest.c +++ b/tools/sdftest.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -1314,6 +1315,481 @@ static int test_SDF_InternalEncrypt_ECC(int key, char *pass) return 1; } +static int speed_SDF_Encrypt_SM4_CBC(int kek) +{ + void *hDeviceHandle = NULL; + void *hSessionHandle = NULL; + void *hKeyHandle = NULL; + unsigned int uiKeyBits = 128; + unsigned int uiKEKIndex = (unsigned int)kek; + unsigned char ucWrappedKey[64]; + unsigned int uiWrappedKeyLength = (unsigned int)sizeof(ucWrappedKey); + unsigned char ucIV[16] = {0}; + unsigned char ucData[4096] = {0}; + unsigned int uiLength; + int ret; + clock_t begin, end; + double seconds; + size_t i; + + ret = SDF_OpenDevice(&hDeviceHandle); + if (ret != SDR_OK) { + error_print(); + return -1; + } + + ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle); + if (ret != SDR_OK) { + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + ret = SDF_GenerateKeyWithKEK(hSessionHandle, uiKeyBits, SGD_SM4_CBC, uiKEKIndex, ucWrappedKey, &uiWrappedKeyLength, &hKeyHandle); + if (ret != SDR_OK) { + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + begin = clock(); + for (i = 0; i < 16; i++) { + ret = SDF_Encrypt(hSessionHandle, hKeyHandle, SGD_SM4_CBC, ucIV, ucData, sizeof(ucData), ucData, &uiLength); + if (ret != SDR_OK) { + (void)SDF_DestroyKey(hSessionHandle, hKeyHandle); + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + } + for (i = 0; i < 4096; i++) { + (void)SDF_Encrypt(hSessionHandle, hKeyHandle, SGD_SM4_CBC, ucIV, ucData, sizeof(ucData), ucData, &uiLength); + } + end = clock(); + seconds = (double)(end - begin)/ CLOCKS_PER_SEC; + + (void)SDF_DestroyKey(hSessionHandle, hKeyHandle); + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + + printf("%s: %f MiB per second\n", __FUNCTION__, 16/seconds); + return 1; +} + +static int speed_SDF_Decrypt_SM4_CBC(int kek) +{ + void *hDeviceHandle = NULL; + void *hSessionHandle = NULL; + void *hKeyHandle = NULL; + unsigned int uiKeyBits = 128; + unsigned int uiKEKIndex = (unsigned int)kek; + unsigned char ucWrappedKey[64]; + unsigned int uiWrappedKeyLength = (unsigned int)sizeof(ucWrappedKey); + unsigned char ucIV[16] = {0}; + unsigned char ucData[4096] = {0}; + unsigned int uiLength; + clock_t begin, end; + double seconds; + int i, ret; + + ret = SDF_OpenDevice(&hDeviceHandle); + if (ret != SDR_OK) { + error_print(); + return -1; + } + + ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle); + if (ret != SDR_OK) { + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + ret = SDF_GenerateKeyWithKEK(hSessionHandle, uiKeyBits, SGD_SM4_CBC, uiKEKIndex, ucWrappedKey, &uiWrappedKeyLength, &hKeyHandle); + if (ret != SDR_OK) { + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + begin = clock(); + for (i = 0; i < 16; i++) { + ret = SDF_Decrypt(hSessionHandle, hKeyHandle, SGD_SM4_CBC, ucIV, ucData, sizeof(ucData), ucData, &uiLength); + if (ret != SDR_OK) { + (void)SDF_DestroyKey(hSessionHandle, hKeyHandle); + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + } + for (i = 16; i < 4096; i++) { + (void)SDF_Decrypt(hSessionHandle, hKeyHandle, SGD_SM4_CBC, ucIV, ucData, sizeof(ucData), ucData, &uiLength); + } + end = clock(); + seconds = (double)(end - begin)/ CLOCKS_PER_SEC; + + (void)SDF_DestroyKey(hSessionHandle, hKeyHandle); + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + + printf("%s: %f MiB per second\n", __FUNCTION__, 16/seconds); + return 1; +} + +static int speed_SDF_Hash(void) +{ + void *hDeviceHandle = NULL; + void *hSessionHandle = NULL; + unsigned char ucData[4096] = {0}; + unsigned int uiDataLength = (unsigned int)sizeof(ucData); + unsigned char ucHash[32]; + unsigned int uiHashLength; + clock_t begin, end; + double seconds; + int i, ret; + + ret = SDF_OpenDevice(&hDeviceHandle); + if (ret != SDR_OK) { + error_print(); + return -1; + } + + ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle); + if (ret != SDR_OK) { + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + begin = clock(); + ret = SDF_HashInit(hSessionHandle, SGD_SM3, NULL, NULL, 0); + if (ret != SDR_OK) { + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + for (i = 0; i < 16; i++) { + ret = SDF_HashUpdate(hSessionHandle, ucData, uiDataLength); + if (ret != SDR_OK) { + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + } + for (i = 16; i < 4096; i++) { + (void)SDF_HashUpdate(hSessionHandle, ucData, uiDataLength); + } + end = clock(); + seconds = (double)(end - begin)/ CLOCKS_PER_SEC; + + (void)SDF_HashFinal(hSessionHandle, ucHash, &uiHashLength); + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + + printf("%s: %f MiB per second\n", __FUNCTION__, 16/seconds); + return 1; +} + +static int speed_SDF_GenerateKeyPair_ECC(void) +{ + void *hDeviceHandle = NULL; + void *hSessionHandle = NULL; + ECCrefPublicKey eccPublicKey; + ECCrefPrivateKey eccPrivateKey; + clock_t begin, end; + double seconds; + int i, ret; + + ret = SDF_OpenDevice(&hDeviceHandle); + if (ret != SDR_OK) { + error_print(); + return -1; + } + + ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle); + if (ret != SDR_OK) { + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + begin = clock(); + for (i = 0; i < 16; i++) { + ret = SDF_GenerateKeyPair_ECC(hSessionHandle, SGD_SM2_1, 256, &eccPublicKey, &eccPrivateKey); + if (ret != SDR_OK) { + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + } + for (i = 0; i < 4096; i++) { + (void)SDF_GenerateKeyPair_ECC(hSessionHandle, SGD_SM2_1, 256, &eccPublicKey, &eccPrivateKey); + } + end = clock(); + seconds = (double)(end - begin)/ CLOCKS_PER_SEC; + + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + + printf("%s: %f keypairs generated per second\n", __FUNCTION__, 4096/seconds); + return 1; +} + +static int speed_SDF_InternalSign_ECC(int key, char *pass) +{ + void *hDeviceHandle = NULL; + void *hSessionHandle = NULL; + unsigned int uiIPKIndex = (unsigned int)key; + unsigned char *ucPassword = (unsigned char *)pass; + unsigned int uiPwdLength = (unsigned int)strlen(pass); + unsigned char ucData[32] = { 1,2,3,4 }; + unsigned int uiDataLength = 32; + ECCSignature eccSignature; + clock_t begin, end; + double seconds; + int i, ret; + + ret = SDF_OpenDevice(&hDeviceHandle); + if (ret != SDR_OK) { + error_print(); + return -1; + } + + ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle); + if (ret != SDR_OK) { + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + ret = SDF_GetPrivateKeyAccessRight(hSessionHandle, uiIPKIndex, ucPassword, uiPwdLength); + if (ret != SDR_OK) { + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + begin = clock(); + for (i = 0; i < 16; i++) { + ret = SDF_InternalSign_ECC(hSessionHandle, uiIPKIndex, ucData, uiDataLength, &eccSignature); + if (ret != SDR_OK) { + (void)SDF_ReleasePrivateKeyAccessRight(hSessionHandle, uiIPKIndex); + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + } + for (i = 16; i < 4096; i++) { + (void)SDF_InternalSign_ECC(hSessionHandle, uiIPKIndex, ucData, uiDataLength, &eccSignature); + } + end = clock(); + seconds = (double)(end - begin)/ CLOCKS_PER_SEC; + + (void)SDF_ReleasePrivateKeyAccessRight(hSessionHandle, uiIPKIndex); + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + + printf("%s: %f signs per second\n", __FUNCTION__, 4096/seconds); + return 1; +} + +static int speed_SDF_InternalVerify_ECC(int key, char *pass) +{ + void *hDeviceHandle = NULL; + void *hSessionHandle = NULL; + unsigned int uiIPKIndex = (unsigned int)key; + unsigned char *ucPassword = (unsigned char *)pass; + unsigned int uiPwdLength = (unsigned int)strlen(pass); + unsigned char ucData[32] = { 1,2,3,4 }; + unsigned int uiDataLength = 32; + ECCSignature eccSignature; + clock_t begin, end; + double seconds; + int i, ret; + + ret = SDF_OpenDevice(&hDeviceHandle); + if (ret != SDR_OK) { + error_print(); + return -1; + } + + ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle); + if (ret != SDR_OK) { + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + ret = SDF_GetPrivateKeyAccessRight(hSessionHandle, uiIPKIndex, ucPassword, uiPwdLength); + if (ret != SDR_OK) { + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + ret = SDF_InternalSign_ECC(hSessionHandle, uiIPKIndex, ucData, uiDataLength, &eccSignature); + if (ret != SDR_OK) { + (void)SDF_ReleasePrivateKeyAccessRight(hSessionHandle, uiIPKIndex); + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + //(void)SDF_ReleasePrivateKeyAccessRight(hSessionHandle, uiIPKIndex); // FIXME: error on calling this + + begin = clock(); + for (i = 0; i < 16; i++) { + ret = SDF_InternalVerify_ECC(hSessionHandle, uiIPKIndex, ucData, uiDataLength, &eccSignature); + if (ret != SDR_OK) { + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + } + for (i = 16; i < 4096; i++) { + (void)SDF_InternalVerify_ECC(hSessionHandle, uiIPKIndex, ucData, uiDataLength, &eccSignature); + } + end = clock(); + seconds = (double)(end - begin)/ CLOCKS_PER_SEC; + + (void)SDF_ReleasePrivateKeyAccessRight(hSessionHandle, uiIPKIndex); + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + + printf("%s: %f verifications per second\n", __FUNCTION__, 4096/seconds); + return 1; +} + +static int speed_SDF_InternalEncrypt_ECC(int key) +{ + void *hDeviceHandle = NULL; + void *hSessionHandle = NULL; + unsigned int uiIPKIndex = (unsigned int)key; + unsigned char ucData[48] = { 1,2,3,4 }; + unsigned int uiDataLength = (unsigned int)sizeof(ucData); + ECCCipher eccCipher; + unsigned char ucDecData[256]; + unsigned int uiDecDataLength; + clock_t begin, end; + double seconds; + int i, ret; + + ret = SDF_OpenDevice(&hDeviceHandle); + if (ret != SDR_OK) { + error_print(); + return -1; + } + + ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle); + if (ret != SDR_OK) { + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + begin = clock(); + for (i = 0; i < 16; i++) { + ret = SDF_InternalEncrypt_ECC(hSessionHandle, uiIPKIndex, SGD_SM2_3, ucData, uiDataLength, &eccCipher); + if (ret != SDR_OK) { + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + } + for (i = 16; i < 4096; i++) { + (void)SDF_InternalEncrypt_ECC(hSessionHandle, uiIPKIndex, SGD_SM2_3, ucData, uiDataLength, &eccCipher); + } + end = clock(); + seconds = (double)(end - begin)/ CLOCKS_PER_SEC; + + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + + printf("%s: %f encryptions per second\n", __FUNCTION__, 4096/seconds); + return 1; +} + +static int speed_SDF_InternalDecrypt_ECC(int key, char *pass) +{ + void *hDeviceHandle = NULL; + void *hSessionHandle = NULL; + unsigned int uiIPKIndex = (unsigned int)key; + unsigned char *ucPassword = (unsigned char *)pass; + unsigned int uiPwdLength = (unsigned int)strlen(pass); + unsigned char ucData[48] = { 1,2,3,4 }; + unsigned int uiDataLength = (unsigned int)sizeof(ucData); + ECCCipher eccCipher; + unsigned char ucDecData[256]; + unsigned int uiDecDataLength; + clock_t begin, end; + double seconds; + int i, ret; + + ret = SDF_OpenDevice(&hDeviceHandle); + if (ret != SDR_OK) { + error_print(); + return -1; + } + + ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle); + if (ret != SDR_OK) { + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + ret = SDF_InternalEncrypt_ECC(hSessionHandle, uiIPKIndex, SGD_SM2_3, ucData, uiDataLength, &eccCipher); + if (ret != SDR_OK) { + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + ret = SDF_GetPrivateKeyAccessRight(hSessionHandle, uiIPKIndex, ucPassword, uiPwdLength); + if (ret != SDR_OK) { + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + + + begin = clock(); + for (i = 0; i < 16; i++) { + ret = SDF_InternalDecrypt_ECC(hSessionHandle, uiIPKIndex, SGD_SM2_3, &eccCipher, ucDecData, &uiDecDataLength); + if (ret != SDR_OK) { + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + error_print(); + return -1; + } + } + for (i = 16; i < 4096; i++) { + (void)SDF_InternalDecrypt_ECC(hSessionHandle, uiIPKIndex, SGD_SM2_3, &eccCipher, ucDecData, &uiDecDataLength); + } + end = clock(); + seconds = (double)(end - begin)/ CLOCKS_PER_SEC; + + (void)SDF_CloseSession(hSessionHandle); + (void)SDF_CloseDevice(hDeviceHandle); + + printf("%s: %f decryptions per second\n", __FUNCTION__, 4096/seconds); + return 1; +} + int sdftest_main(int argc, char **argv) { int ret = 1; @@ -1400,6 +1876,17 @@ bad: if (test_SDF_InternalSign_ECC(key, pass) != 1) goto err; if (test_SDF_InternalEncrypt_ECC(key, pass) != 1) goto err; +#if ENABLE_TEST_SPEED + if (speed_SDF_Hash() != 1) goto err; + if (speed_SDF_Encrypt_SM4_CBC(kek) != 1) goto err; + //if (speed_SDF_Decrypt_SM4_CBC(kek) != 1) goto err; // FIXME: should implement CBC without padding in SoftSDF + if (speed_SDF_GenerateKeyPair_ECC() != 1) goto err; + if (speed_SDF_InternalSign_ECC(key, pass) != 1) goto err; + if (speed_SDF_InternalVerify_ECC(key, pass) != 1) goto err; + if (speed_SDF_InternalEncrypt_ECC(key) != 1) goto err; + if (speed_SDF_InternalDecrypt_ECC(key, pass) != 1) goto err; +#endif + printf("%s all tests passed\n", __FILE__); return 0;