<html><head><meta http-equiv="content-type" content="text/html; charset=us-ascii"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;">Hello Jinze.<br><br><div>The issue doesn't come from OpenSSL. It comes from at least two buffer overruns.<br><br></div><div>In <font face="Monaco"><span style="font-style: normal;">aesEncrypt</span></font>:<blockquote type="cite"><font face="Monaco"><span style="font-style: normal;">ret = EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, (const unsigned char*)key.c_str(), NULL);</span></font></blockquote><br>You use <font face="Monaco"><span style="font-style: normal;">key.c_str()</span></font> to set the key. However, key here is "<font face="Monaco"><span style="font-style: normal;">input</span></font>":<blockquote type="cite"><font face="Monaco"><span style="font-style: normal;">if (!aesEncrypt(content, "input", encrypted_content)) return -1;</span></font></blockquote><br><font face="Monaco"><span style="font-style: normal;">key.c_str()</span></font> returns a buffer of size 6: "<font face="Monaco"><span style="font-style: normal;">input</span></font>" plus the null-terminated byte. However, <font face="Monaco"><span style="font-style: normal;">EVP_aes_128_ecb </span></font>expects a buffer of at least 16 bytes.<div>Therefore, this is UB: you don't control the 10 bytes after the buffer returned by <font face="Monaco"><span style="font-style: normal;">key.c_str()</span></font>.</div><div><br></div><div>Same with <font face="Monaco"><span style="font-style: normal;">aesDecrypt</span></font>:</div><div><blockquote type="cite"><font face="Monaco"><span style="font-style: normal;">ret = EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, (const unsigned char*)key.c_str(), NULL); </span></font></blockquote></div><div><span><br></span></div><div><pre><blockquote type="cite"><font face="Monaco"><span style="font-style: normal;"><span id="x-apple-selection:start"></span>if (!aesDecrypt(encrypted_content, "input", decrypted_content)) {</span></font></blockquote></pre></div><div>If you set "<font face="Monaco"><span style="font-style: normal;">input</span></font>" to "<font face="Monaco"><span style="font-style: normal;">AAAAAAAAAAAAAAAA</span></font>" ("A" x 16), it works.</div><div><br></div><div>The main issue here is that you use the wrong container for storing your key materials and your buffers. You should use "<font face="Monaco"><span style="font-style: normal;">std::vector<std::byte></span></font>" (or "<font face="Monaco"><span style="font-style: normal;">std::vector<uint8_t></span></font>") with <font face="Monaco"><span style="font-style: normal;">std::vector::data()</span></font>.</div><div><br></div><div>Regards,</div><div><br></div><div><span id="x-apple-selection:end"></span><span><br></span><span><span id="x-apple-selection:end"></span><br><blockquote type="cite">On 12 Nov 2022, at 11:25, WuJinze via openssl-users <openssl-users@openssl.org> wrote:<br><br class="Apple-interchange-newline">sorry for my mistake. I found that the gist url can not display well in mail and here is the url: https://gist.github.com/GoGim1/77c9bebec1cc71cea066515b4623a051<br><br>WuJinze<br>294843472@qq.com<br> <br><br>------------------ Original ------------------<br>From:                                                                                                                        "WuJinze"                                                                                    <294843472@qq.com>;<br>Date: Sat, Nov 12, 2022 06:17 PM<br>To: "openssl-users"<openssl-users@openssl.org>;Subject: OpenSSL AES Decryption fails randomly C++<br><br>Dear OpenSSL Group,<br>    Greetings. I was working on writing simple aes encrypt/decrypt wrapper function in c++ and running into a strange problem. The minimal reproducible examples in gist seems working fine but when i uncomment lines 90-92, it will fail to decrypt randomly. Can someone help me to figure out what's wrong with the code?<br>    Here is my code: OpenSSL AES Decryption fails randomly C++ (github.com). OpenSSL version is OpenSSL 1.1.1f. G++ version is 9.4.0.<br>Regards, <br>Jinze<br></blockquote><br></span><div></div></div></div></body></html>