News for hackers. Stuff that matters.
Re:How I implemented my own crypto by thinkMOAR on Thursday August 03, 2017 @03:49AM (#14917728)
Curious about the other language. What other language that doesn't add another layer between the code and system instructions were you thinking about?
Re:How I implemented my own crypto by reikonomusha on Thursday August 03, 2017 @03:57AM (#14917760)
C adds a layer between your code and the system. It abstracts away memory layout, locations, as well as function call semantics, and a many other things. For example, given a C program, you will not be able to tell me with certainty that a variable will occupy memory on the stack, any of your particular processor registers, etc. Formally, C is a language to control the C Abstract Machine, as described in the standard.

The sort of direct control one might look for is, as far as I know, only practically found in your processor's assembly language, and your operating system's collection of syscalls.

I think Rust, Common Lisp, OCaml, D, Haskell, and other languages give you a lot of benefits that are simply not worth giving up in a new crypto library, like safety.

Re:How I implemented my own crypto by adwn on Thursday August 03, 2017 @04:06AM (#14917818)
> Haskell

I think writing timing-attack resistant code in Haskell would be very hard to impossible, at least without writing very unidiomatic code (basically "C in Haskell"). I'm happy to be proven wrong, though.

Re:How I implemented my own crypto by pjmlp on Thursday August 03, 2017 @04:13AM (#14917851)
Writing unidiomatic code would still be compliant with the Haskell Language Report, compiled by an Haskell compiler and safer than plain C code.
Re:How I implemented my own crypto by willtim on Thursday August 03, 2017 @04:26AM (#14917891)
One approach would be do design an embedded DSL for implementing cryto algorithms. And then a compiler for this DSL into C or LLVM.

In fact, I believe something along the lines of this has already been done: https://cryptol.net

Re:How I implemented my own crypto by diegoperini on Thursday August 03, 2017 @04:16AM (#14917859)
Honest question: What makes you think you can't mitigate? A̶ ̶t̶y̶p̶e̶s̶a̶f̶e̶ ̶c̶r̶y̶p̶t̶o̶ ̶t̶h̶a̶t̶ ̶m̶e̶a̶s̶u̶r̶e̶s̶ ̶i̶t̶s̶ ̶e̶x̶e̶c̶u̶t̶i̶o̶n̶ ̶t̶i̶m̶e̶ ̶a̶p̶p̶e̶n̶d̶e̶d̶ ̶w̶i̶t̶h̶ ̶a̶ ̶f̶i̶n̶a̶l̶ ̶d̶e̶l̶a̶y̶ ̶t̶i̶m̶e̶ ̶r̶e̶a̶d̶ ̶f̶r̶o̶m̶ ̶/̶d̶e̶v̶/̶u̶r̶a̶n̶d̶o̶m̶ ̶c̶a̶n̶ ̶s̶t̶i̶l̶l̶ ̶b̶e̶ ̶i̶d̶i̶o̶m̶a̶t̶i̶c̶.̶ It has to be monadic, that is for sure but abstracting a crypto algorithm as IO is actually treating it as a device which sounds like a safe metaphor.

Edit: Whoah, why the hate? :(

Re:How I implemented my own crypto by loup-vaillant on Thursday August 03, 2017 @05:41AM (#14918181)
I believe padding the timing always has been considered a bad idea. If you want to guarantee you won't exceed the timing, you probably have to take so much margin that your primitive becomes prohibitively slow. And I'm not even sure it would protect you against cache timing attacks.

Simply put, there are easier and cheaper ways to guarantee timings. Perhaps Haskell is even capable of harnessing them. (I'm thinking of the usual "no secret dependent branches", and "no secret dependent indices".)

Re:How I implemented my own crypto by Ded7xSEoPKYNsDd on Thursday August 03, 2017 @05:57AM (#14918252)
Random delays do not protect against timing attacks. Best thing it can do, is to force the attacker to collect a few more timing samples. (To the point where the the random noise averages itself out.)
Re:How I implemented my own crypto by bandook_raja on Thursday August 03, 2017 @04:12AM (#14917843)
Rust doesn't even exist on a plethora of platforms which C targets. I have high hopes with D and give or take ~2 years, it'll be able to progress forward in lots of domains. Not sure about other languages.
Re:How I implemented my own crypto by simias on Thursday August 03, 2017 @04:53AM (#14917991)
One big long term advantage of Rust is that it leverages LLVM. Basically anywhere LLVM gets ported Rust follows.

I could run Rust code on a PlayStation and PocketStation (although I had to cheat a bit on the former because LLVM doesn't support MIPS I).

LLVM is not as widely ported as GCC (especially on older, deprecated hardware) but it's getting there.

https://svkt.org/~simias/rustation/pockestation-rust.jpg

Re:How I implemented my own crypto by pjmlp on Thursday August 03, 2017 @04:16AM (#14917862)
That is a matter of tooling, though.

Until the early 90's C had hardly any meaning outside big expensive UNIX boxes.

And it was already on its way out on Windows and OS/2, if it wasn't for the rise of FOSS software and its dependency on C.

Re:How I implemented my own crypto by SidiousL on Thursday August 03, 2017 @04:51AM (#14917984)
I think it might be a bit more than that. As far as I can see, all the primitive data types in Rust have sizes which are multiples of eight. However, there are still architectures out there with 9 bit bytes, or 36-bit words. Maybe it's good that all the primitive data types have an explicit size (encoded in their name), but this comes at a cost (in case a byte is 9 bits long, one of the bits will effectively not be used). On the other hand, C and C++ are less explicit about the sizes of their data types so they can accommodate these more weird architectures more easily (and, if you need an explicit size, you can explicitly ask for it).
Re:How I implemented my own crypto by simias on Thursday August 03, 2017 @05:01AM (#14918025)
You have a point but in general you wouldn't write "generic" C code on these exotic (by modern standards) architectures. So I guess if you wanted to port Rust to these systems you could introduce an i9 and an i36 and use that instead. I doubt you could get firefox and its dependencies to work on a CHAR_BIT == 9 system without some heavy modifications. I could be wrong though, it's not like I tried.

In my experience most modern codebases tend to target POSIX systems (explicitly or implicitly). That gives you a subset of C to work with and makes your life easier. The exceptions are things like DSPs which can have rather non-standard layouts but you generally write very specific and unportable code for these anyway.

Of course if you want to be completely portable you can't assume that CHAR_BIT == 8 or that you can convert back and forth between function and data pointers but in practice a lot of software relies on this.

Even before stdint became part of the standard it was very common for programs and libraries to typedef their own "sized" integer types. It's the only reasonable way to deal with ints in C IMO, otherwise you can't assume that your ints are greater than 16bits (which is probably too tiny for many uses) so you're tempted to put "longs" everywhere. But then longs will be 64bits on modern architectures which could be overkill and reduce performance.

So in theory those variable-size integer types are interesting but in practice they're basically useless because you end up making assumptions, one way or an other. I think it was a good idea for Rust to get rid of them.

Re:How I implemented my own crypto by honestoHeminway on Thursday August 03, 2017 @06:54AM (#14918481)
Any language that needs evangelists is clearly not able to progress on its own merits and archievments.
Re:How I implemented my own crypto by Ar-Curunir on Thursday August 03, 2017 @07:13AM (#14918555)
What? You think other languages don't have "fan-people"? People who like Rust tend to like it very much, and it does provide a real solution to problems that C and C++ create, while providing a no-compromise performance story. So why wouldn't people talk about it?
Re:How I implemented my own crypto by oblio on Thursday August 03, 2017 @09:40AM (#14919445)
Perl had evangelists, Ruby (on Rails) had evangelists, C++ had evangelists, Java had evangelists (some would say Java even had apostles and Messiahs...), almost every language has evangelists.
Re:How I implemented my own crypto by pjmlp on Thursday August 03, 2017 @04:11AM (#14917840)
Most programming languages with compilers to native code (JIT/AOT), the myth of high level Assembler for C only applies if your computer is a PDP-11 or a basic 8-bit CPU like a 6502 or Z80.

The ANSI C and C++ standards define the concept of abstract machine for the language semantics, just like in most languages.

Additionally you have the concepts of sequence points, the new memory model semantics for multi-threaded code and the beloved UB.

UB which doesn't exist in most languages, because their rather leave it implementation defined or lose the opportunity to target some strange CPU not able to support the language semantics.

Also Assembly doesn't has UB, making it ironic that it is safer to write straight Assembly at the expense of portability than C or C derived languages.

Re:How I implemented my own crypto by Ded7xSEoPKYNsDd on Thursday August 03, 2017 @06:05AM (#14918282)
The ARM Architecture Reference Manuals are full of "if you do X, the instruction is UNPREDICTABLE".
Re:How I implemented my own crypto by bandook_raja on Thursday August 03, 2017 @04:19AM (#14917866)
> Also Assembly doesn't has UB, making it ironic that it is safer to write straight Assembly at the expense of portability than C or C derived languages.

You just need to know your compiler flags/configuration to produce exact assembly output (if you want that). And about assembly not having UB, BSF and BSR are pretty good examples for that.

Re:How I implemented my own crypto by pjmlp on Thursday August 03, 2017 @04:36AM (#14917931)
That just is quite ironic, because it is even version dependent across releases of the same compiler, which means anything fine tuned for version X can break on version X + 1, due to changes on the optimizer.

And I doubt anyone is regularly reviewing the produced Assembly every single time they change compilers.

Assembly doesn't have more than 200 documented cases that hardly any human is capable of remembering.

Re:How I implemented my own crypto by daemin on Thursday August 03, 2017 @05:40AM (#14918179)
You could argue that each processor's errata is UB in assembly. I remember that programming for the XScale we had a whole manual of errata that we had to program around. Though because it's documented it's not "Undefined", it's just "Incorrect but we know how incorrect".
Re:How I implemented my own crypto by TheAceOfHearts on Thursday August 03, 2017 @04:28AM (#14917902)
Aren't all undocumented instructions / opcodes actually UB? If you execute an undocumented instruction the processor has to do something.
Re:How I implemented my own crypto by bananaboy on Thursday August 03, 2017 @04:36AM (#14917933)
You can't really accidentally or unintentionally arrive at undefined behaviour in assembly language though. You would have to explicitly use an undocumented instruction or opcode and the result would probably be a hardware interrupt or exception every time.
Re:How I implemented my own crypto by SAI_Peregrinus on Thursday August 03, 2017 @06:53AM (#14918476)
Not quite every time though: https://github.com/xoreaxeaxeax/sandsifter
Re:How I implemented my own crypto by bananaboy on Thursday August 03, 2017 @08:12AM (#14918832)
Woah that's pretty neat! Thanks!
Re:How I implemented my own crypto by JohnStrange on Thursday August 03, 2017 @04:32AM (#14917919)
I'd say that Ada would be well-suited for this. Particularly for implementing crypto. It has a steep learning curve, though.

Edit: Sorry, wrote this too hastily and want to put it in perspective. While Ada is well-suited for cryptography and has some good implementations, it is probably not as secure as C crypto libraries in practice. The reason is that the implementations I know don't do OS-specific things like locking memory or preventing core dumps, and doing this in Ada is a bit harder than in C.

Re:How I implemented my own crypto by willtim on Thursday August 03, 2017 @04:01AM (#14917792)
OCaml was used recently by the MirageOS folks to implement a better TLS than OpenSSL.
Re:How I implemented my own crypto by moosingin3space on Thursday August 03, 2017 @01:06PM (#14921434)
TLS != crypto. TLS is actually very well-suited for a higher-level language like Rust or OCaml, especially due to how state machine patterns can be expressed safely in those languages. Crypto, on the other hand, often relies on low-level intrinsics to get good performance and cache timing attack resistance.
Re:How I implemented my own crypto by willtim on Thursday August 03, 2017 @04:03PM (#14923257)
TLS depends on crypto and the mirage team implemented it using OCaml. It supports low-level intrinsics for timing attack resistance. See: https://github.com/mirleft/ocaml-nocrypto