cryptography in haskell

Hi, I've been wondering about the state of cryptography in haskell. Not so much in the sense of "what libraries are out there?", but rather about the question what crpyto and IT security people think about ideas like rewriting something as OpenSSL in haskell. I know it can be technically done, but are there any remarks in this area that are important for practical security? For example, some people think that it can be dangerous to implement something like this in high-level languages (e.g. java which was vulnerable to timing attacks). Of course I think haskell can do a lot for us to make things safer: * type safety * referential transparency * explicit knowledge about side-effects and which kind ... But that doesn't tell me if it introduces new pitfalls/attack-vectors for practical cryptography implementations. -- Regards, Julian Ospald

On Sat, Feb 7, 2015 at 11:21 AM, Julian Ospald
I've been wondering about the state of cryptography in haskell. Not so much in the sense of "what libraries are out there?", but rather about the question what crpyto and IT security people think about ideas like rewriting something as OpenSSL in haskell.
I know it can be technically done, but are there any remarks in this
area that are important for practical security?
From a high-level security perspective, libressl offers the most hope in
It's an interesting exercise (at least implementing TLSv1.2+) One thing to remember, though, is that a lot of the issues with OpenSSL aren't solved by having memory safety or isolation of effects: downgrade attacks, mac-then-encrypt instead of encrypt-then-mac, Heartbleed, etc. In other words, Haskell eliminates several classes of errors, but doesn't prevent logic errors, and can do nothing about poor standards. Aside from this, I think the main issues would be: - Timing resistance: This is not as simple as sprinkling some bitwise operations on your crypto code. It took a long time to figure out even the basics in OpenSSL, and for better and worse it's more difficult to intuit what your Haskell code will be compiled to than it is with C (though C compilers have been known to optimize away constant-time code.) - Usability: C is easy to embed in any program written in any language. Haskell, not so much. If nobody is using your library, it doesn't matter if it's the safest TLS implementation in the world! (Another small issue is that it's not easy to erase something, e.g. a private key, from memory in Haskell, so they may stick around for some time. This isn't a big deal as long as your application's memory isn't swapped to disk.) the short-term, and it's possible that e.g. a Rust (which at least is a lot like Haskell, but without a runtime!) TLS implementation could replace OpenSSL in a lot of existing applications in the longer-term. But because of the complexity of TLS and the possibility of logic errors, it's not a sure thing that implementations in Rust, Haskell, or what have you, would be more, or even equally as safe as something like libressl. That being said, if you're thinking about doing something like this, go for it! Implementing TLS is fun (as long as you don't try to reach compatibility with anything but recent clients,) and it might be useful for other Haskellers.

On Sat, Feb 7, 2015 at 8:53 AM, Patrick Mylund Nielsen < haskell@patrickmylund.com> wrote:
On Sat, Feb 7, 2015 at 11:21 AM, Julian Ospald
wrote: I've been wondering about the state of cryptography in haskell. Not so much in the sense of "what libraries are out there?", but rather about the question what crpyto and IT security people think about ideas like rewriting something as OpenSSL in haskell.
I know it can be technically done, but are there any remarks in this
area that are important for practical security?
It's an interesting exercise (at least implementing TLSv1.2+) One thing to remember, though, is that a lot of the issues with OpenSSL aren't solved by having memory safety or isolation of effects: downgrade attacks, mac-then-encrypt instead of encrypt-then-mac, Heartbleed, etc.
In other words, Haskell eliminates several classes of errors, but doesn't prevent logic errors, and can do nothing about poor standards.
Aside from this, I think the main issues would be:
- Timing resistance: This is not as simple as sprinkling some bitwise operations on your crypto code. It took a long time to figure out even the basics in OpenSSL, and for better and worse it's more difficult to intuit what your Haskell code will be compiled to than it is with C (though C compilers have been known to optimize away constant-time code.)
- Usability: C is easy to embed in any program written in any language. Haskell, not so much. If nobody is using your library, it doesn't matter if it's the safest TLS implementation in the world!
TLS contains numerous design errors that we all have to live with like "mac then encrypt" which we have to deal with through incredibly "careful" implementation. It would be an interesting research project to try to understand if laziness is a benefit or a challenge when working with flawed protocol designs that known to vulnerable to timing oracle attacks. The Open Mirage project which uses an OCAML unikernel architecture makes a strong case for putting security critical parts of an applications stack in a virtualized type safe container. Haskell seems like it offers the strongest properties when demonstrating a good design is implemented as intended. It isn't likely to have any magic properties for protect against poor design.
(Another small issue is that it's not easy to erase something, e.g. a private key, from memory in Haskell, so they may stick around for some time. This isn't a big deal as long as your application's memory isn't swapped to disk.)
From a high-level security perspective, libressl offers the most hope in the short-term, and it's possible that e.g. a Rust (which at least is a lot like Haskell, but without a runtime!) TLS implementation could replace OpenSSL in a lot of existing applications in the longer-term. But because of the complexity of TLS and the possibility of logic errors, it's not a sure thing that implementations in Rust, Haskell, or what have you, would be more, or even equally as safe as something like libressl.
That being said, if you're thinking about doing something like this, go for it! Implementing TLS is fun (as long as you don't try to reach compatibility with anything but recent clients,) and it might be useful for other Haskellers.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 07/02/15 18:53, Patrick Mylund Nielsen wrote:
(Another small issue is that it's not easy to erase something, e.g. a private key, from memory in Haskell, so they may stick around for some time. This isn't a big deal as long as your application's memory isn't swapped to disk.)
https://hackage.haskell.org/package/securemem
That being said, if you're thinking about doing something like this, go for it! Implementing TLS is fun (as long as you don't try to reach compatibility with anything but recent clients,) and it might be useful for other Haskellers.

On Sat, Feb 7, 2015 at 6:54 PM, Roman Cheplyaka
On 07/02/15 18:53, Patrick Mylund Nielsen wrote:
(Another small issue is that it's not easy to erase something, e.g. a private key, from memory in Haskell, so they may stick around for some time. This isn't a big deal as long as your application's memory isn't swapped to disk.)
Now consider how you'd construct and use a SecureMem without copying your secret all over memory. Again, not a big issue, but it's trickier in garbage-collected languages.

Am Sat, 7 Feb 2015 11:53:42 -0500 schrieb Patrick Mylund Nielsen:
In other words, Haskell eliminates several classes of errors, but doesn't prevent logic errors, and can do nothing about poor standards.
Aside from this, I think the main issues would be:
- Timing resistance: This is not as simple as sprinkling some bitwise operations on your crypto code. It took a long time to figure out even the basics in OpenSSL, and for better and worse it's more difficult to intuit what your Haskell code will be compiled to than it is with C (though C compilers have been known to optimize away constant-time code.)
I feel prompted to add something in here, since I'm working on this as the author of hecc[0]. My published basic implementation of elliptic curve cryptography contains the basic math for the so-called NIST-curves[1] in a best-effort timing-attack resistant way, which I will try to explain here. I will try to make my own experiences into simple guidelines this way, so it might be a bit long-ish. There are numerous ways to see different execution times depending on the bits of the secret key, each of which has to be fixed. First of all, if there is a branch in your code, a construct like if secretkeybit == True then do_something else do_nothing is way too easy to attack. (1) All your branches need to have exactly the same execution times. If you implement RSA or ECC, there will at some point be a square-and-multiply or double-and-add equivalent. A good fix for this is the Montgomery's Ladder, which has the property of juggling both branches in its parameters. In theory, this will leave no timing channel since both branch-parameters will be executed (I learned: {-# LANGUAGE BangPatterns #-} is very helpful here). (2) All branches need to be really executed. Sadly, this is not sufficient in a lazy evaluation environment like Haskell - or any other language which permits the compiler to optimize computations away (I agree: There be dragons if you really want to be compiler-safe in C! ;-)). I found out that special patterns in a secret key can lead a lazy Montgomery Ladder to faster execution times in Haskell, which I will try to explain in a proper paper in detail, time permitting. There are malicious little things in modern CPUs called "caches", which make some executed code quite a bit faster. In company with the branch prediction unit, branches based on bits of the secret key become very volatile, so the best defense is (3) No branches based on the content of bits of the secret key. This point is very hard to do when implementing the NIST-curves for TLS, my own code is not yet made in this fashion. Also currently, most people use Integer-gmp, which is of course based on gmp, which got timing-attack resistant (slower) implementations of methods in its version 6 release, so every one before that may be insecure when regarding timing attacks. This is not a deal-breaker, since other libraries in other languages also depend on gmp, like nocrypto[2] for Mirage OS and GNUTLS. I am currently fixing my new Ed25519/EdDSA implementation in pure Haskell (please don't confuse it with the fully functioning and fast C-wrapper one [3]), which is branch-free per point (3) and should be an example of code under such a rule. Right now I am not sure when the code will be ready for public scrutiny, but it should at least be implementation-secure and correct, being fast can follow later. This release will also feature a rework of hecc and a name-change, since the possible confusion with Hyper-Elliptic Curve Cryptography. Please note: I am currently searching a (paid) position as (preferably) phd student. I like working on functional programming, provabilty and cryptography. If you or anyone you know has interest in having me working for them, please contact me! Best of wishes, Marcel Fourné [0]:https://hackage.haskell.org/package/hecc [1]:http://csrc.nist.gov/groups/ST/toolkit/documents/dss/NISTReCur.pdf [2]:https://github.com/mirleft/ocaml-nocrypto [3]:https://hackage.haskell.org/package/ed25519

Am Tue, 17 Feb 2015 10:43:12 +0100 schrieb Marcel Fourné:
Am Sat, 7 Feb 2015 11:53:42 -0500 schrieb Patrick Mylund Nielsen:
In other words, Haskell eliminates several classes of errors, but doesn't prevent logic errors, and can do nothing about poor standards.
Aside from this, I think the main issues would be:
- Timing resistance: This is not as simple as sprinkling some bitwise operations on your crypto code. It took a long time to figure out even the basics in OpenSSL, and for better and worse it's more difficult to intuit what your Haskell code will be compiled to than it is with C (though C compilers have been known to optimize away constant-time code.) [...] (3) No branches based on the content of bits of the secret key.
Basically, an encoding of the advice[0] Peter Schwabe gave at ShmooCon 2015 for C-like languages translate well to Haskell, but having typecheckable timing attack resistance would be nicer. Cheers, Marcel Fourné [0]:https://cryptojedi.org/peter/data/shmoocon-20150118.pdf

I'm not sure if you have already read it, but at least for the reference in this thread, this is also discussed here: https://news.ycombinator.com/item?id=7557089 Karel On 02/ 7/15 05:21 PM, Julian Ospald wrote:
Hi,
I've been wondering about the state of cryptography in haskell. Not so much in the sense of "what libraries are out there?", but rather about the question what crpyto and IT security people think about ideas like rewriting something as OpenSSL in haskell.
I know it can be technically done, but are there any remarks in this area that are important for practical security?
For example, some people think that it can be dangerous to implement something like this in high-level languages (e.g. java which was vulnerable to timing attacks).
Of course I think haskell can do a lot for us to make things safer: * type safety * referential transparency * explicit knowledge about side-effects and which kind ...
But that doesn't tell me if it introduces new pitfalls/attack-vectors for practical cryptography implementations.
-- Regards, Julian Ospald _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Julian Ospald wrote:
I've been wondering about the state of cryptography in haskell. Not so much in the sense of "what libraries are out there?", but rather about the question what crpyto and IT security people think about ideas like rewriting something as OpenSSL in haskell.
At Galois they have done a lot of work on this during the past few years. Of course, the good points brought up by Patrick have been prominent in their thinking. I'm not sure how much of that work is available publicly. But they did open-source Cryptol, their Haskell-based DSL for designing and testing crypto algorithms. http://galois.com/ http://cryptol.net/ Regards, Yitz

Another path worth investigating would be to construct a Haskell (e)DSL
which produces C code, but adds better constraints and guarantees through
Haskell's type system, a bit like what people are already doing for
embedded systems
On Feb 16, 2015 12:34 PM, "Yitzchak Gale"
Julian Ospald wrote:
I've been wondering about the state of cryptography in haskell. Not so much in the sense of "what libraries are out there?", but rather about the question what crpyto and IT security people think about ideas like rewriting something as OpenSSL in haskell.
At Galois they have done a lot of work on this during the past few years. Of course, the good points brought up by Patrick have been prominent in their thinking.
I'm not sure how much of that work is available publicly. But they did open-source Cryptol, their Haskell-based DSL for designing and testing crypto algorithms.
http://galois.com/ http://cryptol.net/
Regards, Yitz _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (8)
-
Julian Ospald
-
Karel Gardas
-
Marcel Fourné
-
Patrick Mylund Nielsen
-
Roman Cheplyaka
-
Tobias Dammers
-
Yitzchak Gale
-
zaki@manian.org