- Add test cases covering the relevant key length boundaries: 64 bytes +/- 1 byte for HMAC-SHA256 and 128 bytes +/- 1 byte for HMAC-SHA512.
Avoid creating a one-past-the-end pointer in the case of key length 64 (HMAC-SHA256) and key length 128 (HMAC-SHA512).Avoid performing a noop memset call (zero length argument) in the case of key length 64 (HMAC-SHA256) and key length 128 (HMAC-SHA512).
crypto: Add test cases covering the relevant HMAC-SHA{256,512} key length boundaries #11516
pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:one-past-the-end changing 1 files +41 −0-
practicalswift commented at 2:44 PM on October 17, 2017: contributor
- practicalswift renamed this:
crypto: Avoid creating of one-past-the-end pointer and performing a noop memset call (HMAC-SHA{256,512})
crypto: Add HMAC-SHA{256,512} test cases for relevant boundaries. Avoid creating of one-past-the-end pointer and performing a noop memset call.
on Oct 17, 2017 - practicalswift renamed this:
crypto: Add HMAC-SHA{256,512} test cases for relevant boundaries. Avoid creating of one-past-the-end pointer and performing a noop memset call.
crypto: Add HMAC-SHA{256,512} test cases for relevant key length boundaries. Avoid creating of one-past-the-end pointer and performing a noop memset call.
on Oct 17, 2017 - practicalswift renamed this:
crypto: Add HMAC-SHA{256,512} test cases for relevant key length boundaries. Avoid creating of one-past-the-end pointer and performing a noop memset call.
crypto: Avoid creating of one-past-the-end pointer and performing a noop memset call. Add HMAC-SHA{256,512} test cases.
on Oct 17, 2017 - practicalswift renamed this:
crypto: Avoid creating of one-past-the-end pointer and performing a noop memset call. Add HMAC-SHA{256,512} test cases.
crypto: Avoid creating an one-past-the-end pointer and performing a noop memset call. Add HMAC-SHA{256,512} test cases.
on Oct 18, 2017 -
sipa commented at 9:16 AM on October 18, 2017: member
Is this necessary? There is no problem with creating a one-past-end pointer; only dereferencing them, which doesn't happen here.
-
practicalswift commented at 10:15 AM on October 18, 2017: contributor
@sipa Both zero length
memset:s and creating of one-past-end pointers are legal, so from a language legality sense this change is not needed.However, I've seen multiple static analyzers flag this line of code as a potential mistake. This small change makes it apparent for both static analyzers and humans what the real intention of the code is.
My experience is that code that looks like potential mistakes to static analyzers tend to confuse humans too. I think there is a good case for helping static analyzers (and perhaps humans too?) in this case. It also has the added benefit of getting rid of an unnecessary
memsetcall and getting three additional test cases that cover the key length boundaries.In summary I think this change is worth it :-)
-
laanwj commented at 1:29 PM on October 18, 2017: member
Pointers to the end of a range are absolutely valid C/C++. This is commonly used for iteration end-exclusive ranges. So is a zero-length memset.
I tend towards NACK adding unnecessary checks here. I think minor harmless changes to fix static analyzer false-positives are sometimes okay, but at some point it goes too far, we don't write our code to appease static analyzers. This kind of ruffling the code can introduce bugs.
ACK on the tests though.
- laanwj added the label Refactoring on Oct 18, 2017
-
in src/crypto/hmac_sha256.cpp:16 in a4a983db74 outdated
10 | @@ -11,7 +11,9 @@ CHMAC_SHA256::CHMAC_SHA256(const unsigned char* key, size_t keylen) 11 | unsigned char rkey[64]; 12 | if (keylen <= 64) { 13 | memcpy(rkey, key, keylen); 14 | - memset(rkey + keylen, 0, 64 - keylen); 15 | + if (keylen != 64) { 16 | + memset(rkey + keylen, 0, 64 - keylen); 17 | + }
practicalswift commented at 3:26 PM on October 18, 2017:Removing this change as requested.
Add test cases covering the relevant key length boundaries: 64 bytes +/- 1 byte for HMAC-SHA256 and 128 bytes +/- 1 byte for HMAC-SHA512 a3f56578abin src/crypto/hmac_sha512.cpp:16 in a4a983db74 outdated
10 | @@ -11,7 +11,9 @@ CHMAC_SHA512::CHMAC_SHA512(const unsigned char* key, size_t keylen) 11 | unsigned char rkey[128]; 12 | if (keylen <= 128) { 13 | memcpy(rkey, key, keylen); 14 | - memset(rkey + keylen, 0, 128 - keylen); 15 | + if (keylen != 128) { 16 | + memset(rkey + keylen, 0, 128 - keylen); 17 | + }
practicalswift commented at 3:26 PM on October 18, 2017:Removing this change as requested.
practicalswift force-pushed on Oct 18, 2017practicalswift renamed this:crypto: Avoid creating an one-past-the-end pointer and performing a noop memset call. Add HMAC-SHA{256,512} test cases.
crypto: Add test cases covering the relevant key length boundaries: 64 bytes +/- 1 byte for HMAC-SHA256 and 128 bytes +/- 1 byte for HMAC-SHA512
on Oct 18, 2017practicalswift renamed this:crypto: Add test cases covering the relevant key length boundaries: 64 bytes +/- 1 byte for HMAC-SHA256 and 128 bytes +/- 1 byte for HMAC-SHA512
crypto: Add test cases covering the relevant HMAC-SHA{256,512} key length boundaries
on Oct 18, 2017practicalswift commented at 3:30 PM on October 18, 2017: contributorlaanwj commented at 4:11 PM on October 19, 2017: memberpracticalswift commented at 10:40 AM on October 30, 2017: contributorReady for merge? :-)
laanwj merged this on Dec 12, 2017laanwj closed this on Dec 12, 2017laanwj referenced this in commit 0e722e8879 on Dec 12, 2017TheBlueMatt commented at 6:38 PM on December 12, 2017: memberPost-merge was-here-looks-fine.
PastaPastaPasta referenced this in commit 6ae9e79dd8 on Jan 17, 2020PastaPastaPasta referenced this in commit 8673df90f2 on Jan 22, 2020PastaPastaPasta referenced this in commit ff5f85a8ad on Jan 22, 2020PastaPastaPasta referenced this in commit 1bc010ccf8 on Jan 29, 2020PastaPastaPasta referenced this in commit 70936fdd40 on Jan 29, 2020PastaPastaPasta referenced this in commit 7737eb48e7 on Jan 29, 2020PastaPastaPasta referenced this in commit b695b67d5f on Jan 31, 2020ckti referenced this in commit 33c310f075 on Mar 28, 2021practicalswift deleted the branch on Apr 10, 2021furszy referenced this in commit 60d36292bc on Jul 26, 2021DrahtBot locked this on Aug 16, 2022ContributorsLabels
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-16 15:15 UTC
More mirrored repositories can be found on mirror.b10c.me