Commit Graph

422 Commits

Author SHA1 Message Date
Nameless 0a4130f364 std.http: handle relative redirects 2023-03-09 14:55:13 -06:00
Nameless 8d86194b6e add error sets to tcpConnect* and tls.Client.init 2023-03-09 14:54:26 -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
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
Jacob Young 2770159606 std: reenable vectorized code with the C backend 2023-03-06 08:09:32 -05:00
Frank Denis fdee558e45 crypto.25519.field: de-inline _sq()
May fix #14764
2023-03-03 07:51:23 -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
Frank Denis c7f479c3cb crypto/benchmark.zig: fix pointer capture of non pointer type (#14722) 2023-02-24 19:45:24 +00:00
Andrew Kelley 4dd958d585 improve error message for byref capture of byval array 2023-02-18 19:20:19 -07:00
Andrew Kelley aeaef8c0ff update std lib and compiler sources to new for loop syntax 2023-02-18 19:17:21 -07:00
Andrew Kelley f0530385b5 update existing behavior tests and std lib to new for loop semantics 2023-02-18 19:17:21 -07:00
Chris Boesch 438b71155a crypto: add AES-CMAC (RFC 4493) (#14545)
* crypto: add AES-CMAC

Co-authored-by: Frank Denis <124872+jedisct1@users.noreply.github.com>
2023-02-16 21:18:20 +01:00
Frank Denis f62e3b8c0d std.crypto: add the Ascon permutation
Ascon has been selected as new standard for lightweight cryptography
in the NIST Lightweight Cryptography competition.

Ascon won over Gimli and Xoodoo.

The permutation is unlikely to change. However, NIST may tweak
the constructions (XOF, hash, authenticated encryption) before
standardizing them. For that reason, implementations of those
are better maintained outside the standard library for now.

In fact, we already had an Ascon implementation in Zig:
`std.crypto.aead.isap` is based on it. While the implementation was
here, there was no public API to access it directly.

So:

- The Ascon permutation is now available as `std.crypto.core.Ascon`,
with everything needed to use it in AEADs and other Ascon-based
constructions
- The ISAP implementation now uses std.crypto.core.Ascon instead of
keeping a private copy
- The default CSPRNG replaces Xoodoo with Ascon. And instead of an
ad-hoc construction, it's using the XOFa mode of the NIST submission.
2023-02-13 02:22:24 -05:00
Mateusz Poliwczak bbbc4ebf03 support P256 in x509 2023-01-22 17:24:45 -05:00
山下 6c98c8d891 Wildcard certs should only validate one level of sub domain 2023-01-19 19:13:42 +00:00
fn ⌃ ⌥ 2623e1c4d6 Add std.os.darwin.cssm 2023-01-17 09:51:39 -08:00
fn ⌃ ⌥ faf0fe4305 std.crypto.Certificate.Bundle: use parseCert in rescanWindows 2023-01-17 08:59:38 -08:00
fn ⌃ ⌥ 40c400ec9e std.crypto.Certificate.Bundle: clear bundle before macOS rescan 2023-01-17 08:58:06 -08:00
Andrew Kelley d35d086ae6 std.crypto.Certificate: add more object id 2023-01-17 00:09:38 -07:00
Andrew Kelley 7623f3fad0 std.crypto.Certificate: skip unknown attributes 2023-01-17 00:09:34 -07:00
Andrew Kelley 62e3fdcf4f std.crypto.Certificate: add more object ids 2023-01-17 00:08:42 -07:00
Andrew Kelley 09560bc69a clean up windows cert scanning
* keep helper functions out of the DLL bindings APIs
 * unify the logic for linux and windows certificate scanning with
   regards to error handling
2023-01-17 00:08:42 -07:00
star-tek-mb 1f9fa82235 windows root certificate scanning 2023-01-17 00:08:42 -07:00
fn ⌃ ⌥ e45b471ad3 Find system-installed root SSL certificates on macOS (#14325) 2023-01-16 22:34:04 +00:00
Andrew Kelley e3505c0a5a std.crypto.Certificate.Bundle: add more Linux directories
Thanks to the Go project for finding all these paths.
2023-01-15 15:01:42 -07:00
Andrew Kelley 9a0e1704ae std.crypto.Certificate: support v1
closes #14304
2023-01-15 14:59:49 -07:00
Andrew Kelley e579c88f4c std.crypto.siphash: add finalResult() and peek()
Useful for avoiding mutable state when using this API.
2023-01-07 18:06:55 -07:00
Andrew Kelley d711f459ae std.crypto.tls.Client: fix crash in read()
An abstraction for giving bytes to the read buffer didn't check for an
out-of-space condition.
2023-01-05 19:57:00 -07:00
Veikka Tuominen f83834993e std: collect all options under one namespace 2023-01-05 02:31:29 -07:00
Andrew Kelley 7178451d62 std.crypto.tls.Client: make close_notify optional
Although RFC 8446 states:

> Each party MUST send a "close_notify" alert before closing its write
> side of the connection

In practice many servers do not do this. Also in practice, truncation
attacks are thwarted at the application layer by comparing the amount of
bytes received with the amount expected via the HTTP headers.
2023-01-02 18:27:38 -07:00
Andrew Kelley 9ca6d67345 std.crypto.tls.Certificate: make the current time a parameter 2023-01-02 16:57:16 -07:00
Andrew Kelley 97acdeeca8 std.crypto.tls: verify via Subject Alt Name
Previously, the code only checked Common Name, leading to unable to
validate valid certificates which relied on the subject_alt_name
extension for host name verification.

This commit also adds rsa_pss_rsae_* back to the signature algorithms
list in the ClientHello.
2023-01-02 16:57:16 -07:00
Andrew Kelley 611a1fdd6d std.crypto.tls: add API for sending close_notify
This commit adds `writeEnd` and `writeAllEnd` in order to send data and
also notify the server that there will be no more data written.

Unfortunately, it seems most TLS implementations in the wild get this
wrong and immediately close the socket when they see a close_notify,
rather than only ending the data stream on the application layer.
2023-01-02 16:57:16 -07:00
Andrew Kelley 66b07fd672 std.crypto.Certificate: bump RSA needed memory 2023-01-02 16:57:16 -07:00
Andrew Kelley 0fb78b15aa std.crypto.tls: use a Decoder abstraction
This commit introduces tls.Decoder and then uses it in tls.Client. The
purpose is to make it difficult to introduce vulnerabilities in the
parsing code. With this abstraction in place, bugs in the TLS
implementation will trip checks in the decoder, regardless of the actual
length of packets sent by the other party, so that we can have
confidence when using ReleaseFast builds.
2023-01-02 16:57:16 -07:00
Andrew Kelley 341e68ff8f std.crypto.tls.Client: remove debug prints 2023-01-02 16:57:16 -07:00
Andrew Kelley 79b41dbdbf std.crypto.tls: avoid heap allocation
The code we are borrowing from https://github.com/shiguredo/tls13-zig
requires an Allocator for doing RSA certificate verification. As a
stopgap measure, this commit uses a FixedBufferAllocator to avoid heap
allocation for these functions.

Thank you to @naoki9911 for providing this great resource which has been
extremely helpful for me when working on this standard library TLS
implementation. Until Zig has std.crypto.rsa officially, we will borrow
this implementation of RSA. 🙏
2023-01-02 16:57:16 -07:00
Andrew Kelley 05fee3b22b std.crypto.tls.Client: fix eof logic
Before this, it incorrectly returned true when there was still cleartext
to be read.
2023-01-02 16:57:16 -07:00
Andrew Kelley 22e2aaa283 crypto.tls: support rsa_pss_rsae_sha256 and fixes
* fix eof logic
 * fix read logic
 * fix VecPut logic
 * add some debug prints to remove later
2023-01-02 16:57:16 -07:00
Andrew Kelley e4a9b19a14 std.crypto.tls.Client: rework the read function
Here's what I landed on for the TLS client. It's 16896 bytes
(max_ciphertext_record_len is 16640). I believe this is the theoretical
minimum size, give or take a few bytes.

These constraints are satisfied:
 * a call to the readvAdvanced() function makes at most one call to the
   underlying readv function
 * iovecs are provided by the API, and used by the implementation for
   underlying readv() calls to the socket
 * the theoretical minimum number of memcpy() calls are issued in all
   circumstances
 * decryption is only performed once for any given TLS record
 * large read buffers are fully exploited

This is accomplished by using the partial read buffer to storing both
cleartext and ciphertext.
2023-01-02 16:57:16 -07:00
Andrew Kelley 7391df2be5 std.crypto: make proper use of undefined 2023-01-02 16:57:16 -07:00
Andrew Kelley 1d20ada366 std.crypto.tls.Client: refactor to reduce namespace bloat 2023-01-02 16:57:16 -07:00
Andrew Kelley 16af6286c8 std.crypto.tls.Client: support SignatureScheme.ecdsa_secp384r1_sha384 2023-01-02 16:57:16 -07:00
Andrew Kelley 940d368e7e std.crypto.tls.Client: fix the read function
The read function has been renamed to readAdvanced since it has slightly
different semantics than typical read functions, specifically regarding
the end-of-file. A higher level read function is implemented on top.

Now, API users may pass small buffers to the read function and
everything will work fine. This is done by re-decrypting the same
ciphertext record with each call to read() until the record is finished
being transmitted.

If the buffer supplied to read() is large enough, then any given
ciphertext record will only be decrypted once, since it decrypts
directly to the read() buffer and therefore does not need any memcpy. On
the other hand, if the buffer supplied to read() is small, then the
ciphertext is decrypted into a stack buffer, a subset is copied to the
read() buffer, and then the entire ciphertext record is saved for the
next call to read().
2023-01-02 16:57:16 -07:00
Andrew Kelley 21ab99174e std.crypto.tls.Client: use enums more 2023-01-02 16:57:16 -07:00
Andrew Kelley 477864dca5 std.crypto.tls.Client: fix truncation attack vulnerability 2023-01-02 16:57:16 -07:00
Andrew Kelley ceb211e65f std.crypto.tls.Client: handle key_update message 2023-01-02 16:57:15 -07:00