23 Commits

Author SHA1 Message Date
Frank Denis
d75d6e7f77 Remove unused var, sort std.crypto.* 2020-09-30 01:39:55 +02:00
Frank Denis
6eaba61ef5 std/crypto: implement the HKDF construction 2020-09-30 01:39:55 +02:00
Frank Denis
bb1c6bc376 Add AEGIS-256 as well 2020-09-29 17:10:04 +02:00
Frank Denis
9f274e1f7d std/crypto: add the AEGIS128L AEAD
Showcase that Zig can be a great option for high performance cryptography.

The AEGIS family of authenticated encryption algorithms was selected for
high-performance applications in the final portfolio of the CAESAR
competition.

They reuse the AES core function, but are substantially faster than the
CCM, GCM and OCB modes while offering a high level of security.

AEGIS algorithms are especially fast on CPUs with built-in AES support, and
the 128L variant fully takes advantage of the pipeline in modern Intel CPUs.

Performance of the Zig implementation is on par with libsodium.
2020-09-29 17:10:04 +02:00
Frank Denis
bd89bd6fdb Revamp crypto/aes
* Reorganize crypto/aes in order to separate parameters, implementations and
modes.
* Add a zero-cost abstraction over the internal representation of a block,
so that blocks can be kept in vector registers in optimized implementations.
* Add architecture-independent aesenc/aesdec/aesenclast/aesdeclast operations,
so that any AES-based primitive can be implemented, including these that don't
use the original key schedule (AES-PRF, AEGIS, MeowHash...)
* Add support for parallelization/wide blocks to take advantage of hardware
implementations.
* Align T-tables to cache lines in the software implementations to slightly
reduce side channels.
* Add an optimized implementation for modern Intel CPUs with AES-NI.
* Add new tests (AES256 key expansion).
* Reimplement the counter mode to work with any block cipher, any endianness
and to take advantage of wide blocks.
* Add benchmarks for AES.
2020-09-24 13:16:00 -04:00
Frank Denis
c8cd6145ac Move PBKDF2 to a pwhash category, clarify what that category is
Password hashing functions are not general-purpose KDFs, and KDFs
don't have to satisfy the same properties as a PHF.

This will allow fast KDFs such as the HKDF construction to be in a
category of their own, while clarifying what functions are suitable
for using passwords as inputs.
2020-09-24 00:02:31 -04:00
Rocknest
f6195be997 fix ref 2020-09-13 23:31:59 +03:00
Rocknest
d75cbb01db Reference all crypto declarations 2020-09-13 23:00:33 +03:00
Rob Napier
8a1a40276f Extract kdf.zig to provide namespace documentation 2020-09-13 11:08:06 -04:00
Rob Napier
257c5b5348 Explicitly reference std.crypto.kdf in test case 2020-09-13 10:50:46 -04:00
Rob Napier
c2b02d01d5 Add crypto.kdf.pbkdf2 2020-09-11 17:10:27 -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
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
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
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
5fabb44aeb Export crypto.aead 2020-08-17 13:55:40 -07:00
Frank Denis
dd8f7b396c Rename the field and scalar modules
Suggested by @kubkon
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
J.W
5275b01202 hashing algorithms: fix logic and index out of bounds 2020-02-24 13:43:54 -05:00
Jay Petacat
4b86c1e3bb crypto: Add BLAKE3 hashing algorithm
This is a translation of the [official reference implementation][1] with
few other changes. The bad news is that the reference implementation is
designed for simplicity and not speed, so there's a lot of room for
performance improvement. The good news is that, according to the crypto
benchmark, the implementation is still fast relative to the other
hashing algorithms:

```
         md5: 430 MiB/s
        sha1: 386 MiB/s
      sha256: 191 MiB/s
      sha512: 275 MiB/s
    sha3-256: 233 MiB/s
    sha3-512: 137 MiB/s
     blake2s: 464 MiB/s
     blake2b: 526 MiB/s
      blake3: 576 MiB/s
    poly1305: 1479 MiB/s
    hmac-md5: 653 MiB/s
   hmac-sha1: 553 MiB/s
 hmac-sha256: 222 MiB/s
      x25519: 8685 exchanges/s
```

[1]: https://github.com/BLAKE3-team/BLAKE3
2020-02-01 23:03:23 -05:00
lukechampine
f403aa6cee crypto: Add AES implementation 2019-11-01 22:42:25 -04:00
Andrew Kelley
ed36dbbd9c
mv std/ lib/
that's all this commit does. further commits will fix cli flags and
such.

see #2221
2019-09-25 23:35:41 -04:00