This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use volatile reads/writes in Tink constant-time comparisons.
1) The byte reads for the comparison are made volatile, so that the loop doesn't terminate early once it finds a difference. 2) The difference accumulator writes are made volatile, so that writes don't terminate early once it finds a difference. (Even with the reads being volatile, it could, for example, continue accessing memory but not do anything with that memory, as an optimization. Aside from removing xors, this could still improve performance quite a bit, by eliminating data dependency and allowing execution to race ahead of the volatile reads, so I think it isn't any more unrealistic of an optimization. h/t to Richard Smith who pointed this possible optimization out.) Background: The C++ and C standards both require that volatile memory accesses occur ~ basically as the programmer expects. Unlike normal reads and normal writes, which can be removed if the compiler deduces that they aren't necessary, volatile memory accesses are considered an observable behavior. This is one of the only ways in which the standard confesses that an underlying hardware exists, but it handwaves away the exact details as implementation-defined, so even a volatile read is not necessarily enough -- an implementation can define the semantics of a volatile read in a sufficiently convoluted way that it still can enable weird optimizations. In general, though, they aren't that cruel. So while this doesn't guarantee anything, it at least makes the compiler less likely to introduce a covert channel through clever optimization. As prior art, consider e.g. CPython's compare_digest function, which employs volatile reads (but not volatile writes): https://github.com/python/cpython/blob/master/Modules/_operator.c#L716-L762 PiperOrigin-RevId: 312400090
- Loading branch information