📄 speed.c
字号:
int do_RSA_KeyGen_2048( void ){ CK_SLOT_ID slot_id; CK_SESSION_HANDLE session; CK_MECHANISM mech; CK_OBJECT_HANDLE publ_key, priv_key; CK_FLAGS flags; CK_BYTE user_pin[8]; CK_ULONG user_pin_len; CK_ULONG diff, max_time, min_time, avg_time, i; CK_RV rc; printf("do_RSA_KeyGen_2048...\n"); slot_id = SLOT_ID; memcpy( user_pin, "12345678", 8 ); user_pin_len = 8; mech.mechanism = CKM_RSA_PKCS_KEY_PAIR_GEN; mech.ulParameterLen = 0; mech.pParameter = NULL; { SYSTEMTIME t1, t2; CK_ULONG bits = 2048; CK_BYTE pub_exp[] = { 0x3 }; CK_ATTRIBUTE pub_tmpl[] = { {CKA_MODULUS_BITS, &bits, sizeof(bits) }, {CKA_PUBLIC_EXPONENT, &pub_exp, sizeof(pub_exp) } }; flags = CKF_SERIAL_SESSION; rc = funcs->C_OpenSession( slot_id, flags, NULL, NULL, &session ); if (rc != CKR_OK) { show_error(" C_OpenSession #3", rc ); return FALSE; } rc = funcs->C_Login( session, CKU_USER, user_pin, user_pin_len ); if (rc != CKR_OK) { show_error(" C_Login #1", rc ); return FALSE; } // skip the first one // rc = funcs->C_GenerateKeyPair( session, &mech, pub_tmpl, 2, NULL, 0, &publ_key, &priv_key ); if (rc != CKR_OK) { show_error(" C_GenerateKeyPair #1", rc ); return FALSE; } min_time = 0xFFFFFFFF; max_time = 0x00000000; avg_time = 0x00000000; for (i=0; i < 12; i++) { GetSystemTime(&t1); rc = funcs->C_GenerateKeyPair( session, &mech, pub_tmpl, 2, NULL, 0, &publ_key, &priv_key ); if (rc != CKR_OK) { show_error(" C_GenerateKeyPair #2", rc ); return FALSE; } GetSystemTime(&t2); diff = process_time( t1, t2 ); printf(" %3d: %d\n", i, diff ); avg_time += diff; if (diff < min_time) min_time = diff; if (diff > max_time) max_time = diff; } avg_time -= min_time; avg_time -= max_time; printf("10 iterations: %dms\n", avg_time ); printf("Minimum: %dms\n", min_time ); printf("Maximum: %dms\n", max_time ); rc = funcs->C_CloseSession( session ); if (rc != CKR_OK) { show_error(" C_CloseSession #3", rc ); return FALSE; } } printf("Looks okay...\n"); return TRUE;}////int do_RSA_KeyGen_1024( void ){ CK_SLOT_ID slot_id; CK_SESSION_HANDLE session; CK_MECHANISM mech; CK_OBJECT_HANDLE publ_key, priv_key; CK_FLAGS flags; CK_BYTE user_pin[8]; CK_ULONG user_pin_len; CK_ULONG diff, max_time, min_time, avg_time, i; CK_RV rc; printf("do_RSA_KeyGen_1024...\n"); slot_id = SLOT_ID; memcpy( user_pin, "12345678", 8 ); user_pin_len = 8; mech.mechanism = CKM_RSA_PKCS_KEY_PAIR_GEN; mech.ulParameterLen = 0; mech.pParameter = NULL; { SYSTEMTIME t1, t2; CK_ULONG bits = 1024; CK_BYTE pub_exp[] = { 0x3 }; CK_ATTRIBUTE pub_tmpl[] = { {CKA_MODULUS_BITS, &bits, sizeof(bits) }, {CKA_PUBLIC_EXPONENT, &pub_exp, sizeof(pub_exp) } }; flags = CKF_SERIAL_SESSION; rc = funcs->C_OpenSession( slot_id, flags, NULL, NULL, &session ); if (rc != CKR_OK) { show_error(" C_OpenSession #3", rc ); return FALSE; } rc = funcs->C_Login( session, CKU_USER, user_pin, user_pin_len ); if (rc != CKR_OK) { show_error(" C_Login #1", rc ); return FALSE; } // skip the first one // rc = funcs->C_GenerateKeyPair( session, &mech, pub_tmpl, 2, NULL, 0, &publ_key, &priv_key ); if (rc != CKR_OK) { show_error(" C_GenerateKeyPair #1", rc ); return FALSE; } min_time = 0xFFFFFFFF; max_time = 0x00000000; avg_time = 0x00000000; for (i=0; i < 12; i++) { GetSystemTime(&t1); rc = funcs->C_GenerateKeyPair( session, &mech, pub_tmpl, 2, NULL, 0, &publ_key, &priv_key ); if (rc != CKR_OK) { show_error(" C_GenerateKeyPair #2", rc ); return FALSE; } GetSystemTime(&t2); diff = process_time( t1, t2 ); printf(" %3d: %d\n", i, diff ); avg_time += diff; if (diff < min_time) min_time = diff; if (diff > max_time) max_time = diff; } avg_time -= min_time; avg_time -= max_time; printf("10 iterations: %dms\n", avg_time ); printf("Minimum: %dms\n", min_time ); printf("Maximum: %dms\n", max_time ); rc = funcs->C_CloseSession( session ); if (rc != CKR_OK) { show_error(" C_CloseSession #3", rc ); return FALSE; } } printf("Looks okay...\n"); return TRUE;}////int do_RSA_PKCS_SignVerify_1024( void ){ CK_BYTE data1[100]; CK_BYTE signature[256]; CK_SLOT_ID slot_id; CK_SESSION_HANDLE session; CK_MECHANISM mech; CK_OBJECT_HANDLE publ_key, priv_key; CK_FLAGS flags; CK_BYTE user_pin[8]; CK_ULONG user_pin_len; CK_ULONG i; CK_ULONG len1, sig_len; CK_RV rc; SYSTEMTIME t1, t2; CK_ULONG diff, min_time, max_time, avg_time; CK_ULONG bits = 1024; CK_BYTE pub_exp[] = { 0x3 }; CK_ATTRIBUTE pub_tmpl[] = { {CKA_MODULUS_BITS, &bits, sizeof(bits) }, {CKA_PUBLIC_EXPONENT, &pub_exp, sizeof(pub_exp) } }; printf("do_RSA_PKCS_Sign_1024...\n"); slot_id = SLOT_ID; flags = CKF_SERIAL_SESSION | CKF_RW_SESSION; rc = funcs->C_OpenSession( slot_id, flags, NULL, NULL, &session ); if (rc != CKR_OK) { show_error(" C_OpenSession #1", rc ); return FALSE; } memcpy( user_pin, "12345678", 8 ); user_pin_len = 8; rc = funcs->C_Login( session, CKU_USER, user_pin, user_pin_len ); if (rc != CKR_OK) { show_error(" C_Login #1", rc ); return FALSE; } mech.mechanism = CKM_RSA_PKCS_KEY_PAIR_GEN; mech.ulParameterLen = 0; mech.pParameter = NULL; printf("Generating a 1024-bit keypair...\n"); rc = funcs->C_GenerateKeyPair( session, &mech, pub_tmpl, 2, NULL, 0, &publ_key, &priv_key ); if (rc != CKR_OK) { show_error(" C_GenerateKeyPair #1", rc ); return FALSE; } printf("Done...computing signatures...\n"); // now, encrypt some data // len1 = sizeof(data1); sig_len = sizeof(signature); for (i=0; i < len1; i++) data1[i] = i % 255; mech.mechanism = CKM_RSA_PKCS; mech.ulParameterLen = 0; mech.pParameter = NULL; avg_time = 0; max_time = 0; min_time = 0xFFFFFFFF; for (i=0; i < 1000; i++) { GetSystemTime(&t1); rc = funcs->C_SignInit( session, &mech, priv_key ); if (rc != CKR_OK) { show_error(" C_SignInit #1", rc ); return FALSE; } sig_len = sizeof(signature); rc = funcs->C_Sign( session, data1, len1, signature, &sig_len ); if (rc != CKR_OK) { show_error(" C_Sign #1", rc ); return FALSE; } GetSystemTime(&t2); diff = process_time(t1, t2); avg_time += diff; if (diff < min_time) min_time = diff; if (diff > max_time) max_time = diff; } avg_time -= min_time; avg_time -= max_time; printf("1000 RSA PKCS Sign operations: %d ms\n", avg_time ); printf("Minimum: %d ms\n", min_time ); printf("Maximum: %d ms\n", max_time ); printf("\n"); avg_time = 0; max_time = 0; min_time = 0xFFFFFFFF; for (i=0; i < 1000; i++) { GetSystemTime(&t1); rc = funcs->C_VerifyInit( session, &mech, publ_key ); if (rc != CKR_OK) { show_error(" C_VerifyInit #1", rc ); return FALSE; } rc = funcs->C_Verify( session, data1, len1, signature, sig_len ); if (rc != CKR_OK) { show_error(" C_Verify #1", rc ); return FALSE; } GetSystemTime(&t2); diff = process_time(t1, t2); avg_time += diff; if (diff < min_time) min_time = diff; if (diff > max_time) max_time = diff; } avg_time -= min_time; avg_time -= max_time; printf("1000 RSA PKCS Verify operations: %d ms\n", avg_time ); printf("Minimum: %d ms\n", min_time ); printf("Maximum: %d ms\n", max_time ); printf("\n"); rc = funcs->C_CloseAllSessions( slot_id ); if (rc != CKR_OK) { show_error(" C_CloseAllSessions #1", rc ); return FALSE; } printf("Looks okay...\n"); return TRUE;}////int do_DES3_ECB_EncrDecr( void ){ CK_BYTE *original; CK_BYTE *cipher; CK_BYTE *clear; CK_SLOT_ID slot_id; CK_SESSION_HANDLE session; CK_MECHANISM mech; CK_OBJECT_HANDLE h_key; CK_FLAGS flags; CK_BYTE user_pin[8]; CK_ULONG user_pin_len; CK_ULONG i; CK_ULONG orig_len, cipher_len, clear_len; CK_RV rc; SYSTEMTIME t1, t2; CK_ULONG avg_time, min_time, max_time, diff; printf("do_DES3_ECB_EncrDecr\n"); original = (CK_BYTE *)malloc(BIG_REQUEST); cipher = (CK_BYTE *)malloc(BIG_REQUEST); clear = (CK_BYTE *)malloc(BIG_REQUEST); if (!original || !cipher || !clear) { if (original) free( original ); if (cipher) free( cipher ); if (clear) free( clear ); printf("HOST MEMORY ERROR\n"); return FALSE; } slot_id = SLOT_ID; flags = CKF_SERIAL_SESSION | CKF_RW_SESSION; rc = funcs->C_OpenSession( slot_id, flags, NULL, NULL, &session ); if (rc != CKR_OK) { show_error(" C_OpenSession #1", rc ); return FALSE; } memcpy( user_pin, "12345678", 8 ); user_pin_len = 8; rc = funcs->C_Login( session, CKU_USER, user_pin, user_pin_len ); if (rc != CKR_OK) { show_error(" C_Login #1", rc ); return FALSE; } mech.mechanism = CKM_DES3_KEY_GEN; mech.ulParameterLen = 0; mech.pParameter = NULL; // first, generate a DES key // rc = funcs->C_GenerateKey( session, &mech, NULL, 0, &h_key ); if (rc != CKR_OK) { show_error(" C_GenerateKey #1", rc ); return FALSE; } // now, encrypt some data // orig_len = BIG_REQUEST; for (i=0; i < orig_len; i++) { original[i] = i % 255; } mech.mechanism = CKM_DES3_ECB; mech.ulParameterLen = 0; mech.pParameter = NULL; avg_time = 0; max_time = 0; min_time = 0xFFFFFFFF; for (i=0; i < 1000; i++) { GetSystemTime(&t1); rc = funcs->C_EncryptInit( session, &mech, h_key ); if (rc != CKR_OK) { show_error(" C_EncryptInit #1", rc ); return FALSE; } cipher_len = BIG_REQUEST; rc = funcs->C_Encrypt( session, original, orig_len, cipher, &cipher_len ); if (rc != CKR_OK) { show_error(" C_Encrypt #1", rc ); return FALSE; } GetSystemTime(&t2); diff = process_time(t1, t2); avg_time += diff;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -