开放式rand_bytes函数导致内存泄漏

发布于 2025-02-03 01:11:25 字数 2704 浏览 1 评论 0原文

我已经为我的加密算法创建了此类,但是当调用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文