void CPKCSDemoDlg::OnBtnKeypairgen() { StartOP(); CK_TOKEN_INFO tokenInfo = {0}; CK_RV rv = C_GetTokenInfo(m_pSlotList[0], &tokenInfo); if (CKR_OK != rv) { ShowMsg(NEWLINE"Can not get token information!"NEWLINE); return; } DlgUserPIN* dlgUserPIN = new DlgUserPIN; dlgUserPIN->DoModal(); delete dlgUserPIN; if("" == g_strUserPIN) { ShowMsg(NEWLINE"You should enter User PIN before generating RSA Key pair!"NEWLINE); return; } CK_ULONG ulPIN = g_strUserPIN.GetLength(); CK_BYTE_PTR pPIN = (CK_BYTE_PTR)g_strUserPIN.GetBuffer(ulPIN); ::SetCursor(::LoadCursor(NULL, IDC_WAIT)); rv = C_Login(m_hSession, CKU_USER, pPIN, ulPIN); if(CKR_OK != rv) { ShowErr(NEWLINE"Can't use your User PIN login to token ,ErrorCode: 0x%08X."NEWLINE, rv); return; } else ShowMsg(NEWLINE"Logging in to token Successfully!"NEWLINE); ::SetCursor(::LoadCursor(NULL, IDC_WAIT)); rv = C_GenerateKeyPair( m_hSession, &keyGenMechanism, pubTemplate, countof(pubTemplate), priTemplate, countof(priTemplate), &m_hPubKey, &m_hPriKey); if(CKR_OK != rv) { ShowErr(NEWLINE"Can't generate RSA key pair, ErrorCode: 0x%08X."NEWLINE, rv); return; } else { m_btnKeyPairGen.EnableWindow(FALSE); m_btnSign.EnableWindow(TRUE); m_btnEncrypt.EnableWindow(TRUE); ShowMsg(NEWLINE"Generate Key Pair Successfully!"NEWLINE); ShowMsg(NEWLINE"Now you can Sign, Verify, Encrypt and Decrypt by using the Key Pair!"NEWLINE); m_bKeyGen = TRUE; } }
CK_RV DeriveTests::generateEcKeyPair(const char* curve, CK_SESSION_HANDLE hSession, CK_BBOOL bTokenPuk, CK_BBOOL bPrivatePuk, CK_BBOOL bTokenPrk, CK_BBOOL bPrivatePrk, CK_OBJECT_HANDLE &hPuk, CK_OBJECT_HANDLE &hPrk) { CK_MECHANISM mechanism = { CKM_EC_KEY_PAIR_GEN, NULL_PTR, 0 }; CK_KEY_TYPE keyType = CKK_EC; CK_BYTE oidP256[] = { 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07 }; CK_BYTE oidP384[] = { 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22 }; CK_BYTE oidP521[] = { 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x23 }; CK_BBOOL bTrue = CK_TRUE; CK_ATTRIBUTE pukAttribs[] = { { CKA_EC_PARAMS, NULL, 0 }, { CKA_KEY_TYPE, &keyType, sizeof(keyType) }, { CKA_TOKEN, &bTokenPuk, sizeof(bTokenPuk) }, { CKA_PRIVATE, &bPrivatePuk, sizeof(bPrivatePuk) } }; CK_ATTRIBUTE prkAttribs[] = { { CKA_KEY_TYPE, &keyType, sizeof(keyType) }, { CKA_TOKEN, &bTokenPrk, sizeof(bTokenPrk) }, { CKA_PRIVATE, &bPrivatePrk, sizeof(bPrivatePrk) }, { CKA_SENSITIVE, &bTrue, sizeof(bTrue) }, { CKA_DERIVE, &bTrue, sizeof(bTrue) } }; /* Select the curve */ if (strcmp(curve, "P-256") == 0) { pukAttribs[0].pValue = oidP256; pukAttribs[0].ulValueLen = sizeof(oidP256); } else if (strcmp(curve, "P-384") == 0) { pukAttribs[0].pValue = oidP384; pukAttribs[0].ulValueLen = sizeof(oidP384); } else if (strcmp(curve, "P-521") == 0) { pukAttribs[0].pValue = oidP521; pukAttribs[0].ulValueLen = sizeof(oidP521); } else { return CKR_GENERAL_ERROR; } hPuk = CK_INVALID_HANDLE; hPrk = CK_INVALID_HANDLE; return CRYPTOKI_F_PTR( C_GenerateKeyPair(hSession, &mechanism, pukAttribs, sizeof(pukAttribs)/sizeof(CK_ATTRIBUTE), prkAttribs, sizeof(prkAttribs)/sizeof(CK_ATTRIBUTE), &hPuk, &hPrk) ); }
CK_RV SymmetricAlgorithmTests::generateRsaPrivateKey(CK_SESSION_HANDLE hSession, CK_BBOOL bToken, CK_BBOOL bPrivate, CK_OBJECT_HANDLE &hKey) { CK_MECHANISM mechanism = { CKM_RSA_PKCS_KEY_PAIR_GEN, NULL_PTR, 0 }; CK_ULONG bits = 1536; CK_BYTE pubExp[] = {0x01, 0x00, 0x01}; CK_BYTE subject[] = { 0x12, 0x34 }; // dummy CK_BYTE id[] = { 123 } ; // dummy CK_BBOOL bFalse = CK_FALSE; CK_BBOOL bTrue = CK_TRUE; CK_ATTRIBUTE pubAttribs[] = { { CKA_TOKEN, &bToken, sizeof(bToken) }, { CKA_PRIVATE, &bPrivate, sizeof(bPrivate) }, { CKA_ENCRYPT, &bFalse, sizeof(bFalse) }, { CKA_VERIFY, &bTrue, sizeof(bTrue) }, { CKA_WRAP, &bFalse, sizeof(bFalse) }, { CKA_MODULUS_BITS, &bits, sizeof(bits) }, { CKA_PUBLIC_EXPONENT, &pubExp[0], sizeof(pubExp) } }; CK_ATTRIBUTE privAttribs[] = { { CKA_TOKEN, &bToken, sizeof(bToken) }, { CKA_PRIVATE, &bPrivate, sizeof(bPrivate) }, { CKA_SUBJECT, &subject[0], sizeof(subject) }, { CKA_ID, &id[0], sizeof(id) }, { CKA_SENSITIVE, &bTrue, sizeof(bTrue) }, { CKA_DECRYPT, &bFalse, sizeof(bFalse) }, { CKA_SIGN, &bTrue, sizeof(bTrue) }, { CKA_UNWRAP, &bFalse, sizeof(bFalse) }, { CKA_SENSITIVE, &bFalse, sizeof(bFalse) }, { CKA_EXTRACTABLE, &bTrue, sizeof(bTrue) } }; CK_OBJECT_HANDLE hPub = CK_INVALID_HANDLE; hKey = CK_INVALID_HANDLE; CK_RV rv; rv = C_GenerateKeyPair(hSession, &mechanism, pubAttribs, sizeof(pubAttribs)/sizeof(CK_ATTRIBUTE), privAttribs, sizeof(privAttribs)/sizeof(CK_ATTRIBUTE), &hPub, &hKey); if (hPub != CK_INVALID_HANDLE) { C_DestroyObject(hSession, hPub); } return rv; }
CK_RV DeriveTests::generateDhKeyPair(CK_SESSION_HANDLE hSession, CK_BBOOL bTokenPuk, CK_BBOOL bPrivatePuk, CK_BBOOL bTokenPrk, CK_BBOOL bPrivatePrk, CK_OBJECT_HANDLE &hPuk, CK_OBJECT_HANDLE &hPrk) { CK_MECHANISM mechanism = { CKM_DH_PKCS_KEY_PAIR_GEN, NULL_PTR, 0 }; CK_BBOOL bTrue = CK_TRUE; CK_BYTE bn1024[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc9, 0x0f, 0xda, 0xa2, 0x21, 0x68, 0xc2, 0x34, 0xc4, 0xc6, 0x62, 0x8b, 0x80, 0xdc, 0x1c, 0xd1, 0x29, 0x02, 0x4e, 0x08, 0x8a, 0x67, 0xcc, 0x74, 0x02, 0x0b, 0xbe, 0xa6, 0x3b, 0x13, 0x9b, 0x22, 0x51, 0x4a, 0x08, 0x79, 0x8e, 0x34, 0x04, 0xdd, 0xef, 0x95, 0x19, 0xb3, 0xcd, 0x3a, 0x43, 0x1b, 0x30, 0x2b, 0x0a, 0x6d, 0xf2, 0x5f, 0x14, 0x37, 0x4f, 0xe1, 0x35, 0x6d, 0x6d, 0x51, 0xc2, 0x45, 0xe4, 0x85, 0xb5, 0x76, 0x62, 0x5e, 0x7e, 0xc6, 0xf4, 0x4c, 0x42, 0xe9, 0xa6, 0x37, 0xed, 0x6b, 0x0b, 0xff, 0x5c, 0xb6, 0xf4, 0x06, 0xb7, 0xed, 0xee, 0x38, 0x6b, 0xfb, 0x5a, 0x89, 0x9f, 0xa5, 0xae, 0x9f, 0x24, 0x11, 0x7c, 0x4b, 0x1f, 0xe6, 0x49, 0x28, 0x66, 0x51, 0xec, 0xe6, 0x53, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; CK_BYTE bn2[] = { 2 }; CK_ATTRIBUTE pukAttribs[] = { { CKA_TOKEN, &bTokenPuk, sizeof(bTokenPuk) }, { CKA_PRIVATE, &bPrivatePuk, sizeof(bPrivatePuk) }, { CKA_PRIME, &bn1024, sizeof(bn1024) }, { CKA_BASE, &bn2, sizeof(bn2) } }; CK_ATTRIBUTE prkAttribs[] = { { CKA_TOKEN, &bTokenPrk, sizeof(bTokenPrk) }, { CKA_PRIVATE, &bPrivatePrk, sizeof(bPrivatePrk) }, { CKA_SENSITIVE, &bTrue, sizeof(bTrue) }, { CKA_DERIVE, &bTrue, sizeof(bTrue) } }; hPuk = CK_INVALID_HANDLE; hPrk = CK_INVALID_HANDLE; return CRYPTOKI_F_PTR( C_GenerateKeyPair(hSession, &mechanism, pukAttribs, sizeof(pukAttribs)/sizeof(CK_ATTRIBUTE), prkAttribs, sizeof(prkAttribs)/sizeof(CK_ATTRIBUTE), &hPuk, &hPrk) ); }
CK_RV AsymEncryptDecryptTests::generateRsaKeyPair(CK_SESSION_HANDLE hSession, CK_BBOOL bTokenPuk, CK_BBOOL bPrivatePuk, CK_BBOOL bTokenPrk, CK_BBOOL bPrivatePrk, CK_OBJECT_HANDLE &hPuk, CK_OBJECT_HANDLE &hPrk) { CK_MECHANISM mechanism = { CKM_RSA_PKCS_KEY_PAIR_GEN, NULL_PTR, 0 }; CK_ULONG bits = 1536; CK_BYTE pubExp[] = {0x01, 0x00, 0x01}; CK_BYTE subject[] = { 0x12, 0x34 }; // dummy CK_BYTE id[] = { 123 } ; // dummy CK_BBOOL bFalse = CK_FALSE; CK_BBOOL bTrue = CK_TRUE; CK_ATTRIBUTE pukAttribs[] = { { CKA_TOKEN, &bTokenPuk, sizeof(bTokenPuk) }, { CKA_PRIVATE, &bPrivatePuk, sizeof(bPrivatePuk) }, { CKA_ENCRYPT, &bTrue, sizeof(bTrue) }, { CKA_VERIFY, &bTrue, sizeof(bTrue) }, { CKA_WRAP, &bFalse, sizeof(bFalse) }, { CKA_MODULUS_BITS, &bits, sizeof(bits) }, { CKA_PUBLIC_EXPONENT, &pubExp[0], sizeof(pubExp) } }; CK_ATTRIBUTE prkAttribs[] = { { CKA_TOKEN, &bTokenPrk, sizeof(bTokenPrk) }, { CKA_PRIVATE, &bPrivatePrk, sizeof(bPrivatePrk) }, { CKA_SUBJECT, &subject[0], sizeof(subject) }, { CKA_ID, &id[0], sizeof(id) }, { CKA_SENSITIVE, &bTrue, sizeof(bTrue) }, { CKA_DECRYPT, &bTrue, sizeof(bTrue) }, { CKA_SIGN, &bTrue, sizeof(bTrue) }, { CKA_UNWRAP, &bFalse, sizeof(bFalse) } }; hPuk = CK_INVALID_HANDLE; hPrk = CK_INVALID_HANDLE; return C_GenerateKeyPair(hSession, &mechanism, pukAttribs, sizeof(pukAttribs)/sizeof(CK_ATTRIBUTE), prkAttribs, sizeof(prkAttribs)/sizeof(CK_ATTRIBUTE), &hPuk, &hPrk); }
void ECCSpeedTest() { CK_RV rv = 0; CK_SLOT_ID SlotId = 0; CK_SESSION_HANDLE hSession = 0; CK_OBJECT_HANDLE hECCPubKey = 0; CK_OBJECT_HANDLE hECCPriKey = 0; CK_BBOOL isTrue = FALSE; CK_ULONG ECCPubKeyClass = CKO_PUBLIC_KEY; CK_ULONG ECCPriKeyClass = CKO_PRIVATE_KEY; CK_BYTE Id[] = "MY_ECC_KEY"; CK_ATTRIBUTE ECCPubKey[] = { {CKA_CLASS, &ECCPubKeyClass, sizeof(ECCPubKeyClass)}, {CKA_ID, &Id, strlen(Id)}, {CKA_TOKEN, &isTrue, sizeof(isTrue)} }; CK_ATTRIBUTE ECCPriKey[] = { {CKA_CLASS, &ECCPriKeyClass, sizeof(ECCPriKeyClass)}, {CKA_ID, &Id, strlen(Id)}, {CKA_TOKEN, &isTrue, sizeof(isTrue)} }; CK_MECHANISM ECCGenKey = {CKM_ECDSA_KEY_PAIR_GEN, NULL_PTR, 0}; CK_MECHANISM mECCEncrypt = {CKM_ECDSA_PKCS, NULL_PTR, 0}; CK_BYTE indata[160] = {0x00}; CK_BYTE outdata[300] = {0x00}; CK_BYTE temp[300] = {0x00}; CK_ULONG outlength = 0; CK_ULONG templength = 0; CK_ULONG i = 0; LARGE_INTEGER Frequency; LARGE_INTEGER StartNum; LARGE_INTEGER EndNum; double Second; memset(indata, 0x42, 111); memset(outdata, 0x00, 300); memset(temp, 0x00, 300); rv = C_OpenSession(SlotId, CKF_SERIAL_SESSION|CKF_RW_SESSION, NULL_PTR, NULL, &hSession); #ifdef DEVICE_KEY C_Login(hSession, CKU_SO, "11111111", 8); #endif QueryPerformanceFrequency(&Frequency); QueryPerformanceCounter(&StartNum); for (i=0; i<LOOPTIMES; i++) { C_GenerateKeyPair(hSession, &ECCGenKey, ECCPubKey, 3, ECCPriKey, 3, &hECCPubKey, &hECCPriKey); } QueryPerformanceCounter(&EndNum); Second = (double)(((EndNum.QuadPart - StartNum.QuadPart)+0.0)/Frequency.QuadPart); printf("Generate ECC Key Time is: %03f s\n", Second/LOOPTIMES); QueryPerformanceCounter(&StartNum); for (i=0; i<LOOPTIMES; i++) { C_EncryptInit(hSession, &mECCEncrypt, hECCPubKey); C_Encrypt(hSession, indata, 111, outdata, &outlength); C_DecryptInit(hSession, &mECCEncrypt, hECCPriKey); C_Decrypt(hSession, outdata, outlength, temp, &templength); } QueryPerformanceCounter(&EndNum); Second = (double)(((EndNum.QuadPart - StartNum.QuadPart)+0.0)/Frequency.QuadPart); printf("ECC Encrypt and Decrypt Time is: %03f s\n", Second/LOOPTIMES); #ifdef DEVICE_KEY C_Logout(hSession); #endif C_CloseSession(hSession); }
FLAG ECCTest_out() { CK_RV rv = 0; CK_SLOT_ID SlotId = 0; CK_SESSION_HANDLE hSession = 0; CK_OBJECT_HANDLE hECCPubKey = 0; CK_OBJECT_HANDLE hECCPriKey = 0; CK_BBOOL isTrue = TRUE; CK_ULONG ECCPubKeyClass = CKO_PUBLIC_KEY; CK_ULONG ECCPriKeyClass = CKO_PRIVATE_KEY; CK_BYTE Id[] = "MY_ECC_KEY"; CK_ATTRIBUTE ECCPubKey[] = { {CKA_CLASS, &ECCPubKeyClass, sizeof(ECCPubKeyClass)}, {CKA_ID, &Id, strlen(Id)}, {CKA_TOKEN, &isTrue, sizeof(isTrue)} }; CK_ATTRIBUTE ECCPriKey[] = { {CKA_CLASS, &ECCPriKeyClass, sizeof(ECCPriKeyClass)}, {CKA_ID, &Id, strlen(Id)}, {CKA_TOKEN, &isTrue, sizeof(isTrue)} }; CK_ATTRIBUTE CreateECCPubKey[] = { {CKA_CLASS, NULL_PTR, 0}, {CKA_MODULUS_BITS, NULL_PTR, 0}, {CKA_ID, NULL_PTR, 0}, {CKA_EC_POINT, NULL_PTR, 0} }; CK_ATTRIBUTE CreateECCPriKey[] = { {CKA_CLASS, NULL_PTR, 0}, {CKA_ID, NULL_PTR, 0}, {CKA_MODULUS_BITS, NULL_PTR, 0}, {CKA_VALUE, NULL_PTR, 0} }; CK_MECHANISM ECCGenKey = {CKM_ECDSA_KEY_PAIR_GEN, NULL_PTR, 0}; CK_MECHANISM mECCEncrypt = {CKM_ECDSA_PKCS, NULL_PTR, 0}; CK_BYTE indata[160] = {0x00}; CK_BYTE outdata[300] = {0x00}; CK_BYTE temp[300] = {0x00}; CK_ULONG outlength = 0; CK_ULONG templength = 0; printf("ECC Outside Encrypt and Decrypt!\n"); rv = C_OpenSession(SlotId, CKF_SERIAL_SESSION|CKF_RW_SESSION, NULL_PTR, NULL, &hSession); #ifdef DEVICE_KEY C_Login(hSession, CKU_SO, "11111111", 8); #endif rv = C_GenerateKeyPair(hSession, &ECCGenKey, ECCPubKey, 3, ECCPriKey, 3, &hECCPubKey, &hECCPriKey); rv = C_CloseSession(hSession); rv = C_OpenSession(SlotId, CKF_SERIAL_SESSION|CKF_RW_SESSION, NULL_PTR, NULL, &hSession); #ifdef DEVICE_KEY C_Login(hSession, CKU_SO, "11111111", 8); #endif rv = C_GetAttributeValue(hSession, hECCPubKey, CreateECCPubKey, 4); if (rv == 0) { CreateECCPubKey[0].pValue = malloc(CreateECCPubKey[0].ulValueLen); if (CreateECCPubKey[0].pValue == NULL) { printf("ECCTest_out->malloc ERROR!\n"); return -1; } CreateECCPubKey[1].pValue = malloc(CreateECCPubKey[1].ulValueLen); if (CreateECCPubKey[1].pValue == NULL) { printf("ECCTest_out->malloc ERROR!\n"); FreeAttribute(CreateECCPubKey, 1); return -1; } CreateECCPubKey[2].pValue = malloc(CreateECCPubKey[2].ulValueLen); if (CreateECCPubKey[2].pValue == NULL) { printf("ECCTest_out->malloc ERROR!\n"); FreeAttribute(CreateECCPubKey, 2); return -1; } CreateECCPubKey[3].pValue = malloc(CreateECCPubKey[3].ulValueLen); if (CreateECCPubKey[3].pValue == NULL) { printf("ECCTest_out->malloc ERROR!\n"); FreeAttribute(CreateECCPubKey, 3); return -1; } rv = C_GetAttributeValue(hSession, hECCPubKey, CreateECCPubKey, 4); rv = C_DestroyObject(hSession, hECCPubKey); rv = C_CreateObject(hSession, CreateECCPubKey, 4, &hECCPubKey); } FreeAttribute(CreateECCPubKey, 4); memset(indata, 0x33, 110); memset(outdata, 0x00, 300); memset(temp, 0x00, 300); rv = C_EncryptInit(hSession, &mECCEncrypt, hECCPubKey); rv = C_Encrypt(hSession, indata, 110, outdata, &outlength); rv = C_GetAttributeValue(hSession, hECCPriKey, CreateECCPriKey, 4); if (rv == 0) { CreateECCPriKey[0].pValue = malloc(CreateECCPriKey[0].ulValueLen); if (CreateECCPriKey[0].pValue == NULL) { printf("ECCTest_out->malloc ERROR!\n"); return -1; } CreateECCPriKey[1].pValue = malloc(CreateECCPriKey[1].ulValueLen); if (CreateECCPriKey[1].pValue == NULL) { printf("ECCTest_out->malloc ERROR!\n"); FreeAttribute(CreateECCPriKey, 1); return -1; } CreateECCPriKey[2].pValue = malloc(CreateECCPriKey[2].ulValueLen); if (CreateECCPriKey[2].pValue == NULL) { printf("ECCTest_out->malloc ERROR!\n"); FreeAttribute(CreateECCPriKey, 2); return -1; } CreateECCPriKey[3].pValue = malloc(CreateECCPriKey[3].ulValueLen); if (CreateECCPriKey[3].pValue == NULL) { printf("ECCTest_out->malloc ERROR!\n"); FreeAttribute(CreateECCPriKey, 3); return -1; } rv = C_GetAttributeValue(hSession, hECCPriKey, CreateECCPriKey, 4); rv = C_DestroyObject(hSession, hECCPriKey); rv = C_CreateObject(hSession, CreateECCPriKey, 4, &hECCPriKey); } FreeAttribute(CreateECCPriKey, 4); rv = C_DecryptInit(hSession, &mECCEncrypt, hECCPriKey); rv = C_Decrypt(hSession, outdata, outlength, temp, &templength); #ifdef DEVICE_KEY C_Logout(hSession); #endif rv = C_CloseSession(hSession); if (memcmp(indata, temp, 110) == 0 && templength == 110) { printf("OK ECC OUT!\n"); } else { printf("ERROR ECC OUT!\n"); } return 0; }
FLAG ECCTest_in() { CK_RV rv = 0; CK_SLOT_ID SlotId = 0; CK_SESSION_HANDLE hSession = 0; CK_ULONG ECCPubKeyClass = CKO_PUBLIC_KEY; CK_ULONG ECCPriKeyClass = CKO_PRIVATE_KEY; CK_BYTE ECCPubLabel[] = "ECC_PUBLIC_KEY_7"; CK_BYTE ECCPriLabel[] = "ECC_PRIVATE_KEY_7"; CK_BBOOL isFalse = TRUE; #if 0 /* CKA_LABEL TEST*/ CK_ATTRUBUTE ECCKeyLabel = { CAK_LABEL,//type ECCPub//value //value_len }; #endif /* END CAK_LABEL TEST*/ CK_ATTRIBUTE ECCPubKey[] = { {CKA_CLASS, &ECCPubKeyClass, sizeof(ECCPubKeyClass)}, {CKA_LABEL, &ECCPubLabel, sizeof(ECCPubLabel)}, {CKA_TOKEN, &isFalse, sizeof(isFalse)} }; CK_ATTRIBUTE ECCPriKey[] = { {CKA_CLASS, &ECCPriKeyClass, sizeof(ECCPriKeyClass)}, {CKA_LABEL, &ECCPriLabel, sizeof(ECCPriLabel)}, {CKA_TOKEN, &isFalse, sizeof(isFalse)} }; CK_ATTRIBUTE CreateECCPriKey[] = { {CKA_CLASS, NULL_PTR, 0}, {CKA_MODULUS_BITS, NULL_PTR, 0}, {CKA_VALUE, NULL_PTR, 0} }; CK_MECHANISM ECCGenKey = {CKM_ECDSA_KEY_PAIR_GEN, NULL_PTR, 0}; CK_MECHANISM mECCEncrypt = {CKM_ECDSA_PKCS, NULL_PTR, 0}; CK_OBJECT_HANDLE hECCPubKey = 0; CK_OBJECT_HANDLE hECCPriKey = 0; CK_BYTE indata[160] = {0x00}; CK_BYTE outdata[300] = {0x00}; CK_BYTE temp[300] = {0x00}; CK_ULONG outlength = 0; CK_ULONG templength = 0; memset(indata, 0x31, 40); memset(outdata, 0x00, 300); memset(temp, 0x00, 300); printf("ECC Inside Encrypt and Decrypt!\n"); rv = C_OpenSession(SlotId, CKF_SERIAL_SESSION|CKF_RW_SESSION, NULL_PTR, NULL, &hSession); #ifdef DEVICE_KEY C_Login(hSession, CKU_SO, "11111111", 8); #endif rv = C_GenerateKeyPair(hSession, &ECCGenKey, ECCPubKey, 3, ECCPriKey, 3, &hECCPubKey, &hECCPriKey); rv = C_EncryptInit(hSession, &mECCEncrypt, hECCPubKey); rv = C_Encrypt(hSession, indata, 40, outdata, &outlength); rv = C_DecryptInit(hSession, &mECCEncrypt, hECCPriKey); rv = C_Decrypt(hSession, outdata, outlength, temp, &templength); if (memcmp(indata, temp, 40) == 0 && templength == 40) { printf("OK ECC IN!\n"); } else { printf("ERROR ECC IN!\n"); } printf("ECC Crossing Encrypt and Decrypt!\n"); memset(indata, 0x33, 110); memset(outdata, 0x00, 300); memset(temp, 0x00, 300); rv = C_GetAttributeValue(hSession, hECCPriKey, CreateECCPriKey, 3); if (rv == 0) { CreateECCPriKey[0].pValue = malloc(CreateECCPriKey[0].ulValueLen); if (CreateECCPriKey[0].pValue == NULL) { printf("ECCTest_out->malloc ERROR!\n"); return -1; } CreateECCPriKey[1].pValue = malloc(CreateECCPriKey[1].ulValueLen); if (CreateECCPriKey[1].pValue == NULL) { printf("ECCTest_out->malloc ERROR!\n"); FreeAttribute(CreateECCPriKey, 1); return -1; } CreateECCPriKey[2].pValue = malloc(CreateECCPriKey[2].ulValueLen); if (CreateECCPriKey[2].pValue == NULL) { printf("ECCTest_out->malloc ERROR!\n"); FreeAttribute(CreateECCPriKey, 2); return -1; } rv = C_GetAttributeValue(hSession, hECCPriKey, CreateECCPriKey, 3); rv = C_CreateObject(hSession, CreateECCPriKey, 3, &hECCPriKey); } FreeAttribute(CreateECCPriKey, 3); rv = C_EncryptInit(hSession, &mECCEncrypt, hECCPubKey); rv = C_Encrypt(hSession, indata, 110, outdata, &outlength); rv = C_DecryptInit(hSession, &mECCEncrypt, hECCPriKey); rv = C_Decrypt(hSession, outdata, outlength, temp, &templength); if (memcmp(indata, temp, 110) == 0 && templength == 110) { printf("OK ECC Crossing!\n"); } else { printf("ERROR ECC Crossing!\n"); } #ifdef DEVICE_KEY C_Logout(hSession); #endif C_CloseSession(hSession); return 0; }