Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std/crypto: add (X)Salsa20 and NaCl boxes #6809

Merged
merged 2 commits into from
Oct 26, 2020
Merged

Conversation

jedisct1
Copy link
Contributor

The NaCl constructions are available in pretty much all programming languages, making them a solid choice for applications that require interoperability.

Go includes them in the standard library, JavaScript has the popular tweetnacl.js module, and reimplementations and ports of TweetNaCl have been made everywhere.

Zig has almost everything that NaCl has at this point, the main missing component being the Salsa20 cipher, on top on which NaCl's secretboxes, boxes, and sealedboxes can be implemented.

So, here they are!

And clean the X25519 API up a little bit by the way.

The NaCl constructions are available in pretty much all programming
languages, making them a solid choice for applications that require
interoperability.

Go includes them in the standard library, JavaScript has the popular
tweetnacl.js module, and reimplementations and ports of TweetNaCl
have been made everywhere.

Zig has almost everything that NaCl has at this point, the main
missing component being the Salsa20 cipher, on top on which NaCl's
secretboxes, boxes, and sealedboxes can be implemented.

So, here they are!

And clean the X25519 API up a little bit by the way.
@zigazeljko
Copy link
Contributor

Why does X25519 use a KeyPair struct with separate fields for private and public keys, but Ed25519 uses a single [keypair_length]u8 as a keypair?

@jedisct1
Copy link
Contributor Author

Ed25519 will move to using a KeyPair as well.

These are quite different from X25519 key pairs, as the actual secret key is not stored. What is stored as a "secret" is a concatenation of the seed and the public key. Changing that interface requires too many changes unrelated to the topic of this PR, so that will be done separately.

@jayschwa jayschwa added the standard library This issue involves writing Zig code for the standard library. label Oct 25, 2020
@jedisct1
Copy link
Contributor Author

Not a lot of changes needed to move ed25519 key pairs to a struct, after all. So, added here as well.

@zigazeljko
Copy link
Contributor

Why is the secret_key still 64 bytes? Since the public key is already stored as a separate struct field, the secret_key only needs to store the seed.

@jedisct1
Copy link
Contributor Author

EdDSA requires the public key in addition to the secret key for signing.

If the public key has to be recomputed every time a message has to be signed, it has a noticeable performance impact.

In addition to that, EdDSA fails catastrophically if the wrong public key is used to compute a signature.

For these reasons, the most common way to serialize EdDSA secret keys is as an aggregate of the seed (the secret scalar wouldn't be enough) and the public key.

@andrewrk andrewrk merged commit 0088efc into ziglang:master Oct 26, 2020
Copy link
Contributor

@daurnimator daurnimator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I didn't finish my review before this was merged >>

lib/std/crypto/salsa20.zig Show resolved Hide resolved
lib/std/crypto.zig Show resolved Hide resolved
lib/std/crypto/salsa20.zig Show resolved Hide resolved
lib/std/crypto/salsa20.zig Show resolved Hide resolved
lib/std/crypto/salsa20.zig Show resolved Hide resolved
lib/std/crypto/salsa20.zig Show resolved Hide resolved
@jedisct1 jedisct1 deleted the salsa branch October 26, 2020 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants