News for hackers. Stuff that matters.
Re:Twinkle Notes: Cross-platform encrypted notes app by CiPHPerCoder on Wednesday February 05, 2020 @12:57PM (#22249334)
> end-to-end encrypted

https://github.com/twinkle-labs/twinkle-notes/blob/45206f9d6...

For AES in CBC mode, IVs have two requirements:

1. They must never repeat.

2. They must be unpredictable.

Generating them from a SHA256 hash of some low-entropy data is not a good practice.

https://paragonie.com/blog/2016/05/how-generate-secure-rando...

Furthermore, not authenticating your ciphertext means padding oracle attacks can be launched against the app.

https://robertheaton.com/2013/07/29/padding-oracle-attack/

Re:Twinkle Notes: Cross-platform encrypted notes app by twknotes on Wednesday February 05, 2020 @03:45PM (#22250804)
Thank you for digging into the code. This is why we are going open source. The encryption you are referring to is for encrypting a list of keys for your local notes storage, which is not exactly part of the end-to-end encrypted syncing. Since you have got this far, could you please have a look at:

https://github.com/twinkle-labs/twinkle-notes/blob/8ad7d9d0b...

> They must be unpredictable

I am wondering if that is necessary, because the hacker can't perform those attacks without the user's actively using the app at the same time. From what I learned, the attacking process requires the presence of key somewhere. If the attacker can get on user's device while one is using it, then it's almost a hopeless situation. Please educate me if I am wrong.

Re:Twinkle Notes: Cross-platform encrypted notes app by CiPHPerCoder on Wednesday February 05, 2020 @04:08PM (#22251000)
> > They must be unpredictable

> I am wondering if that is necessary,

Yes, it is necessary. The IND security of Cipher Block Chaining (CBC) depends entirely on the IV being from a cryptographically secure random generator.

CBC mode requires unique and random IVs. CTR mode requires unique IVs (but can be predictable).

That's why we call the CTR input a nonce (number to be used once) and the CBC input an IV (initialization vector). Since they have different security requirements, we refer to them differently. Unfortunately, some cryptography libraries just name the parameter IV.

Re:Twinkle Notes: Cross-platform encrypted notes app by metalliqaz on Wednesday February 05, 2020 @06:06PM (#22251841)
Is that distinction between nonce and IV held everywhere in the crypto community? For example, rfc8439 which defines the ChaCha-Poly AEAD does not require an unpredictable input nonce, and indeed calls it a "nonce", but many of the common implementations I've seen use "initialization vector" instead.
Re:Twinkle Notes: Cross-platform encrypted notes app by CiPHPerCoder on Wednesday February 05, 2020 @11:02PM (#22253331)
Loosely. The majority have given up on the public understanding of nuances and just phone it in with "just don't write crypto".

Because AES-CTR and ChaCha both refer to it as a nonce, and CBC calls it an initialization vector, the IV/nonce distinction does matter. But if you misuse the terms folks will know what you meant to say. Just don't mix it up when it comes time to implement.

Re:Twinkle Notes: Cross-platform encrypted notes app by LookOutItsABot on Thursday February 06, 2020 @01:20AM (#22253842)
Please don't ever stop this behavior. I'm just learning much of this in an applicable way and people like you make this not just easier but possible for those of us learning with little outside help.

The father I go down this rabbit hole the more I learn security should be like a religion of security (only one without dogma).

Thanks.

Re:Twinkle Notes: Cross-platform encrypted notes app by trulyrandom on Wednesday February 05, 2020 @04:34PM (#22251211)
3. They should be authenticated.

To OP, I would suggest using secretstream from libsodium, which abstracts all these problems away for you.

Re:Twinkle Notes: Cross-platform encrypted notes app by CiPHPerCoder on Wednesday February 05, 2020 @04:40PM (#22251252)
> 3. They should be authenticated.

I covered that:

> Furthermore, not authenticating your ciphertext means padding oracle attacks can be launched against the app.

The list above was just the requirements for the initialization vector, not the list of problems with the code.

Re:Twinkle Notes: Cross-platform encrypted notes app by trulyrandom on Thursday February 06, 2020 @02:03PM (#22259119)
Right, but I was specifically talking about authenticating the IV.
Re:Twinkle Notes: Cross-platform encrypted notes app by jwtorres on Wednesday February 05, 2020 @03:40PM (#22250765)
Can you cite some source for why the SHA256 of changing data isn't sufficiently random to be used as the IV?
Re:Twinkle Notes: Cross-platform encrypted notes app by CiPHPerCoder on Wednesday February 05, 2020 @04:10PM (#22251027)
https://web.cs.ucdavis.edu/~rogaway/papers/sym-enc.pdf

See the section about "CBC with Counters".

The consequence of the security proof by Rogaway, et al. for CBC mode is that IVs must be unique AND unpredictable for CBC mode to be secure*.

SHA256 is a deterministic pseudorandom function if you know all of the inputs. By studying the source code, we can see what gets fed into the SHA256 inputs. The cost to brute force all possible inputs is definitely much lower than 2^128, therefore it weakens the IND security of the AES-CBC scheme.

CBC security only provides indistinguishability. It isn't secure against adaptive chosen-ciphertext attacks.

Re:Twinkle Notes: Cross-platform encrypted notes app by jwtorres on Wednesday February 05, 2020 @05:24PM (#22251576)
>"See the section about "CBC with Counters"."

That section is talking about using an incrementing counter as the IV--it doesn't say anything about a PRNG being a bad choice (in this case, the PRNG being a SHA256).

>"By studying the source code, we can see what gets fed into the SHA256 inputs. The cost to brute force all possible inputs is definitely much lower than 2^128, therefore it weakens the IND security of the AES-CBC scheme."

Are you talking about brute forcing the IV? The IV is not a secret to anyone--it's usually appended to the cipher text.

Re:Twinkle Notes: Cross-platform encrypted notes app by CiPHPerCoder on Wednesday February 05, 2020 @05:54PM (#22251774)
> That section is talking about using an incrementing counter as the IV--it doesn't say anything about a PRNG being a bad choice (in this case, the PRNG being a SHA256).

Let's try this again.

You have two types of inputs that APIs refer to as "IVs".

Nonces: Must never repeat.

Initialization vectors: Must never repeat AND must be unpredictable.

Counters are acceptable for nonces. When an academic cryptographer says in a security proof that a counter doesn't suffice for the security of a scheme, what they're really saying is that you're in the latter category (must be unpredictable too) rather than the former (where predictable is okay as long as it never repeats).

If it can be predictable, it can be a counter. If it can't be a counter, it must be unpredictable.

The correct way to get an unpredictable IV is to use a CSPRNG.

SHA256(some predictable low-entropy inputs) fails to meet the bar for unpredictability.

> Are you talking about brute forcing the IV? The IV is not a secret to anyone--it's usually appended to the cipher text.

IVs aren't secret, but given the security proof I linked, IVs cannot be predictable. They must be unpredictable.

Re:Twinkle Notes: Cross-platform encrypted notes app by kebman on Wednesday February 05, 2020 @02:30PM (#22250207)
What do you think about NaCl for a project like this?
Re:Twinkle Notes: Cross-platform encrypted notes app by CiPHPerCoder on Wednesday February 05, 2020 @02:49PM (#22250370)
Re:Twinkle Notes: Cross-platform encrypted notes app by lawl on Wednesday February 05, 2020 @05:00PM (#22251398)
> Libsodium (a fork of NaCl) is preferable to NaCl.

Can you elaborate on this? Quickly looking over your libsodium based sodium-plus it doesn't seem like it has been audited. (I know libsodium itself had an audit)

Where as for example tweetnacl-js [0] has been audited.

That's not meant to take a dump on your project, but I just don't understand why it is better, or preferable.

[0]: https://github.com/dchest/tweetnacl-js#audits

Re:Twinkle Notes: Cross-platform encrypted notes app by CiPHPerCoder on Wednesday February 05, 2020 @05:59PM (#22251803)
NaCl is djb abandonware. Libsodium is actively maintained.

Libsodium offers Argon2 password hashing and key stretching, XChaCha20-Poly1305 AEAD constructions, BLAKE2b generic hashing, SipHash-2-4 for collision-resistant hash tables, etc.

Most people who say "use NaCl" really mean "use NaCl/libsodium".

Regarding my project: sodium-plus will wrap sodium-native (a thin Node wrapper to libsodium proper, which has been audited) if it is installed. To date, libsodium.js has not. Therefore, if you care about public code audits, you can use audited libsodium with sodium-plus just by installing sodium-native alongside it.