News for hackers. Stuff that matters.
Re:TLS/SSL implementation in Haskell by chimeracoder on Tuesday April 08, 2014 @10:04PM (#7557311)
While Haskell mitigates or eliminates some classes of bugs common in C (such as buffer overflow), it also makes it more difficult to guard against side-channel attacks like timing attacks[0], because lazy evaluation makes it more difficult to reason about the actual behavior of the code at runtime.

This isn't a dig at either Haskell or C; the point is that all programming languages and environments have their "gotcha!" moments.

[0] https://en.wikipedia.org/wiki/Timing_attack

Re:TLS/SSL implementation in Haskell by joeyh on Tuesday April 08, 2014 @10:37PM (#7557402)
http://hackage.haskell.org/package/securemem implements constant-time bytestring comparisons for haskell. With the bonus that the data is scrubbed when GC'd. This is used by parts of the haskell TLS stack, for example http://hackage.haskell.org/package/cipher-aes

I don't think that laziness makes things much harder. Compare a C function with if foo return 0 in the middle and a Haskell function that lazily builds thunks that don't end up being evaluated in a particular branch. There's obvious things to do to both to make them run in constant time. The hard part about dealing with a timing attack in both cases is recognising that the function is used in such a context and needs to be constant time.

AFAIK, the haskell TLS stack has not yet been audited. For this reason, there's some reluctance to use it in parts of the Haskell community. Although http-conduit and http-client-tls do use it, so everyone writing programs that hit https from haskell using conduit is using it already.

Re:TLS/SSL implementation in Haskell by runeks on Tuesday April 08, 2014 @11:23PM (#7557511)
> http://hackage.haskell.org/package/securemem implements constant-time bytestring comparisons for haskell. With the bonus that the data is scrubbed when GC'd.

As far as I understand, this is not sufficient.

Some things that are needed in addition are (at least):

* constant time math operations (multiply, divide, exponentiation, etc.)

* no branching based on secrets (running the code results in no branch conditions based on secret data)

Here's an interesting article on implementing a form of Elliptic Curve Cryptography without even local timing attacks: http://blog.cr.yp.to/20140323-ecdsa.html

Re:TLS/SSL implementation in Haskell by dpatru on Wednesday April 09, 2014 @06:15AM (#7558524)
Why can't timing be handled external to the scrambling or descrambling operations? Look at the time, do the crypto, then check the time and sleep as necessary so that this particular operation takes about as much time as the worse-case. Something like:

  cryptofunc x = do
    begintime <- gettime
    result <- purecryptofunc x
    endtime <- gettime
    sleep (worstcase - (endtime - begintime))
    return result
Re:TLS/SSL implementation in Haskell by mantasm on Wednesday April 09, 2014 @07:23AM (#7558672)
Properly implemented crypto takes a constant number of instructions to encrypt/decrypt, regardless of key, plaintext, etc.

Time to sleep() would vary across processors, os-es, and even load (!), so it's not exactly a workable solution. Not to mention it would be grossly inefficient (Context switch on every crypto primitive? No thanks).

Re:TLS/SSL implementation in Haskell by ema on Wednesday April 09, 2014 @08:52AM (#7558927)
This still would allow side channel attacks on cpu utilization. cpu utilization can be inferred, for example if the attacker has a vm on the same machine, or the attacker can make a second request which completes sooner if the cpu is less utilized.
Re:TLS/SSL implementation in Haskell by axman6 on Wednesday April 09, 2014 @09:39PM (#7563705)
while not a completely terrible idea, sleep doesn't give you any guarantee of when it'll wake your code up again, only that it'll be asleep for at least X time. I think this is ok, but someone with more info on these sorts of bugs would be better place to comment.

It also seems reasonable to add a random length of time to the sleep time, so that differences in computation time are indistinguisable from the randomness added by the random variable. I could be completely wrong though.

(also, you probably need to force with seq or deepseq the result of purecryptofunc before getting the second time, or you might be adding that evaluation time to another part of the code, and then this code becomes just a constant time added to the computation)

Re:TLS/SSL implementation in Haskell by floody-berry on Wednesday April 09, 2014 @02:01AM (#7557851)
> The AES implementation uses AES-NI when available (on x86 and x86-64 architecture), but fallback gracefully to a software C implementation.

> The software implementation uses S-Boxes, which might suffer for cache timing issues. However do notes that most other known software implementations, including very popular one (openssl, gnutls) also uses similar implementation.

Oh, well if OpenSSL does it..

Re:TLS/SSL implementation in Haskell by floody-berry on Friday April 11, 2014 @12:12AM (#7571183)
Downvotes for quoting from the pages linked, which are defending their use of non-constant time operations by referencing OpenSSL. Do you guys have working brains?
Re:TLS/SSL implementation in Haskell by codygman on Tuesday April 08, 2014 @10:19PM (#7557344)
Perhaps it would be more interesting to discuss how the author of the Haskell TLS/SSL library gets around timing attacks. Actually I have him on twitter... paging @vincenthz.

Timing attacks is the first thing I thought of when I saw this implementation so this should be interesting if he can respond.

Re:TLS/SSL implementation in Haskell by vhz on Wednesday April 09, 2014 @11:42AM (#7559981)
(author here)

Getting around timing attacks is not too different in Haskell from doing the same thing in C. First, there's 2 different classes of timing issues:

1) cryptographic ones

2) and the others (for the lack of better classification)

For cryptographic ones, it's hard to enumerate all the counter measures used/not used, but some examples:

- use of blinding with RSA

- with ghc 7.8, exponiantiation is using GMP expmod_safe

- AES is using native instruction when possible.

- Use of scrubbed memory/memory constant where possible

- some lowlevel cryptographic implementations are actually in C (hash, aes, ..)

Also, while I'm linking against specific set of cryptographic implementations by default, there's nothing preventing anyone from pulling different crypto implementations from other places.

For the other issues, it's usually just a deal of strictness, and not bailing/notifying error too early. (e.g. This is what Bleinchenbacher and CRIME are all about).

- data comparaison is time constant by comparing all bytes where necessary: prevent padding attack

- there's routines to evaluate boolean values without bailing too early (False && ... -> would bail at the first false)

- lots of part in tls is using strict bytestring, and strict values.

- the GC is adding a lot of noise potentially hiding timing issues, potentially exacerbated by pure values instead of mutable values (like you would in C)

Also, I don't want to claim there's no issues or ever will be. Hopefully there's no such things, but I welcome any audits and questions about stuff that look fishy. But people shouldn't only looks at the cryptographic side of security; openssl had so many issues about basic unchecked values, buffer underflow, overflow that's not even funny anymore. Just sayin'

Re:TLS/SSL implementation in Haskell by TheLoneWolfling on Wednesday April 09, 2014 @03:29PM (#7561718)
How do you prevent the compiler optimizing constant-time operations into non-constant time?
Re:TLS/SSL implementation in Haskell by Guvante on Sunday April 13, 2014 @02:04AM (#7580416)
You would want to specify a version of GHC and optimization flags, then it would just involve code analysis of your output.

Likely this work would be necessary to "prove" (not in the strict form) your assumptions about constant-time anyway.

Re:TLS/SSL implementation in Haskell by jerf on Wednesday April 09, 2014 @09:49AM (#7559225)
If I were handed a few million dollars and told to go create an SSL implementation, my approach would be to write the implementation in Haskell in a way that compiles down to LLVM or, possibly, assembler, which is then what the project would distribute. Haskell's type system can be used for all the juicy goodness being described in the other comments, but it would not be running in the Haskell runtime. See: http://hackage.haskell.org/package/atom which goes to C; in principle it's not hard to imagine then how you'd go to another target.

There's two reasons for this... first, a pure-Haskell solution locks it to Haskell only, as there's very little realistic chance of embedding Haskell in any other environment. Having it compile to something like straight assembler or LLVM means that we can almost certainly wrap that in almost any runtime environment effectively. (We might require a bit of new SWIG-like tech to do it, but under the circumstances it would probably be worth it.) To the extent that's not a true statement, imagine that I will research and find the correct level of abstraction. (Maybe it really is C.)

Second, with all due respect to the Haskell runtimes in the world, it's just too big a surface to secure. People are asserting here in this thread that you could avoid side-channel attacks in the Haskell runtime, but how would you prove that? To any degree?

Theoretically, you might be even better off in a real proof language, but it's not clear to me that any of them have a good enough extraction-to-LLVM/assembler story.

This would, in theory, produce a solution that should be embeddable in nearly any other language, and indeed, even in embedded environments, while still leveraging the strengths of Haskell's expressiveness and type safety to build a very safety-critical library. We would also be able to write some powerful tests to verify that the putative time-invariant operations really are time-invariant. My strong suspicion is that we'd discover some processor differences here that would surprise everybody.

Re:TLS/SSL implementation in Haskell by gamegoblin on Tuesday April 08, 2014 @11:18PM (#7557498)
GHC allows you to force fields to be strict rather than lazy.

http://www.haskell.org/ghc/docs/7.4.1/html/users_guide/bang-...

Re:TLS/SSL implementation in Haskell by quchen on Wednesday April 09, 2014 @03:12AM (#7558053)
Strict fields are actually standard Haskell, http://www.haskell.org/onlinereport/haskell2010/haskellch4.h...

(The similarly looking BangPatterns extension is supported by GHC only.)

Re:TLS/SSL implementation in Haskell by tga_d on Tuesday April 08, 2014 @10:38PM (#7557406)
Exactly the first thing I thought. There are plenty of side-channels in cryptographic code that require access to somewhat low-level hardware to fix (e.g. the acoustic attack on GPG that got plenty of press), and I don't see how you can address those without e.g. inline C or something else that negates the advantages of using Haskell.
Re:TLS/SSL implementation in Haskell by chrisdone on Saturday April 12, 2014 @06:14AM (#7577477)
FWIW inline C doesn't negate the advantages of using Haskell. It's inline.
Re:TLS/SSL implementation in Haskell by dhotson on Tuesday April 08, 2014 @11:33PM (#7557537)
The first thing I searched for in the README was "constant time".

This whole thing has got me curious about techniques for formally verifying software.

I'm a complete beginner on this topic, but I assume there are tools to help write software that's formally verified / proven to be correct. Can they make any guarantees about side-channel aspects such as timing in these systems? Anyone care to give me some pointers on where to begin?

Re:TLS/SSL implementation in Haskell by runeks on Wednesday April 09, 2014 @12:01AM (#7557590)
This blog post isn't about writing automated tools to detect timing attacks in code, but it has a lot of stuff about how to write crypto code -- elliptic curve cryptography in particular -- that is impervious to timing attacks, very interesting if you ask me: http://blog.cr.yp.to/20140323-ecdsa.html
Re:TLS/SSL implementation in Haskell by ElliotH on Wednesday April 09, 2014 @06:40AM (#7558574)
http://scholar.google.co.uk/scholar?q=%22timing+attack%22+%2... turns up some papers that consider this. They don't seem to provide much by way of tooling though.

A glance over the modelling doesn't seem to preclude using a model checker like http://www.event-b.org/ for it though.

The challenge comes in when you try to translate. You write your model up and validate your approach, you then try to translate it into C (or whatever) and that point you can be as certain of your design as you like, you still can't be sure that the compiled software will work properly. I haven't seen a convincing solution to that particular problem (and I've done some research on the topic for three separate projects now).

Re:TLS/SSL implementation in Haskell by pintor on Tuesday April 08, 2014 @10:07PM (#7557317)
Maybe Rust will provide the right balance between the two.
Re:TLS/SSL implementation in Haskell by kzrdude on Tuesday April 08, 2014 @10:12PM (#7557326)
No programming language seems to provide primitives for constant-time algorithms.

At this point, gcc and clang should at least provide constant time memcmp primitives; any custom solution not supported by the compiler is not portable and needs to be validated separately for each implementation, each platform, each compiler version.

Re:TLS/SSL implementation in Haskell by kivikakk on Tuesday April 08, 2014 @10:18PM (#7557342)
Re:TLS/SSL implementation in Haskell by sseveran on Wednesday April 09, 2014 @01:20AM (#7557779)
Package subtle implements functions that are often useful in cryptographic code but require careful thought to use correctly.
Re:TLS/SSL implementation in Haskell by awda on Tuesday April 08, 2014 @10:14PM (#7557329)
whoosh!
Re:TLS/SSL implementation in Haskell by carterschonwald on Wednesday April 09, 2014 @09:56AM (#7559258)
Don't make shit up. It'd very easy to write totally strict code in Haskell. It's even possible to write code that won't yield to the run time system for a constant time op.