6678 Commits

Author SHA1 Message Date
Bas Westerbaan
4414f9c46e
Add Kyber post-quantum key encapsulation mechanism (#14902)
Implementation of the IND-CCA2 post-quantum secure key encapsulation
mechanism (KEM) CRYSTALS-Kyber, as submitted to the third round of the NIST
Post-Quantum Cryptography (v3.02/"draft00"), and selected for standardisation.

Co-authored-by: Frank Denis <124872+jedisct1@users.noreply.github.com>
2023-03-15 03:50:45 +00:00
Frank Denis
e17998b396
Argon2: properly handle outputs > 64 bytes in blake2Long() (#14914)
Fixes #14912
2023-03-14 21:40:02 +00:00
Kotaro Inoue
9ecdcb8e30
Fix to use '/' for a empty path (#14884)
Signed-off-by: Kotaro Inoue <k.musaino@gmail.com>
2023-03-14 13:07:25 +02:00
Frank Denis
5a12d00708
Move std.crypto.config options to std.options (#14906)
Options have been moved to a single namespace.
2023-03-14 06:40:23 +00:00
Frank Denis
9622991578
Add configurable side channels mitigations; enable them on soft AES (#13739)
* Add configurable side channels mitigations; enable them on soft AES

Our software AES implementation doesn't have any mitigations against
side channels.

Go's generic implementation is not protected at all either, and even
OpenSSL only has minimal mitigations.

Full mitigations against cache-based attacks (bitslicing, fixslicing)
come at a huge performance cost, making AES-based primitives pretty
much useless for many applications. They also don't offer any
protection against other classes of side channel attacks.

In practice, partially protected, or even unprotected implementations
are not as bad as it sounds. Exploiting these side channels requires
an attacker that is able to submit many plaintexts/ciphertexts and
perform accurate measurements. Noisy measurements can still be
exploited, but require a significant amount of attempts. Wether this
is exploitable or not depends on the platform, application and the
attacker's proximity.

So, some libraries made the choice of minimal mitigations and some
use better mitigations in spite of the performance hit. It's a
tradeoff (security vs performance), and there's no one-size-fits all
implementation.

What applies to AES applies to other cryptographic primitives.

For example, RSA signatures are very sensible to fault attacks,
regardless of them using the CRT or not. A mitigation is to verify
every produced signature. That also comes with a performance cost.
Wether to do it or not depends on wether fault attacks are part of
the threat model or not.

Thanks to Zig's comptime, we can try to address these different
requirements.

This PR adds a `side_channels_protection` global, that can later
be complemented with `fault_attacks_protection` and possibly other
knobs.

It can have 4 different values:

- `none`: which doesn't enable additional mitigations.
"Additional", because it only disables mitigations that don't have
a big performance cost. For example, checking authentication tags
will still be done in constant time.

- `basic`: which enables mitigations protecting against attacks in
a common scenario, where an attacker doesn't have physical access to
the device, cannot run arbitrary code on the same thread, and cannot
conduct brute-force attacks without being throttled.

- `medium`: which enables additional mitigations, offering practical
protection in a shared environement.

- `full`: which enables all the mitigations we have.

The tradeoff is that the more mitigations we enable, the bigger the
performance hit will be. But this let applications choose what's
best for their use case.

`medium` is the default.

Currently, this only affects software AES, but that setting can
later be used by other primitives.

For AES, our implementation is a traditional table-based, with 4
32-bit tables and a sbox.

Lookups in that table have been replaced by function calls. These
functions can add a configurable noise level, making cache-based
attacks more difficult to conduct.

In the `none` mitigation level, the behavior is exactly the same
as before. Performance also remains the same.

In other levels, we compress the T tables into a single one, and
read data from multiple cache lines (all of them in `full` mode),
for all bytes in parallel. More precise measurements and way more
attempts become necessary in order to find correlations.

In addition, we use distinct copies of the sbox for key expansion
and encryption, so that they don't share the same L1 cache entries.

The best known attacks target the first two AES round, or the last
one.

While future attacks may improve on this, AES achieves full
diffusion after 4 rounds. So, we can relax the mitigations after
that. This is what this implementation does, enabling mitigations
again for the last two rounds.

In `full` mode, all the rounds are protected.

The protection assumes that lookups within a cache line are secret.
The cachebleed attack showed that it can be circumvented, but
that requires an attacker to be able to abuse hyperthreading and
run code on the same core as the encryption, which is rarely a
practical scenario.

Still, the current AES API allows us to transparently switch to
using fixslicing/bitslicing later when the `full` mitigation level
is enabled.

* Software AES: use little-endian representation.

Virtually all platforms are little-endian these days, so optimizing
for big-endian CPUs doesn't make sense any more.
2023-03-13 22:18:26 +01:00
Frank Denis
1d96a17af4
crypto.aescrypto.encrypt: do not add the round key in an asm block (#14899)
Apple M1/M2 have an EOR3 instruction that can XOR 2 operands with
another one, and LLVM knows how to take advantage of it.

However, two EOR can't be automatically combined into an EOR3 if
one of them is in an assembly block.

That simple change speeds up ciphers doing an AES round immediately
followed by a XOR operation on Apple Silicon.

Before:

   aegis-128l mac:      12534 MiB/s
    aegis-256 mac:       6722 MiB/s
       aegis-128l:      10634 MiB/s
        aegis-256:       6133 MiB/s
       aes128-gcm:       3890 MiB/s
       aes256-gcm:       3122 MiB/s
       aes128-ocb:       2832 MiB/s
       aes256-ocb:       2057 MiB/s

After:

   aegis-128l mac:      15667 MiB/s
    aegis-256 mac:       8240 MiB/s
       aegis-128l:      12656 MiB/s
        aegis-256:       7214 MiB/s
       aes128-gcm:       3976 MiB/s
       aes256-gcm:       3202 MiB/s
       aes128-ocb:       2835 MiB/s
       aes256-ocb:       2118 MiB/s
2023-03-13 07:06:27 +00:00
Isaac Freund
a097779b61 std: Add ArrayList.insertAssumeCapacity()
Also test and document that inserting at list.items.len is allowed.
2023-03-12 11:02:53 +00:00
Andrew Kelley
3169f0529b eliminate posix_spawn from the standard library
Today I found out that posix_spawn is trash. It's actually implemented
on top of fork/exec inside of libc (or libSystem in the case of macOS).

So, anything posix_spawn can do, we can do better. In particular, what
we can do better is handle spawning of child processes that are
potentially foreign binaries. If you try to spawn a wasm binary, for
example, posix spawn does the following:

 * Goes ahead and creates a child process.
 * The child process writes "foo.wasm: foo.wasm: cannot execute binary file"
   to stderr (yes, it prints the filename twice).
 * The child process then exits with code 126.

This behavior is indistinguishable from the binary being successfully
spawned, and then printing to stderr, and exiting with a failure -
something that is an extremely common occurrence.

Meanwhile, using the lower level fork/exec will simply return ENOEXEC
code from the execve syscall (which is mapped to zig error.InvalidExe).

The posix_spawn behavior means the zig build runner can't tell the
difference between a failure to run a foreign binary, and a binary that
did run, but failed in some other fashion. This is unacceptable, because
attempting to excecve is the proper way to support things like Rosetta.
2023-03-10 15:41:07 -05:00
Nameless
524e0cd987
std.http: rework connection pool into its own type 2023-03-09 14:55:31 -06:00
Nameless
634e715504
std.http: split Client's parts into their own files 2023-03-09 14:55:20 -06:00
Nameless
0a4130f364
std.http: handle relative redirects 2023-03-09 14:55:13 -06:00
Nameless
fd2f906d1e
std.http: handle compressed payloads 2023-03-09 14:54:26 -06:00
Nameless
8d86194b6e
add error sets to tcpConnect* and tls.Client.init 2023-03-09 14:54:26 -06:00
Nameless
afb26f4e6b
std.http: add connection pooling and make keep-alive requests by default 2023-03-09 14:54:23 -06:00
Frank Denis
95f6a5935a
TurboSHAKE: change default delimiter to 0x1F (#14857)
The TurboSHAKE paper just got published:
https://eprint.iacr.org/2023/342.pdf

and unlike the previous K12 paper, suggests 0x1F instead of 0x01
as the default value for "D".
2023-03-09 19:20:57 +00:00
Frank Denis
134e5748e0
Fix incorrect SHA-3 computation with the streaming API (#14852)
* Fix SHA3 with streaming

Leftover bytes should be added to the buffer, not to the state.

(or, always to the state; we can and probably should eventually get
rid of the buffer)

Fixes #14851

* Add a test for SHA-3 with streaming
2023-03-09 05:18:15 +00:00
Jan Philipp Hafer
06b263825a std.os: add missing mmap errors
Man page for posix lists EMFILE, man page for linux ENFILE.
Also posix says "The mmap() function adds an extra reference to the file
associated with the file descriptor fildes which is not removed by a
subsequent close() on that file descriptor. This reference is removed
when there are no more mappings to the file."

It sounds counter-intuitive, that a process limit but no system limit can
be exceeeded.

As far as I understand, fildes is only used for file descriptor backed mmaps.
2023-03-08 13:00:06 -05:00
Gaëtan
bbba701a41 std.os.windows.advapi32: Add RegCloseKey 2023-03-07 18:14:37 -05:00
Frank Denis
8da6b393fb std.fmt: add bytesToHex() to encode bytes as hex digits
We already had `hexToBytes()`, but not the reverse operation
(at least not without using formatters).
2023-03-07 17:45:31 -05:00
Frank Denis
36d47dd199
std.crypto.hash.sha3: add TurboSHAKE (#14824) 2023-03-07 10:04:45 +01:00
Eric Milliken
ccf00ccdf7
crypto.25519.field: de-inline mul for small builds (#14775) 2023-03-06 17:28:11 -05:00
jim price
6ab04b5941 std.os: Allow write functions to return INVAL errors
In Linux when interacting with the virtual file system when writing
in invalid value to a file the OS will return errno 22 (INVAL).

Instead of triggering an unreachable, this change now returns a
newly introduced error.InvalidArgument.
2023-03-06 15:59:18 -05:00
Jacob Young
2770159606 std: reenable vectorized code with the C backend 2023-03-06 08:09:32 -05:00
Jacob Young
c29c4c6f70 tools: add lldb pretty printer for std.MultiArrayList.Slice 2023-03-06 05:58:46 -05:00
Andrew Kelley
8558983c86 std.os.abort: take advantage of @trap 2023-03-05 17:29:28 -07:00
Andrew Kelley
7b01af2bfd
Merge pull request #14745 from jacobly0/bigint
CBE: add support for integers larger than 128 bits (and apparently vectors too)
2023-03-05 14:48:06 -05:00
jim price
a63134a4a5 std.os: Add DeviceBusy as a possible write error
In Linux when writing to various files in the virtual file system,
for example /sys/fs/cgroup, if you write an invalid value to a file
you'll get errno 16.

This change allows for these specific cases to be caught instead of
being lumped together in UnexpectedError.
2023-03-05 17:56:44 +02:00
Jacob Young
874ae81f1b CBE: implement big integer literals 2023-03-05 02:59:01 -05:00
Andrew Kelley
653814f76b std.Build.addModule: return the created module 2023-03-04 05:39:14 -05:00
r00ster91
e0d3904638 Ast: properly handle sentinel-terminated slices in tuple
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2023-03-04 01:08:03 +02:00
Ali Chraghi
75ff34db9e std.Build.Cache: remove 'test-filetimestamp.tmp' once timestamp returned 2023-03-03 13:50:44 -05:00
Frank Denis
fdee558e45 crypto.25519.field: de-inline _sq()
May fix #14764
2023-03-03 07:51:23 -05:00
Andrew Kelley
aaaaab9ec2 std.process.Child: remove pid and handle, add id
Previously, this API had pid, to be used on POSIX systems, and handle,
to be used on Windows.

This commit unifies the API, defining an Id type that is either the pid
or the HANDLE depending on the target OS.

This commit also prepares for the future by allowing one to import via
`std.process.Child` which is the fully qualified namespace that I intend
to migrate to in the future.
2023-03-03 07:49:05 -05:00
Andrew Kelley
bb5006d728 std: add fchmodat
Also add `std.fs.has_executable_bit` for doing conditional compilation.

This adds the linux syscalls for chmod and fchmodat, as well as the
extern libc function declarations.

Only `fchmodat` is added to `std.os`, and it is not yet added to std.fs.
2023-03-03 02:37:45 -05:00
Andrew Kelley
426c13dddf add doc comments to std.fs.File.default_mode 2023-03-03 01:00:44 -05:00
Frank Denis
4789cc0249
crypto.KeccakF: compute rotations at comptime, add a test with f=800 (#14760) 2023-03-02 19:14:41 +00:00
Frank Denis
28364166e8
crypto.hash.sha3: make permutation generic and public, add SHAKE (#14756)
Make the Keccak permutation public, as it's useful for more than
SHA-3 (kMAC, SHAKE, TurboSHAKE, TupleHash, etc).

Our Keccak implementation was accepting f as a comptime parameter,
but always used 64-bit words and 200 byte states, so it actually
didn't work with anything besides f=1600.

That has been fixed. The ability to use reduced-round versions
was also added in order to support M14 and K12.

The state was constantly converted back and forth between bytes
and words, even though only a part of the state is actually used
for absorbing and squeezing bytes. It was changed to something
similar to the other permutations we have, so we can avoid extra
copies, and eventually add vectorized implementations.

In addition, the SHAKE extendable output function (XOF) was
added (SHAKE128, SHAKE256). It is required by newer schemes,
such as the Kyber post-quantum key exchange mechanism, whose
implementation is currently blocked by SHAKE missing from our
standard library.

Breaking change: `Keccak_256` and `Keccak_512` were renamed to
`Keccak256` and `Keccak512` for consistency with all other
hash functions.
2023-03-02 06:13:40 +00:00
Andrew Kelley
874d3a17ae
Merge pull request #14744 from ziglang/std.io.poll
introduce std.io.poll
2023-03-01 18:08:43 -05:00
Jonathan Marler
f2b15420ad std.io.poll: remove done function 2023-03-01 12:21:53 -05:00
Jonathan Marler
138e8b162a std.child_process: use std.io.poll for collectOutput 2023-03-01 12:21:53 -05:00
Jonathan Marler
4f58a80735 std.io.zig: fmt 2023-03-01 12:21:53 -05:00
Binary Craft
a7a709aaa9 Fixes #13893 - some standard library networking tests are failing on Windows 2023-03-01 02:54:43 -05:00
Jonathan Marler
ef72cd6698 std.io.poll initial windows implementation 2023-02-28 15:00:51 -05:00
Andrew Kelley
d8c3738e21 redo std.io.poll with only outputs 2023-02-27 22:39:47 -07:00
Andrew Kelley
814de45bd2 add std.io.poll and implement it for POSIX
I think having inputs is problematic here, it should only be for
outputs.
2023-02-27 22:06:18 -07:00
Andrew Kelley
5236842a9d std.heap.GeneralPurposeAllocator: add doc comment for deinit 2023-02-27 22:04:29 -07:00
Andrew Kelley
f33af7af40 delete a subtly incorrect Haiku collectOutput implementation
It's not OK to half-ass this function. Please implement it correctly, or
not at all.
2023-02-27 20:42:13 -07:00
Jacob Young
a3529c2dea tools: implement more lldb pretty printers 2023-02-27 05:37:03 -05:00
Andrew Kelley
f6c9346773 std.Build.CompileStep.installConfigHeader: add missing step dependency 2023-02-25 23:22:17 -05:00
Andrew Kelley
26196be344 rename std.Build.InstallRawStep to ObjCopyStep
And make it not do any installation, only objcopying. We already have
install steps for doing installation.

This commit also makes ObjCopyStep properly integrate with caching.
2023-02-24 23:48:03 -05:00