85 Commits

Author SHA1 Message Date
Frank Denis
ad18078d53 forceEval() -> doNotOptimizeAway() 2020-08-26 10:50:34 +02:00
Frank Denis
0bd53dd203 Rename blackBox, move it to std.mem.forceEval() 2020-08-26 10:50:34 +02:00
Frank Denis
ff2e82f382 Rename at to tag in AEADs 2020-08-26 10:50:34 +02:00
Frank Denis
b8729ca1a0 Improve crypto benchmarks
- 1MiB objects on the stack doesn't play well with wasmtime.
Reduce these to 512KiB so that the webassembly benchmarks can run.
- Pass expected results to a blackBox() function. Without this, in
release-fast mode, the compiler could detected unused return values,
and would produce results that didn't make sense for siphash.
- Add AEAD constructions to the benchmarks.
- Inline chacha20Core() makes it 4 times faster.
- benchmarkSignatures() -> benchmarkSignature() for consistency.
2020-08-26 10:50:34 +02:00
Frank Denis
2d402157d9 Improve documentation on siphash recommended parameters 2020-08-22 15:12:54 -04:00
Frank Denis
e919744c7a Promote hash/siphash to crypto/siphash
SipHash *is* a cryptographic function, with a 128-bit security level.

However, it is not a regular hash function: a secret key is required,
and knowledge of that key allows collisions to be quickly computed offline.

SipHash is therefore more suitable to be used as a MAC.

The same API as other MACs was implemented in addition to functions directly
returning an integer.

The benchmarks have been updated accordingly.

No changes to the SipHash implementation itself.
2020-08-22 02:47:50 -04:00
Frank Denis
0fe6677736 No need to keep a 128-bit carry around 2020-08-21 20:33:16 +02:00
Frank Denis
2fc2ebd920 Force myself to use unusual integer sizes a little bit more :) 2020-08-21 19:43:06 +02:00
Frank Denis
c9218f1719 Make poly1305 faster 2020-08-21 16:30:52 +02:00
Frank Denis
fc55cd458a Hash functions now accept an option set
- This avoids having multiple `init()` functions for every combination
of optional parameters
- The API is consistent across all hash functions
- New options can be added later without breaking existing applications.
  For example, this is going to come in handy if we implement parallelization
  for BLAKE2 and BLAKE3.
- We don't have a mix of snake_case and camelCase functions any more, at
least in the public crypto API

Support for BLAKE2 salt and personalization (more commonly called context)
parameters have been implemented by the way to illustrate this.
2020-08-21 00:51:14 +02:00
Frank Denis
adf3d00e87 Remove explicit comptime 2020-08-20 23:02:10 +02:00
Frank Denis
446597bd3c Remove the reset() function from hash functions
Justification:
- reset() is unnecessary; states that have to be reused can be copied
- reset() is error-prone. Copying a previous state prevents forgetting
  struct members.
- reset() forces implementation to store sensitive data (key, initial state)
  in memory even when they are not needed.
- reset() is confusing as it has a different meaning elsewhere in Zig.
2020-08-20 23:02:10 +02:00
Frank Denis
f92a5d7944 Repair crypto/benchmark; add BLAKE2b256
Some MACs have a 64-bit output
2020-08-20 23:02:10 +02:00
Frank Denis
3bed749b1c Add truncated SHA512 variants 2020-08-20 23:02:10 +02:00
Frank Denis
6f9ea9eaef Breaking: sort std/crypto functions into categories
Instead of having all primitives and constructions share the same namespace,
they are now organized by category and function family.

Types within the same category are expected to share the exact same API.
2020-08-20 23:02:05 +02:00
Andrew Kelley
4a69b11e74 add license header to all std lib files
add SPDX license identifier
copyright ownership is zig contributors
2020-08-20 16:07:04 -04:00
Frank Denis
8d60047697 ristretto255: add uniform string->element map & fast equivalence check 2020-08-18 00:31:02 -04:00
Frank Denis
cd591a9b25 No need for an explicit deref 2020-08-17 13:54:01 -07:00
Frank Denis
795033c35f std/crypto: XChaCha20, detached modes and standard AEAD API
* Factor redundant code in std/crypto/chacha20

* Add support for XChaCha20, and the XChaCha20-Poly1305 construction.

XChaCha20 is a 24-byte version of ChaCha20, is widely implemented
and is on the standards track:
https://tools.ietf.org/html/draft-irtf-cfrg-xchacha-03

* Add support for encryption/decryption with the authentication tag
detached from the ciphertext

* Add wrappers with an API similar to the Gimli AEAD type, so that
we can use and benchmark AEADs with a common API.
2020-08-17 13:54:01 -07:00
Frank Denis
7f9a227abf deinline edwards25519.{add,dbl} 2020-08-16 22:35:28 -07:00
Frank Denis
37ae246405 Inline Fe.{sub,mul,sq} for a performance boost in release-safe mode 2020-08-16 22:35:28 -07:00
Frank Denis
ab6ffa8a3c Work around sqrtRatioM1() issue in release-safe mode 2020-08-16 22:35:28 -07:00
Frank Denis
08dfbee961 Benchmark signatures 2020-08-16 22:35:27 -07:00
Frank Denis
5ab69633b7 Constify the ladder 2020-08-16 22:35:27 -07:00
Frank Denis
d86cde5752 Add comment, use @truncate 2020-08-16 22:35:27 -07:00
Frank Denis
bcef123d90 Address more review issues 2020-08-16 22:35:27 -07:00
Frank Denis
263c444738 Move loop decrements into continuations
Suggested by @daurnimator
2020-08-16 22:35:27 -07:00
Frank Denis
ed558bfbaa Address @daurnimator feedback 2020-08-16 22:35:27 -07:00
Frank Denis
dd8f7b396c Rename the field and scalar modules
Suggested by @kubkon
2020-08-16 22:35:27 -07:00
Frank Denis
c483bf4f97 Update lib/std/crypto/25519/ristretto255.zig
Co-authored-by: Jakub Konka <kubkon@jakubkonka.com>
2020-08-16 22:35:27 -07:00
Frank Denis
739b68938c Update lib/std/crypto/25519/field25519.zig
Co-authored-by: Jakub Konka <kubkon@jakubkonka.com>
2020-08-16 22:35:27 -07:00
Frank Denis
6af9bc8c68 Initialize structures directly
Suggested by @kubkon, thanks!
2020-08-16 22:35:27 -07:00
Frank Denis
5f9953f41f Remove mem.timingSafeEqual() for now
This requires assembly implementations, and is not needed for
signature verification.

Thanks @daurnimator
2020-08-16 22:35:27 -07:00
Frank Denis
3f0d80f25e Improve curve25519-based crypto
This is a rewrite of the x25519 code, that generalizes support for
common primitives based on the same finite field.

- Low-level operations can now be performed over the curve25519 and
edwards25519 curves, as well as the ristretto255 group.
- Ed25519 signatures have been implemented.
- X25519 is now about twice as fast.
- mem.timingSafeEqual() has been added for constant-time comparison.

Domains have been clearly separated, making it easier to later add
platform-specific implementations.
2020-08-16 22:35:27 -07:00
Frank Denis
f46e375bbe std/crypto: gimli.Aead.decrypt()'s ad should be const 2020-08-17 07:16:06 +02:00
Mantas Jonytis
1df33936d6 remove unnecessary operations 2020-08-01 17:15:53 +03:00
Mantas Jonytis
8286cc77f8 implement keyed blake2 2020-08-01 16:56:11 +03:00
Mantas Jonytis
36ed4623a8 make blake2 public 2020-08-01 16:05:20 +03:00
Mantas Jonytis
42dd737c9e compute blake2 at compile time 2020-08-01 16:00:22 +03:00
Mantas Jonytis
1ae40146e6 blake2b: off-by-one on update 2020-08-01 15:31:46 +03:00
Mantas Jonytis
eee9abe1b4 add more blake2b test cases 2020-08-01 15:31:36 +03:00
Mantas Jonytis
b1cf0196df blake2s: off-by-one on update 2020-08-01 15:15:45 +03:00
Mantas Jonytis
fad87bef9a add more blake2s tests 2020-08-01 15:15:04 +03:00
Vexu
e85fe13e44
run zig fmt on std lib and self hosted 2020-07-11 20:41:19 +03:00
data-man
77bb2dc094 Use writer in benchmarks 2020-06-25 19:07:25 -04:00
Vexu
e2a0f5741d
Merge pull request #5189 from zigazeljko/patch-1
Split AES struct into AESEncrypt and AESDecrypt
2020-05-16 13:20:14 +03:00
Vexu
85fd484f07
std: fix blake3 assignment to constant 2020-05-04 14:45:36 +03:00
Žiga Željko
130435a17a Split AES struct into AESEncrypt and AESDecrypt 2020-04-28 04:52:42 +08:00
Timon Kruiper
c829f2f7b7 Add mips support to standard library 2020-04-24 15:28:55 -04:00
Jay Petacat
0dbf8aaab8
crypto: fix benchmark compile error (#4919) 2020-04-02 23:46:46 -04:00