Live data from Hacker News

How I implemented my own crypto

loup-vaillant.fr

381–390 of 409 posts

Re: How I implemented my own crypto

#381
post #335

Earlier quoted context omitted.

Sure! Now replace flying planes with building your own planes .

Huh? Did you not read the last sentence? "In fact, the aviation community literally encourages people to build their own airplanes!" https://www.google.com/search?client=safari&rls=en&q=build+y... http://www.kitplanes.com/homebuilders-portal/firsttimebuilde...

It doesn't encourage people to try and build commercial jet airliners in their back sheds. You can follow a tutorial and build a KitFox or whatever, which is the equivalent of coding, I dunno, a virtual Enigma machine or something. You can't follow a tutorial to learn how to design an Airbus A380.

Re: How I implemented my own crypto

#382
post #302
post #298

Earlier quoted context omitted.

> Carry propagation is one pixel in the 4K movie that is safe crypto implementation This is exactly the sort of thing I'm talking about. This is semantically equivalent to, "If you don't already know this, then you are too stupid, and my time is too valuable, for me to do anything beyond pointing out that you are stupid and my time is valuable." That may be true, but it's not helpful. > There are programs that teach…

With all due respect, some things should only be done by trained professionals. Crypto is one of them. Flying a commercial airliner is another.

Every professional was an amateur once.

Re: How I implemented my own crypto

#383

Earlier quoted context omitted.

I wish the meme that crypto is hard would just die. The reason is that the statements is wrong and is probably encourage exactly what it wants to discourage. Crypto is hard is not factually incorrect. The problem is that it is trivially correct, because programming is always hard. Every algorithm and design is hard to get right. The correct statement, and the one that should be repeated instead is that crypto's stake…

No, crypto is actually hard. One difference from regular programming is that there really are no 'intermediate' errors. Experience shows that getting one small detail wrong is as bad as getting the whole thing wrong. I can't think of another programming domain where that holds.

Experience shows that getting one small detail wrong is as bad as getting the whole thing wrong.

Then it's highly likely that there isn't a single "completely" secure crypto implementation in existence...

Re: How I implemented my own crypto

#384

Earlier quoted context omitted.

> Improper carry propagation is an example of the kind of security vulnerability for which there are no test vectors I beg your pardon? Some tests vectors from the RFC are designed specifically to handle some overflow that if handled improperly will give you the wrong results. I have checked, this hits the relevant paths. Or were you talking about something else? By the way, I haven't attempted to implement curve2551…

I feel like I'm reading you again claiming that the test vectors for an algorithm in the standards documents constitutes a battery of security tests for the algorithm. That's not the case; I think 'lisper will tell you the same thing, despite being much more on your side of this issue than I am. Test vectors are an implementation aid and mostly intended for interop. And yes, I believe I'm talking about a more complic…

I'll look up this carry propagation business, but again, this looks like a mundane correctness issue (with possibly tragic consequences), though it is hard to assess without looking at intermediate results (I have).

Also, didn't the carry propagation bug reports come with failing tests vectors? Or are they hard to find even if one knows of the bug?

> the only thing I care about is the idea that if you pass test vectors and Valgrind is OK with your binary, you're probably OK. No!

Definitely agree. Even test vectors + safe Rust aren't enough. Which is why I added random comparison tests with libsodium (for every possible length input between 0 and a couple times the size of the block), and property based tests.

I'm still a bit uneasy about carry propagation in Poly1305, but I really checked the hell out of my code.

Re: How I implemented my own crypto

#385

Earlier quoted context omitted.

> I see no mention of how monocypher implementation does anything 5o prevent side channel timing attacks, Oh come on, the chosen primitives are all designed for easy immunity against timing attacks. I haven't verified this formally, but I basically ripped off safe designs, and I tried to be careful about avoiding secret dependant branches and indices. > I haven't looked to see if there is any sensitive memory scrubbi…

The chosen primitives can still be implemented such that there are timing side channels.

This doesn't happen naturally, except for Poly105 and curve25519 (for those one indeed has to be careful). A naive implementation of Chacha20 for instance is pretty much guaranteed to be immune to timing attacks.

Re: How I implemented my own crypto

#386

Earlier quoted context omitted.

> the part about XChaCha20 being a mix of ChaCha20 and XSalsa20 is IMHO dancing on the line It was a few months ago. Then Libsodium danced on the same line, and I was able to compare the results (they behave the same, phew). It's not inventing crypto either: it's a straightforward application of what was done to Salsa20. The security reduction that worked for XSalsa20 (guaranteed secure if Salsa20 is secure) applies…

> Then Libsodium danced on the same line, and I was able to compare the results (they behave the same, phew). Do they really? What about timing attacks? Does all your code run in constant time? If not, there's a whole class of vulnerability that basically will only be found when experienced attackers have a chance to poke at it. Timing attacks are a great example of why "don't roll your own crypto" is sound advice. N…

> Do they really?

Absolutely:

> […] However, XChaCha20 is currently not widely implemented outside the libsodium library, due to the absence of formal specification.

https://download.libsodium.org/doc/advanced/xchacha20.html

Can't say for sure, but it look like they didn't have an independent implementation to compare to. Just like me a few months a go.

---

> What about timing attacks?

Monocypher is immune.

First, the primitive were chosen precisely because they are easily immunised against timing attacks.

Second, this is easily checked simply by looking at the source code: no secret-dependent branches, no secret-dependent indices, and that's about it. One doesn't need to be an expert to check that, a junior programmer could do it after being told what to look for.

Re: How I implemented my own crypto

#387
post #134

Earlier quoted context omitted.

> that X or Y new language completely avoids C pitfalls Yeah, you don't even need a new, hip language to avoid C's pitfalls. > Portability does not equal 'without blemishes'. It simply means that when compiled across platforms it will perform as expected. By that measure, C is not portable, since it will behave differently across multiple platforms – or multiple compilers, multiple compiler versions, even multiple op…

Selection and context are _so_ important, don't you think?

The quotes I selected do not change the context, nor do they feign a different context. Unfortunately, since you replied with nothing but a snarky comment, I do not know in what way you think I misrepresented your arguments.

Re: How I implemented my own crypto

#388

Hey, fantastic work! Really happy to see some amateur crypto development out there. I started as an amateur also, now I work in crypto product dev. Definitely impressed if this is your first crypto work. I really like the simplicity, reminds me of OpenSSH. I gotta agree with some of the naysayers though - this needs more time to brew. Even if it's perfect, there still more proof to show. My suggestions: * A long-runn…

> Definitely impressed if this is your first crypto work.

It is. :-)

> A long-running interoperability test between your lib and another.

Already have one for all primitives. Just tweak the parameters to make it run for a long time. The test code is in test/sodium.c and test/donna.c

> Fuzzing. You need to to catch false results, random crashes, stack overflows, etc. Fuzz every input, do it for random lengths (both underflow and overflow).

I'm not sure how to go about it. I already check every input size, from zero to several times the size of the internal block, in my comparison tests.

Of course, all inputs are correct. Incorrect inputs result in undefined behaviour, checking for that is out of scope. (That's why most functions return void.)

> Maybe port it and test on Win, Mac, ARM, MIPS. I find a lot of bugs shake out during ports.

Yeah, I only tried GCC and Clang on my Intel Ubuntu machine. But I did use Valgrind, ASan, MSan, UBSan, and the TIS-Interpreter. I also compiled under various standards: C99, C11, C++98, C++11, C++14, C++17.

> Timing. […] Use a high-precision timing mechanism to catch any unusual spikes

Isn't that a bit overkill? I'd rather manually review the source code for the absence of secret dependant branches or secret dependent array indices. I'm not implementing AES or RSA, the primitive I have chosen were specifically designed to easily avoid timing attacks.

> crypto_aead_unlock has two return paths, it might be better to maintain timing.

No. There are 2 cases to consider: failure, and success. Each path runs in constant time, so timing can only distinguish success from failure… which is revealed anyway by the return code.

> Rather than using int returns, use bool.

I'm afraid bool is a tiny bit less portable than int. I also went with the flow about error codes, which are traditionally int.

> Zero your ctx vars, e.g. a_ctx ctx = {0,};

I'm not sure how to do it portably, with zero dependency.

Re: How I implemented my own crypto

#389

Write a crypto library and blog about how great it is. Apply the tools that other people used to find bugs in it and repeat blogging about how great it is. ... In the end you get a library that is slower and smaller (because you left out the optimized code, duh). It's still not as small as the smallest. Is this easier to audit? Dubious but it doesn't matter because if people hadn't wanted the extra speed they wouldn'…

Let's list the pros and cons of each libraries (let's assume we trust all 3 libraries):

Monocypher vs TweetNaCl:

  Good:
    Monocypher uses more modern primitives (Blake2b, Chacha20)
    Monocypher provides password derivation
    Monocypher is a bit easier to use
    Monocypher is much faster
  Bad:
    Monocypher is twice as big
  Conclusion:
    Monocypher utterly outclasses TweetNaCl.
Monocypher vs Libsodium:

  Good:
    Monocypher is much smaller
    Monocypher is easier to deploy
    Monocypher is a bit easier to use
  Bad:
    Monocypher is a bit slower (except Argon2i, which is faster)
  Conclusion:
    If performance matters, use NaCl.  Otherwise, use Monocypher.
Nothing new, you say?

> By all means, write your own crypto but DON'T USE IT! And of course don't tell other people to use it either.

Get back to me in a couple years, we'll count the CEVs since version 1.0.

Post reply on HN