Skip to content

Commit 0f0e36d

Browse files
committed
Merge bitcoin/bitcoin#29815: crypto: chacha20: always use our fallback timingsafe_bcmp rather than libc's
2d18194 crypto: chacha20: always use our fallback timingsafe_bcmp rather than libc's (Cory Fields) Pull request description: Looking at libc sources, apple and openbsd implementations match our naive fallback. Only FreeBSD (and only x86_64) seems to [implement an optimized version](https://github.com/freebsd/freebsd-src/blob/main/lib/libc/amd64/string/timingsafe_bcmp.S). It's not worth the hassle of using a platform-specific function for such little gain. Additionally, as mentioned below, this is the only case outside of sha2 that requires an autoconf check, and I have upcoming PRs to remove the sha2 ones. Apple's [impl is unoptimized](https://opensource.apple.com/source/Libc/Libc-1244.1.7/string/FreeBSD/timingsafe_bcmp.c.auto.html). As-is [OpenBSD's impl](https://github.com/openbsd/src/blob/master/lib/libc/string/timingsafe_bcmp.c). Relevant IRC conversation with sipa: > \<cfields\> sipa: chacha20poly1305.cpp uses libc's timingsafe_bcmp when possible. But looking around at apple/freebsd/openbsd, I don't see any impl that doesn't use the naive implementation that matches our fallback... > \<cfields\> is there any reason to belive there's an optimized impl somewhere that we're actually hitting? > \<cfields\> asking because after cleaning up sha2, timingsafe_bcmp is the last autoconf check that remains in all of crypto. It'd make life easy if we could just always use our internal one. > \<cfields\> *all of crypto/ > \<sipa\> cfields: let's get rid of the dependency then > \<sipa\> it's a trivial function > \<sipa\> and if we need it for some platforms, no real reason not to use it on all After the above discusstion, I did end up finding the x86_64-optimized FreeBSD impl, but I don't think that's all that significant. ACKs for top commit: sipa: utACK 2d18194 fanquake: ACK 2d18194 TheCharlatan: ACK 2d18194 theStack: ACK 2d18194 Tree-SHA512: b9583e19ac2f77c5d572aa5b95bc4b53669d5717e5708babef930644980de7c5d06a9c7decd5c2b559d70b8597328ecfe513375e3d8c3ef523db80012dfe9266
2 parents b5d2118 + 2d18194 commit 0f0e36d

File tree

2 files changed

+2
-13
lines changed

2 files changed

+2
-13
lines changed

configure.ac

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,8 +968,6 @@ AC_CHECK_DECLS([setsid])
968968

969969
AC_CHECK_DECLS([pipe2])
970970

971-
AC_CHECK_FUNCS([timingsafe_bcmp])
972-
973971
AC_MSG_CHECKING([for __builtin_clzl])
974972
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]], [[
975973
(void) __builtin_clzl(0);

src/crypto/chacha20poly1305.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#if defined(HAVE_CONFIG_H)
6-
#include <config/bitcoin-config.h>
7-
#endif
8-
95
#include <crypto/chacha20poly1305.h>
106

117
#include <crypto/common.h>
@@ -30,10 +26,7 @@ void AEADChaCha20Poly1305::SetKey(Span<const std::byte> key) noexcept
3026

3127
namespace {
3228

33-
#ifndef HAVE_TIMINGSAFE_BCMP
34-
#define HAVE_TIMINGSAFE_BCMP
35-
36-
int timingsafe_bcmp(const unsigned char* b1, const unsigned char* b2, size_t n) noexcept
29+
int timingsafe_bcmp_internal(const unsigned char* b1, const unsigned char* b2, size_t n) noexcept
3730
{
3831
const unsigned char *p1 = b1, *p2 = b2;
3932
int ret = 0;
@@ -42,8 +35,6 @@ int timingsafe_bcmp(const unsigned char* b1, const unsigned char* b2, size_t n)
4235
return (ret != 0);
4336
}
4437

45-
#endif
46-
4738
/** Compute poly1305 tag. chacha20 must be set to the right nonce, block 0. Will be at block 1 after. */
4839
void ComputeTag(ChaCha20& chacha20, Span<const std::byte> aad, Span<const std::byte> cipher, Span<std::byte> tag) noexcept
4940
{
@@ -97,7 +88,7 @@ bool AEADChaCha20Poly1305::Decrypt(Span<const std::byte> cipher, Span<const std:
9788
m_chacha20.Seek(nonce, 0);
9889
std::byte expected_tag[EXPANSION];
9990
ComputeTag(m_chacha20, aad, cipher.first(cipher.size() - EXPANSION), expected_tag);
100-
if (timingsafe_bcmp(UCharCast(expected_tag), UCharCast(cipher.last(EXPANSION).data()), EXPANSION)) return false;
91+
if (timingsafe_bcmp_internal(UCharCast(expected_tag), UCharCast(cipher.last(EXPANSION).data()), EXPANSION)) return false;
10192

10293
// Decrypt (starting at block 1).
10394
m_chacha20.Crypt(cipher.first(plain1.size()), plain1);

0 commit comments

Comments
 (0)