What is Constant-time implementation?

A way of writing cryptographic code so that branch directions, loop counts and memory addresses accessed do not depend on secret values.

Field: Hardware Security

Japanese name: 定数時間実装

What must be observed

“Constant time” does not mean always the same time. It means the processing time does not vary with secret values. It is acceptable for the time to vary with public information such as the length of a message. Three rules apply: do not branch on secret values, do not let the number of loop iterations depend on secret values, and do not read or write memory at addresses derived from secret values. It is also necessary not to pass secret values to instructions whose execution time depends on their operands.

An example of the technique

Instead of branching, a value is selected with bit operations. A mask is made that is all ones when the condition holds and all zeros when it does not. The AND of one value with the mask and the AND of the other value with the inverted mask are computed, and the two are ORed together. The same instructions then execute whichever value is selected. Instead of a table lookup, every entry of the table can be read and one selected with a mask, or dedicated cryptographic instructions can be used.

Pitfalls

Even when the source code is written correctly, compiler optimization can turn it back into a branch. The generated machine code is therefore checked, or tools are used to test whether behavior changes with secret values. The processor also needs attention. Some processors provide an operating mode that guarantees instruction timing independent of the data handled, and that mode is enabled for cryptographic processing.

Try it: questions about Constant-time implementation

Which correctly pairs the principle behind timing attacks with a typical countermeasure?

  1. AIt exploits processing that is too fast; deliberately slow it down
  2. BIt exploits an inaccurate clock time; synchronize the clock
  3. CIt exploits processing time varying with secret values; implement code that takes the same time regardless of secrets (constant-time)
Show answer and explanation

Answer: C. It exploits processing time varying with secret values; implement code that takes the same time regardless of secrets (constant-time)

In an implementation that does extra work only when a key bit is 1, for example, repeated timing measurements reveal the key bits. The countermeasure is constant-time code, where branches, loop counts and memory access locations do not depend on secrets. Merely slowing everything down or adding random delays is not enough, because attackers can average away the noise with more measurements.

FAQ

Does constant-time code run slower?

It can be somewhat slower, because operations such as reading a whole table are added. The dedicated cryptographic instructions of a processor, on the other hand, are both fast and constant-time.

Can it be written in any language?

It is difficult in languages whose runtime environment affects processing time. Translation to machine code during execution and arithmetic on integers of unbounded size change the timing. Using a verified cryptographic library is the safe choice.

Books on Hardware Security Ad

As an Amazon Associate I earn from qualifying purchases.

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary