News for hackers. Stuff that matters.
[dead]
Re:BearSSL – Smaller SSL/TLS by sctb on Thursday November 03, 2016 @04:43PM (#12867758)
Please don't do this here.
Re:BearSSL – Smaller SSL/TLS by bandrami on Thursday November 03, 2016 @10:13AM (#12864060)
Why? Crypto in a memory-managed language is not just a bad idea but a horrifically bad idea because you expose yourself to every single bug in the runtime's memory management.
Re:BearSSL – Smaller SSL/TLS by oconnor663 on Thursday November 03, 2016 @10:16AM (#12864075)
A lot of people are arguing that new projects like this should use Rust. Like https://github.com/ctz/rustls.
Re:BearSSL – Smaller SSL/TLS by falcolas on Thursday November 03, 2016 @10:24AM (#12864141)
Rust can't target the same platforms C can. Rust is also (generally) more memory intensive than C for similar programs. This makes its use in embedded situations a non-starter (at least for now).
Re:BearSSL – Smaller SSL/TLS by pcwalton on Thursday November 03, 2016 @03:13PM (#12867020)
> Rust can't target the same platforms C can.

I keep seeing this argument, but nobody actually names those platforms. Name one platform you need to run a TLS stack on that LLVM doesn't have support for.

> Rust is also (generally) more memory intensive than C for similar programs

[citation needed]

Re:BearSSL – Smaller SSL/TLS by falcolas on Thursday November 03, 2016 @06:52PM (#12868798)
> [citation needed]

With all the usual disclaimers about the inaccuracy of benchmarks:

https://benchmarksgame.alioth.debian.org/u64q/compare.php?la...

As a side note, can you guarantee you are not allocating memory (calling malloc) with Rust? I know how to do it in C, but Rust has another layer of indirection.

> Name one platform you need to run a TLS stack on that LLVM doesn't have support for.

Have a look yourself:

    $ llc --version # CPU architectures supported by LLVM
vs. https://en.wikipedia.org/wiki/GNU_Compiler_Collection#Archit...

Of particular interest to me are the AVR ATMega line of microprocessors, popular in Arduino boards and IoT devices everywhere. I'd personally love to see more IoT communication secured with TLS.

I do see a github repo for providing a LLVM backend for AVR processors, but it's not part of the LLVM project, nor is it even passing its own test suite at the point I'm writing this.

Re:BearSSL – Smaller SSL/TLS by pcwalton on Thursday November 03, 2016 @07:09PM (#12868913)
> https://benchmarksgame.alioth.debian.org/u64q/compare.php?la....

There are so many variables here that using the benchmarks game will not give you an accurate picture. You may well just be benchmarking jemalloc vs. glibc's allocator. jemalloc is tuned for speed of allocation in threaded contexts (with e.g. TLABs), not minimum memory usage. If you want, you can use the system allocator with Rust; for the benchmarks game we use jemalloc since it's faster.

> I know how to do it in C, but Rust has another layer of indirection.

How? Any function you call from some other library can call malloc under the hood.

> $ llc --version # CPU architectures supported by LLVM

I know there are architectures out there that LLVM doesn't support, but that doesn't mean anything unless people actually need those architectures. Not having PDP-11 support (one of the architectures on that list) doesn't make Rust a "non-starter".

> I do see a github repo for providing a LLVM backend for AVR processors, but it's not part of the LLVM project, nor is it even passing its own test suite at the point I'm writing this.

AVR support is coming along well.

AVR isn't exactly a first-class citizen among commonly used projects like OpenSSL. The reason why AVR support is immature is that few people need it. Not having that support isn't enough to make Rust a "non-starter", any more than it makes OpenSSL a "non-starter".

Re:BearSSL – Smaller SSL/TLS by falcolas on Thursday November 03, 2016 @09:05PM (#12869590)
> There are so many variables here that using the benchmarks game will not give you an accurate picture.

Hence the comment "With all the usual disclaimers about the inaccuracy of benchmarks". But it still gives a good, practical starting point for discussions. You're welcome to provide a counter data set to further the discussion.

> Any function you call from some other library can call malloc under the hood.

We're discussing low level libraries which aren't really calling out to other libraries, allowing you to quite easily control your memory allocation. It's also fairly easy to grep for 'malloc' in a C codebase, or inspect a compiled library for the malloc syscall.

I took a few minutes and looked at the current state of Rust with memory allocation as well, and it certainly does seem possible to remain within the stack only, by avoiding Box, Vec, String, and the various reference counted containers. Great to see.

There do seem to be some lingering limitations with stack-based data primitives and generics, but it's nothing you couldn't work around if you wanted to.

> unless people actually need those architectures

Like ATMega? If an architecture is supported in modern versions of GCC, there's a good chance that it's needed by somebody. And that doesn't count the dozens of specialized C compilers for other non-standard architectures. Modern banks are still running Cobol on mainframes, after all, and microcontrollers are everywhere.

> AVR support is coming along well.

Mind pointing me at a reference on, say, LLVM's roadmap? I see a fairly constant level of activity on the avr-llvm github repo, but nothing that appears to state it's ready for inclusion in LLVM in any defined timeframe.

Also curious about MSP430 moving out of experimental, Microchip's PIC, the Intel 8051, Hitachi's SuperH...

> make Rust a "non-starter", any more than it makes OpenSSL a "non-starter".

Wait, you're comparing a programming language to a heavy duty crypto library? Quite the literal version of comparing apples and oranges. Or apples and an Orange Julius.

There are other plenty of other crypto libraries that are aimed at microprocessors that aren't OpenSSL; wolfSSL as an example.

Ultimately, with enough time and money, Rust is capable of competing with C for the embedded space, making a lot of embedded developers happy. But not in the forseeable future.

Re:BearSSL – Smaller SSL/TLS by pcwalton on Thursday November 03, 2016 @09:24PM (#12869658)
> Hence the comment "With all the usual disclaimers about the inaccuracy of benchmarks". But it still gives a good, practical starting point for discussions. You're welcome to provide a counter data set to further the discussion.

There is nothing you can do in C++ that you can't do in Rust as far as memory is concerned. The compiler backends are even identical! You can drop libstd if you want in Rust, which you probably would in the microcontroller use case. You can even translate C to Rust [1], which should result in virtually identical LLVM IR!

That's what's so frustrating about throwing out benchmarks game numbers: the languages are isomorphic, so you end up ultimately comparing things like jemalloc implementations.

> Like ATMega? If an architecture is supported in modern versions of GCC, there's a good chance that it's needed by somebody. And that doesn't count the dozens of specialized C compilers for other non-standard architectures. Modern banks are still running Cobol on mainframes, after all, and microcontrollers are everywhere.

Why is COBOL on mainframes relevant? We're talking about C++ here. (Anyway, if you want to bring up mainframes, IBM has a working SystemZ backend for LLVM.)

Talking about "microcontrollers" is too broad of a brush. A lot of microcontrollers are ARM (or MIPS, etc.). Rust runs just fine on those.

> Ultimately, with enough time and money, Rust is capable of competing with C for the embedded space, making a lot of embedded developers happy. But not in the forseeable future.

This is again way too strong of a statement, because we have people using Rust right now for embedded IoT use cases (including some at Mozilla!) It all depends on what you need. If LLVM supports your architecture (which is probably does) then great! If it doesn't, then let's talk about the specific architecture you need and what you need it for, rather than making blanket "Rust is a non-starter for embedded use" statements.

We're never going to get support for every architecture anyone can come up with that C has ever run on. But who cares? What matters is whether Rust runs on a platform you were seriously considering using Rust for. If it doesn't, then we can get that fixed; chances are if you want that architecture, someone else using LLVM does too.

[1]: https://github.com/jameysharp/corrode

Re:BearSSL – Smaller SSL/TLS by umanwizard on Thursday November 03, 2016 @10:01PM (#12869806)
If Rust and C are in fact "isomorphic" then what's the advantage of using Rust over C?
Re:BearSSL – Smaller SSL/TLS by pcwalton on Thursday November 03, 2016 @11:07PM (#12870061)
I don't mean "isomorphic" in that every Rust feature has a corresponding C feature. I mean that you can replicate every runtime C feature in Rust, and vice versa. The advantage of Rust is that you get a lot more safety guarantees and ergonomic features out of the compiler than you would with C.
Re:BearSSL – Smaller SSL/TLS by bandrami on Friday November 04, 2016 @06:48AM (#12871600)
Are any of those safety guarantees important to a crypto project, though? Even heartbleed wasn't a "buffer overrun" in the memory safety sense of that word: the client was asking for and receiving the contents of memory that the server had every right, from a safety standpoint, to read from.
Re:BearSSL – Smaller SSL/TLS by pcwalton on Friday November 04, 2016 @02:07PM (#12874594)
Absolutely. You're confusing Heartbleed and Tedbleed.

https://tonyarcieri.com/would-rust-have-prevented-heartbleed...

Re:BearSSL – Smaller SSL/TLS by bandrami on Saturday November 05, 2016 @04:49PM (#12881818)
Oh, wow, thank you! I totally had conflated those two in my head. (Let this go down as a rare instance of a HN commenter not doubling down on being wrong...)
Re:BearSSL – Smaller SSL/TLS by reinar on Thursday November 03, 2016 @04:49PM (#12867810)
> Name one platform you need to run a TLS stack on that LLVM doesn't have support for.

MPC55xx

there's no proper support even in GCC and we need to work with expensive WindRiver compiler for this target.

personally I don't need TLS for my firmware, but someone might - as there's static without runtime alloc implementation available.

Re:BearSSL – Smaller SSL/TLS by pcwalton on Thursday November 03, 2016 @06:22PM (#12868551)
> MPC55xx

Google suggests that this is essentially PowerPC. Is that not true?

Re:BearSSL – Smaller SSL/TLS by reinar on Thursday November 03, 2016 @07:17PM (#12868973)
I wish it was that easy.

our e200z6 core has PowerPC ISA, but there's a difference in hardware floating point support (here it's done by SPE) and we also need VLE. First one you could somehow get working, but with the latter - no way, its support was dropped by mainline gcc even after the patches were proposed, since it complicates the other parts of PowerPC port and is needed only by few people like me with deep embedded stuff (source: https://gcc.gnu.org/ml/gcc/2013-03/msg00172.html).

Re:BearSSL – Smaller SSL/TLS by oconnor663 on Thursday November 03, 2016 @04:34PM (#12867690)
Maybe falcolas means that in the same way that C++ is more memory intensive than C for similar programs, because both C++ and Rust encourage a lot of Vecs/Strings that implicitly allocate on the heap?
Re:BearSSL – Smaller SSL/TLS by pcwalton on Thursday November 03, 2016 @06:20PM (#12868535)
Rust doesn't encourage that.
Re:BearSSL – Smaller SSL/TLS by oconnor663 on Thursday November 03, 2016 @08:57PM (#12869560)
I suppose everything's a continuum with this stuff, but here's one library I ran into where I was a little disappointed to find a heap allocation: https://dnaq.github.io/sodiumoxide/sodiumoxide/crypto/secret...
Re:BearSSL – Smaller SSL/TLS by steveklabnik on Thursday November 03, 2016 @11:23AM (#12864705)
Isn't the "more memory intensive" thing mostly due to jemalloc? How are you getting this data?
Re:BearSSL – Smaller SSL/TLS by mbanzi on Thursday November 03, 2016 @10:19AM (#12864106)
Are there mature versions of rust for the hundreds of microcontrollers that people are in production right now? Most of them have a decent GCC port and C works on all of them..
Re:BearSSL – Smaller SSL/TLS by steveklabnik on Thursday November 03, 2016 @11:24AM (#12864715)
In general, the versions of Rust end up being most of them, rather than being pinned to one specific version.

As my sibling mentions, if LLVM can target it, then we can, but generally anything < 32 bits isn't gonna work at the moment. We're slowly getting support for some of that stuff.

There's a whole group dedicated to working on this: https://github.com/rust-embedded/

Re:BearSSL – Smaller SSL/TLS by oconnor663 on Thursday November 03, 2016 @10:25AM (#12864150)
Rust uses LLVM on the backend, so it gets a lot of platform support for free. Not sure how mature that stuff is yet though. Maybe someone who's used Rust for embedded can jump in and comment?
Re:BearSSL – Smaller SSL/TLS by reinar on Thursday November 03, 2016 @01:54PM (#12866272)
> Are there mature versions

let's start from this

Re:BearSSL – Smaller SSL/TLS by user5994461 on Thursday November 03, 2016 @10:17AM (#12864082)
Only because they don't know Ada and they wanna reinvent the wheel again /s
Re:BearSSL – Smaller SSL/TLS by adrianN on Thursday November 03, 2016 @10:21AM (#12864120)
I agree that Ada is a lot better than C, but the guarantees Ada provides are different from the guarantees Rust provides.
Re:BearSSL – Smaller SSL/TLS by pcwalton on Thursday November 03, 2016 @03:15PM (#12867047)
As soon as you call (the equivalent of) malloc/free in Ada you either need a GC or you're in undefined behavior land. In Rust, that is not true.

Obviously, being able to call malloc without a GC is a nice to have for TLS on embedded systems.

Re:BearSSL – Smaller SSL/TLS by bandrami on Thursday November 03, 2016 @08:50PM (#12869521)
But rust has the exact same problem: if it's "safe", it's ironically not "secure" because the algorithm no longer controls its own memory operations. If you can't say, for instance, that the key is never swapped out, then it's not secure. But if you can say the key is never swapped out, you're at a level of direct memory control that is no longer "safe".
Re:BearSSL – Smaller SSL/TLS by oconnor663 on Friday November 04, 2016 @11:55PM (#12878071)
I think "memory that is never swapped to disk" would fit nicely into Rust's ownership/borrowing model. It would probably just be a variant on Box that makes more guarantees, but which exposes the same safe interfaces? Having a safe API built on top of unsafe calls is more or less how everything in Rust works (maybe how any safe language works?), when you drill down far enough.

That said, I guess you'd have to be careful to avoid creating something like a NoSwapBox<Vec>, because the NoSwap property would only apply to the (ptr, capacity, length) triple of the vec, and not to its actual heap allocation that you probably care about. Maybe the answer there is something like requiring that the contents of NoSwapBox implement Copy, or some other way of saying "this is plain old data"? I don't know enough about the plans for custom allocators to say anything intelligent :p

Re:BearSSL – Smaller SSL/TLS by mbanzi on Thursday November 03, 2016 @10:17AM (#12864087)
you should have read the rest before commenting. This is ideal for embedded targets and C is the best way to write a truly portable library for these small targets. Consider that a lot of embedded code is designed to avoid mallocs() altogether and that helps a lot.
Re:BearSSL – Smaller SSL/TLS by sp332 on Thursday November 03, 2016 @10:36AM (#12864239)
Having a C binding doesn't mean you have to implement the bulk of the code in C.
Re:BearSSL – Smaller SSL/TLS by reinar on Thursday November 03, 2016 @01:52PM (#12866254)
For many embedded systems anything that has 'bindings' is bloatware.
Re:BearSSL – Smaller SSL/TLS by dbaupp on Thursday November 03, 2016 @06:31PM (#12868632)
Some platforms only have (bug-ridden, poorly supported) C compilers available: i.e. there's no other language to write in, let alone bind to.
Re:BearSSL – Smaller SSL/TLS by Florin_Andrei on Thursday November 03, 2016 @06:23PM (#12868560)
Have you ever programmed a microcontroller?

(And yes, it would actually be nice to have reliable crypto on a micro-c.)

Re:BearSSL – Smaller SSL/TLS by seibelj on Thursday November 03, 2016 @10:13AM (#12864064)
What would be a better language for creating a small, portable crypto library that works on embedded systems? Would you really put your trust in a brand new language?
Re:BearSSL – Smaller SSL/TLS by pornin on Thursday November 03, 2016 @10:25AM (#12864152)
Actually, C is a horrible language in many respects, but it is also the only language that can achieve any kind of decent compatibility in embedded systems, which is why I used it.

Also, when I say that BearSSL is written in C, it is partly a lie: some of it (especially X.509 certificate decoding, and handling of handshake messages) is done in T0, a new Forth-like language that I invented for the task (compiler is provided, and produces C code), specifically to have coroutines (incidentally, it means that most of that code read things byte by byte, with relatively few accesses to buffers, thus less potential buffer overflows).

Re:BearSSL – Smaller SSL/TLS by unwind on Thursday November 03, 2016 @11:11AM (#12864593)
Here's a link to the first T0 code I found, for the interested readers out there: https://bearssl.org/gitweb/?p=BearSSL;a=blob;f=src/x509/x509....
Re:BearSSL – Smaller SSL/TLS by stompin on Thursday November 03, 2016 @10:57AM (#12864460)
do you really need coroutines for cert decoding and handling of handshake messages? It's not like you can parallelize these things and do something else in the meantime...
Re:BearSSL – Smaller SSL/TLS by pornin on Thursday November 03, 2016 @11:13AM (#12864608)
Certificates and handshake messages are nested structures, and I want to process them in a streamed fashion (e.g. I don't buffer a complete certificate, I don't have the RAM for that; BearSSL can decode certificates and messages that are larger than the total RAM is uses), so without coroutines, I would still need some sort of decoder with a stack of states, so I just accepted it and pushed the idea to its logical conclusion.

Despite the horrors implied by a Forth syntax, this actually made writing the code easier. As a bonus, it turns out that T0-generated code is very compact, so I could pack more behaviour into less bytes.

Re:BearSSL – Smaller SSL/TLS by seibelj on Thursday November 03, 2016 @11:10AM (#12864581)
Thanks for your response! I'm going to shoot you an email, I would be interested in helping you with this project.
Re:BearSSL – Smaller SSL/TLS by banach on Thursday November 03, 2016 @10:20AM (#12864109)
Given that this is a reasonably small code base (compared to, say, an OS kernel), an idea would be to use a language that provides strong guarantees about correctness the implementation. For example, Spark Ada can be used to do that. Alternatively, keeping the implementation in C, a similar level of assurance could be achieved by developing the library along with a specification, and requiring any extensions to pass some kind of correctness proof based on this specification.
Re:BearSSL – Smaller SSL/TLS by pjmlp on Thursday November 03, 2016 @11:28AM (#12864759)
The is approach taken by the F* folks on their TLS library.
Re:BearSSL – Smaller SSL/TLS by peterwwillis on Thursday November 03, 2016 @02:24PM (#12866547)
I too am waiting for the JavaScript implementation.