Commit Graph

799 Commits

Author SHA1 Message Date
Ryan Liptak 3252a05531 Prefer <err> => |e| return e over <err> => return <err>
Avoids the potential for a typo on the `return <err>` side of the prong
2026-04-20 18:03:14 -07:00
Frank Denis 98cc059622 crypto.asn1.Oid: Reject empty OID encodings (#31983)
The DER decoder accepted zero-length OID payloads producing an Oid value that would panic later.

Co-authored-by: Frank Denis <github@pureftpd.org>
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31983
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: Frank Denis <jedisct1@noreply.codeberg.org>
Co-committed-by: Frank Denis <jedisct1@noreply.codeberg.org>
2026-04-20 21:54:04 +02:00
Frank Denis 98ddebc380 std.crypto.Certificate: fix UTCTime year interpretation
UTCTime years in the range 50-99 must map to 1950-1999, but the
parser unconditionally added 2000, producing dates 100 years in the
future.

This caused verify() to accept certificates whose validity actually
expired decades ago.

Change that to match what OpenSSL, BoringSSL, etc. do
2026-04-20 21:45:08 +02:00
Frank Denis 525aff6048 std.crypto.ascon: fix streaming XOF/CXOF
AsconXof128 and AsconCxof128 were applying the padding in update()
calls. That was totally fine for one-shot hashing, but not for
streaming (multiple update() calls before finalization).
2026-04-20 18:29:46 +02:00
Frank Denis 3a07f50dab std.crypto.ml_kem tests: simplify incV
We don't need to reimplement 16-byte subtraction, just use a
u128. As a bonus, that handles the theoretical case of an all-0xff
value properly.
2026-04-20 18:27:22 +02:00
Frank Denis ac7e895df0 tls.Client: reject empty TLS 1.3 inner plaintext and short records
After decryption, TLS 1.3 plaintext is trimmed of zero padding, then
the last byte is read as the content type.

But when the plaintext was entirely zero padding, we got a
"thread panic: integer overflow at msg.len - 1" error. That could be
triggered by any server to crash the client.
2026-04-20 12:07:51 +02:00
Frank Denis bc08199ef1 crypto TLS bundle: make AddCertsFromDirPathError compile
It's calling `addCertsFromDir`, which now requires a timestamp,
so it didn't compile any more.

Add a test by the way.
2026-04-12 00:13:59 +02:00
nekogirl ac6fb0b59a Correct Element and Tag in crypto.codecs.asn1 (#31511)
Previously, `std.crypto.codecs.asn1.der.decode` failed to compile because of `std.Io.Reader` usage in `Element.decode` and `Tag.decode` and `DecodeError` not being compatible with `std.Io.Reader.Error`.

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31511
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: nekogirl <nekogirl@noreply.codeberg.org>
Co-committed-by: nekogirl <nekogirl@noreply.codeberg.org>
2026-04-09 17:59:16 +02:00
nektro e73257dec2 lib/std: BitSet,EnumSet: replace initEmpty/initFull with decl literals (#31469)
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31469
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: nektro <hello@nektro.net>
Co-committed-by: nektro <hello@nektro.net>
2026-04-05 05:12:13 +02:00
Frank Denis dcb33abc2c crypto.base64: use "-" instead of "+" character in URL-safe mode
/ was turned into _, but + also needs to be turned into -
2026-03-29 09:33:22 +02:00
akhildevelops eec244c5a2 std.crypto.tls.Client: expose InitError (#31610)
Fixes: https://codeberg.org/ziglang/zig/issues/31581
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31610
Co-authored-by: akhildevelops <akhildevelops@noreply.codeberg.org>
Co-committed-by: akhildevelops <akhildevelops@noreply.codeberg.org>
2026-03-25 00:56:22 +01:00
David Rubin 8efd539305 crypto: correct aes-siv s2v
The first issue is that when len(Sn) >= 128,
we perform Sn xor D instead of the Sn xorend D
that is specified in RFC 5297.

The second issue is that we truncate the Sn
if it is larger than 4096 bytes, which could
lead to collisions between inputs. We solve
this by absoring the Sn into the CMAC state
perform the last 16 bytes, xoring those 16
bytes with D as described in the first issue,
and then updating and squeezing the CMAC.
2026-03-22 07:21:41 -07:00
Jacob Young 83c7aba127 windows: trigger automatic fetching of root certificates 2026-03-20 19:23:48 +01:00
UraniaZPM 485b996b61 Make benchmarking use std.Io.Clock.awake for timing (#31553)
In #31086, the `std.time.Timer` struct was removed, but this broke the last few programs that used it, those being the benchmarking programs for `std.Random`, `std.hash`, `std.crypto` and `std.unicode`. One more is `zig/perf_test.zig`, but as far as I can tell, that one is broken due to changes in file import rules too, unless I'm launching it wrong.

I also spotted some performance and benchmarking issues with the RNGs, detailed in #31554.

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31553
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: UraniaZPM <uraniazpm@noreply.codeberg.org>
Co-committed-by: UraniaZPM <uraniazpm@noreply.codeberg.org>
2026-03-18 21:00:08 +01:00
Alex Rønne Petersen e0d38561f0 std.crypto.argon2: disable flaky test kdf derived key length
https://codeberg.org/ziglang/zig/issues/31504
2026-03-14 05:55:24 +01:00
Kendall Condon 02e8339ca7 zig build fmt 2026-03-12 17:44:03 -04:00
Alex Rønne Petersen b778826df5 std.crypto.argon2: disable kdf test
https://codeberg.org/ziglang/zig/issues/31402
2026-03-05 18:49:31 +01:00
Frank Denis e8ca9229c8 Expose the elligator map for Curve25519
This is the same as for Edwards25519 without the y coordinate,
since it returns Montgomery coordinates, but it can be confusing
to call the Edwards25519 function while working on the
Curve25519 representation.

New protocols such as CPACE requires the map over Curve25519.
2026-02-23 13:04:58 +01:00
Andrew Kelley 54eb03cbf6 std.Io.Select: remove "outstanding" field
it is not fundamentally part of this abstraction
2026-02-20 16:42:37 -08:00
IntegratedQuantum 0bed4fb384 crypto: Allow arbitrary types for secureZeroes
also removed some related ptrCasts
2026-02-13 17:07:03 +01:00
Andrew Kelley 922ab8b8bc std: finish moving time to Io interface
Importantly, adds ability to get Clock resolution, which may be zero.
This allows error.Unexpected and error.ClockUnsupported to be removed
from timeout and clock reading error sets.
2026-02-02 23:02:31 -08:00
Andrew Kelley e56563ce3f std.Io.File.MultiReader: implementation fixes 2026-01-30 22:03:14 -08:00
Frank Denis 8709f53d44 crypto.ff: allow seamless chaining regardless of representation (#30913)
Finite field elements can be in regular or Montgomery form, and
chaining different operations use to require manual and error-prone
conversions.

Now:

- `add`, `sub` and `mul` convert the second operand to match the
first operand's form
- `sq` and `pow` preserve the input's Montgomery form
- `toPrimitive` and `toBytes` return `UnexpectedRepresentation` if
the element is in Montgomery form, preventing incorrect serialization

This is fully backwards compatible and allows seamless chaining of
operations regardless of their representation.
2026-01-25 17:42:01 +01:00
Alex Rønne Petersen 9dd2716229 std.crypto.kangarootwelve: disable more flaky tests
https://codeberg.org/ziglang/zig/issues/30676
2026-01-13 09:19:08 +01:00
Frank Denis be84d7cb9b Fix benchmarks after the randomness changes 2026-01-13 07:04:51 +01:00
nash1111 9a7f14354e crypto: correct checkHostName docs about wildcard matching 2026-01-10 22:11:52 +01:00
Andrew Kelley 721bdb6256 Merge pull request 'crypto.scrypt: accept an std.Io parameter instead of direct entropy' (#30738) from jedisct1/zig:scryptfixes into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30738
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-01-09 09:39:35 +01:00
Frank Denis cd8f0aa4ca Add signerWithBaseNonce 2026-01-07 23:02:46 +01:00
Frank Denis 22380c78b1 crypto.ed25519.Signer: get an std.io parameter rather than entropy
This is consistent with what all other similar functions now do,
and is less dangerous.
2026-01-07 23:02:24 +01:00
Frank Denis 3ee092536e Allow the salt to be passed as a parameter in bcrypt 2026-01-07 22:44:37 +01:00
Frank Denis 4a29a6e432 Add scrypt.createWithSalt 2026-01-07 23:14:37 +01:00
Frank Denis 2153759a22 crypto.scrypt: accept an std.Io parameter instead of direct entropy
Safer, and consistent with what is being done in other similar
functions.
2026-01-07 23:01:38 +01:00
Andrew Kelley 006afece53 std.crypto.tls.Client.Options: expose entropy_len 2026-01-07 11:03:37 -08:00
Andrew Kelley 1f1381a866 update API usage of std.crypto.random to io.random 2026-01-07 11:03:36 -08:00
Andrew Kelley 816565dd07 std: move entropy to Io 2026-01-07 11:03:36 -08:00
mlugg 1bef876636 Merge pull request 'std.Thread: mask all signals before unmapping stack' (#30713) from detached-thread-exit-signal-race into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30713
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-01-07 01:11:25 +01:00
David Rubin 8b71ec6db7 crypto: correctly disallow non-digits in time
Previously these functions made the assumption that
when performing a  on the input digits,
there could be no collisions between the less
significant digits being larger than '9', and the
upper digits being small enough to get past the
 checks.

Now we perform a correct check across all of the
digits to ensure they're in between '0'-'9', at
a minimal cost, since all digits are checked in
parallel.
2026-01-06 23:37:43 +01:00
Kristoffer 9c55776d25 std.crypto: mem.trimLeft -> mem.trimStart 2026-01-06 23:28:01 +01:00
Matthew Lugg be0a77efd2 std: re-enable some disabled tests
I believe these tests may have been flaky as a result of the bug fixed
in the previous commit. A big hint is that they were all crashing with
SIGSEGV with no stack trace. I suspect that some lingering SIGIOs from
cancelations were being delivered to a thread after its `munmap` call,
which was happening because the test runner called `Io.Threaded.deinit`
to cause all of the (detached) worker threads to exit.

If this passes, I'll re-run the x86_64-linux CI jobs on this commit a
few times before merge to try and be sure there are no lingering
failures.

Resolves: https://codeberg.org/ziglang/zig/issues/30096
Resolves: https://codeberg.org/ziglang/zig/issues/30592
Resolves: https://codeberg.org/ziglang/zig/issues/30682
2026-01-06 11:15:47 +00:00
Alex Rønne Petersen 9d08eba2e1 std.crypto.argon2: skip flaky argon2d test
https://codeberg.org/ziglang/zig/issues/30074
2026-01-06 10:02:09 +01:00
Alex Rønne Petersen b0da914ae6 std.crypto.argon2: disable flaky phc format hasher test
https://codeberg.org/ziglang/zig/issues/30682
2026-01-06 10:01:16 +01:00
Gregory Mullen 29b9e24df7 add tls.ExtensionType.quic_transport_parameters = 57 2026-01-05 11:26:17 +01:00
Andrew Kelley f28802a9c6 zig libc: fix subcommand
This branch regressed the child process "run" mechanism because it
didn't pass the correct stdin, stdout, stderr values to process.spawn

Fixed now.
2026-01-04 00:27:08 -08:00
Andrew Kelley a0a982f0ea std.crypto.kangarootwelve: disable flaky test
tracked by #30676
2026-01-03 16:27:09 -08:00
Frank Denis 1baa127c65 crypto.edwards25519: optimize rejectLowOrder
Reject low-order points by checking projective coordinates directly
instead of using affine coordinates.

Equivalent, but saves CPU cycles (~254 field multiplications total
before, 3 field multiplications after).
2026-01-01 23:56:20 +01:00
Andrew Kelley 2adfd4d107 std.Io: fix and improve Group API
Rename `wait` to `await` to be consistent with Future API. The
convention here is that this set of functionality goes together:
* async/concurrent
* await/cancel

Also rename Select `wait` to `await` for the same reason.

`Group.await` now can return `error.Canceled`. Furthermore,
`Group.await` does not auto-propagate cancelation. Instead, users should
follow the pattern of `defer group.cancel(io);` after initialization,
and doing `try group.await(io);` at the end of the success path.
Advanced logic can choose to do something other than this pattern in the
event of cancelation.

Additionally, fixes a bug in `std.Io.Threaded` future await, in which it
swallowed an `error.Canceled`. Now if a task is canceled while awaiting
a future, after propagating the cancel request, it also recancels,
meaning that the awaiting task will properly detect its own cancelation
at the next cancelation point.

Furthermore, fixes a bug in the compiler where `error.Canceled` was
being swallowed in `dispatchPrelinkWork`.

Finally, fixes std.crypto code that inappropriately used
`catch unreachable` in response to cancelation without even so much as a
comment explaining why it was believed to be unreachable. Now, those
functions have `error.Canceled` in the error set and propagate
cancelation properly.

With this way of doing things, `Group.await` has a nice property: even if
all tasks in the group are CPU bound and without cancelation points, the
`Group.await` can still be canceled. In such case, the task that was
waiting for `await` wakes up with a chance to do some more resource
cleanup tasks, such as canceling more things, before entering the
deferred `Group.cancel` call at which point it has to suspend until the
canceled but uninterruptible CPU bound tasks complete.

closes #30601
2025-12-29 22:47:34 -08:00
Andrew Kelley 33e302d67a update remaining calls to std.Io.Threaded.init 2025-12-23 22:15:12 -08:00
Andrew Kelley 16bd2e137e compiler: fix most compilation errors from std.fs changes 2025-12-23 22:15:09 -08:00
Andrew Kelley 6484101f78 update occurences of std.fs.openDirAbsolute 2025-12-23 22:15:09 -08:00
Andrew Kelley 16f8af1b9a compiler: update various code to new fs API 2025-12-23 22:15:09 -08:00