ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.83k stars 2.55k forks source link

General crypto api improvements #6371

Open Rocknest opened 4 years ago

Rocknest commented 4 years ago
daurnimator commented 4 years ago
  • consider moving Md5, Sha1 to new unsafe/legacy namespace, document that all algorithms in it are not cryptographically secure. It may be a place for DES #4157. Same for some block cipher modes #5763.

I don't think this is a good idea. Also see https://github.com/ziglang/zig/pull/6095#discussion_r473170318

  • make changes to kdf discussed in #6336.
    1. password kdfs should be in their own namespace

Agreed

  1. implement another kdf and come up with a unified api. suggestion Pbkdf2(HmacSha1).init(iterations, salt?).deriveKey(outSlice)

Consider implementing bcrypt or scrypt. Also consider looking at the openssl api. I've worked on that a bit e.g. https://github.com/openssl/openssl/issues/9139

jedisct1 commented 4 years ago

consider moving Md5, Sha1 to new unsafe/legacy namespace

This is something that has been discussed before, but the consensus was to improve the documentation for these functions rather than move them to a legacy namespace.

MD5 and SHA1 are required by many applications and by Zig itself. But it more difficult to justify DES and unauthenticated modes in a recent project, even less so in the standard library of a new programming language.

Performance of our implementations is currently very poor due to the absence of optimized implementations. Maybe this is something to focus on first (and it has implications on the API, too , such as the way the AES core functions is exposed) before adding more constructions, especially legacy ones.

Pbkdf2(HmacSha1).init(iterations, salt?).deriveKey(outSlice)

Shouldn't the salt be mandatory?

andrewrk commented 4 years ago

I just want to put one piece of guidance here which I think I am qualified to make:

jedisct1 commented 4 years ago

Consider implementing bcrypt or scrypt.

Argon2 may be better a candidate. The reason being that unlike bcrypt/scrypt, it can share code with the existing BLAKE2 implementation, so that the same optimizations can be shared. But doing so will require some refactoring. It is a better case to study than another primitive that is very similar to PBKDF2.

daurnimator commented 4 years ago

It is a better case to study than another primitive that is very similar to PBKDF2.

I thought it would be best to study something quite far removed from PBKDF2 so that assumptions are more general.

jedisct1 commented 4 years ago

It is a better case to study than another primitive that is very similar to PBKDF2.

I thought it would be best to study something quite far removed from PBKDF2 so that assumptions are more general.

Yes, this is exactly why I would suggest Argon2 instead of bcrypt, which is very close to PBKDF2.

x13a commented 3 years ago

What do you think of scrypt key derivation function in std? I have ported one from go. Suppose that it is good to have common used crypto primitives build in. For example scrypt used by crypto-currencies and python has it in std. I see that bcrypt already there.

jedisct1 commented 3 years ago

@x13a sure, that would be a nice addition, and the implementation looks great.

Maybe it could reuse the existing Salsa20 core function instead of embedding its own copy, so that it can benefit from the vectorized implementation. This requires a change to the current API in order to support reduced rounds, but that's not a bad thing to have. Scrypt is also used for password storage, so adding the standard parameters encodings would also be useful. Your repo is a better place to discuss this :)

jedisct1 commented 3 years ago

Also, for memory-hard functions, we probably want the scratch buffer to be a function parameter. The size may not be known at comptime, and more importantly, we probably don't want gigabytes of memory right on the stack :)

dralley commented 3 years ago

This is just my personal opinion, but crypto seems like the sort of thing you wouldn't want in the standard library, precisely because of the huge number of different algorithms which may at any time in the future quickly fall out of favor (and new ones constantly being created). There's a tension between the goal of the standard library - "provide basic, useful functionality that we will never break" and the goal of a security library, which often needs to be a bit more opinionated (even if that means marking functionality deprecated, moving them to unsafe namespaces, etc.)