Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: make libsodium an optional build dependency #9668

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1193,11 +1193,6 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang" AND ARCH_WIDTH EQUAL "32" AND NOT IOS AND
endif()
endif()

if(STATIC)
set(sodium_USE_STATIC_LIBS ON)
endif()
find_package(Sodium REQUIRED)

find_package(PkgConfig REQUIRED)
pkg_check_modules(libzmq REQUIRED IMPORTED_TARGET libzmq)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ library archives (`.a`).
| OpenSSL | basically any | NO | `libssl-dev` | `openssl` | `openssl-devel` | `openssl-devel` | NO | sha256 sum |
| libzmq | 4.2.0 | NO | `libzmq3-dev` | `zeromq` | `zeromq-devel` | `zeromq-devel` | NO | ZeroMQ library |
| libunbound | 1.4.16 | NO | `libunbound-dev` | `unbound` | `unbound-devel` | `unbound-devel` | NO | DNS resolver |
| libsodium | ? | NO | `libsodium-dev` | `libsodium` | `libsodium-devel` | `libsodium-devel` | NO | cryptography |
| libsodium | ? | NO | `libsodium-dev` | `libsodium` | `libsodium-devel` | `libsodium-devel` | YES | cryptography |
| libunwind | any | NO | `libunwind8-dev` | `libunwind` | `libunwind-devel` | `libunwind-devel` | YES | Stack traces |
| liblzma | any | NO | `liblzma-dev` | `xz` | `liblzma-devel` | `xz-devel` | YES | For libunwind |
| libreadline | 6.3.0 | NO | `libreadline6-dev` | `readline` | `readline-devel` | `readline-devel` | YES | Input editing |
Expand Down
5 changes: 5 additions & 0 deletions cmake/CheckTrezor.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ if (USE_DEVICE_TREZOR)
trezor_fatal_msg("Trezor: protobuf library not found")
endif()

if(STATIC)
set(sodium_USE_STATIC_LIBS ON)
endif()
find_package(Sodium REQUIRED)

if(TREZOR_DEBUG)
set(USE_DEVICE_TREZOR_DEBUG 1)
message(STATUS "Trezor: debug build enabled")
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ set(crypto_sources
slow-hash.c
rx-slow-hash.c
CryptonightR_JIT.c
tree-hash.c)
tree-hash.c
verify.c)

if(ARCH_ID STREQUAL "i386" OR ARCH_ID STREQUAL "x86_64" OR ARCH_ID STREQUAL "x86-64" OR ARCH_ID STREQUAL "amd64")
list(APPEND crypto_sources CryptonightR_template.S)
Expand All @@ -73,7 +74,6 @@ target_link_libraries(cncrypto
epee
randomx
${Boost_SYSTEM_LIBRARY}
${sodium_LIBRARIES}
PRIVATE
${EXTRA_LIBRARIES})

Expand Down
2 changes: 1 addition & 1 deletion src/crypto/generic-ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <cstddef>
#include <cstring>
#include <functional>
#include <sodium/crypto_verify_32.h>
#include "verify.h"

#define CRYPTO_MAKE_COMPARABLE(type) \
namespace crypto { \
Expand Down
69 changes: 69 additions & 0 deletions src/crypto/verify.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: ISC
// SPDX-FileCopyrightText: 2013-2024 Frank Denis <j at pureftpd dot org>

#include <stdint.h>

#include "verify.h"

#define crypto_verify_32_BYTES 32U

#if defined(__x86_64__) && defined(__SSE2__)

# ifdef __GNUC__
# pragma GCC target("sse2")
# endif
# include <emmintrin.h>

static inline int
crypto_verify_n(const unsigned char *x_, const unsigned char *y_,
const int n)
{
const __m128i zero = _mm_setzero_si128();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth noting: This very much requires strict aliasing disabled (which sodium does, quite explicitly, and by design; whereas this project's CMakeLists.txt suggest an intent to go the other direction):

  # With GCC 6.1.1 the compiled binary malfunctions due to aliasing. Until that
  # is fixed in the code (Issue #847), force compiler to be conservative.
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-strict-aliasing")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-strict-aliasing")

The more creative the compiler vendors get, the more problematic even a single vendored function like this becomes.

volatile __m128i v1, v2, z;
volatile int m;
int i;

const volatile __m128i *volatile x =
(const volatile __m128i *volatile) (const void *) x_;
const volatile __m128i *volatile y =
(const volatile __m128i *volatile) (const void *) y_;
v1 = _mm_loadu_si128((const __m128i *) &x[0]);
v2 = _mm_loadu_si128((const __m128i *) &y[0]);
z = _mm_xor_si128(v1, v2);
for (i = 1; i < n / 16; i++) {
v1 = _mm_loadu_si128((const __m128i *) &x[i]);
v2 = _mm_loadu_si128((const __m128i *) &y[i]);
z = _mm_or_si128(z, _mm_xor_si128(v1, v2));
}
m = _mm_movemask_epi8(_mm_cmpeq_epi32(z, zero));
v1 = zero; v2 = zero; z = zero;

return (int) (((uint32_t) m + 1U) >> 16) - 1;
}

#else

static inline int
crypto_verify_n(const unsigned char *x_, const unsigned char *y_,
const int n)
{
const volatile unsigned char *volatile x =
(const volatile unsigned char *volatile) x_;
const volatile unsigned char *volatile y =
(const volatile unsigned char *volatile) y_;
volatile uint_fast16_t d = 0U;
int i;

for (i = 0; i < n; i++) {
d |= x[i] ^ y[i];
}
return (1 & ((d - 1) >> 8)) - 1;
}

#endif

int
crypto_verify_32(const unsigned char *x, const unsigned char *y)
{
return crypto_verify_n(x, y, crypto_verify_32_BYTES);
}
20 changes: 20 additions & 0 deletions src/crypto/verify.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: ISC
// SPDX-FileCopyrightText: 2013-2024 Frank Denis <j at pureftpd dot org>

#pragma once

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

// fix naming collision with libsodium
#define crypto_verify_32 monero_crypto_verify_32

int crypto_verify_32(const unsigned char *x, const unsigned char *y)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));

#ifdef __cplusplus
}
#endif
3 changes: 1 addition & 2 deletions src/device_trezor/trezor/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@
#include <common/apply_permutation.h>
#include <common/json_util.h>
#include <crypto/hmac-keccak.h>
#include <crypto/verify.h>
#include <ringct/rctSigs.h>
#include <ringct/bulletproofs.h>
#include <ringct/bulletproofs_plus.h>
#include "cryptonote_config.h"
#include <sodium.h>
#include <sodium/crypto_verify_32.h>
#include <sodium/crypto_aead_chacha20poly1305.h>

#define GET_FIELD_STRING(name, type, jtype) field_##name = std::string(json[#name].GetString(), json[#name].GetStringLength())
Expand Down
1 change: 0 additions & 1 deletion src/ringct/rctTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include <vector>
#include <iostream>
#include <cinttypes>
#include <sodium/crypto_verify_32.h>

extern "C" {
#include "crypto/crypto-ops.h"
Expand Down
2 changes: 1 addition & 1 deletion tests/performance_tests/equality.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#pragma once

#include <string.h>
#include <sodium/crypto_verify_32.h>
#include "crypto/verify.h"

struct memcmp32
{
Expand Down
Loading