开放式rand_bytes函数导致内存泄漏
我已经为我的加密算法创建了此类,但是当调用rand_bytes函数时,我有此例外。 “ 0x007FF92BA7C39B(ntdll.dll)在consoleapplication.exe中:0xc0000005:访问违规写作位置0x00000000000000000000。” 我无法弄清楚在哪里导致内存泄漏。
namespace crypto
{
struct PARAMS
{
const EVP_CIPHER* cipher;
unsigned char* key, * iv;
int key_length, block_length;
public:
PARAMS() : cipher(NULL), key(NULL), iv(NULL)
, key_length(0), block_length(0) {}
};
class CRYPT
{
private:
struct PARAMS _params;
private:
bool GenerateKey() {
this->_params.key = (unsigned char*)calloc(static_cast<size_t>(this->_params.key_length) + 1, sizeof(*this->_params.key));
if (this->_params.key == NULL)
{
MessageBoxA(NULL, strerror(errno), "error", MB_OK | MB_ICONERROR);
return false;
}
if (this->_params.block_length != 0) {
this->_params.iv = (unsigned char*)calloc(static_cast<size_t>(this->_params.block_length) + 1, sizeof(*this->_params.iv));
if (this->_params.iv == NULL)
{
MessageBoxA(NULL, strerror(errno), "error", MB_OK | MB_ICONERROR);
return false;
}
}
// filling buffer with random bytes for key and iv
if (RAND_bytes(this->_params.key, this->_params.key_length) != 1) return false;
if (RAND_bytes(this->_params.iv, this->_params.block_length) != 1) return false;
return true;
}
public:
~CRYPT() {
if (this->_params.key) {
memset(this->_params.key, 0, static_cast<size_t>(this->_params.key_length));
free(this->_params.key);
}
if (this->_params.iv) {
memset(this->_params.iv, 0, static_cast<size_t>(this->_params.block_length));
free(this->_params.iv);
}
}
public:
bool init() {
this->_params.cipher = EVP_aes_128_ctr();
// now we can set key length and iv length
this->_params.key_length = EVP_CIPHER_key_length(this->_params.cipher);
this->_params.block_length = EVP_CIPHER_iv_length(this->_params.cipher);
// generate a key pair
if (!this->GenerateKey()) return false;
return true;
}
};
}
int main()
{
crypto::CRYPT cr;
if (!cr.init()) {
puts("set-up failed!");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
此致。
I've created this class for my cryptography algorithms but i have this exception when rand_bytes function has been called. "0x00007FF92BA7C39B (ntdll.dll) in ConsoleApplication.exe: 0xC0000005: Access violation writing location 0x0000000000000000."
I can't figure out where im causing memory leak.
namespace crypto
{
struct PARAMS
{
const EVP_CIPHER* cipher;
unsigned char* key, * iv;
int key_length, block_length;
public:
PARAMS() : cipher(NULL), key(NULL), iv(NULL)
, key_length(0), block_length(0) {}
};
class CRYPT
{
private:
struct PARAMS _params;
private:
bool GenerateKey() {
this->_params.key = (unsigned char*)calloc(static_cast<size_t>(this->_params.key_length) + 1, sizeof(*this->_params.key));
if (this->_params.key == NULL)
{
MessageBoxA(NULL, strerror(errno), "error", MB_OK | MB_ICONERROR);
return false;
}
if (this->_params.block_length != 0) {
this->_params.iv = (unsigned char*)calloc(static_cast<size_t>(this->_params.block_length) + 1, sizeof(*this->_params.iv));
if (this->_params.iv == NULL)
{
MessageBoxA(NULL, strerror(errno), "error", MB_OK | MB_ICONERROR);
return false;
}
}
// filling buffer with random bytes for key and iv
if (RAND_bytes(this->_params.key, this->_params.key_length) != 1) return false;
if (RAND_bytes(this->_params.iv, this->_params.block_length) != 1) return false;
return true;
}
public:
~CRYPT() {
if (this->_params.key) {
memset(this->_params.key, 0, static_cast<size_t>(this->_params.key_length));
free(this->_params.key);
}
if (this->_params.iv) {
memset(this->_params.iv, 0, static_cast<size_t>(this->_params.block_length));
free(this->_params.iv);
}
}
public:
bool init() {
this->_params.cipher = EVP_aes_128_ctr();
// now we can set key length and iv length
this->_params.key_length = EVP_CIPHER_key_length(this->_params.cipher);
this->_params.block_length = EVP_CIPHER_iv_length(this->_params.cipher);
// generate a key pair
if (!this->GenerateKey()) return false;
return true;
}
};
}
int main()
{
crypto::CRYPT cr;
if (!cr.init()) {
puts("set-up failed!");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Best regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论