Commit graph

734 commits

Author SHA1 Message Date
Alex Rønne Petersen
66b43234bb
std: disable sha3-512 single test on RISC-V with V support
https://github.com/ziglang/zig/issues/25083
2025-08-30 13:27:25 +02:00
Andrew Kelley
07da9567e6 compiler: fix macos build 2025-08-30 00:48:50 -07:00
Andrew Kelley
79f267f6b9 std.Io: delete GenericReader
and delete deprecated alias std.io
2025-08-29 17:14:26 -07:00
Andrew Kelley
888f00e856 std.crypto.ml_kem: update to not use GenericWriter 2025-08-28 18:30:57 -07:00
Andrew Kelley
9860dd475a std: delete most remaining uses of GenericWriter 2025-08-28 18:30:57 -07:00
Andrew Kelley
57dbc9e74a std.Io: delete GenericWriter 2025-08-28 18:30:57 -07:00
Frank Denis
12a58087a4
Fix TLS 1.2 client key exchange to use negotiated named group (#25007)
The TLS 1.2 implementation was incorrectly hardcoded to always send the
secp256r1 public key in the client key exchange message, regardless of
which elliptic curve the server actually negotiated.

This caused TLS handshake failures with servers that preferred other curves
like X25519.

This fix:

- Tracks the negotiated named group from the server key exchange message
- Dynamically selects the correct public key (X25519, secp256r1, or
  secp384r1) based on what the server negotiated
- Properly constructs the client key exchange message with the
  appropriate key size for each curve type

Fixes TLS 1.2 connections to servers like ziglang.freetls.fastly.net
that prefer X25519 over secp256r1.
2025-08-27 11:18:40 +02:00
Erik Schlyter
37f4bee92a
Fix #24999: copy left-overs before we XOR into c. (#25001)
It is important we copy the left-overs in the message *before* we XOR
it into the ciphertext, because if we're encrypting in-place (i.e., m ==
c), we will manipulate the message that will be used for tag generation.
This will generate faulty tags when message length doesn't conform with
16 byte blocks.
2025-08-25 15:59:42 +00:00
Andrew Kelley
a0f9a5e78d std: more reliable HTTP and TLS networking
* std.Io.Reader: fix confused semantics of rebase. Before it was
  ambiguous whether it was supposed to be based on end or seek. Now it
  is clearly based on seek, with an added assertion for clarity.

* std.crypto.tls.Client: fix panic due to not enough buffer size
  available. Also, avoid unnecessary rebasing.

* std.http.Reader: introduce max_head_len to limit HTTP header length.
  This prevents crash in underlying reader which may require a minimum
  buffer length.

* std.http.Client: choose better buffer sizes for streams and TLS
  client. Crucially, the buffer shared by HTTP reader and TLS client
  needs to be big enough for all http headers *and* the max TLS record
  size. Bump HTTP header size default from 4K to 8K.

fixes #24872

I have noticed however that there are still fetch problems
2025-08-16 00:16:15 -07:00
Frank Denis
c1eff72c4a
crypto/aes_ocb.zig: actually check against test vectors (#24835)
And use the correct bit endianness for padding
2025-08-15 13:09:06 +00:00
Frank Denis
96e4825fbb
Validate wildcard TLS certificates correctly (#24829)
Validate wildcard certificates as specified in RFC 6125.

In particular, `*.example.com` should match `foo.example.com` but
NOT `bar.foo.example.com` as it previously did.
2025-08-14 13:57:00 +00:00
Erik Schlyter
7abd62800c std.crypto.aegis: Absorb ad instead of encrypting it.
`Aegis256XGeneric` behaves differently than `Aegis128XGeneric` in that
it currently encrypts associated data instead of just absorbing it. Even
though the end result is the same, there's no point in encrypting and
copying the ad into a buffer that gets overwritten anyway. This fix
makes `Aegis256XGeneric` behave the same as `Aegis128XGeneric`.
2025-08-13 09:00:57 +02:00
Andrew Kelley
749f10af49 std.ArrayList: make unmanaged the default 2025-08-11 15:52:49 -07:00
Andrew Kelley
1ba6838bc3
Merge pull request #24740 from ziglang/http-plus-fixes
fetch, tls, and http fixes
2025-08-08 12:33:53 -07:00
Andrew Kelley
94dd28b7f7 std.Io: delete CountingWriter 2025-08-07 22:26:42 -07:00
Andrew Kelley
8721efece4 std.crypto.tls.Client: always write to buffer
simplifies the logic & makes it respect limit
2025-08-07 19:55:40 -07:00
Andrew Kelley
46b34949c3 TLS, HTTP, and package fetching fixes
* TLS: add missing assert for output buffer length requirement
* TLS: add missing flushes
* TLS: add flush implementation
* TLS: finish drain implementation
* HTTP: correct buffer sizes for TLS
* HTTP: expose a getReadError method on Connection
* HTTP: add missing flush on sendBodyComplete
* Fetch: remove unwanted deinit
* Fetch: improve error reporting
2025-08-07 10:04:52 -07:00
Andrew Kelley
3837862e52 fix 32-bit builds 2025-08-07 10:04:52 -07:00
Andrew Kelley
e2d81bf6c0 http fixes 2025-08-07 10:04:29 -07:00
Andrew Kelley
28190cc404 std.crypto.tls: rework for new std.Io API 2025-08-07 10:04:29 -07:00
Matthew Lugg
fd9cfc39f5
Merge pull request #24199 from Justus2308/24106-fmt-casts
zig fmt: canonicalize nested cast builtin order
2025-08-07 10:55:03 +01:00
David Rubin
def25b9189 crypto: fix typo in ecdsa comment 2025-08-05 07:51:39 +01:00
Justus Klausecker
7c35070b90 zig fmt: apply new cast builtin order 2025-08-03 14:59:56 +02:00
Andrew Kelley
1dcea220a4 std.tar: update to new I/O API 2025-07-22 09:41:44 -07:00
Andrew Kelley
76d04c1662 zig fmt 2025-07-16 10:27:39 -07:00
Andrew Kelley
96a4e9b866 std.crypto: fix Sha1 namespace 2025-07-14 00:14:21 -07:00
Andrew Kelley
e7b18a7ce6 std.crypto: remove inline from most functions
To quote the language reference,

It is generally better to let the compiler decide when to inline a
function, except for these scenarios:

* To change how many stack frames are in the call stack, for debugging
  purposes.
* To force comptime-ness of the arguments to propagate to the return
  value of the function, as in the above example.
* Real world performance measurements demand it. Don't guess!

Note that inline actually restricts what the compiler is allowed to do.
This can harm binary size, compilation speed, and even runtime
performance.

`zig run lib/std/crypto/benchmark.zig -OReleaseFast`
[-before-] vs {+after+}

              md5:        [-990-]        {+998+} MiB/s
             sha1:       [-1144-]       {+1140+} MiB/s
           sha256:       [-2267-]       {+2275+} MiB/s
           sha512:        [-762-]        {+767+} MiB/s
         sha3-256:        [-680-]        {+683+} MiB/s
         sha3-512:        [-362-]        {+363+} MiB/s
        shake-128:        [-835-]        {+839+} MiB/s
        shake-256:        [-680-]        {+681+} MiB/s
   turboshake-128:       [-1567-]       {+1570+} MiB/s
   turboshake-256:       [-1276-]       {+1282+} MiB/s
          blake2s:        [-778-]        {+789+} MiB/s
          blake2b:       [-1071-]       {+1086+} MiB/s
           blake3:       [-1148-]       {+1137+} MiB/s
            ghash:      [-10044-]      {+10033+} MiB/s
          polyval:       [-9726-]      {+10033+} MiB/s
         poly1305:       [-2486-]       {+2703+} MiB/s
         hmac-md5:        [-991-]        {+998+} MiB/s
        hmac-sha1:       [-1134-]       {+1137+} MiB/s
      hmac-sha256:       [-2265-]       {+2288+} MiB/s
      hmac-sha512:        [-765-]        {+764+} MiB/s
      siphash-2-4:       [-4410-]       {+4438+} MiB/s
      siphash-1-3:       [-7144-]       {+7225+} MiB/s
   siphash128-2-4:       [-4397-]       {+4449+} MiB/s
   siphash128-1-3:       [-7281-]       {+7374+} MiB/s
  aegis-128x4 mac:      [-73385-]      {+74523+} MiB/s
  aegis-256x4 mac:      [-30160-]      {+30539+} MiB/s
  aegis-128x2 mac:      [-66662-]      {+67267+} MiB/s
  aegis-256x2 mac:      [-16812-]      {+16806+} MiB/s
   aegis-128l mac:      [-33876-]      {+34055+} MiB/s
    aegis-256 mac:       [-8993-]       {+9087+} MiB/s
         aes-cmac:       2036 MiB/s
           x25519:      [-20670-]      {+16844+} exchanges/s
          ed25519:      [-29763-]      {+29576+} signatures/s
       ecdsa-p256:       [-4762-]       {+4900+} signatures/s
       ecdsa-p384:       [-1465-]       {+1500+} signatures/s
  ecdsa-secp256k1:       [-5643-]       {+5769+} signatures/s
          ed25519:      [-21926-]      {+21721+} verifications/s
          ed25519:      [-51200-]      {+50880+} verifications/s (batch)
 chacha20Poly1305:       [-1189-]       {+1109+} MiB/s
xchacha20Poly1305:       [-1196-]       {+1107+} MiB/s
 xchacha8Poly1305:       [-1466-]       {+1555+} MiB/s
 xsalsa20Poly1305:        [-660-]        {+620+} MiB/s
      aegis-128x4:      [-76389-]      {+78181+} MiB/s
      aegis-128x2:      [-53946-]      {+53495+} MiB/s
       aegis-128l:      [-27219-]      {+25621+} MiB/s
      aegis-256x4:      [-49351-]      {+49542+} MiB/s
      aegis-256x2:      [-32390-]      {+32366+} MiB/s
        aegis-256:       [-8881-]       {+8944+} MiB/s
       aes128-gcm:       [-6095-]       {+6205+} MiB/s
       aes256-gcm:       [-5306-]       {+5427+} MiB/s
       aes128-ocb:       [-8529-]      {+13974+} MiB/s
       aes256-ocb:       [-7241-]       {+9442+} MiB/s
        isapa128a:        [-204-]        {+214+} MiB/s
    aes128-single:  [-133857882-]  {+134170944+} ops/s
    aes256-single:   [-96306962-]   {+96408639+} ops/s
         aes128-8: [-1083210101-] {+1073727253+} ops/s
         aes256-8:  [-762042466-]  {+767091778+} ops/s
           bcrypt:      0.009 s/ops
           scrypt:      [-0.018-]      {+0.017+} s/ops
           argon2:      [-0.037-]      {+0.060+} s/ops
      kyber512d00:     [-206057-]     {+205779+} encaps/s
      kyber768d00:     [-156074-]     {+150711+} encaps/s
     kyber1024d00:     [-116626-]     {+115469+} encaps/s
      kyber512d00:     [-181149-]     {+182046+} decaps/s
      kyber768d00:     [-136965-]     {+135676+} decaps/s
     kyber1024d00:     [-101307-]     {+100643+} decaps/s
      kyber512d00:     [-123624-]     {+123375+} keygen/s
      kyber768d00:      [-69465-]      {+70828+} keygen/s
     kyber1024d00:      [-43117-]      {+43208+} keygen/s
2025-07-13 18:26:13 +02:00
Marcos Gutiérrez Alonso
f97baca6f6
Add documentation to std.crypto.aes_gcm.AesGcm.encrypt (#24427) 2025-07-13 07:33:08 +00:00
Linus Groh
eb37552536 Remove numerous things deprecated during the 0.14 release cycle
Basically everything that has a direct replacement or no uses left.

Notable omissions:

- std.ArrayHashMap: Too much fallout, needs a separate cleanup.
- std.debug.runtime_safety: Too much fallout.
- std.heap.GeneralPurposeAllocator: Lots of references to it remain, not
  a simple find and replace as "debug allocator" is not equivalent to
  "general purpose allocator".
- std.io.Reader: Is being reworked at the moment.
- std.unicode.utf8Decode(): No replacement, needs a new API first.
- Manifest backwards compat options: Removal would break test data used
  by TestFetchBuilder.
- panic handler needs to be a namespace: Many tests still rely on it
  being a function, needs a separate cleanup.
2025-07-11 08:17:43 +02:00
Andrew Kelley
93ac76594a std: fmt.format to io.Writer.print
allows reverting format -> deprecatedFormat, plus I think this is a
nicer place for the function.
2025-07-09 15:31:02 -07:00
Andrew Kelley
0e37ff0d59 std.fmt: breaking API changes
added adapter to AnyWriter and GenericWriter to help bridge the gap
between old and new API

make std.testing.expectFmt work at compile-time

std.fmt no longer has a dependency on std.unicode. Formatted printing
was never properly unicode-aware. Now it no longer pretends to be.

Breakage/deprecations:
* std.fs.File.reader -> std.fs.File.deprecatedReader
* std.fs.File.writer -> std.fs.File.deprecatedWriter
* std.io.GenericReader -> std.io.Reader
* std.io.GenericWriter -> std.io.Writer
* std.io.AnyReader -> std.io.Reader
* std.io.AnyWriter -> std.io.Writer
* std.fmt.format -> std.fmt.deprecatedFormat
* std.fmt.fmtSliceEscapeLower -> std.ascii.hexEscape
* std.fmt.fmtSliceEscapeUpper -> std.ascii.hexEscape
* std.fmt.fmtSliceHexLower -> {x}
* std.fmt.fmtSliceHexUpper -> {X}
* std.fmt.fmtIntSizeDec -> {B}
* std.fmt.fmtIntSizeBin -> {Bi}
* std.fmt.fmtDuration -> {D}
* std.fmt.fmtDurationSigned -> {D}
* {} -> {f} when there is a format method
* format method signature
  - anytype -> *std.io.Writer
  - inferred error set -> error{WriteFailed}
  - options -> (deleted)
* std.fmt.Formatted
  - now takes context type explicitly
  - no fmt string
2025-07-07 22:43:51 -07:00
Andrew Kelley
0b3f0124dc std.io: move getStdIn, getStdOut, getStdErr functions to fs.File
preparing to rearrange std.io namespace into an interface

how to upgrade:

std.io.getStdIn() -> std.fs.File.stdin()
std.io.getStdOut() -> std.fs.File.stdout()
std.io.getStdErr() -> std.fs.File.stderr()
2025-07-07 22:43:51 -07:00
Andrew Kelley
9f27d770a1 std.io: deprecated Reader/Writer; introduce new API 2025-07-07 22:43:51 -07:00
Alex Rønne Petersen
8e511e031f
std.crypto.salsa20: Disable some tests on RISC-V with vector support
https://github.com/ziglang/zig/issues/24299
2025-07-01 23:03:15 +02:00
Elaine Gibson
ed48e2eb75 std.crypto.Certificate.Bundle: haiku support 2025-06-05 13:45:52 +01:00
Alex Rønne Petersen
9d534790eb std.Target: Introduce Cpu convenience functions for feature tests.
Before:

* std.Target.arm.featureSetHas(target.cpu.features, .has_v7)
* std.Target.x86.featureSetHasAny(target.cpu.features, .{ .sse, .avx, .cmov })
* std.Target.wasm.featureSetHasAll(target.cpu.features, .{ .atomics, .bulk_memory })

After:

* target.cpu.has(.arm, .has_v7)
* target.cpu.hasAny(.x86, &.{ .sse, .avx, .cmov })
* target.cpu.hasAll(.wasm, &.{ .atomics, .bulk_memory })
2025-06-05 06:12:00 +02:00
Jacob Young
ec579aa0f3
Legalize: implement scalarization of @shuffle 2025-06-01 08:24:01 +01:00
Jacob Young
1f6f8b0ffe x86_64: implement integer @reduce(.Add) 2025-05-28 15:10:22 -04:00
Jacob Young
d69f4c48fc x86_64: rewrite bitwise @reduce 2025-05-28 15:10:22 -04:00
HydroH
cc1475c91d
std: remove std.crypto.Certificate.Parsed.pubKeySigAlgo method (#23811) 2025-05-16 00:21:25 +02:00
Alex Rønne Petersen
bc3c50c21e
Merge pull request #23700 from sorairolake/rename-trims
chore(std.mem): Rename `trimLeft` and `trimRight` to `trimStart` and `trimEnd`
2025-05-12 17:11:52 +02:00
HydroH
a14352b0b2
std: fix compile errors in std.crypto.ecc (#23797)
Implemented `neg()` method for `AffineCoordinates` struct of p256,
p384 and secp256k1 curves.

Resolves: #20505 (partially)
2025-05-05 18:50:25 +00:00
David Rubin
369177f0ba
crypto: add sub function to Ristretto255 (#23724) 2025-05-05 15:30:59 +02:00
Deatil
8e79fc64cd
fixed codecs.zig (#23706) 2025-04-29 22:07:30 +00:00
Shun Sakai
5fc4448e45 chore(std.mem): Rename trimLeft and trimRight
Rename `trimLeft` to `trimStart`, and `trimRight` to `trimEnd`.
`trimLeft` and `trimRight` functions remain as deprecated aliases for
these new names.
2025-04-27 18:03:59 +09:00
Frank Denis
f01833e03e
crypto.ecdsa: add the ability to sign/verify prehashed messages (#23607) 2025-04-20 04:27:10 +02:00
Frank Denis
933beb4cbd
crypto.ecdsa: stricter DER decoding of signatures (#23554)
Reject DER-encoded signatures with the top bit set but no leading
0x00

Also add test vectors from Project Wycheproof with ECDSA-P384
2025-04-15 18:12:12 +02:00
Andrew Kelley
f32a5d349d std: eradicate u29 and embrace std.mem.Alignment 2025-04-13 02:20:32 -04:00
Frank Denis
a7122b7323
std.crypto: add constant-time codecs (#23420)
std.crypto: add constant-time codecs

Add constant-time hex/base64 codecs designed to process cryptographic
secrets, adapted from libsodium's implementations.

Introduce a `crypto.codecs` namespace for crypto-related encoders and
decoders. Move ASN.1 codecs to this namespace.

This will also naturally accommodate the proposed PEM codecs.
2025-04-12 20:13:45 +02:00
Frank Denis
ca1fc3827e
crypto.ecdsa: fix EcdsaP384Sha3_384 constant name (#23403)
Spotted by @deatil -- Thanks!
2025-03-29 14:52:34 +00:00