News for hackers. Stuff that matters.
Re:Rust Cryptography Should Be Written in Rust by IshKebab on Saturday August 26, 2023 @02:37PM (#37275613)
Probably SIMD support and constant time support. Crypto libraries tend to use SIMD a lot to be fast.

You can write constant time code in Rust by carefully making sure your code only compiles to constant time instructions without branches, but you'd really want some kind of annotation on the code to enforce that.

That's mostly a guess though.

Re:Rust Cryptography Should Be Written in Rust by xdavidliu on Saturday August 26, 2023 @02:41PM (#37275645)
isnt constant time orthogonal to whether there are branches?
Re:Rust Cryptography Should Be Written in Rust by anonymoushn on Saturday August 26, 2023 @02:44PM (#37275677)
No. Constant wall clock time involves not using branches. Maybe you're thinking of "asymptotic constant time" or "runtime bounded above by a constant." These are not what is needed, because what is needed is to not expose any information via timing.
Re:Rust Cryptography Should Be Written in Rust by brobinson on Saturday August 26, 2023 @02:46PM (#37275697)
What if there are branches but both paths result in the same number of cycles being required to execute the instructions?

Is it correct to say: "all branchless code runs in constant time, but not all constant time code is branchless"?

Re:Rust Cryptography Should Be Written in Rust by AlotOfReading on Saturday August 26, 2023 @03:03PM (#37275857)
The subtlety is that eliminating branching isn't sufficient to have constant time code. A simple example is using trigonometric and transcendental opcodes. They don't branch (at the assembly level), but on x86 take variable amounts of time depending on the input operand. Very few algorithms actually use these opcodes though, so a more relevant concern is memory access due to variable latency. Even if you have that nailed down, integer operations like multiplication and especially division can take variable amounts of time depending on the input.

Writing truly constant time code on modern processors ranges is difficult at best, and usually less efficient than variable-time code.

Re:Rust Cryptography Should Be Written in Rust by touisteur on Sunday August 27, 2023 @05:42AM (#37281003)
Add cache misses, bus interference, SMT woes and it quickly becomes harder and harder to write (and check) constant-time (or WCET) properties. Even modern micro-benchmarks are a huge labyrinth of architectural traps.
Re:Rust Cryptography Should Be Written in Rust by brobinson on Wednesday September 06, 2023 @04:06PM (#37410560)
Really interesting information, thank you.
Re:Rust Cryptography Should Be Written in Rust by anonymoushn on Saturday August 26, 2023 @02:49PM (#37275728)
Because of speculative execution, branchy code with equal-runtime branches will still take different amounts of time if it is called repeatedly, usually in ways that reveal information about the input.
Re:Rust Cryptography Should Be Written in Rust by junon on Saturday August 26, 2023 @03:07PM (#37275888)
Timing attacks are common everywhere, by the way. Simplest example, perhaps a bit too contrived:

I'm an attacker doing targeted research. I want to see if a multi-auth system has an association between two email addresses tied to the same account.

Pulling a database record or in-memory record (e.g. via LFU/LRU cache) in some cases may cache the account record, which means a subsequent record might be warm when fetched with the second email.

I run a time analysis against the endpoint with garbage addresses, known addresses (that I've set up) and the two target addresses to check subsequent fetch speeds.

In some cases, this will cause enough of a time difference to tell me if there's a connection.

Timing attacks are hard, and even a well-architected system can expose information indirectly. Encryption is a bit one if the inputs are static (e.g. keys or the like) and are a common way to target endpoints.

Re:Rust Cryptography Should Be Written in Rust by xigency on Saturday August 26, 2023 @02:50PM (#37275742)
The issue is speculative execution. Whenever there is a branch, the CPU makes a guess. If it guesses wrong, it has to go back to the correct path which introduces a delay. So any branching code has the possibility of revealing information through the branch predictor.
Re:Rust Cryptography Should Be Written in Rust by IshKebab on Saturday August 26, 2023 @05:41PM (#37277175)
> all branchless code runs in constant time

No - e.g. division is not constant time.

You have to have branchless code and only use certain instructions.

E.g. here is the list for RISC-V.

https://github.com/rvkrypto/riscv-zkt-list/blob/main/zkt-lis...

Most things except div/rem, branches and floating point are ok. Oh and obviously store/load.

Re:Rust Cryptography Should Be Written in Rust by brobinson on Wednesday September 06, 2023 @04:05PM (#37410547)
Thanks, this is really interesting.
Re:Rust Cryptography Should Be Written in Rust by Groxx on Saturday August 26, 2023 @02:45PM (#37275684)
Because branch prediction exists: sometimes yes, often no. Among other reasons.
Re:Rust Cryptography Should Be Written in Rust by felipellrocha on Saturday August 26, 2023 @02:46PM (#37275695)
Rust has simd support, so maybe the latter is the issue.
Re:Rust Cryptography Should Be Written in Rust by spullara on Saturday August 26, 2023 @02:49PM (#37275735)
not in stable yet
Re:Rust Cryptography Should Be Written in Rust by junon on Saturday August 26, 2023 @03:02PM (#37275846)
AFAIK intrinsics are stable. Idk about auto-vectorizers and the like.
Re:Rust Cryptography Should Be Written in Rust by anonymoushn on Saturday August 26, 2023 @02:53PM (#37275768)
core::arch::x86_64::_mm256_shuffle_epi8 and such seem to be in stable.
Re:Rust Cryptography Should Be Written in Rust by burntsushi on Saturday August 26, 2023 @04:49PM (#37276774)
This is incorrect. Rust has had x86_64 (up to and including AVX2) intrinsics stable since Rust 1.26. Wasm32 simd128 and aarch64 neon intrinsics are also stable.
Re:Rust Cryptography Should Be Written in Rust by spullara on Sunday August 27, 2023 @02:41AM (#37280023)
[delayed]