Re: [cryptopp-users] StringSource is using "new" operator. Should be deleting the memory or is it auto destroyed?

2023-04-27 Thread Jeffrey Walton
On Thu, Apr 27, 2023 at 1:57 PM Dwight Kulkarni  wrote:
>
> Ok I think those RSA errors with memory are due to this pipelining, because 
> the b64decode(..) b64encode(..) is called every where, and if I understand 
> correctly, the string inside will be destroyed and I have to use the 
> redirector. Can you confirm if you agree with this code change and my 
> understanding of the problem ?
>
> Previous Code:
> std::string b64encode(std::string str)
> {
>using namespace CryptoPP;
>
> std::string encoded;
> StringSource(str, true,
> new Base64Encoder(
>new StringSink(encoded),
>false
> )
> );
> return encoded; //encoded will be destroyed once StringSource(..) goes 
> out of scope
> }

This code is Ok.

The variable 'encoded' is not destroyed because it is a reference, and
not a pointer. 'encoded' is a stack variable. It will be destroyed
when the stack frame is cleaned up.

> New Code:
>
> std::string b64encode(std::string str)
> {
>using namespace CryptoPP;
>
> std::string encoded;
> Base64Encoder df(new CryptoPP::StringSink(encoded),false);
> StringSource(str, true, new Redirector(df) );
> return encoded; //encoded won't be destroyed
> }

This code is Ok, too. This code is effectively the same as the first
example. The Base64Encoder is now an automatic variable, and it will
be cleaned up when the destructor runs during stack cleanup.

The variable 'encoded' is not destroyed because it is a reference, and
not a pointer. 'encoded' is a stack variable. It will be destroyed
when the stack frame is cleaned up.

Your memory error lies elsewhere. Run Asan over your program.

Jeff

-- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cryptopp-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/cryptopp-users/CAH8yC8m%2B8_ARwU6ZSfQcpW-LQ_V%3DZVRvxJLJ4vzpKaiobzMBFQ%40mail.gmail.com.


Re: [cryptopp-users] StringSource is using "new" operator. Should be deleting the memory or is it auto destroyed?

2023-04-27 Thread Dwight Kulkarni
Hi Jeff,

Ok I think those RSA errors with memory are due to this pipelining, because 
the b64decode(..) b64encode(..) is called every where, and if I understand 
correctly, the string inside will be destroyed and I have to use the 
redirector. Can you confirm if you agree with this code change and my 
understanding of the problem ?

*Previous Code:*
std::string b64encode(std::string str)
{
std::string encoded;
CryptoPP::StringSource(str, true,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(encoded),
false 
)
);
return encoded; //encoded will be destroyed once StringSource(..) goes out 
of scope
}


*New Code:*

std::string b64encode(std::string str)
{
std::string encoded;
CryptoPP::Base64Encoder df(new CryptoPP::StringSink(encoded),false);
CryptoPP::StringSource(str, true, new Redirector(df) );
return encoded; //encoded won't be destroyed
}


based on this:

CCM< AES, TAG_SIZE >::Decryption d; 
d.SetKeyWithIV( key, key.size(), iv, sizeof(iv) ); 
AuthenticatedDecryptionFilter df( d, new StringSink( recovered ) ); // 
AuthenticatedDecryptionFilter // Cipher text includes the MAC tag 
StringSource ss( cipher, true, new Redirector( df ) ); // StringSource // 
If the object does not throw, here's the only // opportunity to check the 
data's integrity 
bool b = df.GetLastResult(); 
if( true == b ) { cout << recovered << endl; }




On Thursday, April 27, 2023 at 1:35:43 PM UTC-4 Jeffrey Walton wrote:

On Thu, Apr 27, 2023 at 1:31 PM Dwight Kulkarni  
wrote: 
> 
> I note an example code sample for b64encode(..). I note that there are 
"new" calls inside the constructor(..). 
> 
> The result is returned in the encoded string. 
> 
> Do I need to do anything to delete the memory from the new calls ? 
> 
> What happens to the memory in the variable "encoded", if the variable is 
passed up to another function and the StringSink is destroyed. 
> 
> Should I make a copy of encoded and send it on to be safe ? 

https://www.cryptopp.com/wiki/Pipelining#Ownership 

Jeff 

-- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cryptopp-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/cryptopp-users/3cb2a4e9-3319-49ee-a7f3-4ab8e7596043n%40googlegroups.com.


Re: [cryptopp-users] StringSource is using "new" operator. Should be deleting the memory or is it auto destroyed?

2023-04-27 Thread Jeffrey Walton
On Thu, Apr 27, 2023 at 1:31 PM Dwight Kulkarni  wrote:
>
> I note an example code sample for b64encode(..).   I note that there are 
> "new" calls inside the constructor(..).
>
> The result is returned in the encoded string.
>
> Do I need to do anything to delete the memory from the new calls ?
>
> What happens to the memory in the variable "encoded", if the variable is 
> passed up to another function and the StringSink is destroyed.
>
> Should I make a copy of encoded and send it on to be safe ?

https://www.cryptopp.com/wiki/Pipelining#Ownership

Jeff

-- 
You received this message because you are subscribed to the Google Groups 
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to cryptopp-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/cryptopp-users/CAH8yC8%3DbpnWtPR-J4VBPWX0b1-amHU%3DPZcs65UMDtaGnHH_eLg%40mail.gmail.com.