after calling OPENSSLcleanse should i also call .clear() on the vectors? also , does all…
- User
after calling OPENSSL_cleanse should i also call .clear() on the vectors? also , does all these calls to OPENSSL_cleanse and possible .clear() make it easier to track the function when someone is reverse engineering it?
inline bool mEncrypt(QByteArray& data, QByteArray& b64Password) { // Generate the password with the pre-defined size of the byteArray unsigned char randomByte; RAND_bytes(&randomByte, 1); int passwordSize = (b64Password.size() * 3) / 4; // Inverse of base64 encoding ratio std::vector password(passwordSize); RAND_bytes(password.data(), passwordSize);
// Generate a random salt RAND_bytes(&randomByte, 1); int saltSize = 16 + (randomByte % 6); // Random size between 16 and 21 std::vector<unsigned char> salt(saltSize); RAND_bytes(salt.data(), saltSize); // Use PBKDF2 for key derivation std::vector<unsigned char> key(32); std::vector<unsigned char> iv(12); // 12 bytes for GCM if (PKCS5_PBKDF2_HMAC(reinterpret_cast<const char*>(password.data()), password.size(), salt.data(), salt.size(), 5000, EVP_sha256(), key.size(), key.data()) != 1) { return false; } RAND_bytes(iv.data(), iv.size()); // Encode the password as base64 BIO* bio = BIO_push(BIO_new(BIO_f_base64()), BIO_new(BIO_s_mem())); BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); BIO_write(bio, password.data(), password.size()); BIO_flush(bio); BUF_MEM* bptr; BIO_get_mem_ptr(bio, &bptr); b64Password = QByteArray(bptr->data, bptr->length); OPENSSL_cleanse(password.data(), password.size()); OPENSSL_cleanse(bptr->data, bptr->length); BIO_free_all(bio); // Initialize encryption (using AES-256-GCM) EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); if (!ctx) return false; if (EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), nullptr, key.data(), iv.data()) != 1) { EVP_CIPHER_CTX_free(ctx); return false; } OPENSSL_cleanse(key.data(), key.size()); // Encrypt std::vector<unsigned char> encryptedData(data.size() + EVP_CIPHER_block_size(EVP_aes_256_gcm())); int encryptedLength = 0; if (EVP_EncryptUpdate(ctx, encryptedData.data(), &encryptedLength, reinterpret_cast<const unsigned char*>(data.constData()), data.size()) != 1) { EVP_CIPHER_CTX_free(ctx); return false; } int finalLength = 0; if (EVP_EncryptFinal_ex(ctx, encryptedData.data() + encryptedLength, &finalLength) != 1) { EVP_CIPHER_CTX_free(ctx); return false; } encryptedData.resize(encryptedLength + finalLength); // Get the authentication tag std::vector<unsigned char> tag(16); if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag.data()) != 1) { EVP_CIPHER_CTX_free(ctx); return false; } EVP_CIPHER_CTX_free(ctx); // Generate random string to prepend RAND_bytes(&randomByte, 1); int prependStrSize = 6 + (randomByte % 6); // Random size between 6 and 11 std::vector<unsigned char> prependStr(prependStrSize); RAND_bytes(prependStr.data(), prependStrSize); // Combine all parts std::vector<unsigned char> result; result.reserve(prependStr.size() + salt.size() + iv.size() + encryptedData.size() + tag.size()); result.insert(result.end(), prependStr.begin(), prependStr.end()); result.insert(result.end(), salt.begin(), salt.end()); result.insert(result.end(), iv.begin(), iv.end()); result.insert(result.end(), encryptedData.begin(), encryptedData.end()); result.insert(result.end(), tag.begin(), tag.end()); // Base64 encode bio = BIO_push(BIO_new(BIO_f_base64()), BIO_new(BIO_s_mem())); BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); BIO_write(bio, result.data(), result.size()); BIO_flush(bio); BIO_get_mem_ptr(bio, &bptr); OPENSSL_cleanse(data.data(), data.size()); data = QByteArray(bptr->data, bptr->length); OPENSSL_cleanse(bptr->data, bptr->length); BIO_free_all(bio); OPENSSL_cleanse(salt.data(), salt.size()); OPENSSL_cleanse(iv.data(), iv.size()); OPENSSL_cleanse(encryptedData.data(), encryptedData.size()); OPENSSL_cleanse(tag.data(), tag.size()); OPENSSL_cleanse(result.data(), result.size()); return true; - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.