reinterpret_cast<byte*> truncating to 8

17 views
Skip to first unread message

Devharsh Trivedi

unread,
Mar 5, 2021, 5:23:20 PM3/5/21
to Crypto++ Users
I am trying to convert a std::string to cryptopp::byte like this
const byte* pt = reinterpret_cast<const byte*>(&alert[0]);

but it doesn't copy the full string, as confirmed by sizeof
sizeof(alert) = 24
sizeof(pt) = 8

why is it truncating to first 8 bytes?

Jeffrey Walton

unread,
Mar 6, 2021, 4:45:58 AM3/6/21
to Crypto++ Users
pt is a pointer. A pointer on x86_64 is 8 bytes.

Jeff

Devharsh Trivedi

unread,
Mar 9, 2021, 3:45:47 PM3/9/21
to Crypto++ Users
Thank you for your reply.

I am following this example - https://www.cryptopp.com/wiki/XChaCha20Poly1305

and trying to use std::string instead of byte

instead of using byte array for ct like this
dec.DecryptAndVerify(rt, mac, sizeof(mac), iv, sizeof(iv), aad, sizeof(aad), ct, sizeof(ct));

I am trying to use string like this

std::string cipher;

StringSource(ct, sizeof(ct), true, new HexEncoder(new StringSink(cipher)));

const byte* xct = reinterpret_cast<const byte*>(cipher.data());

dec.DecryptAndVerify(rt, mac, sizeof(mac), iv, sizeof(iv), aad, sizeof(aad), xct, sizeof(xct));


but this doesn't seem to work

Jeffrey Walton

unread,
Mar 9, 2021, 3:53:42 PM3/9/21
to Crypto++ Users
On Tuesday, March 9, 2021 at 3:45:47 PM UTC-5 devhar...@gmail.com wrote:
Thank you for your reply.

I am following this example - https://www.cryptopp.com/wiki/XChaCha20Poly1305

and trying to use std::string instead of byte

instead of using byte array for ct like this
dec.DecryptAndVerify(rt, mac, sizeof(mac), iv, sizeof(iv), aad, sizeof(aad), ct, sizeof(ct));

I am trying to use string like this

std::string cipher;

StringSource(ct, sizeof(ct), true, new HexEncoder(new StringSink(cipher)));

const byte* xct = reinterpret_cast<const byte*>(cipher.data());

dec.DecryptAndVerify(rt, mac, sizeof(mac), iv, sizeof(iv), aad, sizeof(aad), xct, sizeof(xct));


 dec.DecryptAndVerify(rt, mac, sizeof(mac), iv, sizeof(iv), aad, sizeof(aad), xct, cipher.size());

Jeff

Devharsh Trivedi

unread,
Mar 9, 2021, 4:00:21 PM3/9/21
to Crypto++ Users
cipher.size() doesn't help :/

the problem is not related to size but the xct pointer itself

however, if I do something similar with mac, it works

const byte* xmac = reinterpret_cast<const byte*>(strMac.data());

dec.DecryptAndVerify(rt, xmac, sizeof(xmac), iv, sizeof(iv), aad, sizeof(aad), ct, sizeof(ct));


the issue arises when I try to work with xct

Reply all
Reply to author
Forward
0 new messages