aboutsummaryrefslogtreecommitdiff
path: root/lib/std/crypto
AgeCommit message (Collapse)Author
2022-08-22stage2+stage1: remove type parameter from bit builtinsVeikka Tuominen
Closes #12529 Closes #12511 Closes #6835
2022-08-09std: add workaround for stage2 bugVeikka Tuominen
2022-08-03crypto.sign.ed25519: include a context string in blind key signatures (#12316)Frank Denis
The next revision of the specification is going to include a context string in the way blinded scalars are computed. See: https://github.com/cfrg/draft-irtf-cfrg-signature-key-blinding/issues/30#issuecomment-1180516152 https://github.com/cfrg/draft-irtf-cfrg-signature-key-blinding/pull/37
2022-07-26std.fmt: require specifier for unwrapping ?T and E!TInKryption
2022-07-25std.mem: add `first` method to `SplitIterator` and `SplitBackwardsIterator`r00ster
2022-07-24Revert "std.fmt: require specifier for unwrapping ?T and E!T."Andrew Kelley
This reverts commit 7cbd586ace46a8e8cebab660ebca3cfc049305d9. This is causing a fail to build from source: ``` ./lib/std/fmt.zig:492:17: error: cannot format optional without a specifier (i.e. {?} or {any}) @compileError("cannot format optional without a specifier (i.e. {?} or {any})"); ^ ./src/link/MachO/Atom.zig:544:26: note: called from here log.debug(" RELA({s}) @ {x} => %{d} in object({d})", .{ ^ ``` I looked at the code to fix it but none of those args are optionals.
2022-07-24std.fmt: require specifier for unwrapping ?T and E!T.InKryption
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2022-07-08crypto.sign.ed25519: add support for blind key signatures (#11868)Frank Denis
Key blinding allows public keys to be augmented with a secret scalar, making multiple signatures from the same signer unlinkable. https://datatracker.ietf.org/doc/draft-dew-cfrg-signature-key-blinding/ This is required by privacy-preserving applications such as Tor onion services and the PrivacyPass protocol.
2022-07-06crypto.sign.ecdsa: fix toCompressedSec1()/toUnompressedSec1() (#12009)Frank Denis
The Ecdsa.PublicKey type is not a direct alias for a curve element. So, use the inner field containing the curve element for serialization.
2022-07-01crypto: add the Xoodoo permutation, prepare for Gimli deprecation (#11866)Frank Denis
Gimli was a game changer. A permutation that is large enough to be used in sponge-like constructions, yet small enough to be compact to implement and fast on a wide range of platforms. And Gimli being part of the Zig standard library was awesome. But since then, Gimli entered the NIST Lightweight Cryptography Competition, competing againt other candidates sharing a similar set of properties. Unfortunately, Gimli didn't pass the 3rd round. There are no practical attacks against Gimli when used correctly, but NIST's decision means that Gimli is unlikely to ever get any traction. So, maybe the time has come to move Gimli from the standard library to another repository. We shouldn't do it without providing an alternative, though. And the best candidate for this is probably Xoodoo. Xoodoo is the core function of Xoodyak, one of the finalists of the NIST LWC competition, and the most direct competitor to Gimli. It is also a 384-bit permutation, so it can easily be used everywhere Gimli was used with no parameter changes. It is the building block of Xoodyak (for actual encryption and hashing) as well as Charm, that some Zig applications are already using. Like Gimli that it was heavily inspired from, it is compact and suitable for constrained environments. This change adds the Xoodoo permutation to std.crypto.core. The set of public functions includes everything required to later implement existing Xoodoo-based constructions. In order to prepare for the Gimli deprecation, the default CSPRNG was changed to a Xoodoo-based that works exactly the same way.
2022-07-01std.crypto.hash: allow creating hash functions from compositions (#11965)Frank Denis
A hash function cascade was a common way to avoid length-extension attacks with traditional hash functions such as the SHA-2 family. Add `std.crypto.hash.composition` to do exactly that using arbitrary hash functions, and pre-define the common SHA2-based ones. With this, we can now sign and verify Bitcoin signatures in pure Zig.
2022-06-29std.crypto.ecc: add support for the secp256k1 curve (#11880)Frank Denis
std.crypto.ecc: add support for the secp256k1 curve Usage of the secp256k1 elliptic curve recently grew exponentially, since this is the curve used by Bitcoin and other popular blockchains such as Ethereum. With this, Zig has support for all the widely deployed elliptic curves today.
2022-06-29std/crypto/{25519,pcurves}: make the scalar field order public (#11955)Frank Denis
For 25519, it's very likely that applications would ever need the serialized representation. Expose the value as an integer as in other curves. Rename the internal representation from `field_size` to `field_order` for consistency. Also fix a common typo in `scalar.sub()`.
2022-06-29std.crypto.{p256,p384}: process the top nibble in mulDoubleBasePublic (#11956)Frank Denis
Unlike curve25519 where the scalar size is not large enough to fill the top nibble, this can definitely be the case for p256 and p384.
2022-06-27std.crypto.25519.field: avoid excessive inliningAndrew Kelley
This valid zig code produces reasonable LLVM IR, however, on the wasm32-wasi target, when using the wasmtime runtime, the number of locals of the `isSquare` function exceeds 50000, causing wasmtime to refuse to execute the binary. The `inline` keyword in Zig is intended to be used only where it is semantically necessary; not as an optimization hint. Otherwise, this may produce unwanted binary bloat for the -OReleaseSmall use case. In the future, it is possible that we may end up with both `inline` keyword, which operates as it does in status quo, and additionally `callconv(.inline_hint)` which has no semantic impact, but may be observed by optimization passes. In this commit, I also cleaned up `isSquare` by eliminating an unnecessary mutable variable, replacing it with several local constants. Closes #11947.
2022-06-20std.crypto: fix invalid pass by valueVeikka Tuominen
2022-06-15std/crypto: add support for ECDSA signatures (#11855)Frank Denis
ECDSA is the most commonly used signature scheme today, mainly for historical and conformance reasons. It is a necessary evil for many standard protocols such as TLS and JWT. It is tricky to implement securely and has been the root cause of multiple security disasters, from the Playstation 3 hack to multiple critical issues in OpenSSL and Java. This implementation combines lessons learned from the past with recent recommendations. In Zig, the NIST curves that ECDSA is almost always instantied with use formally verified field arithmetic, giving us peace of mind even on edge cases. And the API rejects neutral elements where it matters, and unconditionally checks for non-canonical encoding for scalars and group elements. This automatically eliminates common vulnerabilities such as https://sk.tl/2LpS695v . ECDSA's security heavily relies on the security of the random number generator, which is a concern in some environments. This implementation mitigates this by computing deterministic nonces using the conservative scheme from Pornin et al. with the optional addition of randomness as proposed in Ericsson's "Deterministic ECDSA and EdDSA Signatures with Additional Randomness" document. This approach mitigates both the implications of a weak RNG and the practical implications of fault attacks. Project Wycheproof is a Google project to test crypto libraries against known attacks by triggering edge cases. It discovered vulnerabilities in virtually all major ECDSA implementations. The entire set of ECDSA-P256-SHA256 test vectors from Project Wycheproof is included here. Zero defects were found in this implementation. The public API differs from the Ed25519 one. Instead of raw byte strings for keys and signatures, we introduce Signature, PublicKey and SecretKey structures. The reason is that a raw byte representation would not be optimal. There are multiple standard representations for keys and signatures, and decoding/encoding them may not be cheap (field elements have to be converted from/to the montgomery domain). So, the intent is to eventually move ed25519 to the same API, which is not going to introduce any performance regression, but will bring us a consistent API, that we can also reuse for RSA.
2022-06-13crypto/pcurves: compute constants for inversion at comptime (#11780)Frank Denis
2022-06-12std: disable failing tests, add zig2 build test-std to CIVeikka Tuominen
2022-06-06std: add necessary `@alignCast`sVeikka Tuominen
2022-06-03std: update tests to stage2 semanticsVeikka Tuominen
2022-05-31crypto: add support for the NIST P-384 curve (#11735)Frank Denis
After P-256, here comes P-384, also known as secp384r1. Like P-256, it is required for TLS, and is the current NIST recommendation for key exchange and signatures, for better or for worse. Like P-256, all the finite field arithmetic has been computed and verified to be correct by fiat-crypto.
2022-05-26crypto/25519: add scalar.random(), use CompressedScalar typeFrank Denis
Add the ability to generate a random, canonical curve25519 scalar, like we do for p256. Also leverage the existing CompressedScalar type to represent these scalars.
2022-05-25std.crypto: cosmetic improvement to AES multiplication algorithm (#11616)Helio Machado
std.crypto: cosmetic improvement to AES multiplication algorithm (#11616)
2022-05-16Fixes comptime 'error: cannot assign to constant' error in siphash.Felix "xq" Queißner
2022-05-10TypoFrank Denis
2022-05-09std.crypto: generate AES constants at compile time (#11612)Helio Machado
* std/crypto: generate AES constants at compile time * Apply suggestions from code review Co-authored-by: Frank Denis <124872+jedisct1@users.noreply.github.com> * Update lib/std/crypto/aes/soft.zig * Separate encryption and decryption tables * Run `zig fmt` * Increase branch quota and remove redundant align * Update lib/std/crypto/aes/soft.zig Co-authored-by: Frank Denis <124872+jedisct1@users.noreply.github.com> * Rename identifiers and simplify dataflow * Increase branch quota (again) and fix comment Co-authored-by: Frank Denis <124872+jedisct1@users.noreply.github.com>
2022-05-03edwards25519 fixes (#11568)Frank Denis
* edwards25519: fix X coordinate of the base point Reported by @OfekShochat -- Thanks! * edwards25519: reduce public scalar when the top bit is set, not cleared This is an optimization for the unexpected case of a scalar larger than the field size. Fixes #11563 * edwards25519: add a test implicit reduction of invalid scalars
2022-04-27std: replace usage of std.meta.bitCount() with @bitSizeOf()Isaac Freund
2022-04-24std: fix crypto and hash benchmarkjagt
2022-04-07crypto/x25519: implement clearCofactor() (#11355)Frank Denis
This is the x25519 counterpart to `edwards25519.clearCofactor()`. It is useful to check for low-order points in protocols where it matters and where clamping cannot work, such as PAKEs.
2022-04-05zig fmt: remove trailing whitespace on doc commentsDamien Firmenich
Fixes #11353 The renderer treats comments and doc comments differently since doc comments are parsed into the Ast. This commit adds a check after getting the text for the doc comment and trims whitespace at the end before rendering. The `a = 0,` in the test is here to avoid a ParseError while parsing the test.
2022-03-30replace other uses of `std.meta.Vector` with `@Vector` (#11346)Meghan
2022-03-27std.crypto.blake3: use `@Vector` instead of `std.meta.Vector`Andrew Kelley
2022-02-01stage2: remove anytype fields from the languageAndrew Kelley
closes #10705
2022-01-28std: break up some long linesAndrew Kelley
This makes packaging Zig for Debian slightly easier since it will no longer trigger a Lintian warning for long lines.
2022-01-24std.crypto.25519.scalar: implement edwards25519 scalar field inversionFrank Denis
This operation is extremely useful for multiplicative blinding.
2021-12-27crypto/edwards25519: faster point decompressionFrank Denis
Make recovery of the x-coordinate slightly faster. See https://mailarchive.ietf.org/arch/msg/cfrg/qlKpMBqxXZYmDpXXIx6LO3Oznv4/ for details.
2021-12-27Bcrypt pbkdf (#10331)daurnimator
* Make bcrypt State struct public This is useful to implement the various protocols outside of the standard library * Implement bcrypt pbkdf This variant is used in e.g. SSH The OpenBSD implementation was used as a reference
2021-12-19stage1, stage2: rename c_void to anyopaque (#10316)Isaac Freund
zig fmt now replaces c_void with anyopaque to make updating code easy.
2021-11-30allocgate: renamed getAllocator function to allocatorLee Cannon
2021-11-30allocgate: std Allocator interface refactorLee Cannon
2021-11-30std lib API deprecations for the upcoming 0.9.0 releaseAndrew Kelley
See #3811
2021-11-24Merge pull request #10073 from hoanga/haiku-support-build2Andrew Kelley
more haiku support
2021-11-20std: add `writer` methods on all crypto.hash types (#10168)Meghan
2021-11-15update docs (#10150)lucky
add fast kdf test fix inconsistent kdf error refactor Co-authored-by: lucky <>
2021-11-15add fork case for haikuAl Hoang
2021-11-15add argon2 kdf (#9756)lucky
2021-11-08crypto/edwards25519: correctly flip the Y sign in the H2C operationFrank Denis
No security implications, but the current hash-to-curve standard defines the sign of the Y coordinate to be negative if `gx1` is a square, positive otherwise. We were doing it the other way round.
2021-10-27std.rand: Refactor `Random` interfaceOminitay
These changes have been made to resolve issue #10037. The `Random` interface was implemented in such a way that causes significant slowdown when calling the `fill` function of the rng used. The `Random` interface is no longer stored in a field of the rng, and is instead returned by the child function `random()` of the rng. This avoids the performance issues caused by the interface.