22897 Commits

Author SHA1 Message Date
Jacob Young
ba9d93dc9f x86_64: implement more binary immediate combinations 2023-03-15 01:04:21 -04:00
Jacob Young
05b12e6779 x86_64: handle duplicate prong deaths 2023-03-15 01:04:21 -04:00
Jacob Young
2386159840 x86_64: use short union init 2023-03-15 01:04:21 -04:00
Jacob Young
d14a9e82fe x86_64: use new for loop syntax 2023-03-15 01:04:21 -04:00
Jacob Young
bb6b9c19e0 x86_64: fix lowering of non-pointer optional is null 2023-03-15 01:04:21 -04:00
Jacob Young
c51930b060 behavior: enable passing behavior tests on stage2_x86_64 2023-03-15 01:04:21 -04:00
Jacob Young
5ab426a302 x86_64: fix store of undefined 2023-03-15 00:01:44 -04:00
Jacob Young
9a4e9215fc x86_64: fix error code paths to not have extra pops 2023-03-15 00:01:44 -04:00
Jacob Young
8be6073480 tools: fix typo in lldb command 2023-03-15 00:01:44 -04:00
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
DerryAlex
d6e48abde8
Implement readFromMemory/writeToMemory for ptrLikeOptional 2023-03-14 13:08:56 +02: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
mlugg
1e6d7f7763 Sema: allow comptime mutation of multiple array elements
Previously, if you had a pointer to multiple array elements and tried to
write to it at comptime, it was incorrectly treated as a pointer to one
specific array value, leading to an assertion down the line. If we try
to mutate a value at an elem_ptr larger than the element type, we need
to perform a modification to multiple array elements.

This solution isn't ideal, since it will result in storePtrVal
serializing the whole array, modifying the relevant parts, and storing
it back. Ideally, it would only take the required elements. However,
this change would have been more complex, and this is a fairly rare
operation (nobody ever ran into the bug before after all), so it doesn't
matter all that much.
2023-03-14 13:06:23 +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
Jakub Konka
d525ecb523
Merge pull request #14886 from ziglang/x86_64-encoder
x86_64: add table-driven instruction encoder
2023-03-13 20:48:41 +01:00
Hashi364
4942e4e870
Resolve docs inconsistency with Overflow builtins
In 41 (Undefined Behavior) . 5 (Integer Overflow) . 3 (Builtin Overflow Functions), it is stated that

> These builtins return a bool of whether or not overflow occurred, as well as returning the overflowed bits:
> * @addWithOverflow
> * @subWithOverflow
> * @mulWithOverflow
> * @shlWithOverflow

but in their definition says that it returns a `tuple`/`struct`.

Example;
`@addWithOverflow(a: anytype, b: anytype) struct { @TypeOf(a, b), u1 }`

Co-authored-by: zooster <r00ster91@proton.me>
2023-03-13 14:47:20 +00:00
Ian Johnson
adc6dec26b Sema: avoid panic on callconv(.C) generic return type
Fixes #14854
2023-03-13 13:17:13 +02: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
Techatrix
10c74631b3 langref: add missing comma in assembly expressions 2023-03-12 23:51:46 +02:00
Jakub Konka
e9fc0aba4c x86_64: add missing source files to CMakeLists.txt 2023-03-12 22:08:29 +01:00
Jakub Konka
ac434fd8cc x86_64: avoid inline for-loops when scanning the encodings table 2023-03-12 22:06:22 +01:00
mlugg
a8bd55e085 translate-c: translate extern unknown-length arrays using @extern
Resolves: #14743
2023-03-12 19:07:06 +02:00
mlugg
c93e0d8618 Sema: @extern fixes
* There was an edge case where the arena could be destroyed twice on
  error: once from the arena itself and once from the decl destruction.

* The type of the created decl was incorrect (it should have been the
  pointer child type), but it's not required anyway, so it's now just
  initialized to anyopaque (which more accurately reflects what's
  actually at that memory, since e.g. [*]T may correspond to nothing).

* A runtime bitcast of the pointer was performed, meaning @extern didn't
  work at comptime. This is unnecessary: the decl_ref can just be
  initialized with the correct pointer type.
2023-03-12 18:55:23 +02:00
mlugg
948926c513 Sema: improve error message when calling non-member function as method
Resolves: #14880
2023-03-12 18:47:02 +02: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
Jakub Konka
955e394792 x86_64: fix 32bit build issues in the encoder 2023-03-12 08:47:23 +01:00
Jakub Konka
707a74655b x86_64: downstream encoder/assembler tests 2023-03-12 08:41:44 +01:00
Jakub Konka
433558a92f x86_64: clean up 2023-03-11 20:05:50 +01:00
Jakub Konka
f279ccb807 x86_64: rename asmNone to asmOpOnly 2023-03-11 20:05:50 +01:00
Jakub Konka
fb38e3d6b2 x86_64: simplify immediate handling at MIR level 2023-03-11 20:05:50 +01:00
Jakub Konka
0a8b5c20aa x86_64: add wrapper for .jcc with relocation 2023-03-11 20:05:50 +01:00
Jakub Konka
c9a153c797 x86_64: add .dead pseudo-instruction to mark an unused MIR instruction 2023-03-11 20:05:50 +01:00
Jakub Konka
621fc36b55 x86_64: add wrapper for .jmp_reloc 2023-03-11 20:05:50 +01:00
Jakub Konka
21630ea17f x86_64: apply couple of tweaks and pass behavior tests 2023-03-11 20:05:50 +01:00
Jakub Konka
6e1da36503 x86_64: PtrSize.fromSize() should take into account nonexact sizes too 2023-03-11 20:05:50 +01:00
Jakub Konka
e34e7d5ad1 x86_64: add missing decodings for .movsx 2023-03-11 20:05:50 +01:00
Jakub Konka
fe1fab4a8e x86_64: fix CALL emits for ELF and Plan9 2023-03-11 20:05:49 +01:00
Jakub Konka
d0e7212539 x86_64: finish rolling out all MIR assembly helpers 2023-03-11 20:05:49 +01:00
Jakub Konka
022b308d6a x86_64: start converting MI references 2023-03-11 20:05:49 +01:00
Jakub Konka
4af8313f36 x86_64: plug up all RM/MR references 2023-03-11 20:05:49 +01:00
Jakub Konka
32708dd6e2 x86_64: add RM and MR helpers to codegen 2023-03-11 20:05:49 +01:00
Jakub Konka
9658ab6766 x86_64: handle all instructions without introducing Memory operand 2023-03-11 20:05:49 +01:00
Jakub Konka
1bde522c2c x86_64: add helper for Jcc instruction 2023-03-11 20:05:49 +01:00
Jakub Konka
7221cd8ec9 x86_64: add helpers for CMOVcc and SETcc at the MIR level 2023-03-11 20:05:49 +01:00
Jakub Konka
f61a70e812 x86_64: handle encoding and decoding Imm64 unsigned 2023-03-11 20:05:49 +01:00
Jakub Konka
aa8fda799e x86_64: split up assemble() into more declarative single-purpose helpers 2023-03-11 20:05:49 +01:00
Jakub Konka
6e882d730b x86_64: introduce assemble() helper which encodes/decodes into MIR -> Instruction 2023-03-11 20:05:49 +01:00
Jakub Konka
5b37701028 x86_64: refactor immediate selection logic 2023-03-11 20:05:49 +01:00