Skip to content

Commit

Permalink
Use Qt impl of blake with Qt6
Browse files Browse the repository at this point in the history
  • Loading branch information
Ri0n committed Apr 20, 2024
1 parent f84fbda commit 53ef4a4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ endif()

option(IRIS_ENABLE_INSTALL "Enable installation" ON)
option(IRIS_ENABLE_JINGLE_SCTP "Enable SCTP over ICE Jingle transport / data channels" ON)
option(IRIS_BUNDLED_QCA "Adds: DTLS, Blake2b and other useful for XMPP crypto-stuff" ${IRIS_DEFAULT_BUNDLED_QCA})
# note Blake2b is needed only with Qt5. Qt6 has its own implementation
option(IRIS_BUNDLED_QCA "Adds: DTLS, Blake2b (needed with Qt5) and other useful for XMPP crypto-stuff" ${IRIS_DEFAULT_BUNDLED_QCA})
option(IRIS_BUNDLED_USRSCTP "Compile compatible UsrSCTP lib (required for datachannel Jingle transport)" ${IRIS_DEFAULT_BUNDLED_USRSCTP})
option(IRIS_BUILD_TOOLS "Build tools and examples" OFF)
option(IRIS_ENABLE_DEBUG "Enable debugging code paths" OFF)
Expand Down Expand Up @@ -101,7 +102,7 @@ if(IRIS_ENABLE_JINGLE_SCTP)
include(IrisSCTP)
endif()

if(NOT IRIS_BUNDLED_QCA)
if(NOT IRIS_BUNDLED_QCA AND QT_DEFAULT_MAJOR_VERSION LESS 6)
find_package(B2 QUIET)
if(B2_FOUND)
message(STATUS "Found B2: ${B2_LIBRARY}")
Expand Down
26 changes: 13 additions & 13 deletions src/xmpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ set(XMPP_HEADERS_PRIVATE
sasl/scramsha1signature.h
zlib/zlibcompressor.h
zlib/zlibdecompressor.h
blake2/blake2qt.h
base/timezone.h
)

Expand Down Expand Up @@ -154,8 +153,6 @@ target_sources(iris PRIVATE
zlib/zlibcompressor.cpp
zlib/zlibdecompressor.cpp

blake2/blake2qt.cpp

jid/jid.cpp

sasl/digestmd5proplist.cpp
Expand Down Expand Up @@ -183,17 +180,20 @@ if(IRIS_ENABLE_JINGLE_SCTP)
)
endif()

if(B2_FOUND)
message(STATUS "Building with system blake2 library")
target_link_libraries(iris PRIVATE ${B2_LIBRARY})
else()
if(NOT IRIS_BUNDLED_QCA)
message(STATUS "No system blake2 and bundled QCA is disabled. Expect slow hashing.")
if(QT_DEFAULT_MAJOR_VERSION LESS 6)
target_sources(iris blake2/blake2qt.cpp blake2/blake2qt.h)
if(B2_FOUND)
message(STATUS "Building with system blake2 library")
target_link_libraries(iris PRIVATE ${B2_LIBRARY})
else()
if(NOT IRIS_BUNDLED_QCA)
message(STATUS "No system blake2 and bundled QCA is disabled. Expect slow hashing.")
endif()
target_sources(iris PRIVATE
blake2/blake2b-ref.c
blake2/blake2s-ref.c
)
endif()
target_sources(iris PRIVATE
blake2/blake2b-ref.c
blake2/blake2s-ref.c
)
endif()

target_link_libraries(iris
Expand Down
66 changes: 62 additions & 4 deletions src/xmpp/xmpp-im/xmpp_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,18 @@ static const std::array hashTypes {
HashDesc { "blake2b-256", Hash::Type::Blake2b256 }, HashDesc { "blake2b-512", Hash::Type::Blake2b512 }
};

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
using HashVariant = std::variant<std::nullptr_t, QCryptographicHash, QCA::Hash, Blake2Hash>;
#else
using HashVariant = std::variant<std::nullptr_t, QCryptographicHash, QCA::Hash>;
#endif
HashVariant findHasher(Hash::Type hashType)
{
QString qcaType;
QCryptographicHash::Algorithm qtType = QCryptographicHash::Algorithm(-1);
Blake2Hash::DigestSize blakeDS = Blake2Hash::DigestSize(-1);
QCryptographicHash::Algorithm qtType = QCryptographicHash::Algorithm(-1);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Blake2Hash::DigestSize blakeDS = Blake2Hash::DigestSize(-1);
#endif

switch (hashType) {
case Hash::Type::Sha1:
Expand All @@ -76,6 +82,7 @@ HashVariant findHasher(Hash::Type hashType)
qtType = QCryptographicHash::Sha3_512;
qcaType = "sha3_512";
break;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
case Hash::Type::Blake2b256:
qcaType = "blake2b_256";
blakeDS = Blake2Hash::Digest256;
Expand All @@ -84,6 +91,16 @@ HashVariant findHasher(Hash::Type hashType)
qcaType = "blake2b_512";
blakeDS = Blake2Hash::Digest512;
break;
#else
case Hash::Type::Blake2b256:
qtType = QCryptographicHash::Blake2b_256;
qcaType = "blake2b_256";
break;
case Hash::Type::Blake2b512:
qtType = QCryptographicHash::Blake2b_512;
qcaType = "blake2b_512";
break;
#endif
case Hash::Type::Unknown:
default:
qDebug("invalid hash type");
Expand All @@ -99,12 +116,15 @@ HashVariant findHasher(Hash::Type hashType)

if (qtType != QCryptographicHash::Algorithm(-1)) {
return HashVariant { std::in_place_type<QCryptographicHash>, qtType };
} else if (blakeDS != Blake2Hash::DigestSize(-1)) {
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
else if (blakeDS != Blake2Hash::DigestSize(-1)) {
Blake2Hash bh(blakeDS);
if (bh.isValid()) {
return HashVariant { std::in_place_type<Blake2Hash>, std::move(bh) };
}
}
#endif
return nullptr;
}

Expand Down Expand Up @@ -156,6 +176,7 @@ bool Hash::compute(const QByteArray &ba)
std::visit(
[&ba, this](auto &&arg) {
using T = std::decay_t<decltype(arg)>;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
if constexpr (std::is_same_v<T, QCA::Hash>) {
arg.update(ba);
v_data = arg.final().toByteArray();
Expand All @@ -166,6 +187,16 @@ bool Hash::compute(const QByteArray &ba)
if (arg.addData(ba))
v_data = arg.final();
}
#else
// Qt6 claims to have openssl backend and it doesn't copy data (?)
if constexpr (std::is_same_v<T, QCryptographicHash>) {
arg.addData(ba);
v_data = arg.result();
} else if constexpr (std::is_same_v<T, QCA::Hash>) {
arg.update(ba);
v_data = arg.final().toByteArray();
}
#endif
},
hasher);

Expand All @@ -183,6 +214,7 @@ bool Hash::compute(QIODevice *dev)
std::visit(
[dev, this](auto &&arg) {
using T = std::decay_t<decltype(arg)>;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
if constexpr (std::is_same_v<T, QCA::Hash>) {
arg.update(dev);
v_data = arg.final().toByteArray();
Expand All @@ -193,6 +225,15 @@ bool Hash::compute(QIODevice *dev)
if (arg.addData(dev))
v_data = arg.final();
}
#else
if constexpr (std::is_same_v<T, QCryptographicHash>) {
arg.addData(dev);
v_data = arg.result();
} else if constexpr (std::is_same_v<T, QCA::Hash>) {
arg.update(dev);
v_data = arg.final().toByteArray();
}
#endif
},
hasher);

Expand Down Expand Up @@ -306,13 +347,22 @@ bool StreamHash::addData(const QByteArray &data)
std::visit(
[&data, &ret](auto &&arg) {
using T = std::decay_t<decltype(arg)>;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
if constexpr (std::is_same_v<T, QCA::Hash>) {
arg.update(data);
} else if constexpr (std::is_same_v<T, QCryptographicHash>) {
arg.addData(data);
} else if constexpr (std::is_same_v<T, Blake2Hash>) {
ret = arg.addData(data);
} else
}
#else
if constexpr (std::is_same_v<T, QCryptographicHash>) {
arg.addData(data);
} else if constexpr (std::is_same_v<T, QCA::Hash>) {
arg.update(data);
}
#endif
else
ret = false;
},
d->hasher);
Expand All @@ -324,13 +374,21 @@ Hash StreamHash::final()
auto data = std::visit(
[](auto &&arg) {
using T = std::decay_t<decltype(arg)>;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
if constexpr (std::is_same_v<T, QCA::Hash>) {
return arg.final().toByteArray();
} else if constexpr (std::is_same_v<T, QCryptographicHash>) {
return arg.result();
} else if constexpr (std::is_same_v<T, Blake2Hash>) {
return arg.final();
}
#else
if constexpr (std::is_same_v<T, QCryptographicHash>) {
return arg.result();
} else if constexpr (std::is_same_v<T, QCA::Hash>) {
return arg.final().toByteArray();
}
#endif
return QByteArray();
},
d->hasher);
Expand Down

0 comments on commit 53ef4a4

Please sign in to comment.