oddělení generatoru od readeru
This commit is contained in:
135
src/common/SDCard.cpp
Normal file
135
src/common/SDCard.cpp
Normal file
@@ -0,0 +1,135 @@
|
||||
#include "utils.h"
|
||||
#include "SDCard.h"
|
||||
|
||||
SDCard::SDCard()
|
||||
{
|
||||
}
|
||||
|
||||
SDCard::SDCard(const string cds_cid_Path)
|
||||
{
|
||||
this->filePath = cds_cid_Path;
|
||||
if (getCIDFromFile() == false) return;
|
||||
if (getCSDFromFile() == false) return;
|
||||
this->isLoaded = SDCard::readSDCard();
|
||||
}
|
||||
|
||||
SDCard::SDCard(string cid, string csd)
|
||||
{
|
||||
for (unsigned int i = 0; i < cid.length(); i++) this->cid[i] = cid[i];
|
||||
for (unsigned int i = 0; i < csd.length(); i++) this->csd[i] = csd[i];
|
||||
|
||||
this->isLoaded = SDCard::readSDCard();
|
||||
}
|
||||
|
||||
bool SDCard:: readSDCard()
|
||||
{
|
||||
|
||||
BYTE cHexNibbleToNo[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
10, 11, 12, 13, 14, 15,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0,
|
||||
10, 11, 12, 13, 14, 15,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
for (int i = 0; i < CID_LENGTH; i++)
|
||||
this->cardData.CID_nibble[i] = (BYTE)cid[i];
|
||||
|
||||
for (int i = 0; i < cnibblescount / 2; i++)
|
||||
{
|
||||
this->cardData.CID[i] = cHexNibbleToNo[this->cardData.CID_nibble[2 * i]] << 4 | cHexNibbleToNo[this->cardData.CID_nibble[2 * i + 1]];
|
||||
}
|
||||
|
||||
this->cardData.manufacturerID = this->cardData.CID[0];
|
||||
this->cardData.oemID[0] = this->cardData.CID[1];
|
||||
this->cardData.oemID[1] = this->cardData.CID[2];
|
||||
this->cardData.name[0] = this->cardData.CID[3];
|
||||
this->cardData.name[1] = this->cardData.CID[4];
|
||||
this->cardData.name[2] = this->cardData.CID[5];
|
||||
this->cardData.name[3] = this->cardData.CID[6];
|
||||
this->cardData.name[4] = this->cardData.CID[7];
|
||||
this->cardData.productRevision_hw = cHexNibbleToNo[this->cardData.CID[16]];
|
||||
this->cardData.productRevision_sw = cHexNibbleToNo[this->cardData.CID[17]];
|
||||
|
||||
if (this->cardData.productRevision_sw < 10)
|
||||
this->cardData.productRevision = (float)this->cardData.productRevision_hw + ((float)this->cardData.productRevision_sw * 0.1);
|
||||
else
|
||||
this->cardData.productRevision = (float)this->cardData.productRevision_hw + ((float)this->cardData.productRevision_sw * 0.01);
|
||||
|
||||
this->cardData.serialNumber = this->cardData.CID[9] << 24 | this->cardData.CID[10] << 16 | this->cardData.CID[11] << 8 | this->cardData.CID[12];
|
||||
|
||||
// CSD
|
||||
for (int i = 0; i < CSD_LENGTH; i++)
|
||||
this->cardData.CSD_nibble[i] = (BYTE)csd[i];
|
||||
|
||||
for (int i = 0; i < cnibblescount / 2; i++)
|
||||
{
|
||||
this->cardData.CSD[i] = cHexNibbleToNo[this->cardData.CSD_nibble[2 * i]] << 4 | cHexNibbleToNo[this->cardData.CSD_nibble[2 * i + 1]];
|
||||
}
|
||||
|
||||
if (this->cardData.CSD_nibble[0] == 0x34)
|
||||
{
|
||||
this->cardData.cardSize = this->cardData.CSD[7] << 16 | this->cardData.CSD[8] << 8 | this->cardData.CSD[9];
|
||||
this->cardData.cardGSize = (this->cardData.cardSize + 1) / 2048;
|
||||
}
|
||||
|
||||
BYTE sdCrc = 0;
|
||||
for (int i = 0; i <= 14; i++)
|
||||
{
|
||||
BYTE sdChar = this->cardData.CID[i];
|
||||
for (int j = 0; j <= 7; j++)
|
||||
{
|
||||
sdCrc = sdCrc << 1;
|
||||
if ((sdChar ^ sdCrc) & 0x80)
|
||||
sdCrc = sdCrc ^ 0x09;
|
||||
sdChar = sdChar << 1;
|
||||
}
|
||||
sdCrc = sdCrc & 0x7F;
|
||||
}
|
||||
this->cardData.CRCOK = ((sdCrc << 1) | 1) == this->cardData.CID[15];
|
||||
this->cardData.crcCorrect = (int)this->cardData.CID[15] == (int)((sdCrc << 1) | 1);
|
||||
return this->cardData.crcCorrect;
|
||||
|
||||
}
|
||||
|
||||
bool SDCard::getCIDFromFile()
|
||||
{
|
||||
vector<char> content;
|
||||
if (readFile(this->filePath + "cid", content) == false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (content.size() >= 32)
|
||||
{
|
||||
for (int i = 0; i<32;i++) this->cid[i] = content[i];
|
||||
}
|
||||
else return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SDCard::getCSDFromFile()
|
||||
{
|
||||
vector<char> content;
|
||||
if (readFile(this->filePath + "csd", content) == false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (content.size() >= 32)
|
||||
{
|
||||
for (int i = 0; i<32;i++) this->csd[i] = content[i];
|
||||
}
|
||||
else return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
2
src/common/SDCard.d
Normal file
2
src/common/SDCard.d
Normal file
@@ -0,0 +1,2 @@
|
||||
src/common/SDCard.o: src/common/SDCard.cpp include/common/utils.h \
|
||||
include/common/SDCard.h
|
||||
BIN
src/common/SDCard.o
Normal file
BIN
src/common/SDCard.o
Normal file
Binary file not shown.
172
src/common/licenceELC2.cpp
Normal file
172
src/common/licenceELC2.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
#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<int, string> baseString;
|
||||
baseString.insert(std::pair<int, string>((int)LicenceType::EOS_EOV, "ezlic_eovosv"));
|
||||
baseString.insert(std::pair<int, string>((int)LicenceType::DDTS, "ezlic_ddts"));
|
||||
baseString.insert(std::pair<int, string>((int)LicenceType::DRT, "ezlic_drt"));
|
||||
|
||||
struct Index
|
||||
{
|
||||
int index[11];
|
||||
};
|
||||
|
||||
std::unordered_map<int, Index> 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<int, Index>(1, indexes1));
|
||||
indexes.insert(std::pair<int, Index>(2, indexes2));
|
||||
indexes.insert(std::pair<int, Index>(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<unsigned char> LicenceELC2::cryptPrivateContent(const std::vector<unsigned char> &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<unsigned char> result(encrypted, encrypted + finalEncryptedLength);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
vector<unsigned char> LicenceELC2::decryptPrivateContent(const std::vector<unsigned char> &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<unsigned char> 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<int, Vector15> vectors;
|
||||
vectors.insert(std::pair<int, Vector15>(1, vec1));
|
||||
vectors.insert(std::pair<int, Vector15>(2, vec2));
|
||||
vectors.insert(std::pair<int, Vector15>(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<int, Key32> keys;
|
||||
keys.insert(std::pair<int, Key32>(1, key1));
|
||||
keys.insert(std::pair<int, Key32>(2, key2));
|
||||
keys.insert(std::pair<int, Key32>(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);
|
||||
}
|
||||
|
||||
|
||||
3
src/common/licenceELC2.d
Normal file
3
src/common/licenceELC2.d
Normal file
@@ -0,0 +1,3 @@
|
||||
src/common/licenceELC2.o: src/common/licenceELC2.cpp \
|
||||
include/common/licenceELC2.h include/common/licenceCommon.h \
|
||||
include/common/SDCard.h include/common/utils.h
|
||||
BIN
src/common/licenceELC2.o
Normal file
BIN
src/common/licenceELC2.o
Normal file
Binary file not shown.
409
src/common/utils.cpp
Normal file
409
src/common/utils.cpp
Normal file
@@ -0,0 +1,409 @@
|
||||
|
||||
#include <openssl/conf.h>
|
||||
#include <openssl/ssl.h> /* core library */
|
||||
#include "utils.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
const std::string base64_chars =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
const char base64_url_alphabet[] = {
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
||||
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
||||
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'};
|
||||
|
||||
void getCharsFromString(string &source, char *charArray, size_t length)
|
||||
{
|
||||
for (size_t i = 0; i < length; i++)
|
||||
{
|
||||
charArray[i] = source[i];
|
||||
}
|
||||
}
|
||||
|
||||
void getCharsFromString(string source, char *charArray)
|
||||
{
|
||||
size_t length = source.length();
|
||||
for (size_t i = 0; i < length; i++)
|
||||
{
|
||||
charArray[i] = source[i];
|
||||
}
|
||||
}
|
||||
|
||||
std::string right(const std::string &sourceString, size_t numChars)
|
||||
{
|
||||
if (numChars >= sourceString.size())
|
||||
{
|
||||
return sourceString; // If numChars is greater or equal to the string size, return the whole string.
|
||||
}
|
||||
return sourceString.substr(sourceString.size() - numChars);
|
||||
}
|
||||
|
||||
uint16_t calculateCRC16(const uint8_t *data, size_t length)
|
||||
{
|
||||
const uint16_t polynomial = 0xA001; // CRC16-CCITT polynomial
|
||||
uint16_t crc = 0xFFFF; // Initial value
|
||||
|
||||
for (size_t i = 0; i < length; i++)
|
||||
{
|
||||
crc ^= data[i]; // XOR with the current data byte
|
||||
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
if (crc & 0x0001)
|
||||
{
|
||||
crc = (crc >> 1) ^ polynomial;
|
||||
}
|
||||
else
|
||||
{
|
||||
crc = crc >> 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint16_t crc16(const unsigned char *data_p, unsigned char length)
|
||||
{
|
||||
uint16_t x;
|
||||
uint16_t crc = 0xFFFF;
|
||||
|
||||
while (length--)
|
||||
{
|
||||
x = crc >> 8 ^ *data_p++;
|
||||
x ^= x >> 4;
|
||||
crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x << 5)) ^ ((unsigned short)x);
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
DATE getLicDate()
|
||||
{
|
||||
time_t ttime = time(0);
|
||||
tm *local_time = localtime(&ttime);
|
||||
int hoursSeconds = 3600 * local_time->tm_hour;
|
||||
int minutesSeconds = 60 * local_time->tm_min;
|
||||
int seconds = 1 * local_time->tm_sec;
|
||||
int totalSeconds = hoursSeconds + minutesSeconds + seconds;
|
||||
|
||||
#ifdef WINDOWS
|
||||
DATE dateOnly = ttime - totalSeconds + 7200; // (pro windows); // 7200 + vteřina za dvě hodiny pro srování
|
||||
#else
|
||||
DATE dateOnly = ttime - totalSeconds;
|
||||
#endif
|
||||
|
||||
return dateOnly;
|
||||
}
|
||||
|
||||
string getDate()
|
||||
{
|
||||
auto r = std::chrono::system_clock::now();
|
||||
auto rp = std::chrono::system_clock::to_time_t(r);
|
||||
std::string h(ctime(&rp)); // converting to c++ string
|
||||
return h;
|
||||
}
|
||||
|
||||
int encrypt(const unsigned char *plaintext, int plaintext_len, unsigned char *key,
|
||||
unsigned char *iv, unsigned char *ciphertext)
|
||||
{
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
|
||||
int len;
|
||||
|
||||
int ciphertext_len;
|
||||
|
||||
/* Create and initialise the context */
|
||||
if (!(ctx = EVP_CIPHER_CTX_new())) return -1;
|
||||
|
||||
|
||||
/*
|
||||
* Initialise the encryption operation. IMPORTANT - ensure you use a key
|
||||
* and IV size appropriate for your cipher
|
||||
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
|
||||
* IV size for *most* modes is the same as the block size. For AES this
|
||||
* is 128 bits
|
||||
*/
|
||||
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
|
||||
return -1;
|
||||
|
||||
/*
|
||||
* Provide the message to be encrypted, and obtain the encrypted output.
|
||||
* EVP_EncryptUpdate can be called multiple times if necessary
|
||||
*/
|
||||
if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
|
||||
return -1;
|
||||
ciphertext_len = len;
|
||||
|
||||
/*
|
||||
* Finalise the encryption. Further ciphertext bytes may be written at
|
||||
* this stage.
|
||||
*/
|
||||
if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
|
||||
return -1;
|
||||
ciphertext_len += len;
|
||||
|
||||
/* Clean up */
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
return ciphertext_len;
|
||||
}
|
||||
|
||||
int decrypt(const unsigned char *ciphertext, int ciphertext_len, unsigned char *key,
|
||||
unsigned char *iv, unsigned char *plaintext)
|
||||
{
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
|
||||
int len;
|
||||
|
||||
int plaintext_len;
|
||||
|
||||
/* Create and initialise the context */
|
||||
if (!(ctx = EVP_CIPHER_CTX_new()))
|
||||
return -1;
|
||||
|
||||
/*
|
||||
* Initialise the decryption operation. IMPORTANT - ensure you use a key
|
||||
* and IV size appropriate for your cipher
|
||||
* In this example we are using 256 bit AES (i.e. a 256 bit key). The
|
||||
* IV size for *most* modes is the same as the block size. For AES this
|
||||
* is 128 bits
|
||||
*/
|
||||
if (1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))
|
||||
return -1;
|
||||
|
||||
/*
|
||||
* Provide the message to be decrypted, and obtain the plaintext output.
|
||||
* EVP_DecryptUpdate can be called multiple times if necessary.
|
||||
*/
|
||||
if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
|
||||
return -1;
|
||||
plaintext_len = len;
|
||||
|
||||
/*
|
||||
* Finalise the decryption. Further plaintext bytes may be written at
|
||||
* this stage.
|
||||
*/
|
||||
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len))
|
||||
return -1;
|
||||
|
||||
plaintext_len += len;
|
||||
|
||||
/* Clean up */
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
return plaintext_len;
|
||||
}
|
||||
|
||||
// converts character array
|
||||
// to string and returns it
|
||||
string convertToString(char *a, int size)
|
||||
{
|
||||
int i;
|
||||
string s = "";
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
s = s + a[i];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
std::string base64_decode(const std::string &in)
|
||||
{
|
||||
std::string out;
|
||||
std::vector<int> T(256, -1);
|
||||
unsigned int i;
|
||||
for (i = 0; i < 64; i++)
|
||||
T[base64_url_alphabet[i]] = i;
|
||||
|
||||
int val = 0, valb = -8;
|
||||
for (i = 0; i < in.length(); i++)
|
||||
{
|
||||
unsigned char c = in[i];
|
||||
if (T[c] == -1)
|
||||
break;
|
||||
val = (val << 6) + T[c];
|
||||
valb += 6;
|
||||
if (valb >= 0)
|
||||
{
|
||||
out.push_back(char((val >> valb) & 0xFF));
|
||||
valb -= 8;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string base64_encode_ai(const std::string &input)
|
||||
{
|
||||
std::string encoded;
|
||||
|
||||
size_t input_length = input.length();
|
||||
size_t i = 0;
|
||||
|
||||
while (i < input_length)
|
||||
{
|
||||
unsigned char input_chunk[3] = {0};
|
||||
size_t chunk_size = 0;
|
||||
|
||||
// Fill the input chunk with up to 3 bytes from the input string
|
||||
for (size_t j = 0; j < 3; ++j)
|
||||
{
|
||||
if (i < input_length)
|
||||
{
|
||||
input_chunk[j] = input[i++];
|
||||
++chunk_size;
|
||||
}
|
||||
}
|
||||
|
||||
// Encode the input chunk into 4 Base64 characters
|
||||
encoded += base64_chars[(input_chunk[0] & 0xFC) >> 2];
|
||||
encoded += base64_chars[((input_chunk[0] & 0x03) << 4) |
|
||||
((input_chunk[1] & 0xF0) >> 4)];
|
||||
encoded += (chunk_size > 1)
|
||||
? base64_chars[((input_chunk[1] & 0x0F) << 2) |
|
||||
((input_chunk[2] & 0xC0) >> 6)]
|
||||
: '=';
|
||||
encoded += (chunk_size > 2)
|
||||
? base64_chars[input_chunk[2] & 0x3F]
|
||||
: '=';
|
||||
}
|
||||
|
||||
return encoded;
|
||||
}
|
||||
|
||||
unordered_map<string, string> getArguments(int argc, char *argv[])
|
||||
{
|
||||
const char splitChar = '=';
|
||||
unordered_map<string, string> result;
|
||||
if (argc <= 1)
|
||||
return result;
|
||||
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
bool isArgName = true;
|
||||
int argLength = strlen(argv[i]);
|
||||
string argName;
|
||||
string argValue;
|
||||
|
||||
for (int j = 0; j < argLength; j++)
|
||||
{
|
||||
if (argv[i][j] == splitChar)
|
||||
{
|
||||
isArgName = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isArgName)
|
||||
{
|
||||
argName += argv[i][j];
|
||||
}
|
||||
else
|
||||
{
|
||||
argValue += argv[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
result.insert(make_pair(argName, argValue));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
string getCompletePath(string fileName)
|
||||
{
|
||||
#ifdef WINDOWS
|
||||
return fileName;
|
||||
#else
|
||||
//warning TODO filesystem
|
||||
char path[PATH_MAX+1] = {};
|
||||
ssize_t length = readlink("/proc/self/exe", path, PATH_MAX);
|
||||
path[length] = '\0';
|
||||
string result = string(dirname(path)) + "/" + fileName;
|
||||
return result;
|
||||
//return std::string( result, (count > 0) ? count : 0 );
|
||||
// std::filesystem::path exePath = std::filesystem::canonical("/proc/self/exe"); // / std::filesystem::path(argv[0]));
|
||||
// std::filesystem::path fullPathOther = exePath.parent_path() / fileName;
|
||||
// std::string fullPathStrOther = fullPathOther.string();
|
||||
// return fullPathStrOther;
|
||||
#endif
|
||||
}
|
||||
|
||||
void appendStringToVector(const std::string &str, std::vector<unsigned char> &charVector)
|
||||
{
|
||||
size_t strLength = str.length();
|
||||
for (size_t i = 0; i < strLength; ++i)
|
||||
{
|
||||
charVector.push_back(static_cast<unsigned char>(str[i]));
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t calculateCRC16(std::vector<unsigned char> &charVector)
|
||||
{
|
||||
const uint16_t polynomial = 0xA001; // CRC16-CCITT polynomial
|
||||
uint16_t crc = 0xFFFF; // Initial value
|
||||
|
||||
size_t length = charVector.size();
|
||||
|
||||
for (size_t i = 0; i < length; i++)
|
||||
{
|
||||
crc ^= charVector[i]; // XOR with the current data byte
|
||||
|
||||
for (int j = 0; j < 8; j++)
|
||||
{
|
||||
if (crc & 0x0001)
|
||||
{
|
||||
crc = (crc >> 1) ^ polynomial;
|
||||
}
|
||||
else
|
||||
{
|
||||
crc = crc >> 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint32_t bytesToDword(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
|
||||
{
|
||||
return static_cast<uint32_t>(byte1) |
|
||||
(static_cast<uint32_t>(byte2) << 8) |
|
||||
(static_cast<uint32_t>(byte3) << 16) |
|
||||
(static_cast<uint32_t>(byte4) << 24);
|
||||
}
|
||||
|
||||
uint32_t bytesToWord(uint8_t byte1, uint8_t byte2)
|
||||
{
|
||||
return static_cast<uint32_t>(byte1) | (static_cast<uint32_t>(byte2) << 8);
|
||||
}
|
||||
|
||||
std::vector<unsigned char> joinVectors(const std::vector<unsigned char> &vector1, const std::vector<unsigned char> &vector2)
|
||||
{
|
||||
std::vector<unsigned char> result;
|
||||
result.insert(result.end(), vector1.begin(), vector1.end());
|
||||
result.insert(result.end(), vector2.begin(), vector2.end());
|
||||
return result;
|
||||
}
|
||||
|
||||
bool readFile(string fileName, vector<char> &output)
|
||||
{
|
||||
std::ifstream file(fileName, std::ios::in | std::ios::binary);
|
||||
|
||||
if (file.is_open() != 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
char byte;
|
||||
while (file.get(byte))
|
||||
{
|
||||
// Convert the char to unsigned char and push it into the vector
|
||||
output.push_back(byte);
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
28
src/common/utils.d
Normal file
28
src/common/utils.d
Normal file
@@ -0,0 +1,28 @@
|
||||
src/common/utils.o: src/common/utils.cpp include/openssl/conf.h \
|
||||
include/openssl/macros.h include/openssl/opensslconf.h \
|
||||
include/openssl/configuration.h include/openssl/opensslv.h \
|
||||
include/openssl/bio.h include/openssl/e_os2.h include/openssl/crypto.h \
|
||||
include/openssl/safestack.h include/openssl/stack.h \
|
||||
include/openssl/types.h include/openssl/cryptoerr.h \
|
||||
include/openssl/symhacks.h include/openssl/cryptoerr_legacy.h \
|
||||
include/openssl/core.h include/openssl/bioerr.h include/openssl/lhash.h \
|
||||
include/openssl/conferr.h include/openssl/conftypes.h \
|
||||
include/openssl/ssl.h include/openssl/comp.h include/openssl/comperr.h \
|
||||
include/openssl/x509.h include/openssl/buffer.h \
|
||||
include/openssl/buffererr.h include/openssl/evp.h \
|
||||
include/openssl/core_dispatch.h include/openssl/evperr.h \
|
||||
include/openssl/params.h include/openssl/bn.h include/openssl/bnerr.h \
|
||||
include/openssl/objects.h include/openssl/obj_mac.h \
|
||||
include/openssl/asn1.h include/openssl/asn1err.h \
|
||||
include/openssl/objectserr.h include/openssl/ec.h \
|
||||
include/openssl/ecerr.h include/openssl/rsa.h include/openssl/rsaerr.h \
|
||||
include/openssl/dsa.h include/openssl/dh.h include/openssl/dherr.h \
|
||||
include/openssl/dsaerr.h include/openssl/sha.h include/openssl/x509err.h \
|
||||
include/openssl/x509_vfy.h include/openssl/pkcs7.h \
|
||||
include/openssl/pkcs7err.h include/openssl/http.h include/openssl/pem.h \
|
||||
include/openssl/pemerr.h include/openssl/hmac.h include/openssl/async.h \
|
||||
include/openssl/asyncerr.h include/openssl/ct.h include/openssl/cterr.h \
|
||||
include/openssl/sslerr.h include/openssl/sslerr_legacy.h \
|
||||
include/openssl/prov_ssl.h include/openssl/ssl2.h include/openssl/ssl3.h \
|
||||
include/openssl/tls1.h include/openssl/dtls1.h include/openssl/srtp.h \
|
||||
include/common/utils.h
|
||||
BIN
src/common/utils.o
Normal file
BIN
src/common/utils.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user