Skip to content

rfc6979: add K-163 test vector; fix nonaligned use#781

Merged
tarcieri merged 1 commit intomasterfrom
rfc6979/K-163-test-vector
Jan 17, 2024
Merged

rfc6979: add K-163 test vector; fix nonaligned use#781
tarcieri merged 1 commit intomasterfrom
rfc6979/K-163-test-vector

Conversation

@tarcieri
Copy link
Copy Markdown
Member

@tarcieri tarcieri commented Jan 17, 2024

RFC6979 Appendix A.1. provides a "Detailed Example" which exercises several edge cases in the protocol:

  • bits2int for an input which is not byte-aligned
  • Rejecting inputs which exceed the modulus

This PR adds what was missing from the previous implementation which assumed inputs were always aligned to the size of the digest output: a constant-time right shift by the number of bits by which the modulus is smaller than a byte-aligned value.

@tarcieri
Copy link
Copy Markdown
Member Author

The first HMAC-DRBG output appears correct (albeit truncated to the byte size of the modulus):

0x9305a46de7ff8eb107194debd3fd48aa20d5e7656c

However, RFC6979 suggests the correct candidate value is this:

k1 = 0x4982D236F3FFC758838CA6F5E9FEA455106AF3B2B

The problem is that our candidate value is 168-bits, and our modulus is 163-bits. So, we need to add a right shift with the difference:

>> hex(0x9305a46de7ff8eb107194debd3fd48aa20d5e7656c >> 5)
0x4982d236f3ffc758838ca6f5e9fea455106af3b2b

...so we need our implementation to count the bit length of the modulus first, then perform a right shift if necessary.

RFC6979 Appendix A.1. provides a "Detailed Example" which exercises
several edge cases in the protocol:

- `bits2int` for an input which is not byte-aligned
- Rejecting inputs which exceed the modulus

This commit adds what was missing from the previous implementation which
assumed inputs were always aligned to the size of the digest output: a
constant-time right shift by the number of bits by which the modulus is
smaller than a byte-aligned value.
@tarcieri tarcieri force-pushed the rfc6979/K-163-test-vector branch from 5965788 to c021851 Compare January 17, 2024 04:23
@tarcieri tarcieri changed the title [WIP] rfc6979: add K-163 test vector rfc6979: add K-163 test vector; fix nonaligned use Jan 17, 2024
@tarcieri tarcieri marked this pull request as ready for review January 17, 2024 04:24
@tarcieri tarcieri merged commit f6426cf into master Jan 17, 2024
@tarcieri tarcieri deleted the rfc6979/K-163-test-vector branch January 17, 2024 04:25
tarcieri added a commit that referenced this pull request Jan 17, 2024
tarcieri added a commit that referenced this pull request Jan 17, 2024
@tarcieri tarcieri mentioned this pull request May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant