]> git.ipfire.org Git - thirdparty/chrony.git/commit
keys: compare MACs in constant time
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 2 Apr 2025 13:34:43 +0000 (15:34 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 3 Apr 2025 14:27:02 +0000 (16:27 +0200)
commitd115449e854728662647b0edd95559b8a09c2b8c
treed922057d0911484fbade480ea54919af22788a2c
parentdab98fa8da2ff7a4637ff2229dfeefb3accdcff4
keys: compare MACs in constant time

Switch from memcmp() to the new constant-time function to compare the
received and expected authentication data generated with a symmetric key
(NTP MAC or AES CMAC).

While this doesn't seem to be strictly necessary with the current
code, it is a recommended practice to prevent timing attacks. If
memcmp() compared the MACs one byte at a time (a typical memcmp()
implementation works with wider integers for better performance) and
chronyd as an NTP client/server/peer was leaking the timing of the
comparison (e.g. in the monitoring protocol), an attacker might be able
for a given NTP request or response find in a sequence the individual
bytes of the MAC by observing differences in the timing over a large
number of attempts. However, this process would likely be so slow the
authenticated request or response would not be useful in a MITM attack
as the expected origin timestamp is changing with each poll.

Extend the keys unit test to compare the time the function takes to
compare two identical MACs and MACs differing in the first byte
(maximizing the timing difference). It should fail if the compiler's
optimizations figure out the function can return early. The test is not
included in the util unit test to avoid compile-time optimizations with
the function and its caller together. The test can be disabled by
setting NO_TIMING_TESTS environment variable if it turns out to be
unreliable.
keys.c
test/unit/keys.c