#include "licenceELC2.h" #include "utils.h" LicenceELC2::LicenceELC2(){} LicenceELC2::~LicenceELC2(){} LicenceELC2::LicenceELC2(LicenceIdentification &licIdentification): lIdentification(licIdentification) {} /// @brief get proper licencename /// @param licPostfix /// @return string LicenceELC2::getLicenceName() { string result = ""; char prefixChar = 97; int licType = (int)lIdentification.licLicenceType; int lVersion = lIdentification.licenceVersion; unordered_map baseString; baseString.insert(std::pair((int)LicenceType::EOS_EOV, "ezlic_eovosv")); baseString.insert(std::pair((int)LicenceType::DDTS, "ezlic_ddts")); baseString.insert(std::pair((int)LicenceType::DRT, "ezlic_drt")); struct Index { int index[11]; }; std::unordered_map indexes; Index indexes1 = {7, 16, 20, 23, 18, 4, 9, 11, 6, 9, 13}; Index indexes2 = {12, 10, 22, 23, 24, 25, 9, 11, 2, 1, 3}; // puvodní indexy Index indexes3 = {8, 13, 11, 9, 7, 11, 10, 13, 5, 20, 19}; indexes.insert(std::pair(1, indexes1)); indexes.insert(std::pair(2, indexes2)); indexes.insert(std::pair(3, indexes3)); result = baseString.at(licType) + to_string(lIdentification.licenceIndex) + "_"; result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[0]] + (lIdentification.licenceIndex * 11)) % 25); result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[1]] + (lIdentification.licenceIndex * 39)) % 25); result += prefixChar + ((this->sdCard.cardData.CID_nibble[indexes.at(lVersion).index[2]] + (lIdentification.licenceIndex * 1)) % 25); result += prefixChar + ((this->sdCard.cardData.CID_nibble[indexes.at(lVersion).index[3]] * 2) % 25); result += prefixChar + ((this->sdCard.cardData.CID_nibble[indexes.at(lVersion).index[4]] + (lIdentification.licenceIndex * 5)) % 25); result += prefixChar + ((this->sdCard.cardData.CID_nibble[indexes.at(lVersion).index[5]] * 3) % 25); result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[6]] + (lIdentification.licenceIndex * 52)) % 25); result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[7]] + (lIdentification.licenceIndex * 34)) % 25); result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[8]] + (lIdentification.licenceIndex * 21)) % 25); result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[9]] + (lIdentification.licenceIndex * 47)) % 25); result += prefixChar + ((this->sdCard.cardData.CID[indexes.at(lVersion).index[10]] + (lIdentification.licenceIndex * 7)) % 25); result += ".lic"; return result; } vector LicenceELC2::cryptPrivateContent(const std::vector &content) { BYTE initVector[15] = {}; BYTE aesKey[32] = {}; LicenceELC2::initVector(initVector, aesKey); unsigned char encrypted[10000] = {}; const unsigned char *plainTextArray = content.data(); int finalEncryptedLength = encrypt(plainTextArray, content.size(), aesKey, initVector, encrypted); if (finalEncryptedLength <= 0) throw LicenceException((int)GeneralError::EncryptError, "Chyba při kryptování."); std::vector result(encrypted, encrypted + finalEncryptedLength); return result; } vector LicenceELC2::decryptPrivateContent(const std::vector &content) { BYTE initVector[15] = {0}; BYTE aesKey[32] = {0}; LicenceELC2::initVector(initVector, aesKey); const unsigned char *encryptedData = content.data(); unsigned char decrypted[10000] = {}; int decrypted_len = decrypt(encryptedData, content.size(), aesKey, initVector, decrypted); if (decrypted_len <= 0) throw LicenceException((int)GeneralError::DecryptError, "Chyba při dekryptování."); std::vector result(decrypted, decrypted + decrypted_len); return result; } void LicenceELC2::initVector(BYTE (&iVector)[], BYTE (&key)[]) { struct Vector15 { int vec[15]; }; Vector15 vec1 = {this->sdCard.cardData.CID[10], this->sdCard.cardData.CID[12], this->sdCard.cardData.CID[11], this->sdCard.cardData.CID[9], this->sdCard.cardData.CID_nibble[22] - 15, this->sdCard.cardData.CID_nibble[24] - 15, this->sdCard.cardData.CID_nibble[25] - 15, this->sdCard.cardData.CID_nibble[21] - 15, 9, 10, 11, 12, 13, 14, 15}; Vector15 vec2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; //TODO přidat smysluplnější indexy Vector15 vec3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; std::unordered_map vectors; vectors.insert(std::pair(1, vec1)); vectors.insert(std::pair(2, vec2)); vectors.insert(std::pair(3, vec3)); struct Key32 { int key[32]; }; Key32 key1 = {this->sdCard.cardData.CID[12], this->sdCard.cardData.CID[23] - 15, this->sdCard.cardData.CID[25] - 15, this->sdCard.cardData.CID[11], this->sdCard.cardData.CID[9], this->sdCard.cardData.CID_nibble[21], this->sdCard.cardData.CID[9] % 25, this->sdCard.cardData.CID_nibble[22] - 15, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; Key32 key2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; Key32 key3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; std::unordered_map keys; keys.insert(std::pair(1, key1)); keys.insert(std::pair(2, key2)); keys.insert(std::pair(3, key3)); int lVersion = lIdentification.licenceVersion; iVector[0] = vectors.at(lVersion).vec[0]; iVector[1] = vectors.at(lVersion).vec[1]; iVector[2] = vectors.at(lVersion).vec[2]; iVector[3] = vectors.at(lVersion).vec[3]; iVector[4] = vectors.at(lVersion).vec[4]; iVector[5] = vectors.at(lVersion).vec[5]; iVector[6] = vectors.at(lVersion).vec[6]; iVector[7] = vectors.at(lVersion).vec[7]; memcpy(&iVector[8], &iVector[0], 8); key[0] = keys.at(lVersion).key[0]; key[1] = keys.at(lVersion).key[1]; key[2] = keys.at(lVersion).key[2]; key[3] = keys.at(lVersion).key[3]; key[4] = keys.at(lVersion).key[4]; key[5] = keys.at(lVersion).key[5]; key[6] = keys.at(lVersion).key[6]; key[7] = keys.at(lVersion).key[7]; memcpy(&key[8], &key[0], 8); memcpy(&key[16], &key[6], 8); memcpy(&key[24], &key[12], 8); }