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

Add new Base32 implementation #1069

Merged
merged 8 commits into from
Oct 21, 2017
Merged

Add new Base32 implementation #1069

merged 8 commits into from
Oct 21, 2017

Conversation

adolfogc
Copy link
Contributor

Description

Provides a port of the new Base32 implementation, plus fixes to make it compatible with Google Authenticator's implementation, that was previously introduced in PRs #984 and #1001.

Motivation and context

See issue #1022.

How has this been tested?

Manually on Linux.

Screenshots (if appropriate):

N/A.

Types of changes

  • ✅ Bug fix (non-breaking change which fixes an issue)

Checklist:

  • ✅ I have read the CONTRIBUTING document. [REQUIRED]
  • ✅ My code follows the code style of this project. [REQUIRED]
  • ✅ All new and existing tests passed. [REQUIRED]
  • ✅ I have compiled and verified my code with -DWITH_ASAN=ON. [REQUIRED]
  • ✅ I have added tests to cover my changes.

@adolfogc
Copy link
Contributor Author

I've rebased.

}

// add pad characters
while (o < encodedData.size())
Copy link
Contributor

Choose a reason for hiding this comment

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

add brackets

quint64 quantum = 0;
int nQuantumBytes = 5;

for (int n = 0; n < 8; n++) {
Copy link
Member

Choose a reason for hiding this comment

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

++n

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, will change it.

quint64 mask = quint64(0xFF) << offset;
for (int n = offset; n >= 0; n -= 8) {
char c = static_cast<char>((quantum & mask) >> n);
data[o++] = c;
Copy link
Member

Choose a reason for hiding this comment

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

I would sleep better if you added a few range checking assertions here and for encodedData inside the encode() method when doing something like this with long-lived counters. QByteArray implicitly extends the array if you assign an element beyond the array's range. So we wouldn't get a buffer overflow, but probably wouldn't notice either.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! This helped me find a bug in Base32::decode() affecting how the number of bytes of decoded data was being calculated, even though it didn't truncate the result, it was causing the array to be resized unnecessarily. I will be submitting the changes soon.

Copy link
Member

Choose a reason for hiding this comment

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

Yay!

* by all the main compiler toolchains.
*/

template <typename T> class Optional
Copy link
Member

@phoerious phoerious Oct 20, 2017

Choose a reason for hiding this comment

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

Do we really need our own Optional implementation? Wouldn't the same be possible with QVariant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we can make this change to reduce the code base size, as the variant type can serve as an optional.

Copy link
Member

@phoerious phoerious Oct 20, 2017

Choose a reason for hiding this comment

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

Just use QVariant::isNull() or QVariant::isValid().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm almost done making this change, I decided for QVariant::isNull; as I understand thatQVariant::isValid is used for detecting when a type not defined for the variant (i.e. not registered with Q_DECLARE_METATYPE) is used.

| ((hmac[offset + 1] & 0xff) << 16)
| ((hmac[offset + 2] & 0xff) << 8)
| (hmac[offset + 3] & 0xff);
int binary = ((hmac[offset] & 0x7f) << 24) | ((hmac[offset + 1] & 0xff) << 16) | ((hmac[offset + 2] & 0xff) << 8) |
Copy link
Member

Choose a reason for hiding this comment

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

I liked the previous formatting better. It's hard to read on a single line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with you, this was the result of running clang-format; I'll change it.

The bug that was fixed, was affecting how the number of bytes of decoded data was  calculated and thus, even though it didn't truncate the result, it was causing the array to be resized unnecessarily.
@adolfogc
Copy link
Contributor Author

@phoerious I've finished making the changes solicited, please review them.

@phoerious phoerious merged commit 86cd2c0 into keepassxreboot:release/2.2.2 Oct 21, 2017
@phoerious
Copy link
Member

Nice work. Thanks!

phoerious added a commit that referenced this pull request Oct 21, 2017
- Fixed entries with empty URLs being reported to KeePassHTTP clients [#1031]
- Fixed YubiKey detection and enabled CLI tool for AppImage binary [#1100]
- Added AppStream description [#1082]
- Improved TOTP compatibility and added new Base32 implementation [#1069]
- Fixed error handling when processing invalid cipher stream [#1099]
- Fixed double warning display when opening a database [#1037]
- Fixed unlocking databases with --pw-stdin [#1087]
- Added ability to override QT_PLUGIN_PATH environment variable for AppImages [#1079]
- Fixed transform seed not being regenerated when saving the database [#1068]
- Fixed only one YubiKey slot being polled [#1048]
- Corrected an issue with entry icons while merging [#1008]
- Corrected desktop and tray icons in Snap package [#1030]
- Fixed screen lock and Google fallback settings [#1029]
@adolfogc adolfogc deleted the new-base32-implementation-with-fixes branch November 6, 2017 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants