Re: TOTP implementation
Catherine Issabel <[email protected]> Thu, 2 Nov 2023 11:23:24 -0700 (PDT)
| Newsgroups | gmane.comp.encryption.cryptopp |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_6494_497137516.1698949404874 Content-Type: multipart/alternative; boundary="----=_Part_6495_896873809.1698949404874" ------=_Part_6495_896873809.1698949404874 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable $500 million dollars available only for good and trusted receiver or=20 mandate=20 Kindly contact the sender for more information:+1 (336) 345-9681 On Friday, August 4, 2023 at 6:28:39=E2=80=AFPM UTC+1 [email protected] w= rote: > Also, their version of Base32Decoder uses DUDE instead of RFC 4648. > > On Friday, August 4, 2023 at 2:14:12=E2=80=AFPM UTC+1 Steven Green wrote: > >> Shouldn't... >> >> * hmac.Update(byteArray, sizeof(byteArraySize));* >> >> be simply.. >> >> >> * hmac.Update(byteArray, byteArraySize); * >> >> Otherwise you passing in sizeof(size_t) rather than the size of the=20 >> byteArray (which is sizeof(uint64_t)). Although on a 64 bit system I wou= ld=20 >> expect them both to be 8 bytes, so maybe there is another problem. >> >> Otherwise the function looks very similar to what I have. >> >> - Steven >> On 03/08/2023 10:51, Aravindh B wrote: >> >> Need to get some information how counter value is handled in this librar= y. >> >> std::string generateTOTP(const std::string& secretKey) { >> >> using namespace CryptoPP; >> const int timeStep =3D 30; // Time step in seconds >> SecByteBlock key(HMAC<SHA1>::DEFAULT_KEYLENGTH); >> StringSource(secretKey, true, new Base32Decoder(new ArraySink(key,=20 >> key.size()))); >> >> // Get the current time in 30-second intervals (TOTP time step) >> std::time_t currentTime =3D std::time(nullptr); >> uint64_t counter =3D static_cast<uint64_t>(currentTime) / timeStep; >> const size_t byteArraySize =3D sizeof(counter); >> byte* byteArray =3D new byte[byteArraySize]; >> >> //Big endian representation >> for (size_t i =3D 0; i < byteArraySize; ++i) { >> byteArray[sizeof(counter) - 1 - i] =3D static_cast<byte>((counter >> (8 = *=20 >> i)) & 0xFF); >> } >> >> // Calculate the HMAC-SHA1 using the secret key and the counter bytes >> byte mac[CryptoPP::HMAC<CryptoPP::SHA1>::DIGESTSIZE]; >> CryptoPP::HMAC<CryptoPP::SHA1> hmac(key, sizeof(key)); >> >> * hmac.Update(byteArray, sizeof(byteArraySize)); * hmac.Final(mac); >> >> // Generate the TOTP value from the last 4 bits of the HMAC-SHA1 result >> int offset =3D mac[CryptoPP::HMAC<CryptoPP::SHA1>::DIGESTSIZE - 1] & 0xF= ; >> uint32_t otpValue =3D (mac[offset] & 0x7F) << 24 | >> (mac[offset + 1] & 0xFF) << 16 | >> (mac[offset + 2] & 0xFF) << 8 | >> (mac[offset + 3] & 0xFF); >> >> // Convert the OTP value to a 6-digit OTP (modulo 10^6) >> otpValue %=3D 1000000; >> >> //Format the OTP as a 6-digit string with leading zeros if needed >> std::string otp =3D std::to_string(otpValue); >> otp.insert(otp.begin(), 6 - otp.size(), '0'); >> delete[] byteArray; >> >> return otp; >> } >> >> This code have issues with hmac.update(). Seems problem with counter=20 >> value.Does anyone help to generate correct otp? >> >> --=20 >> You received this message because you are subscribed to the Google Group= s=20 >> "Crypto++ Users" group. >> To unsubscribe from this group and stop receiving emails from it, send a= n=20 >> email to [email protected]. >> To view this discussion on the web visit=20 >> https://groups.google.com/d/msgid/cryptopp-users/5cc93e58-cf8b-41fd-9855= -8b84e707b19fn%40googlegroups.com=20 >> <https://groups.google.com/d/msgid/cryptopp-users/5cc93e58-cf8b-41fd-985= 5-8b84e707b19fn%40googlegroups.com?utm_medium=3Demail&utm_source=3Dfooter> >> . >> >> --=20 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 e= mail to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/= cryptopp-users/ef830633-e0ca-417f-b23a-6cdb32ac237dn%40googlegroups.com. ------=_Part_6495_896873809.1698949404874 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <br />$500 million dollars available only for good and trusted receiver or = mandate <br />Kindly contact the sender for more information:+1 (336) 345-9= 681<br /><div class=3D"gmail_quote"><div dir=3D"auto" class=3D"gmail_attr">= On Friday, August 4, 2023 at 6:28:39=E2=80=AFPM UTC+1 [email protected] w= rote:<br/></div><blockquote class=3D"gmail_quote" style=3D"margin: 0 0 0 0.= 8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Also, t= heir version of Base32Decoder uses DUDE instead of RFC 4648.<br><br><div cl= ass=3D"gmail_quote"><div dir=3D"auto" class=3D"gmail_attr">On Friday, Augus= t 4, 2023 at 2:14:12=E2=80=AFPM UTC+1 Steven Green wrote:<br></div><blockqu= ote class=3D"gmail_quote" style=3D"margin:0 0 0 0.8ex;border-left:1px solid= rgb(204,204,204);padding-left:1ex"><u></u> =20 =20 =20 <div> <p>Shouldn't...<br> </p> <p><b><span style=3D"white-space:pre"> </span>hmac.Update(byteArray, sizeof(byteArraySize));</b></p> <p>be simply..<br> </p> <p><b><span style=3D"white-space:pre"> </span>hmac.Update(byteArray, byteArraySize);<br> </b></p> <p>Otherwise you passing in sizeof(size_t) rather than the size of the byteArray (which is sizeof(uint64_t)). Although on a 64 bit system I would expect them both to be 8 bytes, so maybe there is another problem.<br> </p> <p>Otherwise the function looks very similar to what I have.</p> <p>- Steven<br> </p></div><div> <div>On 03/08/2023 10:51, Aravindh B wrote:<br> </div> </div><div><blockquote type=3D"cite"> =20 Need to get some information how counter value is handled in this library.<br> <br> <div>std::string generateTOTP(const std::string& secretKey) {<br> <br> <span style=3D"white-space:pre"> </span>using namespace CryptoPP;<br> <span style=3D"white-space:pre"> </span>const int timeStep =3D 30; // Time step in seconds<br> <span style=3D"white-space:pre"> </span>SecByteBlock key(HMAC<SHA1>::DEFAULT_KEYLENGTH);<br> <span style=3D"white-space:pre"> </span>StringSource(secretKey, true, new Base32Decoder(new ArraySink(key, key.size())));<br> <br> <span style=3D"white-space:pre"> </span>// Get the current time in 30-second intervals (TOTP time step)<br> <span style=3D"white-space:pre"> </span>std::time_t currentTime =3D std::time(nullptr);<br> <span style=3D"white-space:pre"> </span>uint64_t counter =3D static_cast<uint64_t>(currentTime) / timeStep;<br> <span style=3D"white-space:pre"> </span>const size_t byteArraySize =3D sizeof(counter);<br> <span style=3D"white-space:pre"> </span>byte* byteArray =3D new byte[byteArraySize];<br> <br> <span style=3D"white-space:pre"> </span>//Big endian representation</div> <div>for (size_t i =3D 0; i < byteArraySize; ++i) {<br> <span style=3D"white-space:pre"> </span>byteArray[sizeof(counter) - 1 - i] =3D static_cast<byte>((counter >> (8 * i)) & 0xFF);<br> <span style=3D"white-space:pre"> </span>}<br> <br> <span style=3D"white-space:pre"> </span>// Calculate the HMAC-SHA1 using the secret key and the counter bytes<br> <span style=3D"white-space:pre"> </span>byte mac[CryptoPP::HMAC<CryptoPP::SHA1>::DIGESTSIZE];<br> <span style=3D"white-space:pre"> </span>CryptoPP::HMAC<CryptoPP:= :SHA1> hmac(key, sizeof(key));<br> <b><span style=3D"white-space:pre"> </span>hmac.Update(byteArray, sizeof(byteArraySize));<br> </b><span style=3D"white-space:pre"> </span>hmac.Final(mac);<br> <br> <span style=3D"white-space:pre"> </span>// Generate the TOTP value from the last 4 bits of the HMAC-SHA1 result<br> <span style=3D"white-space:pre"> </span>int offset =3D mac[CryptoPP::HMAC<CryptoPP::SHA1>::DIGESTSIZE - 1] & 0xF;<br> <span style=3D"white-space:pre"> </span>uint32_t otpValue =3D (mac[offset] & 0x7F) << 24 |<br> <span style=3D"white-space:pre"> </span>(mac[offset + 1] & 0xFF) << 16 |<br> <span style=3D"white-space:pre"> </span>(mac[offset + 2] & 0xFF) << 8 |<br> <span style=3D"white-space:pre"> </span>(mac[offset + 3] & 0xFF);<br> <br> <span style=3D"white-space:pre"> </span>// Convert the OTP value to a 6-digit OTP (modulo 10^6)<br> <span style=3D"white-space:pre"> </span>otpValue %=3D 1000000;<br> <br> <span style=3D"white-space:pre"> </span>//Format the OTP as a 6-digit string with leading zeros if needed<br> <span style=3D"white-space:pre"> </span>std::string otp =3D std::to_string(otpValue);<br> <span style=3D"white-space:pre"> </span>otp.insert(otp.begin(), 6 - otp.size(), '0');<br> <span style=3D"white-space:pre"> </span>delete[] byteArray;<br> <br> <span style=3D"white-space:pre"> </span>return otp;<br> }<br> <br> This code have issues with hmac.update(). Seems problem with counter value.Does anyone help to generate correct otp?</div></bloc= kquote></div><div><blockquote type=3D"cite"> -- <br> You received this message because you are subscribed to the Google Groups "Crypto++ Users" group.<br> To unsubscribe from this group and stop receiving emails from it, send an email to <a rel=3D"nofollow">[email protected]= m</a>.<br> To view this discussion on the web visit <a href=3D"https://groups.go= ogle.com/d/msgid/cryptopp-users/5cc93e58-cf8b-41fd-9855-8b84e707b19fn%40goo= glegroups.com?utm_medium=3Demail&utm_source=3Dfooter" rel=3D"nofollow" = target=3D"_blank" data-saferedirecturl=3D"https://www.google.com/url?hl=3De= n&q=3Dhttps://groups.google.com/d/msgid/cryptopp-users/5cc93e58-cf8b-41= fd-9855-8b84e707b19fn%2540googlegroups.com?utm_medium%3Demail%26utm_source%= 3Dfooter&source=3Dgmail&ust=3D1699035788682000&usg=3DAOvVaw1B-2= FJaKqplkoEpOtP5INs">https://groups.google.com/d/msgid/cryptopp-users/5cc93e= 58-cf8b-41fd-9855-8b84e707b19fn%40googlegroups.com</a>.<br> </blockquote> </div> </blockquote></div></blockquote></div> <p></p> -- <br /> You received this message because you are subscribed to the Google Groups &= quot;Crypto++ Users" group.<br /> To unsubscribe from this group and stop receiving emails from it, send an e= mail to <a href=3D"mailto:[email protected]">cryp= [email protected]</a>.<br /> To view this discussion on the web visit <a href=3D"https://groups.google.c= om/d/msgid/cryptopp-users/ef830633-e0ca-417f-b23a-6cdb32ac237dn%40googlegro= ups.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/d= /msgid/cryptopp-users/ef830633-e0ca-417f-b23a-6cdb32ac237dn%40googlegroups.= com</a>.<br /> ------=_Part_6495_896873809.1698949404874-- ------=_Part_6494_497137516.1698949404874--