fixes xor memory encryption
uint64_t memoryPass;
-uint64_t maxSupportedPass = 692533995824480255;
+// uint64_t maxSupportedPass = 692533995824480255;
char* xorCrypt(const char* string, uint64_t key, size_t len) {
size_t length = sizeof(key);
char* str = secAlloc(len + 1);
+ char* s = str;
for (size_t i = 0; i < len; i++) {
- *str++ = *string++ ^ *((unsigned char*)&key + (i % length));
+ *str = *string ^ *((unsigned char*)&key + (i % length));
+ str++;
+ string++;
}
- return str;
+ return s;
}
char* memoryDecrypt(const char* cipher) {
@@ -27,24 +30,32 @@ char* memoryDecrypt(const char* cipher) {
}
char* fileText = oidc_strcopy(cipher);
size_t len = atoi(strtok(fileText, ":"));
- char* cipher_tex = strtok(NULL, ":");
- char* decrypted = xorCrypt(cipher_tex, memoryPass, len);
+ char* cipher_hex = strtok(NULL, ":");
+ unsigned char* cipher_bin = secAlloc(sizeof(char) * (len + 1));
+ sodium_hex2bin(cipher_bin, len + 1, cipher_hex, 2 * len + 1, NULL, NULL,
+ NULL);
+ char* decrypted = xorCrypt((char*)cipher_bin, memoryPass, len);
+ secFree(cipher_bin);
secFree(fileText);
return decrypted;
}
char* memoryEncrypt(const char* text) {
size_t len = strlen(text);
- char* cipher_hex = xorCrypt(text, memoryPass, len);
+ char* cipher = xorCrypt(text, memoryPass, len);
+ char* cipher_hex = secAlloc(sizeof(char) * (2 * len + 1));
+ sodium_bin2hex(cipher_hex, 2 * len + 1, (unsigned char*)cipher, len);
+ secFree(cipher);
char* fmt = "%lu:%s";
- char* cipher = oidc_sprintf(fmt, len, cipher_hex);
+ char* ciphered = oidc_sprintf(fmt, len, cipher_hex);
secFree(cipher_hex);
- return cipher;
+ return ciphered;
}
void initMemoryCrypt() {
- uint32_t limit = maxSupportedPass - 0xffffffff;
- uint64_t a = randombytes_uniform(limit);
+ // uint32_t limit = maxSupportedPass - 0xffffffff;
+ // uint64_t a = randombytes_uniform(limit);
+ uint64_t a = randombytes_random();
uint32_t b = randombytes_random();
uint64_t pass = (a << 32) | b;
memoryPass = pass;
