Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/FilesCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,12 @@ bool FilesCache::setCardCPZ(QByteArray cardCPZ)
m_filePath = dataDir.absoluteFilePath(fileName);

qint64 m_key = 0;
// The shift must stay 32-bit and wrap at 32 ((i*8) & 31) to derive the
// same key historical x86 builds produced: shifting a 32-bit value by
// >= 32 is undefined behaviour that x86 silently wrapped, while on
// arm64 the compiler turns it into a trap (crash on Apple Silicon).
for (int i = 0;i < std::min(8, static_cast<int>(cardCPZ.size()));i++)
m_key += (static_cast<unsigned int>(cardCPZ[i]) & 0xFF) << (i * 8);
m_key += (static_cast<unsigned int>(cardCPZ[i]) & 0xFF) << ((i * 8) & 31);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

(unless I'm mistaken) and that's why AI isn't there yet: for the same input, this change will lead to different m_key outputs as nothing will be populated after the 32th bit.
https://github.com/mooltipass/minible/wiki/Mooltipass-Database-Model#-cpz-lookup-table


m_simpleCrypt.setKey(m_key);
m_simpleCrypt.setIntegrityProtectionMode(SimpleCrypt::ProtectionHash);
Expand Down
6 changes: 5 additions & 1 deletion src/MPDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5414,9 +5414,13 @@ quint64 MPDevice::getUInt64EncryptionKey()
quint64 MPDevice::getUInt64EncryptionKeyOld()
{
qint64 key = 0;
// This legacy key derivation must reproduce what historical x86 builds
// computed: the 32-bit shift by >= 32 was undefined behaviour that x86
// silently wrapped ((i*8) & 31), while on arm64 the compiler turns the
// unmasked form into a trap (crash on Apple Silicon).
for (int i = 0; i < std::min(8, static_cast<int>(m_cardCPZ.size())) ; i++)
{
key += (static_cast<unsigned int>(m_cardCPZ[i]) & 0xFF) << (i*8);
key += (static_cast<unsigned int>(m_cardCPZ[i]) & 0xFF) << ((i*8) & 31);
}

return key;
Expand Down
12 changes: 11 additions & 1 deletion src/MPDevice_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ void _read_report_callback(void *context,
uint8_t *report,
CFIndex report_length)
{
Q_UNUSED(result);
Q_UNUSED(sender);
Q_UNUSED(report_type);
Q_UNUSED(report_id);

MPDevice_mac *dev = reinterpret_cast<MPDevice_mac *>(context);

// IOKit invokes this callback with an error result (and meaningless
// buffer/length) when the device drops mid-read, e.g. while the BLE
// device re-enumerates its interfaces. Never feed that to the parser.
if (result != kIOReturnSuccess || !report || report_length <= 0)
{
qWarning() << "Ignoring errored HID input report, result:" << result
<< "length:" << static_cast<qint64>(report_length);
return;
}

QByteArray data((const char *)report, report_length);
if (dev->isBT())
{
Expand Down